如何在Python中将数据框关联到SHP多边形?

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

当我在 Google 地球中使用

SHP files
并单击它时,有时,我可以看到显示为“弹出窗口”的信息。这是一个例子:

在这种情况下,我在

Python
中有一个数据框,其中包含我想链接到每个相应多边形的相关信息,然后使用以下代码行将其导出为SHP:

data = ['26-05-2022', '25-05-2024', 18, 'La Quebrada', 'Ecorer', -14.980752362756956, -75.01587133592898, 'In Effect']
columns = ['Approval Date', 'Expiring Date', 'Zone', 'Project', 'Owner', 'Latitud', 'Longitud', 'Status']
df = df = pd.DataFrame(data,columns)

其中

CT
是另一个包含坐标的数据框:

[['A', 491570.3984, 8340340.536],
['B', 489904.7803, 8346300.1778],
['C', 490269.4017, 8346486.0674],
['D', 490146.1051, 8347050.3125],
['E', 489647.7213, 8347249.0144],
['F', 488383.3197, 8351830.6802],
['H', 491729.1146, 8349476.4402],
['I', 498146.31, 8348131.19],
['J', 498293.5603, 8343802.8176],
['K', 496885.6292, 8343768.3656],
['L', 496916.4466, 8342839.3843],
['M', 498302.6741, 8342910.4866],
['N', 498398.2149, 8340421.8451]]

polygon_geom = Polygon(zip(CT['Longitude'],CT['Latitude']))
polygon = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon_geom])
polygon.to_file(filename=url + projectName + '.shp', driver="ESRI Shapefile")

那么,在将

df
信息保存到 SHP 中之前,如何将其关联到
polygon
以获得类似的内容?

python pandas dataframe geopandas shapefile
1个回答
0
投票

首先,您的代码根本不完整或不可重现。 但对于您的用例,您可以使用 gpd.sjoin 这将根据点的纬度/经度将数据框与多边形连接起来。

参见下面的示例:

import geopandas as gpd
from shapely.geometry import Polygon
import pandas as pd
from shapely.geometry import  Point

# Points
data = [['26-05-2022', '25-05-2024', 18, 'La Quebrada', 'Ecorer', 49.387774157332444, 83.45161530012626, 'In Effect']]
columns = ['Approval Date', 'Expiring Date', 'Zone', 'Project', 'Owner', 'Latitude', 'Longitude', 'Status']
df = pd.DataFrame(data=data,columns=columns)
geom = [Point(xy) for xy in zip(df['Latitude'],df['Longitude'])]
gdf = gpd.GeoDataFrame(df, crs='epsg:4326', geometry=geom)

# Polygon 
CT = pd.DataFrame([['A', 491570.3984, 8340340.536],
['B', 489904.7803, 8346300.1778],
['C', 490269.4017, 8346486.0674],
['D', 490146.1051, 8347050.3125],
['E', 489647.7213, 8347249.0144],
['F', 488383.3197, 8351830.6802],
['H', 491729.1146, 8349476.4402],
['I', 498146.31, 8348131.19],
['J', 498293.5603, 8343802.8176],
['K', 496885.6292, 8343768.3656],
['L', 496916.4466, 8342839.3843],
['M', 498302.6741, 8342910.4866],
['N', 498398.2149, 8340421.8451]], columns=['index', 'Longitude', 'Latitude'])
polygon_geom = Polygon(zip(CT['Longitude']/1e4,CT['Latitude']/1e5))
polygon = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon_geom])

polygon.sjoin(gdf, how='left')

输出:

几何 index_right 批准日期 到期日 区域 项目 业主 纬度 经度 状态
多边形 ((49.15 83.40, ...)) 0 2022年5月26日 2024年5月25日 18 拉克夫拉达 Ecorer 49.3878 83.4516 生效中
© www.soinside.com 2019 - 2024. All rights reserved.