Thursday 18 July 2019

How do I extract a raster's extent in python?


I am looking for an easy way to extract a raster's extent in python.


Currently, I am using gdalinfo and then parsing out the corner coordinates. The problem with this approach is that gdalinfo is not consistent in how it reports the corner coordinates. I am working with a variety of file types (hdf, tif, img, asc, etc). I need the extent to be in decimal degrees (or DMS - that I can convert to decimal degrees).


This is being used with a PostgreSQL database so I have access to any PostGIS function, gdal, or ArcGIS, although I would prefer a function that does not involve ArcGIS unless it is fast (I am trying to index a large number of files, frequently). It can be run on either Windows or Unix.


Thank you, Kevin



Answer



Probably the python port of gdalinfo would help you. You can see at the top of the file that all the coordinates are reported using the GDALInfoReportCorner method:


#/* -------------------------------------------------------------------- */

#/* Report corners. */
#/* -------------------------------------------------------------------- */
print( "Corner Coordinates:" )
GDALInfoReportCorner( hDataset, hTransform, "Upper Left", \
0.0, 0.0 );
GDALInfoReportCorner( hDataset, hTransform, "Lower Left", \
0.0, hDataset.RasterYSize);
GDALInfoReportCorner( hDataset, hTransform, "Upper Right", \
hDataset.RasterXSize, 0.0 );
GDALInfoReportCorner( hDataset, hTransform, "Lower Right", \

hDataset.RasterXSize, \
hDataset.RasterYSize );
GDALInfoReportCorner( hDataset, hTransform, "Center", \
hDataset.RasterXSize/2.0, \
hDataset.RasterYSize/2.0 );

you can change the method itself which is implemented at the bottom:


#/************************************************************************/
#/* GDALInfoReportCorner() */
#/************************************************************************/


def GDALInfoReportCorner( hDataset, hTransform, corner_name, x, y ):


line = "%-11s " % corner_name

#/* -------------------------------------------------------------------- */
#/* Transform the point into georeferenced coordinates. */
#/* -------------------------------------------------------------------- */
adfGeoTransform = hDataset.GetGeoTransform(can_return_null = True)

if adfGeoTransform is not None:
dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x \
+ adfGeoTransform[2] * y
dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x \
+ adfGeoTransform[5] * y

else:
line = line + ("(%7.1f,%7.1f)" % (x, y ))
print(line)
return False


#/* -------------------------------------------------------------------- */
#/* Report the georeferenced coordinates. */
#/* -------------------------------------------------------------------- */
if abs(dfGeoX) < 181 and abs(dfGeoY) < 91:
line = line + ( "(%12.7f,%12.7f) " % (dfGeoX, dfGeoY ))

else:
line = line + ( "(%12.3f,%12.3f) " % (dfGeoX, dfGeoY ))


#/* -------------------------------------------------------------------- */
#/* Transform to latlong and report. */
#/* -------------------------------------------------------------------- */
if hTransform is not None:
pnt = hTransform.TransformPoint(dfGeoX, dfGeoY, 0)
if pnt is not None:
line = line + ( "(%s," % gdal.DecToDMS( pnt[0], "Long", 2 ) )
line = line + ( "%s)" % gdal.DecToDMS( pnt[1], "Lat", 2 ) )

print(line)


return True

Make it print whatever you want :)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...