I am using QGIS 3.6.

I'm trying to create a map which labels each parcel according to it's position along the red line, from West to East. I've tried creating a column which uses each features position within the attribute table to do this, however as you can see in the picture, they are not in any sensible order. I normally do this manually, however I have over 3000+ features to edit in that case, it would be good to find an automated solution.

enter image description here

1 Answer 1


This answer will number parcels from West to East.

You can create a new layer with the ordered ID

Go the the menu layer / add layer / add-edit virtual layer and enter the following query.

select *, ROW_NUMBER() OVER(ORDER BY ST_MinX(geometry) asc, ST_MinY(geometry) asc) as newID
FROM myLayer

you can replace the layer name (myLayer) with the true name.

The query gets all rows from myLayer then orders each polygon by X and Y and generates a new sequential row number.

To number East to West, you would use st_maxX (to get the eastern most coordinate of each polygon) and you would order the coordinates in descending order (ORDER BY ST_MaxX(geometry) desc, ST_MinY(geometry) asc).

  • This example unfortunately does not work for me in QGIS 3.8.1. And unfortunately I'm too stupid to debug the even more stupid sql-error output: 1 - near "(": syntax error
    – Bernd V.
    Commented Sep 14, 2019 at 15:04
  • @BerndV. Please provide the complete query you have used. It could be a missing coma or parentheses. If the reported error location doesn't make sense, the trick is to look at the previous statement in the query. PS: don't be too hard on yourself.... not knowing doesn't mean being stupid - else schools would be FULL of stupids
    – JGH
    Commented Sep 14, 2019 at 15:53
  • I simply copy/pasted the example above. I even renamed my layer to myLayer like in the example. So try yourself with a copy your own code ;)
    – Bernd V.
    Commented Sep 14, 2019 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.