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.

user2856
73.9k7 gold badges123 silver badges207 bronze badges
asked Mar 24, 2020 at 7:56
0

1 Answer 1

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

answered Mar 24, 2020 at 8:44
1
  • This solved my problem. Thanks a lot for your help! Commented Mar 24, 2020 at 9:33

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.