在 python 中定义一个外接一组点 (shapefile) 的圆

Define a circe that circumscribes a set of points (shapefile) in python

我有一个点的 shapefile,由 X 和 Y 坐标定义,带有 ID 特征。 我至少有3个相同ID号的不同点。

我想为每个 ID 定义一个外接点的圆的 shapefile。

如何在 python 环境中完成此操作?

def circle(points):
    p, r = miniball.get_bounding_ball(np.array([points.x, points.y]).T)
    return shapely.geometry.Point(p).buffer(math.sqrt(r))

col = "group"
# generate circles around groups of points
gdf_c = cities.groupby(col, as_index=False).agg(geometry=("geometry", circle))
  • 通过示例和可视化,由于 epsg:4326 投影限制
  • ,圆圈确实会变形

完整的工作示例

import geopandas as gpd
import numpy as np
import shapely
import miniball
import math
import pandas as pd

cities = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

# a semi-synthetic grouping of cities
world["size"] = world.groupby("continent")["pop_est"].apply(
    lambda d: pd.cut(d, 2, labels=list("ab"), duplicates="drop").astype(str)
)
cities = cities.sjoin(world.loc[:, ["continent", "iso_a3", "size", "geometry"]])
cities["group"] = cities["continent"] + cities["size"]


def circle(points):
    p, r = miniball.get_bounding_ball(np.array([points.x, points.y]).T)
    return shapely.geometry.Point(p).buffer(math.sqrt(r))

col = "group"
# generate circles around groups of points
gdf_c = cities.groupby(col, as_index=False).agg(geometry=("geometry", circle))

# visualize it
m = cities.explore(column=col, height=300, width=600, legend=False)
gdf_c.loc[~gdf_c["geometry"].is_empty].explore(
    m=m, column=col, marker_kwds={"radius": 20}, legend=False
)

输出