在Python中用geopandas重叠两个shapefile

问题描述

我有这两个shapefile。 [第一个链接上带有省的shapefile] [带有链接2上的区的第二个shapefile]

我需要加入/合并这两个shapefile以返回如下图: [![莫桑比克各区如下图所示]

moz_admin = 1https://drive.google.com/file/d/1MYNkuKFXP0dt76G9OgeBotjF5UmiRqEl/view?usp=sharing

moz_admin_district = [2]:https://drive.google.com/file/d/1idf5VKgN8PZgdoAcBVEcY9pDa1WYpg7-/view?usp=sharing

Mozambique Districts on link 3

到目前为止我所做的:

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,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,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,op='intersects')

merged=sjoin(moz_admin_district,op='intersects')

没有成功!

解决方法

#将两个形状文件重叠为一个 ax = moz_admin.plot(颜色='none',edgecolor ='black',线宽= 1.5) moz_admin_district.plot(ax = ax,color ='none',edgecolor ='grey',线宽= 0.5)enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...