Skip to content
This repository was archived by the owner on Nov 16, 2025. It is now read-only.
This repository was archived by the owner on Nov 16, 2025. It is now read-only.

The get_bounds() function returns a BBox that does not take into account the search radius. #5

@berettavexee

Description

@berettavexee

The get_bounds() function returns a BBox that does not take into account the search radius.
The function only returns the minimum and maximum latitude and longitude of the GPX points. The points at the maximum and minimum therefore have a half of their search area outside the BBOX. This can be particularly problematic on a spiral-shaped track.

Here is a proposed solution to this problem (sorry, I don't have a pull request because I got a little carried away on my fork).

def get_bounds(gpx, max_distance_m):
    """
    Return GPX trace bounding box [south, west, north, est]
    iterate over points only once.
    """
    # Initialize min/max values with extreme values
    min_lat = float('inf')
    max_lat = float('-inf')
    min_lon = float('inf')
    max_lon = float('-inf')

    # Compute angular margin use to expand the bounds
    approx_degrees_per_meter = 1 / 111320.0
    angular_margin = max_distance_m * approx_degrees_per_meter * 1.5 #arbitrary safety factor

    # Flag to check if any points were found
    found_points = False

    for trk in gpx.tracks:
        for seg in trk.segments:
            for pt in seg.points:
                found_points = True
                # Update min/max latitude
                min_lat = min(min_lat, pt.latitude)
                max_lat = max(max_lat, pt.latitude)
                # Update min/max longitude
                min_lon = min(min_lon, pt.longitude)
                max_lon = max(max_lon, pt.longitude)

    if not found_points:
        return None

    min_lat -= angular_margin
    max_lat += angular_margin
    min_lon -= angular_margin
    max_lon += angular_margin


    return min_lat, min_lon, max_lat, max_lon

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions