Python 将 GeoPandas 或 Shapely 中的多边形合并(合并为单个几何图形)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40385782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:28:24  来源:igfitidea点击:

Make a union of polygons in GeoPandas, or Shapely (into a single geometry)

pythonpandasgeopandasshapely

提问by p-robot

I am trying to find the union of two polygons in GeoPandas and output a single geometry that encompasses points from both polygons as its vertices. The geopandas.overlayfunction gives me polygons for each individual union but I would like a single polygon.

我试图在 GeoPandas 中找到两个多边形的并集,并输出一个包含来自两个多边形的点作为其顶点的单个几何图形。该geopandas.overlay函数为我提供了每个联合的多边形,但我想要一个多边形。

For context, I'm using this to combine two administrative areas together into a single area (i.e. include a town district within a country).

就上下文而言,我使用它来将两个行政区域合并为一个区域(即包括一个国家/地区内的一个镇区)。

The following example is from the geopandas website and illustrates what I'd like:

以下示例来自 geopandas 网站并说明了我想要的内容:

from matplotlib import pyplot as plt
import geopandas as gpd
from shapely.geometry import Polygon

polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
                         Polygon([(2,2), (4,2), (4,4), (2,4)])])

polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
                         Polygon([(3,3), (5,3), (5,5), (3,5)])])

df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})

res_union = gpd.overlay(df1, df2, how='union')
res_union.plot()

res_union.png

res_union.png

None of the output geometries are what I was expected, which is the following:

没有任何输出几何图形是我所期望的,如下所示:

poly_union = gpd.GeoSeries([Polygon([(0,0), (0,2), (1,2), (1,3), \
    (2,3), (2,4), (3, 4), (3, 5), (5, 5), (5, 3), (4, 3), (4, 2), \
    (3,2), (3,1), (2, 1), (2, 0), (0, 0)])])

poly_union.plot(color = 'red')
plt.show()

union.png

工会.png

Firstly, how do I output the above polygon (poly_union) from the input polygons (df1, df2) using GeoPandas or shapely?

首先,如何使用 GeoPandas 或 shapelypoly_union从输入多边形 ( df1, df2)输出上述多边形 ( ) ?

Secondly, what is the correct nomenclature associated with the geometry (poly_union) that I'm trying to find? I would call it a 'union' but every example I find that refers to 'unions' does not output this geometry.

其次,与poly_union我试图找到的几何 ( )相关的正确命名法是什么?我将其称为“联合”,但我发现的每个涉及“联合”的示例都不会输出此几何图形。

Note: Thisexample does not seem to output a single polygon either:

注意:这个例子似乎也没有输出单个多边形:

poly1 = df1['geometry']; poly2 = df2['geometry']
mergedpoly = poly1.union(poly2)
mergedpoly.plot()

merged_poly.png

合并多边形.png

回答by p-robot

From the question/answer here, it seems this is called a cascaded_unionwithin shapely:

这里的问题/答案来看,这似乎被称为cascaded_unionshapely

from shapely.ops import cascaded_union
polygons = [poly1[0], poly1[1], poly2[0], poly2[1]]
boundary = gpd.GeoSeries(cascaded_union(polygons))
boundary.plot(color = 'red')
plt.show()

Note: cascaded_unionis superceded by unary_unionif GEOS 3.2+ is used - this allows unions on different geometry types, not only polygons. To check your version,

注意:如果使用 GEOS 3.2+,cascaded_union则被取代unary_union- 这允许不同几何类型的联合,而不仅仅是多边形。要检查您的版本,

>>> shapely.geos.geos_version
(3, 5, 1)

union

联盟

回答by Rutger Hofste

If you prefer Geopandas over Shapely you might consider dissolve and use a column with a constant value for all entries: http://geopandas.org/aggregation_with_dissolve.html

如果您更喜欢 Geopandas 而不是 Shapely,您可以考虑溶解并为所有条目使用具有恒定值的列:http: //geopandas.org/aggregation_with_dissolve.html

回答by ConZZito

@Rutger Hofste's answer worked best for me as well. In case your polygons are lacking of a column with a constant value, just simply create one by

@Rutger Hofste 的回答也最适合我。如果您的多边形缺少具有恒定值的列,只需简单地创建一个

gdf['new_column'] = 0 gdf_new = gdf.dissolve(by='new_column')

gdf['new_column'] = 0 gdf_new = gdf.dissolve(by='new_column')