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

Even Rouault even.rouault at spatialys.com
Sun Nov 23 09:15:14 PST 2014


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


More information about the gdal-dev mailing list