QGIS – Ordering Polygons from North to South

centroidsfield-calculatororderpolygonqgis

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.

Best Answer

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.

Related Question