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

Ken Sewell sewell at dramail.com
Thu Sep 30 11:11:16 EDT 2004


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.

    if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None )
    {
        pszProjection = GDALGetProjectionRef(hDataset);

        dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x
            + adfGeoTransform[2] * y;
        dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x
            + adfGeoTransform[5] * y;
    }

    if( pszProjection != NULL && strlen(pszProjection) > 0 )
    {
        OGRSpatialReferenceH hProj, hLatLong = NULL;

        hProj = OSRNewSpatialReference( pszProjection );
        if( hProj != NULL )
            hLatLong = OSRCloneGeogCS( hProj );

        if( hLatLong != NULL )
        {
            CPLPushErrorHandler( CPLQuietErrorHandler );
            hTransform = OCTNewCoordinateTransformation( hProj, hLatLong
);
            CPLPopErrorHandler();

            OSRDestroySpatialReference( hLatLong );
        }

        if( hProj != NULL )
            OSRDestroySpatialReference( hProj );
    }

    if( hTransform != NULL
        && OCTTransform(hTransform,1,&dfGeoX,&dfGeoY,NULL) )
    {

        printf( "(%s,", GDALDecToDMS( dfGeoX, "Long", 2 ) );
        printf( "%s)", GDALDecToDMS( dfGeoY, "Lat", 2 ) );
    }

    if( hTransform != NULL )
        OCTDestroyCoordinateTransformation( hTransform );


Ken Sewell                   Defense Research Associates
Project Engineer             3915 Germany Lane, Suite 102
937-656-4899 x3004           Beavercreek, Ohio 45431-1608
sewell at dramail.com

On Wed, 29 Sep 2004, Frank Warmerdam wrote:

> Ken Sewell wrote:
> > Given a lat/lon can GDAL tell me what pixel is associated with it(if
> > any)?  I've found the function that will give me the lat/lon from the
> > pixel, but I can't seem to find one that gives me a pixel from a lat/lon?
> > Does such a function exist within GDAL?  Thanks.
>
>
> Ken,
>
> Generally speaking, going from lat/long to pixel is the same process as
> going from pixel to lat/long.  Can you show how you are doing from
> pixel to lat/long?
>
> 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