1

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?

4

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

  • Missing quotes " around field name ID. – Mr. Che Jul 24 '18 at 12:45
  • 1
    It can actually work without the double quotes for the field names. If you double-click on the field name from the list on the right, you will get the double quotes, but you can type the field name and it will work equally well. The expression could be Class = 'A' and ID = minimum(ID). This has worked for me. – Techie_Gus Jul 24 '18 at 12:50
  • 1
    Oh, didn't know that. – Mr. Che Jul 24 '18 at 12:52
  • 3
    While it's often safe to omit " double quotes, it's good practice to always use them, so you don't forget it when you suddenly run into a field name with spaces or other fancy characters. – Matthias Kuhn Jul 24 '18 at 15:31
  • 1
    I test suggested expression and it only work in one situation - when Class's ID value is minimal (zero). Here is the correct expression: "Class" IN ('B', 'N') AND "ID" = minimum( "ID", "Class") – Mr. Che Jul 25 '18 at 10:48

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.