1

I would like to ask for your help with calculating in simple way distance of points feature to one line (for example, what is distance of cities from the national border).

I see there were few topics on this question but I did not manage to calculate it. I tried to use Distance to nearest hub (line to hub) but distances seem to go in the direction of center and not in the direction of the nearest distance.

2

1 Answer 1

4

You may want to select your line using a different criterion, here I opt for the nearest.

To get the shortest distance between points and the (nearest) line feature, you can use the field calculator with this expression:

distance(
    $geometry,            -- the current point geometry
    overlay_nearest(
        'border',         -- the name of your line layer
        $geometry
    )[0]                  -- get the first feature from the array of nearest features
)

enter image description here

enter image description here

To get the distance to the nearest point on the boundary of a polygon:

distance(
    $geometry,
    boundary(                  -- get the boundary of the polygon as a line geometry
        overlay_nearest(       
            'polygon',
            $geometry          
        )[0]                   -- get the polygon nearest to the point
    ) 
)

enter image description here

Note: lines and labels were created separately to visualise the result of the expressions

Get a specific feature using an attribute value:

distance(
    $geometry,
    boundary(
        geometry(
            get_feature(
                'countries',          -- layer name
                'NAME',               -- field name
                'Zambia'              -- attribute value
            )
        )
    )
) / 1000                              -- convert m to km

enter image description here

1
  • 1
    Thank you a lot!
    – Guest
    Commented Dec 16, 2022 at 14:11

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.