PeCaDe
PeCaDe

Reputation: 374

farthest distance from a point to a polygon in python

In order to determine a radius to draw a circle that goes through the farthest point of a polygon given the centroid, I would like to determine the radius given the centroid (point_geometry) and the polygon (polygon_geometry). in python.

enter image description here

From a given polygon (independently on the shape, the centroid will be used).

from shapely import geometry
from haversine import haversine
polygon = geometry.Polygon([[0, 0], [0.5, 0], [0.6, 1], [0, 1], [-0.5, 0.5]])

I compute the maximum distance from the centroid as follows:

np.max([haversine(point_border,list(polygon.centroid.coords)[0]) for point_border in polygon.exterior.coords])

This function is measuring the harvesine distance from the centroid to every point of the polygon exterior points.

is there any other shorter/fast way?

Upvotes: 0

Views: 1467

Answers (1)

Colim
Colim

Reputation: 1303

from shapely import geometry

polygon = geometry.Polygon([[0, 0], [0.5, 0], [0.6, 1], [0, 1], [-0.5, 0.5]])

centroid = polygon.centroid
vertex = polygon.exterior.coords

#calculate all distances from centroid to polygon vertex
distances=[geometry.LineString([centroid, v]).length for v in vertex]

print(f"Max distance: {max(distances)}")
>>>Max distance: 0.6734969118546704

Upvotes: 2

Related Questions