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?

1
  • 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? Commented Dec 15, 2021 at 7:56

3 Answers 3

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.

2
  • 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. Commented Dec 14, 2021 at 20:19
  • I do not understand "... the Field calc does not show the selected features as an input layer." Please elaborate. Commented Dec 14, 2021 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

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:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.