[gdal-dev] Value Error While Writing the File in Chunks

Simen Langseth simlangen at gmail.com
Sun Nov 23 08:04:05 PST 2014


import gdal, numpy as np
from gdalconst import *

infile = r"D:\given.tif"
inraster = gdal.Open(infile, GA_ReadOnly)
x_total,y_total=refraster.RasterXSize,refraster.RasterYSize
n_chunks=10
x_offsets=np.linspace(0,x_total,n_chunks).astype(int)
x_offsets=zip(x_offsets[:-1],x_offsets[1:])
y_offsets=np.linspace(0,y_total,n_chunks).astype(int)
y_offsets=zip(y_offsets[:-1],y_offsets[1:])

outfile =  r"D:\result.tif"
outraster =
gdal.GetDriverByName('GTiff').Create(outfile,y_total,x_total,1,gdal.GDT_Float32)
for x1,x2 in x_offsets:
    for y1,y2 in y_offsets:
        values = refraster.GetRasterBand(1).ReadAsArray(x1,y1,x2-x1,y2-y1)
        outraster.GetRasterBand(1).WriteArray(values, x1, y1)
inraster, outraster = None, None

I got the following error:

Traceback (most recent call last):
  File "D:\test.py", line 20, in <module>
    outraster.GetRasterBand(1).WriteArray(values, x1, y1)
  File "C:\Python27\lib\site-packages\osgeo\gdal.py", line 1167, in
WriteArray
    return gdalnumeric.BandWriteArray( self, array, xoff, yoff )
  File "C:\Python27\lib\site-packages\osgeo\gdal_array.py", line 304, in
BandWriteArray
    raise ValueError("array larger than output file, or offset off edge")
ValueError: array larger than output file, or offset off edge

How can I correct the writeArray?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20141124/4b110a82/attachment.html>


More information about the gdal-dev mailing list