简体   繁体   English

使用 geopandas 和 folium 在 python 中绘制多边形

[英]plotting polygons in python using geopandas and folium

I have to plot polygons based on site 'regions'.我必须根据站点“区域”来 plot 多边形。 I want a line that goes around the outside of a region to define it's perimeter.我想要一条围绕区域外部的线来定义它的周长。

Here is my code:这是我的代码:

#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

The issue is, it comes out like the below.问题是,它如下所示。 It's not a line around the perimeter, rather a big mess of interconnecting points.它不是围绕周边的一条线,而是一大堆相互连接的点。

Any ideas how I can get around this?有什么想法可以解决这个问题吗?

在此处输入图像描述

A convex hull of a non-convex polygon is not the perimeter.非凸多边形的凸包不是周长。 One way of doing this (there is probably a better way) is to recast the polygon as a LinearRing:这样做的一种方法(可能有更好的方法)是将多边形重铸为 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)

Then you can replace the geometry column of Polygons in you geodataframe with a geometry column of LinearRings/LineStrings, and plot those instead.然后,您可以用 LinearRings/LineStrings 的几何列和 plot 替换地理数据框中的多边形几何列。

There is an atribute called convex_hull .有一个名为conve_hull的属性。 According to this documentation:根据此文档:

GeoSeries.convex_hull Returns a GeoSeries of geometries representing the convex hull of each geometry. GeoSeries.convex_hull 返回几何的 GeoSeries,表示每个几何的凸包。

The convex hull of a geometry is the smallest convex Polygon containing all the points in each geometry, unless the number of points in the geometric object is less than three.几何的凸包是包含每个几何中所有点的最小凸多边形,除非几何 object 中的点数少于三个。 For two points, the convex hull collapses to a LineString;对于两个点,凸包折叠为 LineString; for 1, a Point.对于 1,一个点。

Here is an example:这是一个例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM