gpt4 book ai didi

python - 如何在Shapely中获取LineString的端点

转载 作者:行者123 更新时间:2023-12-04 12:12:34 27 4
gpt4 key购买 nike

Linestring1 = LINESTRING (51.2176008 4.4177154, 51.21758 4.4178548, **51.2175729 4.4179023**, *51.21745162000732 4.41871738126533*)
Linestring2 = LINESTRING (*51.21745162000732 4.41871738126533*, **51.2174025 4.4190475**, 51.217338 4.4194807, 51.2172511 4.4200562, 51.2172411 4.4201077, 51.2172246 4.4201654, 51.2172067 4.420205, 51.2171806 4.4202355, 51.2171074 4.4202929, 51.2170063 4.4203409, 51.2169564 4.4203641, 51.2168076 4.4204243, 51.2166588 4.4204833, 51.2159018 4.420431, 51.2154117 4.4203843)

考虑到这两个线串是从一个更大的线串中剪下来的,如何得到一个线串的端点?

- 点 (51.21745162000732 4.41871738126533) 移除

- 线串 1 的新最后一个元素 = “51.2175729 4.4179023

- 线串 2 的新第一个元素 = “51.2174025 4.4190475

简而言之,我想获得第一部分 (linestring1) 的新最后一个值和第二部分 (linestring2) 的新第一个值,但没有切割它们的点。我怎样才能使这项工作?

最佳答案

获取 LineString 的端点,你只需要访问它的 boundary 属性(property):

from shapely.geometry import LineString

line = LineString([(0, 0), (1, 1), (2, 2)])
endpoints = line.boundary
print(endpoints)
# MULTIPOINT (0 0, 2 2)
first, last = line.boundary
print(first, last)
# POINT (0 0) POINT (2 2)

或者,您可以从 coords 中获取第一个和最后一个点。坐标序列:
from shapely.geometry import Point
first = Point(line.coords[0])
last = Point(line.coords[-1])
print(first, last)
# POINT (0 0) POINT (2 2)

但是,在您的特定情况下,由于您要删除第一条线的最后一个点和第二条线的第一个点,并且只有在此之后才能获得端点,因此您应该构造新的 LineString对象首先使用相同的 coords属性(property):
from shapely.wkt import loads

first_line = loads("LINESTRING (51.2176008 4.4177154, 51.21758 4.4178548, 51.2175729 4.4179023, 51.21745162000732 4.41871738126533)")
second_line = loads("LINESTRING (51.21745162000732 4.41871738126533, 51.2174025 4.4190475, 51.217338 4.4194807, 51.2172511 4.4200562, 51.2172411 4.4201077, 51.2172246 4.4201654, 51.2172067 4.420205, 51.2171806 4.4202355, 51.2171074 4.4202929, 51.2170063 4.4203409, 51.2169564 4.4203641, 51.2168076 4.4204243, 51.2166588 4.4204833, 51.2159018 4.420431, 51.2154117 4.4203843)")
first_line = LineString(first_line.coords[:-1])
second_line = LineString(second_line.coords[1:])
print(first_line.boundary[1], second_line.boundary[0])
# POINT (51.2175729 4.4179023) POINT (51.2174025 4.4190475)

关于python - 如何在Shapely中获取LineString的端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61819120/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com