-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
290 lines (228 loc) · 8.52 KB
/
models.py
File metadata and controls
290 lines (228 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
GpyX — GPS Route Converter & Planner
Data models: GpsPoint, GpsRoute, GpsTrack, GpsWaypointArray
Originally inspired by ITN Converter v1.94 by Benichou Software (MIT License)
https://github.com/Benichou34/itnconverter
"""
from __future__ import annotations
import math
from dataclasses import dataclass, field
from typing import List, Optional
from enum import Enum
class ArrayType(Enum):
ROUTE = "route"
TRACK = "track"
WAYPOINT = "waypoint"
POI = "poi"
@dataclass
class GpsPoint:
"""A single GPS point with coordinates and metadata."""
lat: float = 0.0
lng: float = 0.0
alt: float = 0.0
name: str = ""
comment: str = ""
def __bool__(self) -> bool:
return self.lat != 0.0 or self.lng != 0.0
def clear(self):
self.lat = 0.0
self.lng = 0.0
self.alt = 0.0
self.name = ""
self.comment = ""
def distance_from(self, other: GpsPoint) -> float:
"""Haversine distance in meters."""
R = 6371000 # Earth radius in meters
lat1, lat2 = math.radians(self.lat), math.radians(other.lat)
dlat = math.radians(other.lat - self.lat)
dlng = math.radians(other.lng - self.lng)
a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlng / 2) ** 2
return R * 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
def copy(self) -> GpsPoint:
return GpsPoint(self.lat, self.lng, self.alt, self.name, self.comment)
class GpsPointArray:
"""Base class for collections of GPS points."""
def __init__(self, array_type: ArrayType, name: str = ""):
self._points: List[GpsPoint] = []
self._name: str = name
self._type: ArrayType = array_type
@property
def name(self) -> str:
return self._name
@name.setter
def name(self, value: str):
self._name = value
@property
def array_type(self) -> ArrayType:
return self._type
def __len__(self) -> int:
return len(self._points)
def __getitem__(self, index) -> GpsPoint:
return self._points[index]
def __setitem__(self, index, value: GpsPoint):
self._points[index] = value
def __iter__(self):
return iter(self._points)
def __bool__(self) -> bool:
return len(self._points) > 0
@property
def empty(self) -> bool:
return len(self._points) == 0
def append(self, point: GpsPoint):
self._points.append(point)
# Alias for C++ push_back
push_back = append
def insert(self, pos: int, point: GpsPoint):
self._points.insert(pos, point)
def pop(self, index: int = -1) -> GpsPoint:
return self._points.pop(index)
def clear(self):
self._points.clear()
self._name = ""
def reverse(self):
self._points.reverse()
@property
def upper_bound(self) -> int:
return max(0, len(self._points) - 1)
def remove_empties(self):
self._points = [p for p in self._points if p]
def remove_duplicates(self):
seen = set()
unique = []
for p in self._points:
key = (p.lat, p.lng)
if key not in seen:
seen.add(key)
unique.append(p)
self._points = unique
def sort_by_name(self):
self._points.sort(key=lambda p: p.name)
def total_distance(self) -> float:
"""Total distance in meters."""
total = 0.0
for i in range(1, len(self._points)):
total += self._points[i - 1].distance_from(self._points[i])
return total
def bounds(self):
"""Returns (min_lat, min_lng, max_lat, max_lng)."""
if not self._points:
return (0, 0, 0, 0)
lats = [p.lat for p in self._points if p]
lngs = [p.lng for p in self._points if p]
return (min(lats), min(lngs), max(lats), max(lngs))
# ─── Simplification algorithms ────────────────────────────
def decimate(self, keep_every_n: int = 2):
"""Keep only every Nth point (always keeps first and last)."""
if keep_every_n < 2 or len(self._points) < 3:
return
result = [self._points[0]]
for i in range(1, len(self._points) - 1):
if i % keep_every_n == 0:
result.append(self._points[i])
result.append(self._points[-1])
self._points = result
@staticmethod
def _perpendicular_distance(pt: GpsPoint, line_start: GpsPoint, line_end: GpsPoint) -> float:
"""
Perpendicular distance from a point to a line segment, in meters.
Uses a flat-earth approximation scaled by latitude (accurate enough
for the small distances involved in track simplification).
"""
cos_lat = math.cos(math.radians(pt.lat))
# Convert to approximate meters
x0 = pt.lng * cos_lat
y0 = pt.lat
x1 = line_start.lng * cos_lat
y1 = line_start.lat
x2 = line_end.lng * cos_lat
y2 = line_end.lat
dx = x2 - x1
dy = y2 - y1
if dx == 0 and dy == 0:
# line_start == line_end
ddx = x0 - x1
ddy = y0 - y1
else:
t = max(0.0, min(1.0, ((x0 - x1) * dx + (y0 - y1) * dy) / (dx * dx + dy * dy)))
ddx = x0 - (x1 + t * dx)
ddy = y0 - (y1 + t * dy)
# Convert degree-distance to meters (1° lat ≈ 111320 m)
return math.sqrt((ddx * 111320) ** 2 + (ddy * 111320) ** 2)
def douglas_peucker(self, epsilon_m: float = 50.0):
"""
Douglas-Peucker simplification. Removes points that don't significantly
alter the route shape. epsilon_m is the tolerance in meters.
Iterative implementation to avoid stack overflow on huge tracks.
"""
n = len(self._points)
if n < 3 or epsilon_m <= 0:
return
# Iterative Douglas-Peucker using an explicit stack
keep = [False] * n
keep[0] = True
keep[n - 1] = True
stack = [(0, n - 1)]
while stack:
start, end = stack.pop()
if end - start < 2:
continue
max_dist = 0.0
max_idx = start
for i in range(start + 1, end):
d = self._perpendicular_distance(
self._points[i], self._points[start], self._points[end]
)
if d > max_dist:
max_dist = d
max_idx = i
if max_dist > epsilon_m:
keep[max_idx] = True
stack.append((start, max_idx))
stack.append((max_idx, end))
self._points = [p for i, p in enumerate(self._points) if keep[i]]
def simplify_for_routing(self, target_points: int = 50):
"""
Smart simplification for routing: auto-tunes Douglas-Peucker epsilon
to reach approximately the target number of points. Always keeps first
and last points.
"""
if len(self._points) <= target_points:
return
# Binary search for the right epsilon
lo, hi = 1.0, 50000.0 # meters
best_pts = list(self._points)
for _ in range(30): # converge quickly
mid = (lo + hi) / 2
# Work on a copy
test = GpsPointArray(self._type, self._name)
test._points = [p.copy() for p in self._points]
test.douglas_peucker(mid)
count = len(test)
if count > target_points:
lo = mid
else:
hi = mid
best_pts = test._points
if abs(count - target_points) <= max(2, target_points * 0.05):
best_pts = test._points
break
self._points = best_pts
class GpsRoute(GpsPointArray):
def __init__(self, name: str = ""):
super().__init__(ArrayType.ROUTE, name)
@classmethod
def from_array(cls, array: GpsPointArray, start: int = 0, count: int = -1) -> GpsRoute:
route = cls(array.name)
end = len(array) if count < 0 else min(start + count, len(array))
for i in range(start, end):
route.append(array[i].copy())
return route
class GpsTrack(GpsPointArray):
def __init__(self, name: str = ""):
super().__init__(ArrayType.TRACK, name)
class GpsWaypointArray(GpsPointArray):
def __init__(self, name: str = ""):
super().__init__(ArrayType.WAYPOINT, name)
class GpsPoiArray(GpsPointArray):
def __init__(self, name: str = ""):
super().__init__(ArrayType.POI, name)