Source code for shapelib.utils.line_utils
import numpy as np
from shapely.geometry import LineString, Point
[docs]
def get_perpendicular_lines(
line: LineString,
perpendicular_line_length: float = 0.5,
distance_between_points: float = 0.02,
distance_between_neighbors: float = 0.3,
) -> list[LineString]:
"""
This function returns a list of perpendicular lines to the input line.
Args:
-----
line: LineString
The input line given by the user.
perpendicular_line_length: float
The length of the perpendicular lines.
distance_between_points: float
The distance between the points of the given line to be used as reference
to create the perpendicular lines.
distance_between_neighbors: float
The distance between the current point and its neighbors to be used as
reference to create the perpendicular lines.
"""
current_distance = distance_between_points
perpendicular_lines = []
while current_distance < line.length:
current_point = line.interpolate(current_distance)
current_distance += distance_between_points
left_neighbor = line.interpolate(max(0, current_distance - distance_between_neighbors))
right_neighbor = line.interpolate(min(line.length, current_distance + distance_between_neighbors))
current_point_angle = np.arctan2(right_neighbor.y - left_neighbor.y, right_neighbor.x - left_neighbor.x)
delta_x = perpendicular_line_length * 0.5 * np.sin(current_point_angle)
delta_y = perpendicular_line_length * 0.5 * np.cos(current_point_angle)
point1 = Point([current_point.x - delta_x, current_point.y + delta_y])
point2 = Point([current_point.x + delta_x, current_point.y - delta_y])
perpendicular_lines.append(LineString([point1, point2]))
current_distance += distance_between_points
return perpendicular_lines