2

I have an array "array" with numbers range from -5 to 1 (with decimal) and I tried to write it as a geotiff. However, the output geotiff turns out to be all zeros. Can someone tell me why? Here the code.

from osgeo import gdal

ds = gdal.Open('test.tif', gdal.GA_ReadOnly)
driver = gdal.GetDriverByName("GTiff")
outdata = driver.Create('output.tif', array.RasterXSize, array.RasterYSize, 1, gdal.GDT_Float32)
outdata.SetGeoTransform(ds.GetGeoTransform())
outdata.SetProjection(ds.GetProjection())
outband = outdata.GetRasterBand(1)
outband.WriteArray(array)
outband.FlushCache()  
  • Maybe add del outdata to the end of the script althought you are already calling FlushCache(). – Marcelo Villa Sep 12 at 21:58
  • Thanks, Marcelo. You were right. But I don't understand why deleting outdata is necessary? – J.Wang Sep 13 at 14:50
1

You have to delete the output dataset to make sure the script releases that object from memory (although it should do it at the end of the exectution). You can do this by adding del outdata at the end of your script. I always use both band.FlushCache() (which writes the data to disk) and del outdata to make sure I don't end up with an empty array.

  • Thanks! That was helpful. – J.Wang Sep 17 at 15:50
  • Glad it was helpful! If it solved your issue marked the answer as accepted so the question is closed. – Marcelo Villa Sep 17 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.