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")
You can use the
On a sample grid I've obtained this result with my own numbers (my grid has 13 columns per row):
So, in general:
To select exactly the other rows (i.e., 1st, 3rd, and so on) you would use:
Does it make sense?
EDIT: Improved answer
Thanks to @ndawson, who suggests a shorter version of the selection expression. In summary, you could use:
to select 2nd, 4th, 6th, ... rows, or:
to select 1st, 3rd, 5th, ... rows.
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.
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.
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.