1

I'm trying to find the shortest distance from a point (point A uploaded as a CSV) to a polygon uploaded as a shapefile (polygon 1) and then the distance from that point of the polygon to any of the points in the polygon (e.g. point 1B, point 1C, point 1D inside polygon 1, uploaded as another CSV).

So the output expected is:
Point A -> closest point on Polygon 1 (P1): x km
P1 -> point 1B: x km
P1 -> point 1C: x km
P1 -> point 1D: x km

That's what I am trying to get to, also on a bigger scale, i.e. point A is always the same, but there are multiple polygons and multiple points in each polygon. At the end of the day, for each point I would like to have the shortest distance from the point A to the polygon that it sits within and then from there to the point itself.

1 Answer 1

1

I've assumed you have 3 layers called BasePoint (your point A), Polygon, and PointsInPolygon.

Depending on how you want the distance displayed, you can get the distance from each Polygon feature to BasePoint using the following expression:

with_variable('A',geometry(get_feature_by_id('BasePoint',1)),round(
    distance(
        closest_point(@geometry,@A),@A
    )/1000,2
)) || ' km'

And you can get the distance from each PointsInPolygon feature to its containing Polygon using the following similar expression:

with_variable('A',geometry(get_feature_by_id('BasePoint',1)),round(
    distance(
        closest_point(
            overlay_within('Polygon',@geometry,limit:=1)[0],@A
        ),@geometry
    )/1000,2
)) || ' km'

Output is as below.

If for some reason your BasePoint does not have a feature id of 1, you could replace this with get_feature eg get_feature('BasePoint',"name",'A')

enter image description here

If you add more polygons/points, the expressions continue to work:

enter image description here

Note: I used a geometry generator to create the lines joining the relevant features, and also for labelling. For example, the line from Point A to the Polygon layer can be visualised by:

with_variable('A',geometry(get_feature_by_id('BasePoint',1)),
make_line(closest_point(@geometry,@A),@A))

enter image description here

and labelled using rule-based labelling:

enter image description here

enter image description here

(The expressions are the same as for the distance and the creation of the line)

2
  • Thanks Tom! Your explanation was clear and it gave me the exact results that I was hoping to achieve. Can you please guide me on how to use a geometry generator to create the lines joining the relevant features? I've tried looking around and played with the symbology and geometry generator within that then some make line expression to no avail as I don't know what I'm doing. It would be great in helping me illustrate what is going on.
    – QHNL
    Feb 18 at 16:03
  • I've put a brief description on geometry generators in the answer - if you need more information, then best to ask a separate question (site policy is only one question per question!) Feb 18 at 20:15

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.