Figure out the XY position on the resulting map of a point

Paul Spencer pspencer at DMSOLUTIONS.CA
Sun Mar 6 14:10:03 PST 2005


Hi James.

Assuming your output projection is not lat/lon, you can create a point
object and reproject it into the map's projection and do the same
calculation.  Here's some (untested) code that should work:

$oProjIn = ms_newProjectionObj("init=epsg:4326");
$oProjOut = ms_newProjectionObj($oMap->getProjection());
$oPoint = ms_newPointObj();
$oPoint->setXY( $lon, $lat );
$oPoint->project( $oProjIn, $oProjOut );

$px = Geo2Pix( $oPoint->x, 0, $oMap->width
                $oMap->extent->minx, $oMap->extent->maxx, false );

$py = Geo2Pix( $oPoint->y, 0, $oMap->height
                $oMap->extent->miny, $oMap->extent->maxy, true );

/**
  * convert a geocoded position to pixel coord
  *
  * @param nGeoPos double Geocoded position
  * @param dfPixMin double minimum map pixel value
  * @param dfPixMax double maximum map pixel value
  * @param dfGeoMin double minimum map geocoded value
  * @param dfGeoMax double maximum map geocoded value
  * @param nInverseGeo integer optional flag to inverse , set to 1 for
  *                            Y pixel coordinates where UL > LR
  * @return double geocoded position
  */
function Geo2Pix ($nGeoPos, $dfPixMin, $dfPixMax, $dfGeoMin,
                      $dfGeoMax, $nInverseGeo = "")
{
     // calculate the geocoded & pixel width
     $dfWidthGeo = abs($dfGeoMax - $dfGeoMin);
     $dfWidthPix = abs($dfPixMax - $dfPixMin);

     // get ratio
     if ( $dfWidthGeo <= 0 )
     {
         return 0;
     }
     $dfGeoToPix = $dfWidthPix / $dfWidthGeo;

     // get difference
     if (!$nInverseGeo)
         $dfDeltaGeo = $nGeoPos - $dfGeoMin;
     else
         $dfDeltaGeo = $dfGeoMax - $nGeoPos;

     // calculate
     $dfDeltaPix = $dfDeltaGeo * $dfGeoToPix;
     $dfPosPix = $dfPixMin + $dfDeltaPix;

     // return value
     return round ($dfPosPix);

     // end pixel_to_geo function
}

Cheers,

Paul

James wrote:
> Hello,
>
> Using mapscript, is there a way to figure out what the resultant XY position
> of a point object will be on the output image?  As an example, using a
> simple lat/lon projection, knowing the lat/lon position of the point, the
> dimensions of the image and the extent, simple algebra leads me to the XY
> position of said point.
>
> However, when using a different projection this is not possible as each
> pixel no longer represents the same amount of lat/lon distance.  I can
> project my lat/lon point to the new projection, but is there any way to
> extract the XY coordinates?
>
> Obviously Mapserver must do this to actually place the point on the
> resulting image, but I haven't been able to figure out how to get at that
> information.  Any suggestions are greatly appreciated.
>
> Thank you,
>
> James
>

--
+-----------------------------------------------------------------+
|Paul Spencer                           pspencer at dmsolutions.ca   |
+-----------------------------------------------------------------+
|Applications & Software Development                              |
|DM Solutions Group Inc                 http://www.dmsolutions.ca/|
+-----------------------------------------------------------------+



More information about the MapServer-users mailing list