python - 在 Python 中将列转换为多边形以在多边形中执行点
问题描述
我已经编写了代码以在 Python 中建立多边形中的点,该程序使用我读入的 shapefile 作为多边形。我现在有一个数据框,其中包含一个包含多边形的列,例如[[28.050815,-26.242253],[28.050085,-26.25938],[28.011934,-26.25888],[28.020216,-26.230127],[28.049828,-26.230704],[28.050815,-26.242253]]
. 我想将此列转换为多边形以执行多边形中的点,但所有示例都使用geometry = [Point(xy) for xy in zip(dataPoints['Long'], dataPoints['Lat'])]
但我的已经是 zip?我将如何实现这一目标?
谢谢
解决方案
以上面的示例为例,您可以执行以下操作:
list_coords = [[28.050815,-26.242253],[28.050085,-26.25938],[28.011934,-26.25888],[28.020216,-26.230127],[28.049828,-26.230704],[28.050815,-26.242253]]
from shapely.geometry import Point, Polygon
# Create a list of point objects using list comprehension
point_list = [Point(x,y) for [x,y] in list_coords]
# Create a polygon object from the list of Point objects
polygon_feature = Polygon([[poly.x, poly.y] for poly in point_list])
如果您想将其应用于数据框,您可以执行以下操作:
import pandas as pd
import geopandas as gpd
df = pd.DataFrame({'coords': [list_coords]})
def get_polygon(list_coords):
point_list = [Point(x,y) for [x,y] in list_coords]
polygon_feature = Polygon([[poly.x, poly.y] for poly in point_list])
return polygon_feature
df['geom'] = df['coords'].apply(get_polygon)
但是,为了避免“重新发明轮子”,可能有 geopandas 内置功能,所以让我们看看是否有其他人有建议:)
推荐阅读
- google-chrome - 如何在 puppeteer 中获取 (http) 协议版本?
- regex - Nino 正则表达式
- r - 如何创建热图来说明网格差异,以控制发散调色板的中心颜色位置?
- php - SVG 图像生成
- android - 可绘制 xml 中的曲线设计或以编程方式绘制绘制
- javascript - 如何在刻度值中创建具有可变刻度间隔并且刻度值之间具有相同距离的左轴
- java - 如何在java中使用带有条件的比较器
- php - 如何将此邮递员转换为 curl php?
- tensorflow - 重新训练 Keras 模型后如何打印训练数据大小
- html - 谁能告诉我如何在 Python 上提取和显示图像中的文本