[Gdal-dev] Get a pixel from a lat/lon.

Frank Warmerdam warmerdam at pobox.com
Thu Sep 30 11:12:34 EDT 2004

Ken Sewell wrote:
> We're basically using the same code in the gdalinfo.c.  We're still
> very new at using GDAL so how we go about reversing this to get the pixel
> from a lat/lon is still not obvious.  Basically we just need to go both
> directions pixel to lat/lon and lat/lon to pixel, we used this method
> because we found it in an example.  Can anyone recommend a better way to
> accomplish what we need to do?  Thanks.


This is an appropriate mechanism.

To go in reverse you need to :

  1) Generate the hTransform the same way, but pass in the hProj and
     hLatLong arguments in the opposite order indicating that you are
     going from latlong to projected coordinates.

  2) Apply the hTransform (with OCTTransform) before using the geotransform.

  3) To "unapply" the geotransform, you can do the following if you make
     the simplying assumption that the image is northup (that is, that
     adfGeoTransform[2] == 0 and adfGeoTransform[4] is 0).  If this isn't
     the case there is a slightly more complex approach I can describe.

     x = (dfGeoX - adfGeoTransform[0]) / adfGeoTransform[1];
     y = (dfGeoY - adfGeoTransform[3]) / adfGeoTransform[5];

I hope this helps.

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

More information about the Gdal-dev mailing list