[Qgis-developer] Raster histogram in Python plugin

Tim Sutton lists at linfiniti.com
Sun Oct 17 15:36:05 EDT 2010


Hi

On Sun, Oct 17, 2010 at 7:15 PM, Benoit de Cabissole <benoit at exigesa.com> wrote:
>  Hi Tim,
>
> Many thanks for your reply.
>
> I see that the API doc classifies HistogramVector as a QVector but I
> cannot find any reference of QVector in the Qt doc (there are QVector2D,
> QVector3D and QVector4D). Could it be the problem?

I found QVector in my (Qt 4.7) docs using Qt Assistant - not sure why
its not showing up for you. Perhaps it is not ported to the python SIP
lib.

>
> What I'm trying to do is to improve the speed of my plugin '1-band
> raster colour table' that allows to create colour table from a colour
> palette and a colouring method.

Yup I use your plugin regularly - its great!

> As I'm doing it in Python, the
> computation of the stats and histogram takes for ever on large rasters.
> I was hoping to piggyback on the stats and histo function built in QGIS
> (they themselves are (very) slow on large rasters).

Right this is an issue. Part of the reason why QGIS is so slow is that
it actually does two complete scans of each pixel to calculate the
stats. This is needed for computing stddev - although since writing
that code I have seen a way to compute stddev in a single pass - I
just need to find the formula and the time to implement that.

GDAL computes stats more quickly as it a) doesnt calculate stddev and
b) will estimate things like min/max, histograms etc if you like based
on a sampling of the data.

GDAL can also store gathered stats (you may have noticed aux.xml files
lying around in your raster dirs).

In the longer term I would like to a) persist QGIS gathered stats, b)
implement faster stddev calcs as mentioned above and c) only compute
IO intensive stats like stddev if they are actually needed (e.g. if
the user elects to use stddev in the raster symbology options).
>
> I agree that this work should be patched to the current code, but I'm
> not able to program in C++/C...
> So once the python plugin works as expected, it would be a good idea to
> hand it over to a C++ guru for patching!

Yeah if I have time I may take a look - your plugin is really needed
in the core of QGIS.

Regards

Tim

>
> Cheers,
> Benoit
>
>
> On 17/10/2010 19:00, Tim Sutton wrote:
>> Hi
>>
>> I see that member is commented out in the sip file for rasterbandstats:
>>
>> python/core/qgsrasterbandstats.sip
>>
>>  40     //HistogramVector * histogramVector;
>>
>> If I uncomment it I get a compile time error:
>>
>> sip: QgsRasterBandStats::histogramVector has an unsupported type -
>> provide %GetCode and %SetCode
>> make[2]: *** [python/analysis/sipanalysispart0.cpp] Error 1
>> make[1]: *** [python/CMakeFiles/python_module_qgis_analysis.dir/all] Error 2
>>
>> So I guess the sip bindings need some tweaking to make it work.
>>
>> As an aside, if you are planning any tweaks improvements to the raster
>> historgram, it would be great to do it as a patch to existing
>> properties dialog rather than as a separate plugin. I found out how to
>> enable zoom (but I wont do it in time for 1.6 release) on the charts.
>> I also plan to add to the chart the user configured min/max entries
>> per band, and maybe later a way to interactively set them on the
>> chart.
>>
>> Regards
>>
>> Tim
>>
>> On Sun, Oct 17, 2010 at 4:09 PM, Benoit de Cabissole <benoit at exigesa.com> wrote:
>>>  Hi all,
>>>
>>> Is it possible to access the histogramVector attribute from the
>>> QgsRasterBandStats Class in Python code from a plugin?
>>>
>>> When the following code is run it issues the error:
>>> AttributeError: 'QgsRasterBandStats' object has no attribute
>>> 'histogramVector'
>>>
>>> self.theLayer.populateHistogram(1,grd.S.nClass)
>>> myStats = self.theLayer.bandStatistics(1)
>>> grd.S.Histo[0, 0] = myStats.histogramVector[0].y()
>>>
>>> where 'self.theLayer' is a one band raster image and 'grd.S.nClass' = 40001
>>>
>>> Python version: 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32
>>> bit (Intel)]
>>> QGIS version: 1.6.0-Trunk Trunk, 14378
>>> >From OSGeo4W on WinXP.
>>>
>>> Cheers,
>>> Benoit
>>>
>>> _______________________________________________
>>> Qgis-developer mailing list
>>> Qgis-developer at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>>
>>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Tim Sutton - QGIS Project Steering Committee Member (Release  Manager)
==============================================
Please do not email me off-list with technical
support questions. Using the lists will gain
more exposure for your issues and the knowledge
surrounding your issue will be shared with all.

Visit http://linfiniti.com to find out about:
 * QGIS programming and support services
 * Mapserver and PostGIS based hosting plans
 * FOSS Consulting Services
Skype: timlinux
Irc: timlinux on #qgis at freenode.net
==============================================


More information about the Qgis-developer mailing list