# QGIS GRASS maximum distance to a given feature

I want to calculate the maximum distance between a point and the boundary of a polygon (representing an animal's home range).

Here is an example of the point layer, always within the polygon layer:

When I needed the minimum distance, I used "QGIS GRASS > v.distance > minimum distance to nearest feature".

Does anyone know how to calculte the maximum distance in QGIS/GRASS?

• You can use PyQGIS instead of QGIS/GRASS. Commented Oct 5, 2016 at 6:16

You probably need the maximum minimum distance between all your values. For this reason you don't have available this option in v.distance. However, it's not very difficult to determinate it in QGIS by using PyQGIS. Next code does the work (in your case change the names of respective shapefiles):

``````registry = QgsMapLayerRegistry.instance()

points = registry.mapLayersByName('Random points')
polygon = registry.mapLayersByName('polygon2')

feats_point = [ feat for feat in points[0].getFeatures() ]
feat_polygon = polygon[0].getFeatures().next()

geom_polygon = feat_polygon.geometry().asPolygon()

geom_polygon_line = QgsGeometry.fromPolyline(geom_polygon[0])

distances = [ feat.geometry().distance(geom_polygon_line)
for feat in feats_point ]

print min(distances), distances.index(min(distances))
print max(distances), distances.index(max(distances))
``````

I ran above code with the shapefiles of next image. At the Python Console of QGIS were printed "minimum" and "maximum" distances for comparison purposes. Indices of respective points were also printed for corroborating the correct code execution. For this reason it could be selected these features (yellow color) at next image.

• The code worked perfectly, but I would only get one value. Now I want to get the maximum value for each point; how can I obtain that? Commented May 15, 2018 at 20:07
• I suppose that 'each point' is referred as 'each polygon vertex'. In this case, you need to determine for each polygon point what is its farthest vertex and afterward, for the latter, which is closest point inside polygon. Maximum distance is determined with these pair of points. However, this is another question. Commented May 21, 2018 at 4:11

This isn't strictly QGIS, but if you can be bothered to convert any of your datasets into PostGIS (which is easy with the DBManager plugin), then http://postgis.net/docs/ST_MaxDistance.html will be your friend.

• Ok, thanks! I have never used that but I'll give it a try! Commented Oct 4, 2016 at 18:00