Last active
March 20, 2021 04:50
-
-
Save karolba/71755c3ac66c58ee02c095bd70a82457 to your computer and use it in GitHub Desktop.
Python shapely: distance from point to LineString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import shapely.geometry as geometry | |
from typing import Optional | |
def move_point(point: geometry.Point, angle: float, distance: float) -> geometry.Point: | |
return geometry.Point( | |
point.x + math.cos(angle) * distance, | |
point.y + math.sin(angle) * distance) | |
def radar_line(starting_point: geometry.Point, angle: float, distance: float) -> geometry.LineString: | |
distant_point = move_point(starting_point, angle, distance) | |
return geometry.LineString((starting_point, distant_point)) | |
def distance(point: tuple, angle: float, way: geometry.LineString, max_distance: float = 1000) -> Optional[float]: | |
starting_point = geometry.Point(point) | |
radar = radar_line(starting_point, angle, max_distance) | |
intersection_points = radar.intersection(way) | |
if intersection_points.is_empty: | |
return None | |
return starting_point.distance(intersection_points) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment