python - 如何在同一地理数据框中获取多边形与其他多边形的交集?
问题描述
我有一个包含数千个多边形的 shapefile。他们中的许多人接触但不交叉。我需要得到触摸多边形的公共线。
我尝试使用以下函数来实现我的目的,但输出显示一些MultiLineString
只有两个点的线条,应该是一个整体LineString
。
def calcu_intersect_lines(cgidf):
intersection = gpd.GeoDataFrame(columns=['geometry'], crs=cgidf.crs)
while len(cgidf) > 1:
choose = cgidf.iloc[0]
cgidf.drop(cgidf.index[0], inplace=True)
for i in range(len(cgidf.index)):
cgids = cgidf.iloc[i]
if choose.geometry.exterior.intersects(cgids.geometry.exterior):
intersects = choose.geometry.exterior.intersection(cgids.geometry.exterior)
index = len(intersection)
intersection.loc[index] = [intersects]
else:
continue
return intersection
对于MultiLineString
,我尝试使用该shapely.geometry.LineString.union()
功能将两条短线连接MultiLineString
在一起,如果它们相互接触。但结果也显示了MultiLineString
。
geopandas 本身的交集函数似乎也导致了 a MultiLineString
。
有没有返回正常结果的方法(LineString
不是MultiLineString
连续的公共线路)?
这是输入和输出数据的一个小例子:
a = Polygon(((0, 0), (0, 0.5), (0.5, 1), (1, 0.5), (1, 0), (0.5, -0.5), (0, 0)))
b = Polygon(((0, 0.5), (0.5, 1), (1, 0.5), (1, 2), (0, 0.5)))
c = Polygon(((1, 0.5), (1, 0), (0.5, -0.5), (1.5, -1), (1, 0.5)))
gdf = gpd.GeoDataFrame(columns=['geometry'], data = [a, b, c])
h = calcu_intersect_lines(gdf)
以下是 的值h
:
index geometry
0 MULTILINESTRING ((0 0.5, 0.5 1), (0.5 1, 1 0.5))
1 MULTILINESTRING ((1 0.5, 1 0), (1 0, 0.5 -0.5))
两者LineString
中的 分别MultiLineString
具有公共点(0.5, 1)
和(1, 0)
。
我想要的结果如下:
index geometry
0 LINESTRING (0 0.5, 0.5 1, 1 0.5))
1 LINESTRING (1 0.5, 1 0, 0.5 -0.5))
可能的解决方案:
在评论中,我被提议替换以下行
intersection.loc[index] = [intersects]
经过
intersection.loc[index] = [LineString([*intersects[0].coords, *map(lambda x: x.coords[1], intersects[1:])])]
它在我的简单示例中运行良好。然而,对于真正的 shapefile,它会比这复杂得多。可能有以下几种情况:
具有多条公共线的两个多边形。
from shapely.geometry import Polygon a = Polygon(((0., 0.), (0., 0.5), (0.5, 1.), (1., 0.5), (1., 0.), (0.5, -0.5), (0., 0.))) b = Polygon(((0., 0.5), (0.5, 1.), (1.2, 0.7), (1., 0.), (0.5, -0.5), (2., 0.5), (0., 2.)))
对于
a
和b
,他们有两条公共线路LineString(((0., 0.5), (0.5, 1.)))
和LineString(((1., 0.), (0.5, -0.5)))
。在这种情况下,我可以简单地使用intersects
函数来测试线条是否接触。但是还有另一个问题:a 中的行
MultiLineString
不按顺序排列。from shapely.geometry import MultiLineString ml = MultiLineString((((2, 3), (3, 4)), ((0, 2), (2, 3))))
对于
ml
,此建议将返回错误结果。你对上面的第二个例子有什么想法吗?
解决方案
感谢 Georgy 和其他贡献者的帮助,我已经解决了我的问题。这里shapely.ops.linemerge()
介绍的功能是我解决的重点。
我在这里发布我的解决方案:
from shapely import ops
def union_multils(ml):
'''Union touched LineStrings in MultiLineString or GeometryCollection.
Parameter
---------
ml: GeometryCollection, MultiLineString or LineString
return
------
ul: MultiLineString or LineString: a MultiLineString suggest the LineStrings
in input ml is not connect entitly.
'''
# Drop Point and other geom_type(if exist) out
ml = list(ml)
ml = [l for l in ml if l.geom_type == 'LineString']
# Union
if len(ml) == 1 and ml[0].geom_type == 'LineString':
ul = ml[0]
else:
ul = ops.linemerge(ml)
return ul
推荐阅读
- python - python:如何在路径中转义斜杠?
- perl - 如何在 HPC 集群上使用 PBS 运行 perl 脚本?
- php - codeigniter - 查询字符串中的网站 url 给出未找到错误
- twitter - Twitter Oauth 请求令牌响应 401,我无法解决
- javascript - 系列表格输入
- java - 如何使用 gradle 或 maven 一次仅对一个模块执行声纳分析?
- mysql - LEFT JOIN 创建多行,GROUP BY 或 DISTINCT
- swift - 如何在 ios 中将数据类型声明为时间戳 - swift
- angular - 类型“NodeList”上不存在属性“forEach”
- php - 摘录和“阅读更多”后的省略号