# Finding maximum possible distance of point to polygon boundary using QGIS Expression

Points layer is structured as below

Polygons layer also has code as a field

For each point, I want to get maximum possible distance to the edge of the polygons listed with code in the overlap column. Line must not leave the polygon at any point e.g. in the case of complex polygon shapes.

The red arrow is the max distance to the edge

I tried this from the Field Calculator on the points layer: seems to work but gives more distances than overlaps! If there are two overlaps I need two maximum distances.

``````array_to_string(
array_foreach(
array_distinct(string_to_array("overlaps", ',')),
format_number(
maximum(
distance(
\$geometry,
boundary(
geometry(
get_feature('polygons___majorlang_6318baec_9d66_4f7c_ae25_a2b3a2e2ec27', 'code', trim(@element))
)
)
)
),
2
)
),
', '
)
``````

• Can you add a screenshot to see what you mean with "maximum possible distance to the edge of the polygons"? Jun 9, 2023 at 14:55
• done, say the yellow arrow is the nearest distance to the edge, then the red is the farthest possible distance to the edge Jun 9, 2023 at 15:10
• gis.stackexchange.com/questions/338921/… A similar concept but uses Postgres Jun 9, 2023 at 15:46
• Can you share the data?
– BERA
Jun 9, 2023 at 16:44
• github.com/datavantage/sampledata Jun 9, 2023 at 17:48

Use the following expression to find the vertex most distant (farthes) from a point inside a polygon - replace `polygon` on line 3 with the name of your polygon layer:

``````with_variable(
'vertices',
geometries_to_array (nodes_to_points( overlay_within ('polygon', \$geometry)[0])),
with_variable(
'max',
array_max(
array_foreach (@vertices, length (make_line(\$geometry,@element)))
),
array_filter (
array_foreach (
@vertices,
if (length (make_line(\$geometry,@element)) = @max, @element, NULL)
),
@element is not NULL
)
))[0]
``````

The expression checks for each vertex of the blue polygon the distance to the orange point (black dotted line; just for visualization) and returns just the vertex with the largest distance; the yellow circle (buffer) shows that indeed, the red point is the farthes away from the orange one:

• This is excellent I will try to do this for all overlapping polygons Jun 10, 2023 at 3:48