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()
1 Answer 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– J.Wang2019年09月17日 15:50:12 +00:00Commented 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.Marcelo Villa– Marcelo Villa2019年09月17日 15:53:50 +00:00Commented Sep 17, 2019 at 15:53
del outdata
to the end of the script althought you are already callingFlushCache()
.