GeoPandas 中的分割线

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

我可以找到如何使用 geopandas/shapely 分割 1 条线

def split_line_by_point(line, point, tolerance: float=1.0e-12):
    return split(snap(line, point, tolerance), point)

但是我不知道如何在保持其他值的同时将其应用于整个几何列。

将上述函数应用到

df.geometry
会丢失一堆信息

如何拆分下面的线串,使其“爆炸”并保持“类型”和“属性”列?

{"Feature":'Hi',"ID":1,Linestring([1,1],[2,2],[3,3])},
{"Feature":'bye',"ID":2,Linestring([10,10],[20,20],[30,30])}

    {"Feature":'Hi',"ID":1,Linestring([1,1],[2,2])},
    {"Feature":'Hi',"ID":1,Linestring([2,2],[3,3])},
    {"Feature":'bye',"ID":2,Linestring([10,10],[20,20])}
    {"Feature":'bye',"ID":2,Linestring([20,20],[30,30])}

线条需要更小,其中

length > x

geopandas shapely
1个回答
1
投票

这对我有用:

import geopandas
from shapely import geometry
from shapely.ops import split, snap

gdf = geopandas.GeoDataFrame([
    {"Feature":'Hi',"ID":1,"geometry": geometry.LineString(([1,1],[2,2],[3,3]))},
    {"Feature":'bye',"ID":2, "geometry": geometry.LineString(([10,10],[20,20],[30,30]))}
])

def split_line_by_point(line, point, tolerance: float=1.0e-12):
    return split(snap(line, point, tolerance), point)

result = (
    gdf
    .assign(geometry=gdf.apply(
        lambda x: split_line_by_point(
            x.geometry, 
            geometry.Point(x.geometry.coords[1])
        ), axis=1
    ))
    .explode()
    .reset_index(drop=True)
)

输出:

  Feature  ID                                           geometry
0      Hi   1      LINESTRING (1.00000 1.00000, 2.00000 2.00000)
1      Hi   1      LINESTRING (2.00000 2.00000, 3.00000 3.00000)
2     bye   2  LINESTRING (10.00000 10.00000, 20.00000 20.00000)
3     bye   2  LINESTRING (20.00000 20.00000, 30.00000 30.00000)
© www.soinside.com 2019 - 2024. All rights reserved.