[Gdal-dev] How to efficiently read in a GDAL image into a matrix

Jose Luis Gomez Dans josegomez at gmx.net
Thu Nov 8 05:48:20 EST 2007


Hi Ryan,

> I would like to read in an entire ENVI image into a GDAL Dataset, and  
> then, create a matrix the size of which is pixels X number of raster  
> bands.

I do this sort of thing in Python (+numpy et al.) fairly often. Unfortunately, I haven't got gdal, python or anything installed over here, but I think the code goes like this:

g=gdal.Open ( fname )
for band in xrange g.GetRasterCount):
   b = g.GetRasterBand( band ).ReadAsArray()
   #b is a 2D array with your band.
   #Use numpy's reshape to stack it however you like
   #In particular, you can use slice notation for this sort of thing

Using large-ish images (Landsat, around 6400x6500 pixels and a few bands), it is fairly confortable to read the data and apply algorithms using numpy and scipy. If you apply this to whole images, it is very fast. If you're using a few pixels at a time, use a masked array to improve performance.

Finally, another way to speed things up is to read N rows from B bands at a time and work on smaller chunks. If you have dual cores or several machines, you can try to process "several smaller chunks" in parallel, but I haven't tried that yet.


> Apparently it is a really bad idea to try and read one value at a  
> time out of each raster band, so could someone just tell me the best  
> method for doing this.

You might as well read a line (or several lines into a buffer, and deal with them. It is significantly faster (you make use of IO cache, processor cache, etc.) than going pixel by pixel. 

I am sorry I can't provide any code (I have done the approaches mentioned here in both python and C. In the end, when factoring the debugging and me screwing up with things, python  ends up being 100x faster than C :D).

Hope that helps,
Jose
-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser



More information about the gdal-dev mailing list