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

Obtain coordinates of a Polygon / Multi-polygon around a point in python(在Python中获取点周围的多边形/多多边形的坐标)

本文介绍了在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

推荐答案

  • 使用此帮助器https://github.com/Turbo87/utm在GPS(WSG84/EPSG:4326)和UTM之间转换
  • 您将POI圆/多边形半径表示为距离x miles,因此需要使用UTM投影将半径表示为距离
  • 使用英国医院作为POI数据样本。如果没有明显的缩放级别,圆将不会显示,因为它们的半径只有1英里。插入具有纬度经度列的任何其他数据框将非常简单
  • 使用散点映射框作为样图,然后添加为相关点圆/多边形的图层
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()

这篇关于在Python中获取点周围的多边形/多多边形的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在Python中获取点周围的多边形/多多边形的坐标

基础教程推荐