Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write manipulated raster values to ASCII grid with GDAL?

I am trying to manipulate raster values in a grid (ASCII Grid) with GDAL. But before proceeding with this, I have trouble writing the new values into the file. I get these error messages when slopeband.WriteArray(s) is called.

ERROR 6: slope.asc, band 1: WriteBlock() not supported for this dataset.

ERROR 1: slope.asc, band 1: An error occured while writing a dirty block

I'm sorry if this is very basic, but I'm still new to python and GDAL in particular. I use GDAL 1.9.0 on Mac OS X 10.6.8 and Python 2.7. Thank you!

import numpy
import gdal
import gdalconst

dgm = gdal.Open("DGM_10_MR.asc", gdalconst.GA_ReadOnly)
driver = dgm.GetDriver()
geotransform = dgm.GetGeoTransform()
band = dgm.GetRasterBand(1)
data = band.ReadAsArray()

cols = dgm.RasterXSize
rows = dgm.RasterYSize
slope = driver.CreateCopy("slope.asc", dgm)
slope = None
dgm = None
slope = gdal.Open("slope.asc", gdalconst.GA_Update)
slope.SetGeoTransform(geotransform)
slopeband = slope.GetRasterBand(1)
s = slopeband.ReadAsArray()

for y in range(rows):
    for x in range(cols):
        s[y, x] = 0.0

slopeband.WriteArray(s)
slopeband.FlushCache()
del s

dgm = None
slope = None
print "done"
like image 968
Lucia Avatar asked Jan 17 '23 15:01

Lucia


1 Answers

Unfortunately, GDAL cannot read and write to the same degrees across all filetypes. Arc ASCII grid happens to be one of those filetypes that GDAL cannot write to. As your error message says: WriteBlock() not supported for this dataset., so you can't write to Arc ASCII grids.

As alternative, you could convert your existing ASCII dataset to a different filetype, one that GDAL more fully spports such as GeoTiff. To convert filetypes, you could use the gdal_translate command-line program like so:

gdal_translate -of GTiff DGM_10_R.asc DGM_10_R.tif

I was able to reproduce your errors on my computer, and simply changing the filetype fixes the errors.

like image 156
James Avatar answered Jan 30 '23 19:01

James