In geometry, there are basically two main concepts Convex hull and Alpha shape (also called Concave hull) to get the shape of a finite set of points. The following picture should visually describe the differences between them [2].
In case you want to have the convex hull you can use scipy.spatial.ConvexHull
[4].
Please find below a basic example of how to calculate the convex hull for some randomly generated coordinates with scipy.spatial.ConvexHull
and to draw the shape with plotly
.
import plotly.express as px
import numpy as np
from scipy.spatial import ConvexHull
def get_random_coordinate():
return [ np.random.uniform(54.9, 56.2), np.random.uniform(2.3, 3.2) ]
points = [ get_random_coordinate() for _ in range(20) ]
def get_convex_hull():
hull = ConvexHull(points)
return [ [hull.points[visible_facet][1],hull.points[visible_facet][0]] for visible_facet in hull.vertices ]
convex_hull_points = get_convex_hull()
fig = px.scatter_mapbox(points, lat=0, lon=1, zoom=6)
fig.update_layout(
mapbox = {
'style': "open-street-map",
'layers': [
{
'source': {
'type': "FeatureCollection",
'features': [{
'type': "Feature",
'geometry': {
'type': "MultiPolygon",
'coordinates': [[convex_hull_points]]
}
}]
},
'type': "fill", 'below': "traces", 'color': "#FF00AA", 'opacity': 0.5
}
]}
)
fig.show()
Below the final result.
As alternatives to scipy
you can also check geopandas and alphashape.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…