将列转换为Python中的多边形以执行多边形中的点

问题描述 投票:0回答:1

我已经编写了在Python中用多边形建立Point的代码,该程序使用了一个我读为Polygons的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吗?我将如何实现这一目标?

谢谢

python geopandas point-in-polygon
1个回答
0
投票

以上面的示例为例,您可以执行以下操作:

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内置函数,所以让我们看看是否有其他人有建议:)

© www.soinside.com 2019 - 2024. All rights reserved.