Extract longest distance between a point and the edge of a polygon for multiple polygons in QGIS

I want to calculate the longest distance between the edge of a polygon and the point that is inside it (or if no point is inside, then the closest point). The polygons are catchments and the travelling distance from the edge of a polygon to a point inside the polygon is wanted. If no points are inside the polygon, the nearest point is used (the neasrest point by means of the nearest line). The longest distance are kept within the polygons and do not cross the polygon border unless there are no points within the polygon.

I find a lot of solutions for the nearest line (such as distance to nearest hub), but not so many on the longest distance. My points are not connected to the polygons (i.e. they are not the centroids), but they have an attribute column in common with ID names of the points. That is, each of the polygons have an attribute with the ID of a point.

I'm still beginner to QGIS, so I'm not that comfortable with post-gis or any kind of coding (but field calculator is okay). Since there are multiple polygons in my layer (100+), I'm searching for a method that does this 'automatically' / without me having to 'measure' each polygon manually.

Does anyone know a good way to solve this?

I've looked at: Distance between centroid and farthest point of polygon which had the focus from a centroid, but otherwise similar.

Also this one is similar and describes the longest distance wish: Finding maximum possible distance of point to polygon boundary using QGIS Expression . Maybe I'm just not good at adapting codes but it didn't work for me. The description was fine. My polygons do not overlap, I just have a lot of them. The figure with gren area describes the case from the link, with the red arrow representing longest distance and yellow the shortest (seen below).

And this: How could you measure the minimum and maximum distance to the outer edge of a polygon in QGIS actually gave me some results, but the max distance was too short in some samples I checked.

• Maybe it would help if you could show us what you mean with "longest distance", this is not clear to me. Also try to find an unambiguous definition of the line (longest distance) you're looking for. Oct 24, 2023 at 21:16
• Mathematically, this distance should be equivalent to the radius of the smallest circle with the point as the centre that completely contains the polygon. I haven't spotted a solution that uses this observation though! Oct 26, 2023 at 0:48

I think you can use Hausdorff distance:

it is the greatest of all the distances from a point in one set to the closest point in the other set

1. Field Calculate a string field in the polygon layer with the expression `geom_to_wkt( boundary(\$geometry ))`. This will create a string representation of the polygon boundary. Name it `polyboundary`
2. Join attributes by nearest with point layer as input layer and the field calculator output layer as input layer 2. Join only the `polyboundary` field from step 1. Maximum nearest neighbors = 1.
3. Field calculate a double field with the expression: `hausdorff_distance( \$geometry, geom_from_wkt("polyboundary"))`. It will return the longest distance from each point to the polygon boundary.

Result:

• BERA, thank you for your suggestion and easy to follow method. It works for most of the cases. However, the suggested method is focusing on the points and relating a polygon, but my objective is that all of the polygons should be connected to a point (so polygon find a point opposed to point finding a polygon). As I have more polygons than points, several polygons could be connected to the same point. Should I just join polygon and point layers, and then handle the double cases manually by measuring, or do you have another suggestion? Oct 27, 2023 at 10:43
• The same logic can be used. Join a point to each polygon instead of a polygon to each point.
– BERA
Oct 30, 2023 at 10:11