本文介绍了使用python在scattermapbox中的点周围绘制一个多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am using plotlys scattermapbox to plot points on a map. I'd like to draw the polygon that cover 'x' mile radius from a POI.
dcc.Graph(id="map-graph"), @application.callback([ Output("map-graph", "figure"), ], [ Input("address", "value"), Input("type", "value") ] ) def update_graph(address, type): for i, row in df.iterrows(): lat = row["Lat"] lng = row["Long"] data.append({ "type": "scattermapbox", "lat": [lat], "lon": [lng], "name": "Location", "showlegend": False, "hoverinfo": "text", "mode": "markers", "marker": { "symbol": "circle", "size": 8, "opacity": 0.8, "color": "black" } } ) # Plot POI POI_Lat = 37.785908 POI_Long = -122.400803 data.append({ "type": "scattermapbox", "lat": [POI_Lat], "lon": [POI_Long], "marker": { "symbol": "circle, "size": 28, "opacity": 0.7, "color": "rgb(128, 128, 128)" } } )df is a pandas dataframe that includes coordinates for locations within x miles of POI. How do I update the map-graph to draw a polygon that covers all the points?
Adding a layer to layout dictionary:
gdf = circles(Lat, Long, radius=1609.34) print(gdf['geometry'][0]) POLYGON ((385272.0167249573 3768678.19769511, 385264.2673129799 3768520.454790493,.......)) layout = { "autosize": True, "hovermode": "closest", "mapbox": { "accesstoken": MAPBOX_KEY, "bearing": 0, "center": { "lat": layout_lat, "lon": layout_lon }, "layers": [ { "source": json.loads(gdf.geometry.to_json()), "below": "traces", "type": "line", "color": "purple", "line": {"width": 1.5}, } ], "pitch": 0, "zoom": zoom, "style": "outdoors", }, "margin": { "r": 0, "t": 0, "l": 0, "b": 0, "pad": 0 } }解决方案
- based on answer to this duplicate question Obtain coordinates of a Polygon / Multi-polygon around a point in python
- no sample data provided in question, so I've used UK hospital data
- have created a helper function poi_poly(). NB radius is in meters as per UTM geometry
- UTM geometry is used to create a polygon of specified radius
- markers are then intersected with this polygon. Then get the convex hull
- have provided option to return radius polygon as well, in example below I've returned this to demonstrate that the convex hull polygon is within the radius of the POI
- poi_poly() has been updated. DataFrame is no longer mandatory for finding markers within POI
- simple example of creating a circle (actually a polygon) centred on a single set of GPS co-ordinates
更多推荐
使用python在scattermapbox中的点周围绘制一个多边形
发布评论