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)
Run Code Online (Sandbox Code Playgroud)
考虑到这两个线串是从一个更大的线串中剪下来的,如何得到一个线串的端点?
- 点(51.21745162000732 4.41871738126533)被移除
- 线串 1 的新最后一个元素 = “ 51.2175729 4.4179023
- 线串 2 的新第一个元素 = “ 51.2174025 4.4190475
简而言之,我想获得第一部分 (linestring1) 的新最后一个值和第二部分 (linestring2) 的新第一个值,但没有切割它们的点。我怎样才能使这项工作?
Geo*_*rgy 15
要获取 a 的端点LineString,您只需要访问其boundary属性:
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)
Run Code Online (Sandbox Code Playgroud)
或者,您可以从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)
Run Code Online (Sandbox Code Playgroud)
但是,在您的特定情况下,由于您要删除第一条线的最后一个点和第二条线的第一个点,并且只有在此之后才能获取端点,因此您应该LineString首先使用相同的coords属性构造新对象:
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)
Run Code Online (Sandbox Code Playgroud)
Dar*_*ylG -4
通过两个例程解决,允许将线串分割如下。
功能: split_first 返回第一个点,以及没有第一个点的 LineString
功能: split_last 返回最后一个点,以及从第一个点开始不包括最后一个点的 LineString
代码
from shapely.ops import nearest_points
from shapely.geometry import Point
from shapely.geometry import LineString
def split_first(linestring):
" returns first point and linestring without first point "
coords = list(linestring.coords)
p, *x = coords
return Point(p), LineString(x)
def split_last(linestring):
" returns first point and linestring without first point "
*x, p = list(linestring.coords) = list(linestring.coords)
return Point(p), LineString(x)
Run Code Online (Sandbox Code Playgroud)
测试
数据
linestring = LineString([(51.2176008,4.4177154), (51.21758,4.4178548), (51.2175729,4.4179023), (51.21745162000732,4.41871738126533)])
Run Code Online (Sandbox Code Playgroud)
第一个点和不包括第一个点的线串
p, l = split_first(linestring)
print(p)
print(l)
Run Code Online (Sandbox Code Playgroud)
出去
POINT (51.2176008 4.4177154)
LINESTRING (51.21758 4.4178548, 51.2175729 4.4179023, 51.21745162000732 4.41871738126533)
Run Code Online (Sandbox Code Playgroud)
第一个最后一个点,以及不包括最后一个的线串
p, l = split_last(linestring)
print(p)
print(l)
Run Code Online (Sandbox Code Playgroud)
出去
POINT (51.21745162000732 4.41871738126533)
LINESTRING (51.2176008 4.4177154, 51.21758 4.4178548, 51.2175729 4.4179023)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3321 次 |
| 最近记录: |