[trails] Use half way waypoints
All checks were successful
build & deploy / test (push) Successful in 1m51s
build & deploy / build-and-deploy (push) Successful in 29s

This commit is contained in:
2026-05-21 17:13:04 -04:00
parent 4d8e925f8c
commit 9bafe45951
4 changed files with 131 additions and 6 deletions

View File

@ -27,6 +27,27 @@ def haversine(lat1, lon1, lat2, lon2):
return R * 2 * asin(sqrt(a))
def find_route_waypoint(points):
if not points:
return None
total = 0.0
for i in range(1, len(points)):
lat1, lon1 = points[i - 1][0], points[i - 1][1]
lat2, lon2 = points[i][0], points[i][1]
if lat1 is not None and lon1 is not None and lat2 is not None and lon2 is not None:
total += haversine(lat1, lon1, lat2, lon2)
halfway = total / 2.0
cumulative = 0.0
for i in range(1, len(points)):
lat1, lon1 = points[i - 1][0], points[i - 1][1]
lat2, lon2 = points[i][0], points[i][1]
if lat1 is not None and lon1 is not None and lat2 is not None and lon2 is not None:
cumulative += haversine(lat1, lon1, lat2, lon2)
if cumulative >= halfway:
return (lat2, lon2)
return (points[-1][0], points[-1][1])
def compute_trail_stats(points):
distance_m = 0.0
elevation_gain = 0.0
@ -221,15 +242,27 @@ def import_trail_gpx(file_path, user_id, original_filename=None):
logger.warning(f"No timestamps in {file_path}")
return []
route_pt = find_route_waypoint(points)
geo, _ = GeoLocation.objects.get_or_create(
lat=round(first_lat, 6),
lon=round(first_lon, 6),
defaults={"altitude": None},
)
trail = Trail.find_by_trailhead(first_lat, first_lon, tolerance_m=100)
trail = Trail.find_by_trailhead(
first_lat, first_lon,
route_lat=route_pt[0] if route_pt else None,
route_lon=route_pt[1] if route_pt else None,
tolerance_m=100,
)
if not trail:
trail = Trail.objects.create(title=track_name, trailhead_location=geo)
trail = Trail.objects.create(
title=track_name,
trailhead_location=geo,
route_lat=route_pt[0] if route_pt else None,
route_lon=route_pt[1] if route_pt else None,
)
timestamp = first_time
stop_timestamp = last_time