Geopandas 组合线条并连接

问题描述 投票:0回答:1

我有一个地理数据框。我想加入重叠的功能并连接“文本”字段。 我尝试过用溶解来做到这一点,但它不起作用

import geopandas as gpd
import pandas as pd
from shapely.geometry import MultiLineString
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)

我正在寻找的是:

result the I'm looking for

合并重叠的特征

python geopandas
1个回答
0
投票

您需要通过几个步骤来完成此操作,但以下脚本显示了执行此操作的选项:

import geopandas as gpd
from matplotlib import pyplot as plt
import pandas as pd
from shapely.geometry import MultiLineString
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)

print(gdf)

# Create the linestrings we want to have in the output
diss_gdf = gdf.dissolve().explode(ignore_index=True)[['geometry']]
diss_gdf["geom_id"] = diss_gdf.index

# Determine which attributes belong with which geometries.
diss_intersection_gdf = diss_gdf.overlay(gdf)
concat_gdf = diss_intersection_gdf.groupby("geom_id").agg({'Text': ' '.join})

# Join the output geometries with the output attributes
result_gdf = diss_gdf.join(concat_gdf)

print(result_gdf)

结果:

                                         geometry  geom_id Text
0                          LINESTRING (0 0, 10 0)        0  A B
1                   LINESTRING (10 0, 16.37798 0)        1    B
2               LINESTRING (0 0, 4.24724 7.04133)        2  C D
3  LINESTRING (4.24724 7.04133, 16.74264 8.98697)        3    C
© www.soinside.com 2019 - 2024. All rights reserved.