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

Simen Langseth simlangen at gmail.com
Sun Nov 23 09:37:57 PST 2014


Even Rouault, Thank you! Now the program is working.

On Mon, Nov 24, 2014 at 2:15 AM, Even Rouault <even.rouault at spatialys.com>
wrote:

> Le dimanche 23 novembre 2014 17:04:05, Simen Langseth a écrit :
> > 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_Flo
> > at32) 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?
>
> Presumably outraster dimensions are not big enough.
> Create(outfile,y_total,x_total,....) is wrong. That should rather be
> Create(outfile,x_total,y_total,....)
>
> --
> Spatialys - Geospatial professional services
> http://www.spatialys.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20141124/84626c9e/attachment.html>


More information about the gdal-dev mailing list