OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Finding the Intersection of 2 Paths (using Geographical Coordinates) by converting to a Cartesian Plane

  • Thread starter Thread starter André Clérigo
  • Start date Start date
A

André Clérigo

Guest
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?

Code:
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
<p>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?</p>
<pre class="lang-py prettyprint-override"><code>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
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
</code></pre>
 

Latest posts

Top