You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Geopandas合并重叠线要素并拼接Text字段问题求助

合并GeoDataFrame中重叠要素并拼接文本字段

你尝试用dissolve直接合并重叠要素但未成功,是因为dissolve需要明确的分组键,而重叠要素的分组是基于空间关系的,而非普通字段。下面是可行的解决方案:

解决方案步骤

  1. 识别空间相交的要素组:通过空间自连接找到所有相交的要素对,再用图论的连通分量确定哪些要素属于同一组。
  2. 按组合并要素:使用dissolve按分组ID合并,同时拼接Text字段、合并几何。

完整代码实现

import geopandas as gpd
import pandas as pd
from shapely.geometry import MultiLineString
import networkx as nx

# 你的原始数据加载代码
data = {
    "type": "FeatureCollection",
    "name": "lines",
    "features": [
        { "type": "Feature", "properties": { "Text": "A" }, 
          "geometry": { "type": "MultiLineString", 
                        "coordinates": [ [ [ 0.0, 0.0, 0.0 ], [ 10.0, 0.0, 0.0 ] ] ] } },
        { "type": "Feature", "properties": { "Text": "B" }, 
          "geometry": { "type": "MultiLineString", 
                        "coordinates": [ [ [ 0.0, 0.0, 0.0 ], [ 16.37797725526616, 0.0, 0.0 ] ] ] } },
        { "type": "Feature", "properties": { "Text": "C" }, 
          "geometry": { "type": "MultiLineString", 
                        "coordinates": [ [ [ 0.0, 0.0, 0.0 ], [ 4.247235166607424, 7.041334981156978, 0.0 ], 
                                          [ 16.742636807728559, 8.986970615165774, 0.0 ] ] ] } },
        { "type": "Feature", "properties": { "Text": "D" }, 
          "geometry": { "type": "MultiLineString", 
                        "coordinates": [ [ [ 0.0, 0.0, 0.0 ], [ 4.247235166607424, 7.041334981156978, 0.0 ] ] ] } }
    ]
}
features = data['features']
geometries = []
properties = []
for feature in features:
    coords_3d = feature['geometry']['coordinates']
    coords_2d = [[(x, y) for x, y, z in line] for line in coords_3d]
    multiline = MultiLineString(coords_2d)
    geometries.append(multiline)
    properties.append(feature['properties'])
df = pd.DataFrame(properties)
gdf = gpd.GeoDataFrame(df, geometry=geometries)

# 开始合并重叠要素
# 1. 空间自连接找到所有相交要素对
sjoined = gpd.sjoin(gdf, gdf, predicate='intersects', how='left')

# 2. 构建图结构,确定连通分量(即重叠/相交的要素组)
G = nx.Graph()
G.add_nodes_from(gdf.index)
for idx, row in sjoined.iterrows():
    if idx != row['index_right']:
        G.add_edge(idx, row['index_right'])

# 3. 为每个要素分配组ID
group_mapping = {}
for group_idx, component in enumerate(nx.connected_components(G)):
    for node in component:
        group_mapping[node] = group_idx
gdf['group_id'] = gdf.index.map(group_mapping)

# 4. 按组ID合并,拼接Text字段并合并几何
merged_gdf = gdf.dissolve(
    by='group_id',
    aggfunc={
        'Text': lambda texts: ','.join(texts),
        'geometry': lambda geoms: geoms.unary_union
    }
).reset_index(drop=True)

# 输出结果
print(merged_gdf)
# 可视化验证
merged_gdf.plot(column='Text', cmap='viridis', figsize=(10,6))

代码解释

  • 空间自连接:gpd.sjoin用intersects谓词匹配所有相交的要素,为后续分组提供依据。
  • 图论连通分量:用NetworkX将相交要素连接成图,连通分量就是一组需要合并的重叠/相交要素。
  • dissolve合并:按生成的group_id分组,用lambda函数拼接Text字段,用unary_union合并几何对象,得到最终的合并结果。

内容的提问来源于stack exchange,提问作者Eduardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 06:28:18