2

I need to number the polygons in a certain order in QGIS 3.26. A good solution is the command according to @Babel's solution https://gis.stackexchange.com/a/417062/218260, but if the line crosses the polygon more than once, the numbering of the polygon is sometimes wrong, see the attachment.

array_find ( 
   array_sort( 
        array_filter(  
            array_agg(  
                line_locate_point( 
                    overlay_crosses(
                        'line', 
                        $geometry
                    )[0],
                    array_foreach (
                        overlay_crosses(
                            'line', 
                            $geometry
                        ),
                        centroid (
                            intersection ($geometry, @element)
                        )
                    )[0]
                )
            ), 
            @element > 0
        )
    ),
    line_locate_point( 
        overlay_crosses(
            'line', 
            $geometry
        )[0],
        array_foreach (
            overlay_crosses('line', $geometry),
            centroid (intersection ($geometry, @element)
        )
    )[0]
    )
)+1    

enter image description here

Or do you know another way to number the polygons in any order without having to manually enter the number?

1
  • 1
    Have a look at this post, though you would probably also have to filter for the minimum found index when there is multiple line-polygon intersection. If all fails, see this post for a way of numbering the polygons by clicking on them
    – JGH
    Aug 22, 2023 at 12:38

1 Answer 1

2

First create a field (I'll call it distance), that stores the distance from the star point of the linear geometry to the first intersection with the polygon.

with_variable(
  'line',
  overlay_intersects('line_layer', $geometry)[0], -- put inside quoted line layer name/id
  length(
    array_filter(
      geometries_to_array(difference(@line, $geometry)),
      touches(point_n(@line, 1), @element)
    )[0]
  )
)

Then, calculate a new field numbering in ascending order the result of the distance field.

array_find(
  array_agg(
    "distance",
    order_by:="distance"
  ),
  "distance"
)

Replace wherever "distance" appears with the name of the field calculated with the first expression.

2
  • First command does not work. I am sending a picture in the attachment. Do I need to change 'line_layer' or 'line' according to my line name? i.stack.imgur.com/YYpdP.jpg
    – dysano
    Aug 24, 2023 at 10:23
  • 1
    @dysano, Yes, there was an error with a function name, I already fix it.
    – Mayo
    Aug 27, 2023 at 16:37

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.