我必须根据站点的“区域”绘制多边形。我想要一条绕区域外部的线来定义其周长。
这是我的代码:
#Import the source data and libraries
import pandas as pd
import geopandas as gpd
import folium
from shapely.geometry import Polygon
df = pd.read_csv('tacs.csv')
#Extract the lat long lists from the datasource
lat_point_list = df['magnet.latitude'].tolist()
lon_point_list = df['magnet.longitude'].tolist()
#Some wizardry
polygon_geom = Polygon(zip(lon_point_list, lat_point_list))
crs = {'init': 'epsg:4326'}
polygon = gpd.GeoDataFrame(index=[0], crs=crs, geometry=[polygon_geom])
#output to files
polygon.to_file(filename='polygon.geojson', driver='GeoJSON')
polygon.to_file(filename='polygon.shp', driver="ESRI Shapefile")
#plot on a map with central point being birmingham
m = folium.Map([51.509865, -0.118092], zoom_start=12, tiles='cartodbpositron')
folium.GeoJson(polygon).add_to(m)
folium.LatLngPopup().add_to(m)
m
问题是,它的出现如下所示。这不是围绕外围的线,而是一大堆互连点。
有什么想法可以解决这个问题吗?
答案 0 :(得分:1)
非凸多边形的凸包不是周长。一种方法(可能有更好的方法)是将多边形重铸为LinearRing:
#a polygon:
R = shapely.geometry.Polygon([[1,2],[2,3],[3,2],[1,2]])
#cast as linearring:
L = shapely.geometry.LinearRing(R.exterior.coords)
然后,您可以用LinearRings / LineStrings的几何列替换地理数据框中的Polygons的几何列,并绘制它们。
答案 1 :(得分:0)
有一个名为convex_hull的属性。根据此文档:
GeoSeries.convex_hull返回一个表示几何的GeoSeries 每个几何图形的凸包。
几何的凸包是最小的凸多边形 包含每个几何中的所有点,除非 几何对象中的点小于3。对于两点, 凸包折叠为LineString;等于1分。
这里是一个例子:
import geopandas as gpd
from shapely.geometry import Polygon
lat_point_list = [50.854457, 48.853033, 52.518172, 50.072651, 50.854457]
lon_point_list = [4.377184, 2.349553, 13.407759, 14.435935, 4.377184]
polygon_geom = Polygon(zip(lon_point_list, lat_point_list))
polygon_geom2 = polygon_geom.convex_hull # the atribute
import folium
m = folium.Map([50.854457, 4.377184], zoom_start=5, tiles='cartodbpositron')
folium.GeoJson(polygon_geom).add_to(m)
folium.GeoJson(polygon_geom2).add_to(m)
folium.LatLngPopup().add_to(m)
m