5

In QGIS 3.10 I want to order 22 polygons north to south based on their position using their centroids. I have used the centroids tool to determine where their middle is. But now I'm not sure now how to automate figuring out their north to south order.

1
  • 1
    Welcome to GIS SE. As a new user, please take the Tour. According to relational theory, rows are unordered, so unless an explicit ORDER BY clause is used, the processing engine is always free to return features in an order that is convenient. What is considered "convenient" depends on the data source and driver. Your question doesn't specify the source format or what you have attempted. Please Edit the Question.
    – Vince
    Jun 29 at 11:51

2 Answers 2

6

You can create a new field with this expression :

array_find( -- find the current feature id in the list
    array_reverse( -- reverse the order of the list, so greastest Y coordinate (= Northest) is first
        array_agg( -- create a list of features id ordered by Y coordinate of the centroid
            expression:=$id,
            order_by:=y(centroid($geometry))
        ),
        $id  -- current feature id
    )
) + 1  -- position starts from 0, so add 1

Using features ID instead of a list of Y coordinates is to eliminate the case when 2 features have exactly the same Y coordinate (even if it isn't very probable).

The expression y(centroid($geometry)) can be directly written as y($geometry) because of the definition of the y() function :

Returns the y coordinate of a point geometry, or the y coordinate of the centroid for a non-point geometry.

4

Use Order by expression if you just want to reorder the features:

y(centroid($geometry))

enter image description here

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.