Even<br><br>Thanks for pointing that out. I have now been able to solve that problem by keeping the dataset in scope...<br><br>Riaan<br><br><div class="gmail_quote">On Fri, Sep 3, 2010 at 7:52 PM, Even Rouault <span dir="ltr"><<a href="mailto:even.rouault@mines-paris.org">even.rouault@mines-paris.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">Riann,<br>
<br>
hum, I think you've hit a classical problem with the use of the GDAL python<br>
bindings. The root of your problem is likely the following line:<br>
<br>
</div><div class="im"> reader = RasterReaderWriter(gdal.Open(iFilename).GetRasterBand(1))<br>
<br>
</div><div class="im">Currently the GDAL python bindings do a poor job with life cycle management of<br>
native underlying objects. When chaining the calls, some objects can go out-<br>
of-scope and are then destroyed. Specifically, the band object doesn't keep the<br>
dataset object alive, so the RasterReaderWriter() constructor will get a band<br>
object with a ghost dataset. This can typically result in application crashes,<br>
or as you've observed various corruptions.<br>
<br>
All in all, I'd advise you rewriting the above as :<br>
<br>
ds = gdal.Open(iFilename)<br>
band = ds.GetRasterBand(1)<br>
reader = RasterReaderWriter(band)<br>
....<br>
.....<br>
ds = None # we can drop the reference on the dataset when we don't need to use<br>
the reader.<br>
<br>
An alternative if you don't want to keep the reference on the dataset in the<br>
calling code, is to pass both the dataset and the band to RasterReaderWrite()<br>
so that it can stores them in member variables of the class.<br>
<br>
Best regards,<br>
<br>
Even<br>
<br>
Le vendredi 03 septembre 2010 12:39:09, Riaan van den Dool a écrit :<br>
</div><div><div></div><div class="h5">> I have recently started a new open-source project that will use gdal<br>
> extensively, named scikits.eartho.<br>
><br>
> The vision is to implement some advanced algorithms and ideas that we<br>
> develop and work with at the South African Satellite Applications Centre<br>
> (SAC) for use in Python (SciPy).<br>
><br>
> I am experiencing some trouble with RasterBand.ReadAsArray.<br>
><br>
> My python code:<br>
><br>
> import osgeo.gdal as gdal<br>
><br>
> class RasterReaderWriter:<br>
> def __init__(self, raster):<br>
> self.raster = raster<br>
> self.tileWidth =1024<br>
> self.tileHeight = 1024<br>
> self.tileNum = 0<br>
><br>
> def setTileSize(self, width, height):<br>
> self.tileWidth =width<br>
> self.tileHeight = height<br>
><br>
> def resetIterator(self):<br>
> self.tileNum = 0<br>
><br>
> def readNextTile(self):<br>
> tileRaster, offsetX, offsetY, width, height =<br>
> self.readTile(self.tileNum)<br>
> self.tileNum = self.tileNum + 1<br>
> return tileRaster, offsetX, offsetY, width, height<br>
><br>
> def readTile(self, tileNum):<br>
> tilesInWidth = self.raster.XSize / self.tileWidth + 1<br>
> offsetX = tileNum % tilesInWidth * self.tileWidth<br>
> offsetY = tileNum / tilesInWidth * self.tileHeight<br>
> width = min(self.tileWidth, self.raster.XSize - offsetX)<br>
> height = min(self.tileHeight, self.raster.YSize - offsetY)<br>
> print offsetX, offsetY, width, height<br>
> tileRaster = self.raster.ReadAsArray(offsetX, offsetY, width,<br>
> height)<br>
> return tileRaster, offsetX, offsetY, width, height<br>
><br>
><br>
><br>
><br>
> reader = RasterReaderWriter(gdal.Open(iFilename).GetRasterBand(1))<br>
> print reader.raster.XSize<br>
> print reader.raster.YSize<br>
> tile, offsetX, offsetY, width, height = reader.readNextTile()<br>
><br>
> Before the call to readNextTile() the raster XSize and YSize is correct.<br>
> After the call the raster seems to have been corrupted as the XSize and<br>
> YSize have invalid values.<br>
><br>
> Any help would be appreciated!<br>
><br>
> Riaan<br>
</div></div></blockquote></div><br>