- 基于这个重复问题的答案在python中获取点周围的多边形/多多边形的坐标 https://stackoverflow.com/questions/68960541/obtain-coordinates-of-a-polygon-multi-polygon-around-a-point-in-python/68963478#68963478
- 没有提供相关样本数据,所以我使用了英国医院数据
- 创建了一个辅助函数
poi_poly()
。根据 UTM 几何形状,NB 半径以米为单位
- UTM几何体用于创建指定半径的多边形
- 然后标记与该多边形相交。然后得到凸包 https://geopandas.org/docs/reference/api/geopandas.GeoSeries.convex_hull.html
- 还提供了返回半径多边形的选项,在下面的示例中,我返回了此选项以证明凸包多边形位于 POI 的半径内
import shapely.geometry
import pandas as pd
import geopandas as gpd
import requests, io, json
import plotly.express as px
import random
def poi_poly(
df,
radius=10 ** 5,
poi={"Longitude": 0.06665166467428207, "Latitude": 51.19034957885742},
lon_col="Longitude",
lat_col="Latitude",
include_radius_poly=False,
):
# generate a geopandas data frame of the POI
gdfpoi = gpd.GeoDataFrame(
geometry=[shapely.geometry.Point(poi["Longitude"], poi["Latitude"])],
crs="EPSG:4326",
)
# extend point to radius defined (a polygon). Use UTM so that distances work, then back to WSG84
gdfpoi = (
gdfpoi.to_crs(gdfpoi.estimate_utm_crs())
.geometry.buffer(radius)
.to_crs("EPSG:4326")
)
# create a geopandas data frame of all the points / markers
if not df is None:
gdf = gpd.GeoDataFrame(
geometry=df.loc[:, ["Longitude", "Latitude"]]
.dropna()
.apply(
lambda r: shapely.geometry.Point(r["Longitude"], r["Latitude"]), axis=1
)
.values,
crs="EPSG:4326",
)
else:
gdf = gpd.GeoDataFrame(geometry=gdfpoi)
# create a polygon around the edges of the markers that are within POI polygon
return pd.concat(
[
gpd.GeoDataFrame(
geometry=[
gpd.sjoin(
gdf, gpd.GeoDataFrame(geometry=gdfpoi), how="inner"
).unary_union.convex_hull
]
),
gpd.GeoDataFrame(geometry=gdfpoi if include_radius_poly else None),
]
)
# get some public addressess - hospitals. data that can be scattered
dfhos = pd.read_csv(
io.StringIO(
requests.get("http://media.nhschoices.nhs.uk/data/foi/Hospital.csv").text
),
sep="¬",
engine="python",
)
# generate polygon of markers within 5 mile radius of Point of Interest
poi = dfhos.loc[random.randint(0, len(dfhos) - 1), ["Longitude", "Latitude"]].to_dict()
gdf = poi_poly(dfhos, poi=poi, radius=1609.34 * 5, include_radius_poly=True)
fig = (
px.scatter_mapbox(
dfhos,
lat="Latitude",
lon="Longitude",
color="Sector",
hover_data=["OrganisationName", "Postcode"],
)
.update_traces(marker={"size": 10})
.update_layout(
mapbox={
"style": "open-street-map",
"zoom": 9,
"center": {"lat": poi["Latitude"], "lon": poi["Longitude"]},
"layers": [
{
"source": json.loads(gdf.geometry.to_json()),
"below": "traces",
"type": "line",
"color": "purple",
"line": {"width": 1.5},
}
],
},
margin={"l": 0, "r": 0, "t": 0, "b": 0},
)
)
fig.show()
只画一个圆形多边形
-
poi_poly()
已经升级。在 POI 内查找标记不再强制使用 DataFrame
- 创建以一组 GPS 坐标为中心的圆(实际上是多边形)的简单示例
import plotly.graph_objects as go
poi = {"Latitude": 37.785908, "Longitude": -122.400803}
go.Figure(go.Scattermapbox()).update_layout(
mapbox={
"style": "open-street-map",
"zoom": 9,
"center": {"lat": poi["Latitude"], "lon": poi["Longitude"]},
"layers": [
{
"source": json.loads(poi_poly(None, poi=poi, radius=1609).to_json()),
"below": "traces",
"type": "line",
"color": "purple",
"line": {"width": 1.5},
}
],
},
margin={"l": 0, "r": 0, "t": 0, "b": 0},
)