6

I've tried using "Intersect" and "Join attribute by location" but these aren't achieving exactly what I'm after. I have a series of points inside and outside a polygon.

enter image description here

My aim is to create/update a single field with the distance from the polygon to the points outside it. Any that are inside the polygon will ideally have their distance set to 0. This forms part of a series of operations in a graphical model.

Currently I am using "Distance to nearest hub" to find the distances, but this also gives the distance of the points inside the polygon.

enter image description here

Is there a way to automatically update/append a value of 0 in the Distance field (HubDist) for only the points inside the polygon?

Taras
  • 32,823
  • 4
  • 66
  • 137
Jiinglelocks
  • 480
  • 2
  • 10
  • 2
    You want a field with the average distance to the points outside each polygon? How far do you want to search for points, or should all points be included for each polygon? – BERA Dec 15 '21 at 07:56

3 Answers3

2

Following the Distance to Hub tool, Select by Location all points that are inside the polygon. Then Field calc those points equal to zero.

Stu Smith
  • 8,252
  • 8
  • 35
  • 84
  • This works outside the graphical modeler, however I'm trying to automate these steps so: Distance to Hub tool gets distances > Select by Location selects points within the polygon > Field calc updates the "HubDist" field with 0. Unfortunately the Field calc does not show the selected features as an input layer. – Jiinglelocks Dec 14 '21 at 20:19
  • I do not understand "... the Field calc does not show the selected features as an input layer." Please elaborate. – Stu Smith Dec 14 '21 at 20:54
2

Use this expression:

length (
    if (
        overlay_within ('polygon'),
        '',
        make_line (
            $geometry, 
            closest_point( 
                overlay_nearest ('polygon',$geometry)[0], 
                $geometry
            )
        )
    )
)

Lines and labels created based on the expression from above: enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
1

If only length values are needed, please use the following expression in the "Field Calculator":

if(
    overlay_contains('points_test'),
    0,
    distance(
        $geometry,
        array_first(overlay_nearest('points_test', expression:=$geometry, limit:=1))
        )
    )

p.s. 'points_test' is the name of point layer

result

Mind, that values in the "dist" field are in Ellipsoidal - EPSG:7019.


References:

Taras
  • 32,823
  • 4
  • 66
  • 137