在考虑表格条件后,在空间上溶解相交的线

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

在参加一些表格条件后,我需要溶解

geodataframe
中彼此相交的所有线串:1)具有相同的
ID
和 2)具有相同的
opening
年份。这些数据代表一个道路数据库,我正在使用
geopandas
。数据如下:

    id    opening    geometry
0   30    2020       LINESTRING (45.01679 -12.12937, 45.01681 -12...
1   101   1999       LINESTRING (37.02849 -10.65968, 37.02849 -10...
2   30    2019       LINESTRING (47.10667 -15.49339, 47.10665 -15...
3   101   1999       LINESTRING (41.64170 -12.45764, 41.64180 -12...
4   135   2020       LINESTRING (45.31902 -9.76800, 45.31907 -9.7...

我尝试过以下代码:

import geopandas as gpd
import numpy as np
import shapely

gdf_sof = gpd.read_file('your_path/your_file')

# Concatenate the conected segments
gdf = gpd.GeoDataFrame()
for id in gdf_sof.id.unique():
  print(id)
  unique_id = gdf_sof.loc[gdf_sof.id == id]
  print(unique_id.shape)
  gdf_sof_id = gpd.GeoDataFrame()
  for dt in unique_id.opening.unique():
    print(dt)
    unique_oppening = unique_id.loc[unique_id.opening == dt]
    dissolved = gpd.geoseries.GeoSeries([geom for geom in unique_oppening.unary_union.geoms])
    gdf_sof_id = pd.concat([gdf_sof_id, dissolved], ignore_index=True, axis=0)
  gdf.concat(gdf_sof_id, ignore_index=True, axis=0, inplace=True)

gdf.shape

但收到此错误消息:

---------------------------------------------------------------------------
AttributeError: 'LineString' object has no attribute 'geoms'
python geopandas shapely
1个回答
0
投票

我仍在检查结果是否良好,但我找到的解决方案是这样的:

import geopandas as gpd
import numpy as np
import pandas as pd
import shapely
from shapely.ops import linemerge

gdf_sof = gpd.read_file('your_path/your_file')

# Dissolve the connected segments
gdf = gpd.GeoDataFrame()
for id in gdf_sof.id.unique():
  unique_id = gdf_sof.loc[gdf_sof.id == id]
  gdf_sof_id = gpd.GeoDataFrame()
  for dt in unique_id.opening.unique():
    unique_opening = unique_id.loc[unique_id.opening == dt]
    shp = shapely.get_parts(unique_opening.geometry)
    dissolved = linemerge(shp)
    gdf_dissolved = gpd.GeoDataFrame({'id': [id],
                                      'opening': [dt],
                                      'geometry':dissolved},
                                      geometry = 'geometry', 
                                      crs = gdf_sof.crs)
    gdf_sof_id = pd.concat([gdf_sof_id, gdf_dissolved], ignore_index=True, axis=0)
  gdf = pd.concat([gdf, gdf_sof_id], ignore_index=True, axis=0)

gdf.shape

我的起始 gdf (

gdf_sof
) 的形状为
(7366, 3)
,最终的
gdf
的形状为
(973, 3)
,所以它可能有效,但我正在仔细检查。感谢@Pieter 的评论!

© www.soinside.com 2019 - 2024. All rights reserved.