2个地理数据框之间的交集
Intersection between 2 Geodataframe
我正在 Geopanda 库上做一些工作,我有一个包含多边形的 shapefile 和 excel sheet 上的数据,我将其转换为点。我想将两个 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
两件事:
创建点时需要交换 geoBreite
和 geoLaenge
:
geometry = [Point(xy) for xy in zip(df.geoLaenge, df.geoBreite)]
这是因为形状遵循 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']))
我正在 Geopanda 库上做一些工作,我有一个包含多边形的 shapefile 和 excel sheet 上的数据,我将其转换为点。我想将两个 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
两件事:
创建点时需要交换 geoBreite
和 geoLaenge
:
geometry = [Point(xy) for xy in zip(df.geoLaenge, df.geoBreite)]
这是因为形状遵循 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']))