2

I am able to summarise data about particular area of a raster by generating a temporary polygon and using the Zonal Statistics tool as normal. Is there a tool or a way to generate summary data about an area of a raster using cursor selection - ie without generating the temporary polygon?

Ideally I would like to be able to use the Identify Features tool to define an area and see the summary statistics for the selected area in the Identify Results panel.

1 Answer 1

3

You can use pyqgis to create a function which creates a temporary vector layer of your one selected polygon, executes zonal statistics and print the results if the python console.

Adjust the input layer names in the code below and execute it in the python editor. Then select one polygon and call the function with giveStats() in the console.

def givestats():
    vectorName = 'Grid' #The name of your vector zones layer
    rasterName = 'nh_68_3' #Your raster layername
    vl = QgsProject.instance().mapLayersByName(vectorName)[0]
    rl = QgsProject.instance().mapLayersByName(rasterName)[0]
    vl_selection = vl.materialize(QgsFeatureRequest().setFilterFids(vl.selectedFeatureIds()))
    zonstats = processing.run("native:zonalstatisticsfb", {'INPUT':vl_selection,
        'INPUT_RASTER':rl,'RASTER_BAND':1,'COLUMN_PREFIX':'_',
        'STATISTICS':[0,1,2,3,4,5,6,7,8,9,10,11],'OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT']
    for f in zonstats.getFeatures():
        for field in zonstats.fields():
            if field.name().startswith('_'):
                print(f"{field.name()}:{f[field.name()]}")
                

enter image description here

2
  • This is interesting but requires that I already have in my project a grid or other polygon that defines the area of interest. What I am after is a way to define the area with the cursor (polygon / radius / freehand) and get summary statistics for that area.... maybe this should be pitched as an enhancement request.
    – Albert
    Jun 24 at 8:31
  • Create a temp scratch layer, draw the area. It takes 10 s
    – BERA
    Jun 25 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.