【发布时间】:2020-04-04 07:28:26
【问题描述】:
我已经定义了三个 Shapely 线串和三个 Shapely 多边形,它们在不同的地方相互重叠/相交,如下图所示。
我的理解是,一个匀称的“差异”操作应该返回多边形之外的线条部分。我不知道为什么,但是当我执行“差异”操作时,它似乎保留了位于其中一个多边形内的线的一部分。这显示在下图中,我将原始多边形与差异操作的输出进行了比较。
请注意,同样,如果我运行“交叉”操作,它会丢失这个小段。谁能解释为什么会这样?生成上述所有内容的代码如下:
import geopandas as gpd
import pandas as pd
from shapely.geometry import Polygon, LineString
#Define lines and polygons:
linkID = ['1','2','3']
link_geom = [LineString([(0, 0), (10, 10)]),LineString([(10, 10), (20, 10)]),LineString([(20, 10), (25, 15)])]
gdf_links = gpd.GeoDataFrame({'linkID':linkID,'geometry':link_geom})
polyID = ['100','200','300']
poly_geom = [Polygon([(2, 1), (2, 3), (4, 3), (4, 1)]),Polygon([(15, 7), (15, 13), (18, 13), (18, 7)]),Polygon([(19, 7), (19, 13), (21, 13), (21, 7)])]
gdf_poly = gpd.GeoDataFrame({'polyID':polyID,'geometry':poly_geom})
links = gdf_links.unary_union
polys = gdf_poly.unary_union
#Show plot of lines and polygons together:
gpd.GeoSeries([links,polys]).plot(cmap='tab10')
#Split links at polygons, keeping segments that are outside of polgyon:
difference = gdf_links.difference(gdf_poly).reset_index(drop=True)
#Plot resulting 'difference' vs original polygons:
diff = difference.unary_union
gpd.GeoSeries([diff,polys]).plot(cmap='tab10')
【问题讨论】:
标签: polygon intersection difference geopandas shapely