Python GDAL CopyCreate

I have a geotiff which was exported from SNAP. I read this into python and use CreateCopy to create a copy. When I import the copy into SNAP it does not display correctly. The original is displayed with square pixels while the copy displays with rectangular pixels. A lot of the metadata between the copy and the original is different. How can ensure that the copy has all necessary information so that it displays like the original in SNAP. The return values of both GetGeoTransform() and GetProjection() are the same for both copy and original

Code snippet:
drive = os.path.normpath(‘Products’)
product = ‘S1A_EW_GRDM_1SDH_20191003T042912_20191003T043012_029289_03540B_D417_Cal.data’
extension = ‘.tif’
hh_filename = ‘Sigma0_HH’
hh_src = gdal.Open( os.path.join(drive, product,hh_filename + extension) )
driver = gdal.GetDriverByName(“GTiff”)
outdata = driver.CreateCopy(outFileName, hh_src, strict = 0)
outdata = None
hh_src = None

It seems to be a problem with GDAL GTIF-driver’s CreateCopy function, as it only copies the metadata linked to the projection :

But the projection data copied are correct : SNAP use non-GeoTIFF metadata to correct the projection in the viewer, but that only work here. Try opening the GeoTIFF you exported from SNAP into a GIS and you will get rectangular pixels.

If you want the correct geolocation, you’ll have to terrain-correct your product. If you only want a product with square-pixel without regard to its geolocation, you can use GDAL and python to tweak the GeoTransform :

(...)
outdata = driver.CreateCopy(outFileName, hh_src, strict = 0)

lon_min, res_x, z_x, lat_max, z_y, res_y  = outdata.GetGeoTransform()
res_y = -res_y # y resolution always negative
if res_y > res_x:
    res_x = res_y 
else :
    res_y = res_x
outdata.SetGeoTransform((lon_min, res_x, z_x, lat_max, z_y, -res_y)) # don't forget to make res_y negative again

outdata = None
hh_src = None

Here the result in QGIS and SNAP. Note the world view windows in SNAP : the geolocation is still wrong.


Thanks for taking the trouble to look into this! I knew that the pixels were not square in the original, because I read the GeoTransform from it and compared it to the copy (which was the same then). Therefore my puzzlement why snap displays one that way and the other the other way. Changing the GeoTransorm is not really an option, but I have discovered that if I do band arithmetics between the original and the copy, the result will be displayed with square pixels. :crazy_face: So I can modify the copy programmtically as I need to and then force it to be displayed with square pixels, by first adding and then subtracting the original.