Saturday, 26 November 2016

How to query 1 feature of each category using QGIS (SQL like) expression language?


I use QGIS 3.2 to select features in a shapefile. Lets say I have shapefile which contain 100 features and have such fields:




  1. Class with possible values A,B,C.

  2. ID with possible integer values 0, 1, 2, 3, ... , 99.


How to select exactly one feature (no matter which one) in every class A,B,C?



Answer



Using the Select by Expression you can type the following expression:


"Class" IN ('C', 'B') AND 
"ID" = minimum( "ID", "Class")


This expression will select the feature with Class = B, C with the lowest ID, assuming the ID is a unique value.


If the ID is not a unique value, using the field calculator you can create a new integer column, name it ser_id or something along the line and assign to it the value $id. This is the row number and it is a unique value.


field calculator


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