Sample points along a track, from the ends, or from the place

This commit is contained in:
swaits committed 2026-11-20 10:28:52 -07:00
1 parent 2f4bc6862e
commit f7e2757e2b
2 files changed
+384 -1

No files matched your search

+266 -1
View File
@@ -25,8 +25,18 @@ import sys
sys.dont_write_bytecode = True
import bisect
import json
import math
import os
import statistics
from dataclasses import dataclass
import tomllib
from dataclasses import dataclass, replace
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import season_climate # noqa: E402 (types and distance only; no network)
RULE_VERSION = 1
@@ -129,3 +139,258 @@ def why_not(points, params):
return (f"no month passes the cold, snow and heat tests together "
f"(cold passes in {cold_ok} months, snow in {snow_ok}, "
f"heat in {heat_ok})")
# ---------------------------------------------------------------------------
# Sample points from a record. Track pieces are joined and oriented with
# the same rule tools/elevation.py uses to build elevation.json (ends
# within 100 m join, the whole track runs south to north or west to
# east, parts in order along that axis, gaps between parts not counted
# in the distance), reimplemented here with the standard library only,
# so a sample point's kilometre matches the profile's kilometre and its
# elevation can be read from the profile.
EARTH_R_M = 6371008.8
JOIN_GAP_M = 100.0
LOOP_FRACTION = 0.1
SAMPLE_KM = 50.0
MIN_POINTS = 3
MAX_POINTS = 12
def _track_parts(data):
if data.get("type") == "FeatureCollection":
geoms = [(f.get("geometry") or {}) for f in data.get("features", [])]
elif data.get("type") == "Feature":
geoms = [data.get("geometry") or {}]
else:
geoms = [data]
parts = []
for geom in geoms:
if geom.get("type") == "LineString":
parts.append(geom["coordinates"])
elif geom.get("type") == "MultiLineString":
parts.extend(geom["coordinates"])
return [[(float(c[0]), float(c[1])) for c in p] for p in parts if len(p) >= 2]
def _gap_m(a, b):
dx = (a[0] - b[0]) * math.cos(math.radians((a[1] + b[1]) / 2))
return math.hypot(dx, a[1] - b[1]) * math.pi / 180.0 * EARTH_R_M
def _seg_km(a, b):
return season_climate.haversine_km(a[1], a[0], b[1], b[0])
def _join_parts(parts, gap_m=JOIN_GAP_M):
if not parts:
return []
cell = max(gap_m, 1.0)
k = math.pi / 180.0 * EARTH_R_M
max_lat = max(abs(c[1]) for p in parts for c in (p[0], p[-1]))
scale = max(math.cos(math.radians(max_lat)), 1e-9) * k
def cell_of(c):
return (math.floor(c[0] * scale / cell), math.floor(c[1] * k / cell))
grid = {}
for i, p in enumerate(parts):
for end in (0, 1):
grid.setdefault(cell_of(p[0] if end == 0 else p[-1]), []).append((i, end))
used = [False] * len(parts)
def nearest(point):
gx, gy = cell_of(point)
best = None
for x in (gx - 1, gx, gx + 1):
for y in (gy - 1, gy, gy + 1):
for i, end in grid.get((x, y), ()):
if used[i]:
continue
d = _gap_m(point, parts[i][0] if end == 0 else parts[i][-1])
if d <= gap_m and (best is None or d < best[0]):
best = (d, i, end)
return best
out = []
for i0, first in enumerate(parts):
if used[i0]:
continue
used[i0] = True
chain = list(first)
while True:
hit = nearest(chain[-1])
if hit is None:
break
_, i, end = hit
used[i] = True
chain.extend(parts[i] if end == 0 else parts[i][::-1])
while True:
hit = nearest(chain[0])
if hit is None:
break
_, i, end = hit
used[i] = True
chain[:0] = parts[i][::-1] if end == 0 else parts[i]
out.append(chain)
return out
def _orient(chains):
"""Chains turned to run south to north or west to east and put in
order along that axis, as tools/elevation.py does."""
if not chains:
return []
lons = [c[0] for ch in chains for c in ch]
lats = [c[1] for ch in chains for c in ch]
mid_lat = (min(lats) + max(lats)) / 2.0
width = (max(lons) - min(lons)) * math.cos(math.radians(mid_lat))
axis = 1 if (max(lats) - min(lats)) > width else 0
out = []
for ch in chains:
length_km = sum(_seg_km(a, b) for a, b in zip(ch, ch[1:]))
if (_gap_m(ch[0], ch[-1]) > LOOP_FRACTION * length_km * 1000.0
and ch[-1][axis] < ch[0][axis]):
ch = ch[::-1]
out.append(ch)
out.sort(key=lambda ch: sum(c[axis] for c in ch) / len(ch))
return out
def _ordered_track(path):
"""(vertices, cumulative km) along the joined, oriented track.
The first vertex of each part sits at the same distance as the last
vertex of the part before it: gaps are not counted."""
with open(path, encoding="utf-8") as f:
data = json.load(f)
chains = _orient(_join_parts(_track_parts(data)))
verts, cum = [], []
total = 0.0
for chain in chains:
for j, vertex in enumerate(chain):
if j:
total += _seg_km(chain[j - 1], vertex)
verts.append(vertex)
cum.append(total)
return verts, cum
def _point_at(verts, cum, target_km):
i = bisect.bisect_right(cum, target_km) - 1
i = max(0, min(i, len(verts) - 2))
span = cum[i + 1] - cum[i]
frac = 0.0 if span <= 0 else (target_km - cum[i]) / span
frac = max(0.0, min(1.0, frac))
lon = verts[i][0] + frac * (verts[i + 1][0] - verts[i][0])
lat = verts[i][1] + frac * (verts[i + 1][1] - verts[i][1])
if target_km >= cum[-1]:
lon, lat = verts[-1]
return lat, lon
def _profile_elev(profile, km):
"""Elevation in metres from an elevation.json profile at km."""
if not profile:
return None
kms = [float(p[0]) for p in profile]
i = bisect.bisect_right(kms, km) - 1
if i < 0:
return float(profile[0][1])
if i >= len(profile) - 1:
return float(profile[-1][1])
km0, el0 = float(profile[i][0]), float(profile[i][1])
km1, el1 = float(profile[i + 1][0]), float(profile[i + 1][1])
if km1 <= km0:
return el1
return el0 + (km - km0) / (km1 - km0) * (el1 - el0)
def _load_profile(root, tid):
path = os.path.join(root, tid, "elevation.json")
if not os.path.exists(path):
return None, None
with open(path, encoding="utf-8") as f:
doc = json.load(f)
return doc.get("profile") or None, (doc.get("highest") or None)
def _give_high_point(points, hike, highest):
"""With no profile, one point carries high_point_m so the high
ground is represented at all: the point nearest the profile's
highest place when that is known, else the first point."""
high_m = hike.get("high_point_m")
if high_m is None or not points:
return points
idx = 0
if highest and highest.get("lat") is not None:
idx = min(range(len(points)),
key=lambda i: season_climate.haversine_km(
points[i].lat, points[i].lon,
float(highest["lat"]), float(highest["lon"])))
return [replace(p, elev_m=float(high_m)) if i == idx else p
for i, p in enumerate(points)]
def sample_points(root, rec):
"""Sample points for a record and how they were got: "track" (one
per 50 km of track, at least 3, at most 12, evenly spaced, with
elevations from the profile), "ends" (hike.start_lonlat and
hike.end_lonlat), or "place" (the centre of the first region's box
in web/place-bounds.toml). An elevation of None lets the climate
source supply its own cell elevation. With no profile, one point
carries hike.high_point_m when the record has one."""
tid = rec.get("id") or ""
hike = rec.get("hike") or {}
line_file = (rec.get("source") or {}).get("line_file") or "line.geojson"
line_path = os.path.join(root, tid, line_file)
if tid and os.path.exists(line_path):
try:
verts, cum = _ordered_track(line_path)
except (OSError, ValueError, KeyError, TypeError, IndexError):
verts, cum = [], []
if len(verts) >= 2 and cum and cum[-1] > 0:
total = cum[-1]
n = min(MAX_POINTS, max(MIN_POINTS, int(round(total / SAMPLE_KM)) + 1))
profile, highest = _load_profile(root, tid)
points = []
for i in range(n):
target = total * i / (n - 1)
lat, lon = _point_at(verts, cum, target)
elev = _profile_elev(profile, target)
points.append(SamplePoint(lat=lat, lon=lon, elev_m=elev,
km=round(target, 2)))
if not profile:
points = _give_high_point(points, hike, highest)
return points, "track"
ends = []
for key in ("start_lonlat", "end_lonlat"):
value = hike.get(key)
if value and len(value) == 2:
ends.append(SamplePoint(lat=float(value[1]), lon=float(value[0]),
elev_m=None, km=None))
if ends:
if hike.get("high_point_m") is not None:
ends[0] = replace(ends[0], elev_m=float(hike["high_point_m"]))
return ends, "ends"
regions = rec.get("regions") or []
country = rec.get("country") or ""
bounds_path = os.path.join(root, "web", "place-bounds.toml")
if regions and os.path.exists(bounds_path):
with open(bounds_path, "rb") as f:
boxes = (tomllib.load(f) or {}).get("box") or {}
box = boxes.get(f"{country}-{regions[0]}")
if box is None:
for key, value in boxes.items():
if key.endswith("-" + str(regions[0])):
box = value
break
if box and len(box) == 4:
elev = hike.get("high_point_m")
return [SamplePoint(lat=(float(box[1]) + float(box[3])) / 2,
lon=(float(box[0]) + float(box[2])) / 2,
elev_m=None if elev is None else float(elev),
km=None)], "place"
return [], "place"
+118
View File
@@ -82,5 +82,123 @@ class RuleTest(unittest.TestCase):
self.assertIn("no data", E.why_not([], PARAMS))
class SamplePointsTest(unittest.TestCase):
def make_root(self, td, parts=None, profile=None, multi=False):
import json
import tempfile # noqa: F401 (td is made by the caller)
d = os.path.join(td, "trail-x")
os.makedirs(os.path.join(td, "web"))
os.makedirs(d)
with open(os.path.join(td, "web", "place-bounds.toml"), "w") as f:
f.write('[box]\n"US-VT" = [-73.5, 42.7, -71.5, 45.0]\n')
if parts is not None:
if multi:
geom = {"type": "MultiLineString", "coordinates": parts}
else:
geom = {"type": "LineString", "coordinates": parts[0]}
doc = {"type": "FeatureCollection",
"features": [{"type": "Feature", "geometry": geom}]}
with open(os.path.join(d, "line.geojson"), "w") as f:
json.dump(doc, f)
if profile is not None:
with open(os.path.join(d, "elevation.json"), "w") as f:
json.dump({"profile": profile}, f)
return td
@staticmethod
def meridian(lat0, lat1, step=0.1, lon=-110.0):
n = int(round((lat1 - lat0) / step))
return [[lon, lat0 + i * step] for i in range(n + 1)]
def rec(self, **hike):
base = {"summary": "x", "checked": "2026-01-01", "source": []}
base.update(hike)
return {"id": "trail-x", "country": "US", "regions": ["VT"],
"source": {"line_file": "line.geojson"}, "hike": base}
def test_a_track_gives_points_spaced_along_it(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td, parts=[self.meridian(43.0, 44.8)],
profile=[[0, 1000], [200, 1000]])
points, kind = E.sample_points(root, self.rec())
self.assertEqual(kind, "track")
self.assertEqual(len(points), 5) # about 200 km, one per 50 km
self.assertEqual(points[0].km, 0.0)
self.assertAlmostEqual(points[0].lat, 43.0, places=3)
self.assertLess(abs(points[-1].km - 200.15), 50)
self.assertAlmostEqual(points[-1].lat, 44.8, places=3)
def test_a_short_track_still_gets_three_points(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td, parts=[self.meridian(43.0, 43.09, 0.03)],
profile=[[0, 500], [10, 500]])
points, kind = E.sample_points(root, self.rec())
self.assertEqual(len(points), 3)
def test_a_very_long_track_is_capped_at_twelve(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td, parts=[self.meridian(35.0, 45.0, 0.5)],
profile=[[0, 500], [1112, 500]])
points, kind = E.sample_points(root, self.rec())
self.assertEqual(len(points), 12)
def test_a_multipart_track_is_sampled_along_all_parts(self):
import tempfile
parts = [self.meridian(43.0, 43.9), self.meridian(44.1, 45.0)]
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td, parts=parts, multi=True,
profile=[[0, 500], [200, 500]])
points, kind = E.sample_points(root, self.rec())
self.assertEqual(kind, "track")
self.assertTrue(any(p.lat > 44.1 for p in points))
self.assertTrue(any(p.lat < 43.9 for p in points))
def test_elevation_comes_from_the_profile_by_distance(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td, parts=[self.meridian(43.0, 44.8)],
profile=[[0, 1000], [100, 2000], [200, 1500]])
points, kind = E.sample_points(root, self.rec())
middle = points[len(points) // 2]
self.assertLess(abs(middle.elev_m - 2000), 30)
self.assertLess(abs(points[0].elev_m - 1000), 30)
def test_a_track_without_a_profile_uses_the_high_point_for_the_top_only(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td, parts=[self.meridian(43.0, 44.8)])
points, kind = E.sample_points(root, self.rec(high_point_m=3000))
elevs = [p.elev_m for p in points]
self.assertEqual(elevs.count(3000), 1)
self.assertEqual(elevs.count(None), len(elevs) - 1)
def test_no_track_uses_the_two_ends(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td)
points, kind = E.sample_points(
root, self.rec(start_lonlat=[-110.0, 43.0],
end_lonlat=[-109.0, 44.0]))
self.assertEqual(kind, "ends")
self.assertEqual(len(points), 2)
self.assertEqual((points[0].lat, points[0].lon), (43.0, -110.0))
self.assertEqual((points[1].lat, points[1].lon), (44.0, -109.0))
def test_no_track_and_no_ends_uses_the_place_centre(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
root = self.make_root(td)
points, kind = E.sample_points(root, self.rec())
self.assertEqual(kind, "place")
self.assertEqual(len(points), 1)
self.assertTrue(-73.5 <= points[0].lon <= -71.5)
self.assertTrue(42.7 <= points[0].lat <= 45.0)
self.assertAlmostEqual(points[0].lon, -72.5)
self.assertAlmostEqual(points[0].lat, 43.85)
if __name__ == "__main__":
unittest.main()