Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the endpoint of a LineString in Shapely

Tags:

python

shapely

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)

Considering these two linestrings were cut from a bigger linestring, how to get the endpoint of a LineString?

- Point(51.21745162000732 4.41871738126533) removed

- The new last element of linestring 1 = “ 51.2175729 4.4179023

- The new first element of linestring 2 = “ 51.2174025 4.4190475

In short, I want to get the new last value of the first part (linestring1) and the new first value of the second part (linestring2), but without the point where I cut them. How can I make this work?

like image 633
Wout VC Avatar asked May 15 '20 12:05

Wout VC


People also ask

How to get endpoints of a LineString in Python?

To get endpoints of a LineString, you just need to access its 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)

How to create a straight LineString object from two points?

If have two points, from which I want to create a straight LineString object: from shapely.geometry import Point, LineString A = Point (0,0) B = Point (1,1) The Shapely manual for LineString states: A sequence of Point instances is not a valid constructor parameter. A LineString is described by points, but is not composed of Point instances.

How to get the start and end points of a polyline?

The output point feature class will also have a field called LineOID, which records the OID of the polyline it was created from. That way you can join your attribute data back. In QGIS use Extract Specific Vertices tool. Set Vertex indices to 0 to get start points and -1 to get end points or 0,-1 to get both.

What is nearest_points () function in shapely?

The nearest_points()function in shapely.opscalculates the nearest points in a pair of geometries. shapely.ops.nearest_points(geom1, geom2)¶ Returns a tuple of the nearest points in the input geometries.


2 Answers

To get endpoints of a LineString, you just need to access its 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)

Alternatively, you can get the first and the last points from the coords cordinate sequence:

from shapely.geometry import Point
first = Point(line.coords[0])
last = Point(line.coords[-1])
print(first, last)
# POINT (0 0) POINT (2 2)

In your specific case, though, as you want to remove the last point of the first line, and the first point of the second line, and only after that get the endpoints, you should construct new LineString objects first using the same 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)
like image 195
Georgy Avatar answered Oct 19 '22 15:10

Georgy


Similar to Georgy's solution, you can get their coords by unpacking the one you want and ignoring the rest using *_.

from shapely.geometry import LineString

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)])

*_, last_new_1, last_1 = linestring1.coords
first_2, first_new_2, *_ = linestring2.coords

print(last_new_1)
print(first_new_2)
# (51.2175729, 4.4179023)
# (51.2174025, 4.4190475)
like image 1
yzhou Avatar answered Oct 19 '22 13:10

yzhou