-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps_utils.py
70 lines (59 loc) · 2.18 KB
/
gps_utils.py
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
# -*- coding: utf-8 -*-
import math
import numpy as np
# RDP algorithm as long as the rdp package is not iterative.
# See https://github.com/fhirschmann/rdp/issues/5
def _DouglasPeucker(points, startIndex, lastIndex, epsilon):
stk = []
stk.append([startIndex, lastIndex])
globalStartIndex = startIndex
bitArray = np.ones(lastIndex-startIndex+1, dtype=bool)
while len(stk) > 0:
startIndex = stk[-1][0]
lastIndex = stk[-1][1]
stk.pop()
dmax = 0.
index = startIndex
for i in range(index+1, lastIndex):
if bitArray[i - globalStartIndex]:
d = PointLineDistance(points[i], points[startIndex], points[lastIndex])
if d > dmax:
index = i
dmax = d
if dmax > epsilon:
stk.append([startIndex, index])
stk.append([index, lastIndex])
else:
for i in range(startIndex + 1, lastIndex):
bitArray[i - globalStartIndex] = False
return bitArray
def rdp(points, epsilon):
"""
Ramer-Douglas-Peucker algorithm
"""
bitArray = _DouglasPeucker(points, 0, len(points)-1, epsilon)
resList = []
for i in range(len(points)):
if bitArray[i]:
resList.append(points[i])
return np.array(resList)
def PointLineDistance(point, start, end):
if np.all(np.equal(start, end)) :
return np.linalg.norm(point, start)
n = abs((end[0] - start[0]) * (start[1] - point[1]) - (start[0] - point[0]) * (end[1] - start[1]))
d = math.sqrt((end[0] - start[0]) * (end[0] - start[0]) + (end[1] - start[1]) * (end[1] - start[1]))
return n/d
def haversine(coord1, coord2):
"""
Haversine distance in meters for two (lat, lon) coordinates
"""
lat1, lon1 = coord1
lat2, lon2 = coord2
radius = 6371000 # mean earth radius in meters (GRS 80-Ellipsoid)
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d