在python中获取点周围的多边形/多多边形的坐标

2024-09-27 18:17:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用plotlys scattermapbox围绕点对象绘制多边形。我想画一个多边形,从一个POI开始,覆盖半径为“x”英里

我发现这很有帮助:https://plotly.com/python/filled-area-on-mapbox/。然而,在我的例子中,我需要动态地定义多边形并获得它相应的坐标

如何围绕中心点渲染和填充多边形?我可以使用mapbox获取多边形几何体吗?我们将多边形几何体定义为从中心开始的x miles

供参考:https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#fill

这里的原始问题:Draw a polygon around point in scattermapbox using python


Tags: 对象httpscom定义on半径绘制area
1条回答
网友
1楼 · 发布于 2024-09-27 18:17:29
  • 使用此助手https://github.com/Turbo87/utm在GPS(WSG84/EPSG:4326)和UTM之间进行转换
  • 您将POI圆/多边形半径表示为距离x miles,因此需要使用UTM投影将半径表示为距离
  • 已使用英国医院作为样本POI数据。如果没有显著的缩放级别,圆圈将不会显示,因为它们的半径仅为1英里。插入任何其他带有纬度经度列的数据帧都很简单
  • 使用散点贴图框作为示例图,然后添加属于POI圆/多边形的
import shapely.geometry
import utm
import pandas as pd
import geopandas as gpd
import requests, io, json
import plotly.express as px

# need to use UTM to express radius as a distance. UTM is zoned, so if GPS coords are very widely distributed
# distance will be incorrect.  zone is estimated from first GPS coordinate
# returns geopandas dataframe re-projected to GPS co-ordinates
# radius is expressed in metres
def circles(lonlat, radius=10 ** 4):
    
    utm_coords = utm.from_latlon(lonlat[:, 1], lonlat[:, 0])
    utmcrs = gpd.GeoDataFrame(
        geometry=[shapely.geometry.Point(lonlat[0, 0], lonlat[0, 1])], crs="EPSG:4326"
    ).estimate_utm_crs()

    return gpd.GeoDataFrame(
        geometry=[
            shapely.geometry.Point(easting, northing).buffer(radius)
            for easting, northing in zip(utm_coords[0], utm_coords[1])
        ],
        crs=utmcrs,
    ).to_crs("EPSG:4326")


# 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 circles of diameter 1 mile, which is 1609.34 metres
gdf = circles(dfhos.head(20).loc[:, ["Longitude", "Latitude"]].values, radius=1609.34)

fig = (
    px.scatter_mapbox(
        dfhos.head(20),
        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":gdf.loc[0,"geometry"].centroid.y, "lon":gdf.loc[0,"geometry"].centroid.x},
            "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()

enter image description here

相关问题 更多 >

    热门问题