Converting Geographical Coordinates to a Cartesian Plane

I've got a question, I got the following code to check the intersection between two paths defined by a series of geographical points. The solution involves an R-Tree search for better processing time, however, I am doing the approximation of converting the coordinates into cartesian points, keeping in mind that I am not dealing with distances above 50-80 meters. Is this approximation correct and reasonably accurate?

from shapely.geometry import LineString
from rtree import index

def create_line_segments(path: dict) -> list:
    """
    Create line segments from a path of positions in format {'lat': float, 'lon': float}.
    The segment's length is N-1 where N is the number of positions in the path.

    :param path: the path of positions
    :return: the list of line segments
    """
    segments = []
    for i in range(len(path) - 1):
        p1 = path[i]
        p2 = path[i + 1]
        line = LineString([(p1['lon'], p1['lat']), (p2['lon'], p2['lat'])])
        segments.append(line)
    return segments

def find_intersections(segments1: list, segments2: list) -> list:
    """
    Compute the intersections between two paths of line segments.
    The function uses an R-tree index to speed up the intersection search.

    :param segments1: the list of line segments for the first path
    :param segments2: the list of line segments for the second path
    :return: the list of intersections with the format:
        [{'segments': (segment1, segment2), 'indexes': (index1, index2)}, ...]
    """
    idx = index.Index()
    intersections = []

    # Insert segments from the first path into the R-tree index with their indices
    for pos, segment in enumerate(segments1):
        idx.insert(pos, segment.bounds)
    
    # Check segments from the second path for intersections with indexed segments
    for pos2, segment2 in enumerate(segments2):
        # R-tree query to find possible intersections
        for pos1 in idx.intersection(segment2.bounds):
            if segments1[pos1].intersects(segment2):
                intersections.append({
                        'segments': (segments1[pos1], segments2[pos2]),
                        'indexes': (pos1, pos2)
                    }
                )
    
    return intersections

Upvotes: 0

Views: 43

Answers (0)

Related Questions