0

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.

0

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()

| improve this answer | |
  • This solved my problem. Thanks a lot for your help! – Anuj Arora Mar 24 at 9:33

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.