1.导包
import pyproj
import pandas as pd
import geopandas as gpd
2.相关代码
shp=gpd.read_file('a.shp',encoding='utf-8')
shp_2022_4.to_crs(epsg=4549, inplace=True)
grid=gpd.read_file('grid.shp',encoding='utf-8')
grid.to_crs(epsg=4549, inplace=True)
for i in range(len(grid)):
clip=gpd.clip(shp,grid.iloc[i,1],keep_geom_type =True)
if len(clip.index)==0:
continue
df=pd.DataFrame()
grid_data=pd.DataFrame([len(df),0,0,grid.iloc[i,1]]).T
df=df.append(grid_data)
long=len(df)
for j in range(len(clip)):
geo=clip.iloc[j,-1]
pre=pre_type[clip.iloc[j,6]]
back=after_type[clip.iloc[j,7]]
clip_data=pd.DataFrame([long,pre,back,geo]).T
df=df.append(clip_data)
long=long+1
df.columns=['id','pre_type','back_type','geometry']
df[['pre_type','back_type']]=df[['pre_type','back_type']].astype(object)
data=gpd.GeoDataFrame(df)
data.crs = pyproj.CRS.from_user_input('EPSG:4549')
name=os.path.join(path_shp,f'clip_{i}.shp')
data.to_file(name, driver='ESRI Shapefile',encoding='utf-8')