[Qgis-developer] error for QGIS tutorials in python - adding rasters to canvas

Peter Ersts ersts at amnh.org
Thu Jan 24 10:46:50 EST 2008


QgsRasterLayer::setDrawingStyle()  [Same in 0.9.1]
QgsRasterLayer->setColorShadingAlgorithm() [was setColorRampingType in 0.91]
QgsRasterLayer->setContrastEnhancementAlgorithm() [was 
setColorScalingAlgorithm in 0.9.1]
***Each of these sets a corresponding get method. And these are the only 
three things that must be understood to draw a raster layer

in 0.9.2, the abstracted draw process per pixel is:
For gray scale or RGB images
Read data from band -> Check NoDataValue -> Get transparency level -> 
Stretch -> Invert (if necessary) -> Display
For paletted, pseudo color, for custom color maps
Read data from band -> Check NoDataValue -> Get transparency level -> 
Shade(color) -> Invert (if necessary) -> Display

There are now a couple of new classes:
QgsContrastEnhancement - This class is responsible for implementing the 
contrast enhancement. QgsRasterLayer calls 
QgsContrastEnhancement::stretch(pixel value) and is returned a stretched 
value(int). The actual stretching is done by what ever the current  
QgsContrastEnhancementFunction [virtual base class] does. This is a per 
pixel operation at the moment, because it is close to a direct port of 
what was previously being done in the draw loop in QgsRasterLayer. For 8 
and 16 bit data a lookup table is generated to speed things up, 32-bit 
data are computed each call to stretch().

QgsRasterShader - Is similar in idea to QgsContrastEnhancement, but no 
lookup table at the moment. QgsRasterShader::generateShadedValue(pixel 
value, &returnRed, &returnGreen, &returnBlue) computes a RGB value for 
the provided pixel value. The actual computation is done by a 
QgsRasterShaderFunction[virtual base class].

Programatically, you could create your own shader or contrast 
enhancement function by inheriting the base QgsRasterShaderFunction or 
QgsConstrastEnhancementFunction then calling,
QgsRasterLater::setSetRasterShaderFunction(new MyRasterShaderFunction())

Hope that helps a little....


Martin Dobias wrote:
> Hi Tim,
> thanks for the snippet. I've updated the examples accordingly, I've
> also kept the older version for those interested.
> http://blog.qgis.org/?q=node/59
> Beware - you need most recent SVN trunk since it now contains fixes to
> bindings which got broken by the recent merges.
> Is there any source of information how does the raster
> rendering/handling work now? I was unable to find anything and some of
> the terms are quite new to me...
> Bye
> Martin
> 2008/1/24 Tim Sutton <tim at linfiniti.com>:
>> Hi
>> Yes the raster API is changed. I dont know the python stuff so well
>> but in the case of C++  you would do something like this:
>> 117        mpRasterLayer->setDrawingStyle(QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR);
>> 118        mpRasterLayer->setColorShadingAlgorithm(QgsRasterLayer::PSEUDO_COLOR);
>> 119        mpRasterLayer->setContrastEnhancementAlgorithm(
>> 120               QgsContrastEnhancement::STRETCH_TO_MINMAX, false);
>> You will need to translate that to python api but it should be quite
>> similar. Take a look at
>> http://svn.qgis.org/trac/browser/trunk/qgis/src/core/raster/qgsrasterlayer.h
>> for the different drawing styles and shading algorithms you can use.
>> Regards
>> Tim
>> 2008/1/23, Martin Dobias <wonder.sk at gmail.com>:
>>> 2008/1/23 volkan kepoglu <vkepoglu at gmail.com>:
>>>> downloaded QGIS tutorials in python, changed qgis_prefix and runned
>>>> 2_basic_main_window and 3_basic_labelling successfully,
>>>> but 4_adding_rasters_to_canvas gives the following error;
>>>> vkepoglu at vkepoglu:~/qgis/tutorials/4_adding_rasters_to_canvas$ python
>>>> mainwindow.py
>>>> Traceback (most recent call last):
>>>>   File " mainwindow.py", line 76, in addLayer
>>>>     layer.setColorRampingType(QgsRasterLayer.BLUE_GREEN_RED)
>>>> AttributeError: setColorRampingType
>>> Ah, that will be caused by the recent refactoring of the raster classes.
>>> Peter / Tim - can you suggest how to change the code to make it working again?
>>>> how can ı fix?
>>>> or the most important than to fix this,
>>>> how can ı learn to qgis bindings to python.
>>>> where should ı look for reference of python bindings
>>>> http://wiki.qgis.org/qgiswiki/PythonBindings, is there any other references?
>>> Today I've updated PythonBindings wiki page with some resources
>>> (listed in top part in Using PyQGIS section). Basically it's good to
>>> look at QGIS C++ API reference, QGIS blog, tutorials ported to Python
>>> and QGIS plugins that have been done already. (all links listed there)
>>> Martin
>>> _______________________________________________
>>> Qgis-developer mailing list
>>> Qgis-developer at lists.qgis.org
>>> http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
>> --
>> Tim Sutton
>> QGIS Project Steering Committee Member - Release  Manager
>> Visit http://qgis.org for a great open source GIS
>> openModeller Desktop Developer
>> Visit http://openModeller.sf.net for a great open source ecological
>> niche modelling tool
>> Home Page: http://tim.linfiniti.com
>> Skype: timlinux
>> Irc: timlinux on #qgis at freenode.net
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.qgis.org
> http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer

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