[gdal-dev] get corner coordinates from gdalopen()

Kralidis,Tom [Ontario] Tom.Kralidis at ec.gc.ca
Thu Apr 15 13:06:29 EDT 2010


 

> -----Original Message-----
> From: gdal-dev-bounces at lists.osgeo.org 
> [mailto:gdal-dev-bounces at lists.osgeo.org] On Behalf Of achrysochoou
> Sent: Thursday, 15 April 2010 12:38
> To: gdal-dev at lists.osgeo.org
> Subject: [gdal-dev] get corner coordinates from gdalopen()
> 
> 
> Hi,
>  
> I have a geotiff and i am using python to read it. I am 
> trying to extract the corner coordinates and store them in 
> different variables. This is what i have done so far,
>  
> dataset = gdal.Open(tif_path)
> print 'zone and Hemisphere are:',
> dataset.GetProjection()[26]+dataset.GetProjection()[27]+datase
> t.GetProjection()[28]
> geotransform = dataset.GetGeoTransform() print 'Origin = 
> (',geotransform[0], ',',geotransform[3],')'
>  
> i have tried more methods with no luck (or perhaps i simply do not get
> it...) such as,
> print 'GetProjectionRef is: ',dataset.GetProjectionRef() 
> print 'GetGCPProjection is: ',dataset.GetGCPProjection() 
> print 'GetGCPs() is: ',dataset.GetGCPs()
>  
> I tried shell commands such as 'gdalinfo' which gives 
> information about the geotiff but i can't access the corner 
> coordinates from the shell (or i don't know how...). I am 
> currently thinking of counting the pixels and calculating the 
> coordinates like that but it seems i should ask first before 
> doing sth like that...
> I would like to save the corner coordinates like this, 
> lower_right = ..., lower_left = ..., upper_right = ..., etc
>  

For the corner coordinates, GDAL's python bindings do not have a
GetExtent() type function like OGR does.  So you have to calculate this
manually.  Here's an example:

ds = gdal.Open('path/to/file')

width = ds.RasterXSize
height = ds.RasterYSize

gt = ds.GetGeoTransform()
minx = gt[0]
miny = gt[3] + width*gt[4] + height*gt[5]  # from
http://gdal.org/gdal_datamodel.html
maxx = gt[0] + width*gt[1] + height*gt[2]  # from
http://gdal.org/gdal_datamodel.html
maxy = gt[3]

Hope this helps.

..Tom



More information about the gdal-dev mailing list