qgis – Counting Points Within Polygon by Attribute in QGIS

countpoint-in-polygonqgis

I have one table ('TABLE A') with points of Data divided by Latitude, Longitude & Hour. On the other hand I have another table ('TABLE B') with County names.

How do I count points (from 'TABLE A') within counties ('TABLE B') but grouping them or maintaining hour segmentations?

Best Answer

You can use a virtual layer or execute SQL from processing toolbox with this query:

select pol.*, 
       poi.h as point_hour, 
       count(poi.h) as point_count
from polygonlayer as pol, pointlayer as poi 
where intersects(pol.geometry, poi.geometry) 
group by poi.h

where h is the name of your hour field. Note that this will return no row for empty counts. So if there is no point with hour 3, you will get no row for this hour.

The result will be duplicated polygons with one row for each hour and count that exists at least once.

Related Question