python - 2 个地理数据框之间的交集

标签 python python-3.x pandas geopandas

我正在 Geopanda 库上做一些工作,我有一个 shapefile,其中包含多边形和 Excel 工作表上的数据,我将其转换为点。我想将两个 DataFrame 相交并将其导出到文件中。我还在两个投影 (WGS84) 上使用,以便我可以比较它们。 至少应该有一些点与多边形相交。 我的相交 GeoSeries 没有给我任何适合多边形的点,但我不明白为什么......

我检查了 shapefile 的单位是否真的是公里而不是其他东西。我不熟悉 GeoPlot,所以我无法真正确定 GeoDataFrame 是什么样的。

f = pd.read_excel(io = 'C:\\Users\\peilj\\meteo_sites.xlsx')

#Converting panda dataframe into a GeoDataFrame with CRS projection
geometry = [Point(xy) for xy in zip(df.geoBreite, df.geoLaenge)]
df = df.drop(['geoBreite', 'geoLaenge'], axis=1)
crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
gdf = GeoDataFrame(df, crs=crs, geometry=geometry)

#Reading shapefile and creating buffer
gdfBuffer = geopandas.read_file(filename = 'C:\\Users\\peilj\\lkr_vallanUTM.shp')
gdfBuffer = gdfBuffer.buffer(100) #When the unit is kilometer

#Converting positions long/lat into shapely object
gdfBuffer = gdfBuffer.to_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

#Intersection coordonates from polygon Buffer and points of stations
gdf['intersection'] = gdf.geometry.intersects(gdfBuffer)
#Problem: DOES NOT FIND ANY POINTS INSIDE STATIONS !!!!!!!

#Giving CRS projection to the intersect GeoDataframe
gdf_final = gdf.to_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
gdf_final['intersection'] = gdf_final['intersection'].astype(int) #Shapefile does not accept bool

#Exporting to a file
gdf_final.to_file(driver='ESRI Shapefile', filename=r'C:\\GIS\\dwd_stationen.shp

需要的文件: https://drive.google.com/open?id=11x55aNxPOdJVKDzRWLqrI3S_ExwbqCE9

最佳答案

有两件事:

创建点时,您需要交换 geoBreitegeoLaenge:

geometry = [zip(df.geoLaenge, df.geoBreite) 中 xy 的点(xy)]

这是因为 shapely 遵循 x、y 逻辑,而不是纬度、经度。

对于检查交集,你可以这样做:

gdf['inside'] = gdf['geometry'].apply(lambda shp: shp.intersects(gdfBuffer.dissolve('LAND').iloc[0]['geometry']))

它检测形状文件内的六个站点:

gdf['inside'].sum()

输出:

6

因此,除了其他一些小修复之外,我们还得到了:

import geopandas as gpd
from shapely.geometry import Point

df = pd.read_excel(r'C:\Users\peilj\meteo_sites.xlsx')

geometry = [Point(xy) for xy in zip(df.geoLaenge, df.geoBreite)]
crs = {'init': 'epsg:4326'}
gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)

gdfBuffer = gpd.read_file(filename = r'C:\Users\peilj\lkr_vallanUTM.shp')
gdfBuffer['goemetry'] = gdfBuffer['geometry'].buffer(100)

gdfBuffer = gdfBuffer.to_crs(crs)

gdf['inside'] = gdf['geometry'].apply(lambda shp: shp.intersects(gdfBuffer.dissolve('LAND').iloc[0]['geometry']))

关于python - 2 个地理数据框之间的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57430656/

相关文章:

python - sklearn 管道的正确用法

python - Selenium Python : Can't Get Path To Chat Element On Google Hangouts

python - 使用 Django 项目在 Docker 中创建 postgresql 数据库

python - 每 5 分钟运行一次 cron 作业

python - 支付员工

python - 将装饰器类应用于类方法

使用 FFMpegwriter 的 Python Matplotlib basemap 动画在 820 帧后停止?

python-3.x - 如何减慢使用 cv2 捕获的视频?

python - 从 pandas 数据帧列创建指定长度的组

python - 对于每个 A(key) | 选择列值大于 x 的第一个匹配项 |数据框