python - 如何将 LineString 拆分为段

标签 python postgis geopandas shapely

我的数据集包含一个 LineString我想过滤掉这个 LineString 的各个线段.更准确地说,每一个街道段。

到目前为止,我已经从数据集中提取了各个点并将它们保存在一个单独的列表中。此外,我想再次收集这些点并从中创建单独的 LineStrings 以将它们存储到 Geodataframe 中。
数据具有以下形式:

LINESTRING (3275.284016199762 340555.8579582386, 3241.504528076811 340504.1348617533, 3245.415803206172 340501.457084205, 3280.414559049542 340552.7138220053, 3285.19053022

我的问题是我必须创建并明确保存一个单独的 LineString对于每次迭代。
谁能帮我这个?有没有更好的方法呢?
from shapely.geometry import Point, LineString

#Loop over LineString and gather Points
c=[]

for i in range(0,end):
    c.append(Point(route1.coords[i]))


iterator=len(c)
max=len(c)-1

#Loop to store LineStrings - got stuck here
for i in np.arange(0,iterator):
    if i<max:
        LineString([c[i], c[i+1]]).wkt

    else:
        break;


输出应如下所示:
Linestring(Point A, Point B)  
Linestring(Point B, Point C)  
Linestring(Point C, Point D)  
...  
Linestring(Point Y, Point Z)

最佳答案

说到 Shapely,它没有提供将曲线对象( LineStringLinearRing )分割成段的功能,因此您必须自己编写。这是一个如何使用 zip 执行此操作的示例迭代成对 coordinates map 他们到 LineString的:

from shapely.geometry import LineString, LinearRing


def segments(curve):
    return list(map(LineString, zip(curve.coords[:-1], curve.coords[1:])))


line = LineString([(0, 0), (1, 1), (2, 2)])
ring = LinearRing([(0, 0), (1, 0), (1, 1), (0, 1)])

line_segments = segments(line)
for segment in line_segments:
    print(segment)
# LINESTRING (0 0, 1 1)
# LINESTRING (1 1, 2 2)

ring_segments = segments(ring)
for segment in ring_segments:
    print(segment)
# LINESTRING (0 0, 1 0)
# LINESTRING (1 0, 1 1)
# LINESTRING (1 1, 0 1)
# LINESTRING (0 1, 0 0)

关于python - 如何将 LineString 拆分为段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62053253/

相关文章:

python - Bottle.py服务器部署

python - 如何在 Python 中将 GeoDataFrame 保存到磁盘?

python - 将点转换为线 Geopandas

python - 在 pyparsing 中获取等同于 asXML() 的数据结构?

python - 每行包含整个数据集中特定值的计数的列

php - 如何在 pg_query_params 调用中使用 ST_GeomFromText ('Point($1 $2)' , 4326)

routes - build 道路网

python - OSError :/usr/lib/libgdal. so.20: undefined symbol :sqlite3_column_table_name

geopandas - 在 GeoPandas 中更改 CRS

python - 如何在 AWS EC2 实例上安装 Python 3?