Sunday, 17 December 2017

printing - How to count points within the current Print composer atlas feature in QGIS 2.8?


In the print composer when generating an atlas, does anyone know how to show he feature count per each atlas coverage feature?


For example, I have generated an atlas where each map is a different province (in Cambodia) containing a village point layer. When I click the sigma sign at the bottom of the legend for villages, the feature count will calculate the entire country's number of villages, not for the individual province's number of villages.


Does anyone know how to get a feature count in for the province showing in the atlas?



Answer



This works for me: use the aggregate() expression in a text label, with within($geometry,@atlas_geometry) as a filter for points within your atlas feature.



The full syntax in your text label would be


[% aggregate('LAYER_NAME','count',"UNIQUE_NON_NULL_ATTRIBUTE_FOR_COUNTING",within($geometry,@atlas_geometry)) %]

That will count all the points in your specified layer - by an unique non-null attribute, so each point is distinct - but only if the point geometry is within the atlas geometry.


The [% %] is required to make the expression work. And note the quotation marks - you must use double quotes for the attribute name.


As a bonus you can add further filters, so to address one of the comments above, you can modify the filter to say within($geometry,@atlas_geometry) AND "village" = 'A', or whatever combination.


I haven't extensively tested this out, especially on large/complex datasets, but it seems to do the job.


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...