在 Python 中用 geopandas 重叠两个 shapefile
Overlap two shapefiles with geopandas in Python
我有这两个 shapefile。
[第一个包含 link 1 省份的 shapefile]
[第二个 shapefile link 2]
我需要 join/merge 这两个 shapefile 到 return 地图,如下所示:
[![莫桑比克地区如下图]
moz_admin=1: https://drive.google.com/file/d/1MYNkuKFXP0dt76G9OgeBotjF5UmiRqEl/view?usp=sharing
moz_admin_district=[2]: https://drive.google.com/file/d/1idf5VKgN8PZgdoAcBVEcY9pDa1WYpg7-/view?usp=sharing
到目前为止我做了什么:
import os
import geopandas as gpd
file = os.listdir(r'pathtofilefolder')
path = [os.path.join(r'folder', i) for i in file if ".shp" in i]
gdf = gpd.GeoDataFrame(pd.concat([gpd.read_file(i) for i in path],
ignore_index=True), crs=gpd.read_file(path[0]).crs)
和
moz_admin.to_crs(moz_admin_district.crs, inplace=True) #Change projections
gpd.sjoin(moz_admin_district, moz_admin, how='union', op='within').shape #Join 2 shapefiles
gpd.sjoin(moz_admin_district, moz_admin, how='union', op='within').plot()
和
diffs = []
gdfs = [moz_admin, moz_admin_district]
for idx, gdf in enumerate(gdfs):
if idx < 2:
diffs.append(gdf.symmetric_difference(gdfs[idx+1]).iloc[0])
diffs.append(moz_admin_district.iloc[0].geometry)
和
gdf = gpd.GeoDataFrame(pd.concat([moz_admin, moz_admin_district]))
和
from shapely.geometry import Polygon
res_union = gpd.overlay(moz_admin, moz_admin_district, how='union')
和
so = moz_admin.merge(moz_admin_district, on='ADM1_PT', how='inner')
df = tab_df.merge(spatial_df, on='mukey', how='right')
sof = gpd.GeoDataFrame(so)
和
merged_master = gpd.GeoDataFrame(pd.merge(moz_admin, moz_admin_district, how='left', left_on="ADM1_PT", right_on="ADM1_PT"))
merged = merged_master[['ADM1_PT', 'ADM2_PT', 'geometry']]
和
so = gpd.GeoDataFrame(pd.concat([moz_admin, moz_admin_district], ignore_index=True))
和
sjoined_listings = gpd.sjoin(moz_admin_district, moz_admin, op='intersects')
和
merged=sjoin(moz_admin_district, moz_admin, how='left', op='intersects')
没有成功!
#OVERLAP TWO SHAPEFILES INTO ONE
ax = moz_admin.plot(color='none', edgecolor='black', linewidths=1.5)
moz_admin_district.plot(ax=ax, color='none', edgecolor='grey', linewidths=0.5)
我有这两个 shapefile。 [第一个包含 link 1 省份的 shapefile] [第二个 shapefile link 2]
我需要 join/merge 这两个 shapefile 到 return 地图,如下所示: [![莫桑比克地区如下图]
moz_admin=1: https://drive.google.com/file/d/1MYNkuKFXP0dt76G9OgeBotjF5UmiRqEl/view?usp=sharing
moz_admin_district=[2]: https://drive.google.com/file/d/1idf5VKgN8PZgdoAcBVEcY9pDa1WYpg7-/view?usp=sharing
到目前为止我做了什么:
import os
import geopandas as gpd
file = os.listdir(r'pathtofilefolder')
path = [os.path.join(r'folder', i) for i in file if ".shp" in i]
gdf = gpd.GeoDataFrame(pd.concat([gpd.read_file(i) for i in path],
ignore_index=True), crs=gpd.read_file(path[0]).crs)
和
moz_admin.to_crs(moz_admin_district.crs, inplace=True) #Change projections
gpd.sjoin(moz_admin_district, moz_admin, how='union', op='within').shape #Join 2 shapefiles
gpd.sjoin(moz_admin_district, moz_admin, how='union', op='within').plot()
和
diffs = []
gdfs = [moz_admin, moz_admin_district]
for idx, gdf in enumerate(gdfs):
if idx < 2:
diffs.append(gdf.symmetric_difference(gdfs[idx+1]).iloc[0])
diffs.append(moz_admin_district.iloc[0].geometry)
和
gdf = gpd.GeoDataFrame(pd.concat([moz_admin, moz_admin_district]))
和
from shapely.geometry import Polygon
res_union = gpd.overlay(moz_admin, moz_admin_district, how='union')
和
so = moz_admin.merge(moz_admin_district, on='ADM1_PT', how='inner')
df = tab_df.merge(spatial_df, on='mukey', how='right')
sof = gpd.GeoDataFrame(so)
和
merged_master = gpd.GeoDataFrame(pd.merge(moz_admin, moz_admin_district, how='left', left_on="ADM1_PT", right_on="ADM1_PT"))
merged = merged_master[['ADM1_PT', 'ADM2_PT', 'geometry']]
和
so = gpd.GeoDataFrame(pd.concat([moz_admin, moz_admin_district], ignore_index=True))
和
sjoined_listings = gpd.sjoin(moz_admin_district, moz_admin, op='intersects')
和
merged=sjoin(moz_admin_district, moz_admin, how='left', op='intersects')
没有成功!
#OVERLAP TWO SHAPEFILES INTO ONE
ax = moz_admin.plot(color='none', edgecolor='black', linewidths=1.5)
moz_admin_district.plot(ax=ax, color='none', edgecolor='grey', linewidths=0.5)