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() 
asked Sep 12, 2019 at 19:54
2
  • Maybe add del outdata to the end of the script althought you are already calling FlushCache(). Commented Sep 12, 2019 at 21:58
  • Thanks, Marcelo. You were right. But I don't understand why deleting outdata is necessary? Commented Sep 13, 2019 at 14:50

1 Answer 1

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.

answered Sep 13, 2019 at 15:07
2
  • Thanks! That was helpful. Commented Sep 17, 2019 at 15:50
  • Glad it was helpful! If it solved your issue marked the answer as accepted so the question is closed. Commented Sep 17, 2019 at 15:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.