Tuesday, 6 September 2016

Why do GDAL and rasterio give different projected bounding coordinates for a given input geotiff?


I have a geotiff, and I want to find its extents and reproject them. I can use rasterio:


# Destination CRS

dest_crs = some CRS

# Get projected boundaries with rasterio
clipbox = rasterio.open('mygeotif.tif')
cl, cb, cr, ct = clipbox.bounds
l, b, r, t = transform_bounds(clipbox.crs, dest_crs, clipbox.bounds[0],
clipbox.bounds[1], clipbox.bounds[2], clipbox.bounds[3])
print(l,b,r,t)

-75.0277857229692 -9.469504586908355 -74.09777685862646 -7.796173999420965



...or I can use GDAL:


# Get projected boundaries with GDAL
clipbox = gdal.Open('mygeotif.tif')
wktproj = clipbox.GetProjection()
sref = osr.SpatialReference()
sref.ImportFromWkt(wktproj)

# Get its extents
ulx, xres, xskew, uly, yskew, yres = clipbox.GetGeoTransform()
lrx = ulx + (clipbox.RasterXSize * xres)

lry = uly + (clipbox.RasterYSize * yres)

# Reproject the extents
transform = osr.CoordinateTransformation(dest_crf, sref)
tl, tt, garbage = transform.TransformPoint(ulx, uly)
tr, tb, garbage = transform.TransformPoint(lrx, lry)
print(tl, tb, tr, tt)

-75.02766365052823 -9.468344577976415 -74.09777685862646 -7.797125769469675


So one latitude value is the same, but the other three are different. Does anyone have any ideas why this would be the case? I should note that cl == ulx, cb == lry, etc. I.e. the input boundary coordinates are the same pre-projection.



EDIT:


Pre-projection coordinates:


print(ulx,uly,lrx,lry) # GDAL

496950.0 -861870.0 599040.0 -1046760.0


print(cl, ct, cr, cb) # rasterio

496950.0 -861870.0 599040.0 -1046760.0


Projections:


print(dest_crs) # GDAL


"osgeo.osr.SpatialReference; proxy of Swig Object of type 'OSRSpatialReferenceShadow *' at 0x117b85e10"


and using a function called wkt2epsg, we see


print(wkt2epsg(wktproj))

EPSG:32618


print(dest_crs) # rasterio

EPSG:32618



Answer




The source coordinate reference system (CRS) is WGS84 / UTM zone 18 North. The points are south of the equator. I'm assuming the destination CRS is WGS84, EPSG:4326.


One of the programs is "reboxing" the raster while the other one isn't. When I unproject all 4 corners of the raster:


496950.0   -861870.0
496950.0 -1046760.0
599040.0 -1046760.0
599040.0 -861870.0

Here's what I get:


-75.02766365052825  -7.797125769469677
-75.02778572296923 -9.469503785651575

-74.09777685862647 -9.468344577976417
-74.10173999154692 -7.796173999420967

Because the raster isn't centered across the central meridian, it's tipped slightly to the northwest. You do not have a perfect rectangle.


rasterio is taking the minimum and maximum values for all 4 corners to return you the actual extent of the output raster. GDAL is returning just the unprojected lower left and upper right coordinates.


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...