4

Some time ago I made a post about creating a field in a polygon layer that get its value from length of line intersecting them. The answer I got at the time helped me for a specific purpose, but now I need it to be a little more complex.

The image below represents what I'm trying to achieve: enter image description here

This is the answer I got:

with_variable (
    'measure',
    length (
        make_line (
            start_point (
                collect_geometries (
                    overlay_nearest (
                        'line', 
                        $geometry
                    )
                )
            ),
            closest_point (
                intersection(
                    $geometry, 
                    collect_geometries (
                        overlay_nearest (
                            'line', 
                            $geometry
                        )
                    )
                ),
                end_point (
                    collect_geometries (
                        overlay_nearest (
                            'line', 
                            $geometry
                        )
                    )
                )
            )
        )
    ),
    if (
        round ( @measure) %2 =0,
        round ( @measure),
        if ( 
            @measure -  floor( @measure) < 0.5,
             ceil( @measure),
             floor( @measure) 
        )
    )
)

The problem with this answer now is that it does not recognize any change of directions in the line layer, computing only the straight length from start point to end point, thus not working in a ensemble of land lots that have a slight or accentuated curves. So it is only useful in straight formations.

So my question now is, is there a way to modify this code so that it can also perform the task in irregular shaped set of land lots?

3
  • To understand you correctly: the line is not straight, but the polygons (land plots) are? Can you provide sample data?
    – Babel
    Commented Sep 2, 2021 at 14:14
  • On this link I put a sample to better clarify what I'm seeking. There's two groups of land lots, one pretty straight where the values are all fine, and a curved one, where there's some differences. In the Land lot layer there's also a field named "ItShouldBeNumber" where I put the value that I expected to have. Thank you!
    – Felippe M.
    Commented Sep 2, 2021 at 15:13
  • 1
    Sorry, you're right: the expression measures a straight line always, it doesn't follow the line's shape
    – Babel
    Commented Sep 2, 2021 at 15:31

1 Answer 1

3

You can modify the expression by using:

  1. line_locate_point (to get the distance from the start-point of the line to the point where the line intersects with the polygon (white dot on the screenshot below: the point point where the line intersects the polygon for the second time, = step 5 in the original solution)

  2. line_substring to get the substring (blue on the screenshot) of the line from the start-point to this point where the line intersects the polygon for the second time.

The whole expression looks like this:

with_variable (
    'measure',
    length (
        with_variable (
            'line',
            collect_geometries (
                overlay_nearest (
                    'line', 
                    $geometry
                )
            ),
            line_substring( 
                @line, 
                0,
                line_locate_point (
                    @line,
                    closest_point (
                        intersection(
                            $geometry, 
                            collect_geometries (
                                overlay_nearest (
                                    'line', 
                                    $geometry
                                )
                            )
                        ),
                        end_point (
                            collect_geometries (
                                overlay_nearest (
                                'line', 
                                $geometry
                            )
                        )
                    )
                )
            )
        )
    )
),
if (
    round ( @measure) %2 =0,
    round ( @measure),
    if ( 
        @measure -  floor( @measure) < 0.5,
        ceil( @measure),
        floor( @measure) 
        )
    )
)

Screenshot: white dot and blue line are created here for visualization purpose only and only for the 7th polygon - it is the length of the blue line that is measured: enter image description here

2
  • It works perfectly, thanks!
    – Felippe M.
    Commented Sep 3, 2021 at 21:50
  • 1
    I'm glad. In fact, this expression replaces the older one from the first post as it eork well wirh straight lines and non straight as well.
    – Babel
    Commented Sep 3, 2021 at 21:55

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.