python - 在 geopandas 中使用多多边形 shapefile 剪切 shapefile

标签 python geopandas shapefile clip

我有一个 shapefile(以下将称为源文件),我需要通过多多边形 shapefile 对其进行剪辑,以便我可以为每个多边形都有一个剪辑的 shapefile。我尝试了 geopandas,尽管我可以通过从多多边形形状文件中单独选择多边形来单独剪切源文件来剪切源文件,但是当我尝试循环多边形以自动执行剪切过程时,我得到以下结果错误:

错误: 类型错误:“掩码”应该是 GeoDataFrame、GeoSeries 或(多)多边形,得到

代码:

import geopandas as gpd

source = ('source-shapefile.shp')
mask = ('mask_shapefile.shp')
sourcefile = gpd.read_file(source)
maskfile = gpd.read_file(mask)
for row in maskfile.iterrows():
    gpd.clip(sourcefile, row)

最佳答案

两点

已经构建了一个示例。使用 GeoDataFrame 作为 mask 进行剪辑要简单得多。

import geopandas as gpd
import pandas as pd

# lets build a mask for use in clip, multipolygons and polygons
maskfile = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
maskfile = maskfile.loc[maskfile["continent"].eq("Europe") & maskfile["name"].ne("Russia")].pipe(
    lambda d: d.assign(gdp_grp=pd.cut(d["gdp_md_est"], bins=4, labels=list("abcd")))
).dissolve("gdp_grp").reset_index()

sourcefile = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

# now clip, no looping needed
gpd.clip(sourcefile, maskfile)

关于python - 在 geopandas 中使用多多边形 shapefile 剪切 shapefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71899858/

相关文章:

来自自定义 shapefile 的 R Highcharter map

python - 在函数 : Wrong handling indentation/bad-continuation. 中使用列表作为参数的 Pylint 错误无法找出原因

python - 使用 Paho 的 message_callback_add() 函数,但没有结果

python - 识别包含 None 值的 pandas 列

python - 导入 Geopandas 时导入错误

r - 从点创建凸包多边形并另存为shapefile

python - 无法使用 django-pyodbc 从 django 查询 SQL Server

python - 如何以编程方式访问 coverage.py 结果?

python - conda 错误 : Cannot link a source that does not exist

python - python 中 3D 多边形的交点