I have created a raster.tiff file using GDAL driver with data type as Byte and have also added a palette of unique values to it (0,1,2).
When i try to write a pixel value for a particular Raster Band the value is not getting updated.
I have tried using FlushCache() and del variables, but nothing seems to work.
rasterDS=gdal.Open("C:/Anuj/fyp/Delhi_Sample/Clutter/raster.tiff")
rasterBand=rasterd.GetRasterBand(1)
rasterArray=rasterb.ReadAsArray()
rasterArray[0][1]
1
rasterArray[0][1]=2
rasterBand.WriteArray(rasterArray)
0
rasterBand.FlushCache()
rasterDS.FlushCache()
rasterBand=None
rasterDS=None
del rasterBand
del rasterDS
rasterDS=gdal.Open("C:/Anuj/fyp/Delhi_Sample/Clutter/raster.tiff")
rasterBand=rasterd.GetRasterBand(1)
rasterArray=rasterb.ReadAsArray()
rasterArray[0][1]
1
The pixel value is not getting updated after writing the array to the raster band.
1 Answer 1
You opened the dataset in read-only mode. Open it in update mode (gdal.GA_Update
) to write:
from osgeo import gdal
rasterDS=gdal.Open(myraster, gdal.GA_Update)
rasterBand = rasterDS.GetRasterBand(1)
rasterArray = rasterBand.ReadAsArray()
rasterArray[0][1]
11
rasterArray[0][1]=2
rasterBand.WriteArray(rasterArray)
del rasterDS
rasterDS=gdal.Open(myraster)
rasterBand = rasterDS.GetRasterBand(1)
rasterArray = rasterBand.ReadAsArray()
rasterArray[0][1]
2
I suggest using gdal.UseExceptions()
in your code to help catch these sort of errors:
rasterDS=gdal.Open(myraster)
rasterBand = rasterDS.GetRasterBand(1)
rasterArray = rasterBand.ReadAsArray()
rasterArray[0][1]=2
rasterBand.WriteArray(rasterArray)
del rasterBand, rasterDS
Exception RuntimeError: 'TIFFWriteEncodedStrip:File not open for writing' # <=== This tells you exactly what the problem is...
This is a known "gotcha" - Python bindings do not raise exceptions unless you explicitly call UseExceptions()
-
This solved my problem. Thanks a lot for your help!Anuj Arora– Anuj Arora2020年03月24日 09:33:55 +00:00Commented Mar 24, 2020 at 9:33