Estoy buscando una solución para dividir un LineString
en la intersección con otro LineString
. Mi primera solución es:
from shapely import wkt
from shapely.ops import split
first_line = wkt.loads('LINESTRING (28.112115297354478 57.00625960468798, 8.219782679723316 20.076477637253483)')
second_line = wkt.loads('LINESTRING (-36.57919844025919 19.94938832911169, 8.426621630216765 20.077064414418004, 29.684689833718174 0.223433149286052)')
real_intersection = second_line.intersection(first_line) # POINT (8.219782679723316 20.076477637253483)
split_second_line = split(second_line, real_intersection)
Basado en dividir() ' documentación espero ver al menos dos LINESTRING
en GEOMETRYCOLLECTION
. La salida: GEOMETRYCOLLECTION (LINESTRING (-36.57919844025919 19.94938832911169, 8.426621630216765 20.077064414418004, 29.684689833718174 0.223433149286052))
Aparentemente no hay intersección entre first_line
y second_line
así que construyo una extensión de first_line
:
from math import atan2, cos, sin, degrees
from shapely.geometry import Point, LineString
first_line_coords = list(first_line.coords)
lon1 = first_line_coords[0][0]
lat1 = first_line_coords[0][1]
lon2 = first_line_coords[1][0]
lat2 = first_line_coords[1][1]
angle = atan2(cos(lat1)*sin(lat2)-sin(lat1) * cos(lat2)*cos(lon2-lon1), sin(lon2-lon1)*cos(lat2))
bearing = (degrees(angle) + 360) % 360
new_first_line_length = first_line.length + 10
end_point = Point(lon1 + new_first_line_length * math.cos(bearing), lon1 + new_first_line_length * math.sin(bearing))
start_point = Point(first_line_coords[0])
new_first_line = LineString([start_point, end_point])
new_real_intersection = second_line.intersection(new_first_line) # POINT (7.015156377654302 20.073060257673145)
new_split_second_line = split(second_line, new_real_intersection)
La salida es: GEOMETRYCOLLECTION (LINESTRING (-36.57919844025919 19.94938832911169, 8.426621630216765 20.077064414418004, 29.684689833718174 0.223433149286052))
De nuevo sólo tengo una LINESTRING
. No entiendo dónde está mi error.