Monday 4 March 2019

Is there any simple integration for counting points in polygon using python qgis?



Is there any simple integration for calculating count of points in polygon using QGIS-Python integration?


In other words, are there any pre-defined functions for points in polygon of QGIS using python, so that we can use them in our python script?



Answer



Great methods already posted! There are also tools from the Processing Toolbox which you could call from your script (this is assuming you are running scripts inside QGIS):



  • Count points in polygon

  • Counts points in polygon (weighted)

  • Count unique points in polygon


Taking the first tool, you could call it using:



import processing

Result = "path\to\saved_output.shp"
processing.runalg("qgis:countpointsinpolygon", "path\to\polygon_layer.shp", "path\to\point_layer.shp", 'NUMPOINTS', Result)

where NUMPOINTS is the name of the field which stores the number of points.


I'm using QGIS 2.8.2-Wien, Processing plugin v2.9.3. Hope this helps!


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...