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">&lt;<a href="mailto:even.rouault@mines-paris.org">even.rouault@mines-paris.org</a>&gt;</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&#39;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&#39;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&#39;ve observed various corruptions.<br>
<br>
All in all, I&#39;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&#39;t need to use<br>
the reader.<br>
<br>
An alternative if you don&#39;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">&gt; I have recently started a new open-source project that will use gdal<br>
&gt; extensively, named scikits.eartho.<br>
&gt;<br>
&gt; The vision is to implement some advanced algorithms and ideas that we<br>
&gt; develop and work with at the South African Satellite Applications Centre<br>
&gt; (SAC) for use in Python (SciPy).<br>
&gt;<br>
&gt; I am experiencing some trouble with RasterBand.ReadAsArray.<br>
&gt;<br>
&gt; My python code:<br>
&gt;<br>
&gt; import osgeo.gdal as gdal<br>
&gt;<br>
&gt; class RasterReaderWriter:<br>
&gt;     def __init__(self,  raster):<br>
&gt;         self.raster = raster<br>
&gt;         self.tileWidth =1024<br>
&gt;         self.tileHeight = 1024<br>
&gt;         self.tileNum = 0<br>
&gt;<br>
&gt;     def setTileSize(self,  width,  height):<br>
&gt;         self.tileWidth =width<br>
&gt;         self.tileHeight = height<br>
&gt;<br>
&gt;     def resetIterator(self):<br>
&gt;         self.tileNum = 0<br>
&gt;<br>
&gt;     def readNextTile(self):<br>
&gt;         tileRaster,  offsetX,  offsetY,  width,  height  =<br>
&gt; self.readTile(self.tileNum)<br>
&gt;         self.tileNum = self.tileNum + 1<br>
&gt;         return tileRaster,  offsetX,  offsetY,  width,  height<br>
&gt;<br>
&gt;     def readTile(self,  tileNum):<br>
&gt;         tilesInWidth = self.raster.XSize / self.tileWidth + 1<br>
&gt;         offsetX = tileNum % tilesInWidth * self.tileWidth<br>
&gt;         offsetY = tileNum / tilesInWidth * self.tileHeight<br>
&gt;         width = min(self.tileWidth,  self.raster.XSize - offsetX)<br>
&gt;         height = min(self.tileHeight,  self.raster.YSize - offsetY)<br>
&gt;         print offsetX,  offsetY,  width,  height<br>
&gt;         tileRaster = self.raster.ReadAsArray(offsetX,  offsetY, width,<br>
&gt; height)<br>
&gt;         return tileRaster,  offsetX,  offsetY,  width,  height<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; reader = RasterReaderWriter(gdal.Open(iFilename).GetRasterBand(1))<br>
&gt; print reader.raster.XSize<br>
&gt; print reader.raster.YSize<br>
&gt; tile, offsetX, offsetY, width, height = reader.readNextTile()<br>
&gt;<br>
&gt; Before the call to readNextTile() the raster XSize and YSize is correct.<br>
&gt; After the call the raster seems to have been corrupted as the XSize and<br>
&gt; YSize have invalid values.<br>
&gt;<br>
&gt; Any help would be appreciated!<br>
&gt;<br>
&gt; Riaan<br>
</div></div></blockquote></div><br>