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

James Martin martin426 at YAHOO.COM
Mon Mar 7 12:26:19 EST 2005


Duh.. It works exactly as you say, for some reason it just didn't
seem logical to me so I never tried it and of course dove into looking for a
much more complex solution.  Thanks for setting me straight Paul!

For any pythoners out there I've included a translation (minus the
comments--they're essentially the same):

def Geo2Pix (nGeoPos, dfPixMin, dfPixMax, dfGeoMin, dfGeoMax, nInverseGeo =
False):
   dfWidthGeo = abs(dfGeoMax - dfGeoMin)
   dfWidthPix = abs(dfPixMax - dfPixMin)
   if dfWidthGeo > 0:
       dfGeoToPix = dfWidthPix / dfWidthGeo
       if nInverseGeo:
          dfDeltaGeo = dfGeoMax - nGeoPos
       else:
          dfDeltaGeo = nGeoPos - dfGeoMin
       dfDeltaPix = dfDeltaGeo * dfGeoToPix
       dfPosPix = dfPixMin + dfDeltaPix
       return round(dfPosPix)
   return False # dfWidthGeo is <= 0

-James

On Sun, 6 Mar 2005 17:10:03 -0500, Paul Spencer <pspencer at DMSOLUTIONS.CA> wrote:

>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