[Qgis-developer] Access raster data from python plugins

Peter Ersts ersts at amnh.org
Mon Sep 8 11:03:10 EDT 2008

Ghislain Picard wrote:
> I'd like to access large part of the raster dataset to build 
> scattergram from two raster layers.
> Is there any mean to access raster data from python plugins more 
> efficiently than with the QgsRasterLayer::identify method ?
> A version of identify that return a single band would be interesting too.
> Finally what do you thing of a public access to mGDALDataset ?

Howdy Ghislain,
Welcome to the list and thanks for posting your questions.

At this time QGIS solely relies on GDAL but there has been some 
discussion about looking into the possibility to have other raster 
providers later on down the road (long way down the road). One major 
consideration is minimizing dependencies in the core. As for providing a 
public access to the GDAL dataset itself, it has been collectively 
decided that that would not be a good idea. You can in theory, access it 
(outside of QGIS) by getting calling QgsMapLayer::source() and opening 
the dataset yourself, but that is not an appropriate solution for QGIS 
and I am not promoting that idea.

QgsRasterLayer::identify() is the only method available at this time for 
getting pixel values. Your proposed method of getting a value from one 
band is possible, but still has too much overhead to make it useful for 
things like building scattergrams.

The more appropriate approach would be to ask for "stats" in a QgsRect 
or NxM window centered on a QgsPoint, which could include a scatterplot 
or compress maxtrix of frequency values as a return option. This would 
also pave the way for convolution filters and other functions. But these 
methods would need to be smart about asking for data and not just asking 
for a single pixel or small (3x3) window at a time. Rather these methods 
would/should load whole rows of data (below and possible above the 
window requested) and keep them in memory with the assumption that 
another request for neighboring data will be requested, in addition to 
being smart about preloading rows if it seems that a process is 
systematically asking for subsequent data. This, however, will not make 
it into v1.0 before the feature freeze but could (and should be) added 
somewhere in v1.x.

void QgsRasterLayer::idenitify(const QgsPoint&, QMap<Qstirng, QString>&)
and the Proposed
void double QgsRasterLayer::getValue(const QgsPoint&, int)

Should really be something more like
bool QgsRasterLayer::getValueAtCoordinates(const QgsPoint&, 
QMap<Qstirng, QString>&)
bool QgsRasterLayer::getValueAtCoordinates(const QgsPoint&, int&)
Which would open up other possibilities like (just to name a few),
bool QgsRasterLayer::getValueAtCoordinates(const QList<QgsPoint>&, 
QMap<QgsPoint, int>&)
bool QgsRasterLayer::getValueAtCoordinates(const QgsRect&, int, 
QMap<double, int>&)
Get pixel frequencies in QgsRect for band X and return a list or map 
<pixelvalue, frequency>
which would be great because it would also allow contrast enhancements 
to be defined based on the current data being displayed in the view 
port, but just all the data in the band.

So given the limited utility in the getValue function, I am hesitant to 
just drop it in, unless the other devs agree with move to 
getValueAtCoordinates() approach, then (as their current names are) 
getValue() can be added and identify() could simply call getValue() for 
each band. -- To make life easier identify() can can say in the API 
(marked as depreciated and slated for removal in 2.x ) and just be a 
wrapper for the getValueAtCoordinte().

Keep the ideas and sample code coming!


Peter J. Ersts, Project Specialist
American Museum of Natural History
Center for Biodiversity and Conservation
Central Park West at 79th Street
New York, New York 10024
Tel: Home Office (518)-632-4745 or NYC Office (212)-496-3488
Web: http://biodiversityinformatics.amnh.org
Web: http://cbc.amnh.org

Quantum GIS Raster Development Team. Visit http://www.qgis.org
to learn more about QGIS, a free and open source desktop GIS

Open Source,
...evolving through community cooperation to change the world bit by bit

More information about the Qgis-developer mailing list