6

I have a polygon layer. I would like to select every second row of the polygons but not manually. Is there an expression to select every second row as seen on the picture with Select by expression in QGIS?

*(Numbers refer to the polygon's "ID")

Numbers refer to the polygon's ID

8

You can use the floor function in your expression, like this:

floor($id /24) IN (1,3,5,7,9,11,13,15,17,19,21)

On a sample grid I've obtained this result with my own numbers (my grid has 13 columns per row):

enter image description here

I've used: floor($id/13) IN (1,3,5,7,9)

So, in general:

floor($id / number_of_columns) IN (1, 3, 5, ..., 2*number_of_rows_to_be_selected - 1)

To select exactly the other rows (i.e., 1st, 3rd, and so on) you would use:

floor($id /24) IN (0,2,4,6,8,10,12,14,16,18,20,22)

or:

floor($id / number_of_columns) IN (0, 2, 4, ..., 2*number_of_rows_to_be_selected)

Does it make sense?

EDIT: Improved answer

Thanks to @ndawson, who suggests a shorter version of the selection expression. In summary, you could use:

floor($id / number_of_columns) % 2 = 1

to select 2nd, 4th, 6th, ... rows, or:

floor($id / number_of_columns) % 2 = 0

to select 1st, 3rd, 5th, ... rows.

  • 1
    Perfect, exactly this is what I was looking for! Thank you so much! – Skye Feb 12 '16 at 19:38
  • 1
    The floor function is brilliant! Thanks! I learned something very useful today! – jbgramm Feb 12 '16 at 20:11
  • One more thing came to mind: In case I had awful lot of rows, could I simplify somehow the expression's "number of rows to be selected"-part after the "IN" operator? For example, if I had 1000 rows but I only needed every even-numbered row: (2,4,6,8,....,996,998,1000) could I create some kind of a range-ish expression instead of typing each number one-by-one? (I've tried a few things but none of them worked) – Skye Feb 13 '16 at 19:59
  • 2
    Try "floor($id /24) % 2 = 1" – ndawson Feb 13 '16 at 20:19
  • Thank you @ndawson, that's much better! I'll edit the answer with your comment. – Germán Carrillo Feb 13 '16 at 21:00
4

You can use the "select by expression" geoalgorithm with an expression like id>= and id<=24 or id>=49 and id<=72..... Since the cell values seem to be numbered by standard count laterally, it shouldn't be a problem to figure out the ranges you need even if your data set contains more rows than listed above.

  • Yes, this works, but it takes at least as much time to type all the intervals as if I selected each row one by one manually. It would be great to find a faster solution if possible. – Skye Feb 12 '16 at 20:05
4

Make sure the layer you wish to choose is highlighted in the Layer Control Box Then using the Select By Expression button

Type what you see in the picture into the Expression Box.

It should look like this when you are finished

(( "ID" ) - 1) % 24 = 0

If the expression is correct you will see a result in the bottom of the Expression Builder Dialog that does not "Expression is Invalid" in red letters.

ID is the name of the field you wish to select, the first polygon of each row minus 1 is divisible by 24.

% is the modulo function, it will take the number immediately following, and divide the ID -1 number by it. If the remainder is not 0, the polygon is not chosen.

QExpression

Ignore the error displayed in the above picture, I just did this with a table that was not a grid, and did not have a field named ID.

  • 1
    I think you found a way to select by column, which could also be useful for other use cases. – Germán Carrillo Feb 12 '16 at 20:02
  • 2
    You are absolutely right! I totally misread, and misunderstood the question. – jbgramm Feb 12 '16 at 20:08
  • Now I know how to select the first column (which is a new information for me). I wonder whether it would be achievable to select every second column somehow with the help of your expression – Skye Feb 12 '16 at 20:18
  • I am trying to wrap my head around the SQL logic to be able to choose the odd, or even numbered rows using the % function rather than explicitly typing them It would be something like this, but I am unable to see if it works at this time. floor($id /24) IN ( (% 2 = 0)) for even numbered rows floor($id /24) IN ( (% 2 = 1)) for odd numbered rows Or something like that. – jbgramm Feb 12 '16 at 20:29
  • 2
    I think you would need a list and the IN operator to select more than one column, something like $id % 24 IN (1,3,5,7,9,11,13,15,17,19,21,23) – Germán Carrillo Feb 12 '16 at 20:43

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.