地理:使用环将每个多边形与GeodataFrame之前的每个多边形区分开来?

发布于 2025-02-10 07:14:23 字数 273 浏览 1 评论 0原文

我有一个带有许多多边形的GeodataFrame。我想通过循环自动化差异,以每个先前的每个和polygon进行此操作,然后将它们添加到新的gdf:

new_gdf = polygon [0:1] .overlay(polygon [1:2],如何='差异')

我一次可以做到这一点,但是我想找到一种更有效的方法,尤其是当我(某天)有很多多边形时。

有人会想知道我怎么做吗?我正在尝试使用iterrows(),但是在确切的语法上失败了。

先感谢您。

I have a geodataframe with many polygons. I'd like to automate differencing with a loop, to do this with each preceding and following polygon and add them to a new gdf:

new_gdf = polygon[0:1].overlay(polygon[1:2], how='difference')

I am able to do this one at a time, but I'd like to find a more efficient way, especially when I will (someday) have a lot of polygons.

Would anyone have an idea of how I might do this? I'm trying to use iterrows(), but am failing on the exact syntax.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2025-02-17 07:14:23

而不是覆盖()您可能正在寻找 geoseries.differenies.difference()

返回每个对齐几何形状中的点的地层
不在另一个。

可能是这样的:

from shapely.geometry import Polygon
import geopandas as gpd

# sample polygons
polys = gpd.GeoDataFrame(geometry=[
    Polygon([(0,0), (2,0), (2,2), (0,2)]),
    Polygon([(1,1), (3,1), (3,3), (1,3)]),
    Polygon([(2,2), (4,2), (4,4), (2,4)]),
    Polygon([(3,3), (5,3), (5,5), (3,5)])])

# polys.iloc[:-1] : rows 0 .. 2 (exclude last row as df sizes must match)
# polys.iloc[1:]  : rows 1 .. 3
# align=False : do not aligns GeoSeries based on their indices; 
# order of elements is preserved.
diffs = polys.iloc[:-1].difference(polys.iloc[1:], align=False)

示例polys geodataframe:

”“输入映像在此处”

结果diffs geodataseries:

Instead of overlay() you might be looking for GeoSeries.difference()

Returns a GeoSeries of the points in each aligned geometry that are
not in other.

Would be something like this:

from shapely.geometry import Polygon
import geopandas as gpd

# sample polygons
polys = gpd.GeoDataFrame(geometry=[
    Polygon([(0,0), (2,0), (2,2), (0,2)]),
    Polygon([(1,1), (3,1), (3,3), (1,3)]),
    Polygon([(2,2), (4,2), (4,4), (2,4)]),
    Polygon([(3,3), (5,3), (5,5), (3,5)])])

# polys.iloc[:-1] : rows 0 .. 2 (exclude last row as df sizes must match)
# polys.iloc[1:]  : rows 1 .. 3
# align=False : do not aligns GeoSeries based on their indices; 
# order of elements is preserved.
diffs = polys.iloc[:-1].difference(polys.iloc[1:], align=False)

Sample polys GeoDataFrame:

enter image description here

Resulting diffs GeoDataSeries:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文