-
Notifications
You must be signed in to change notification settings - Fork 17
/
point.py
202 lines (167 loc) · 5.93 KB
/
point.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
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
"""
Point class
"""
import math
import datetime
from .utils import isostr_to_datetime
EPOCH = datetime.datetime.utcfromtimestamp(0)
class Point(object):
""" Spaciotemporal point representation
Attributes:
lat (float): latitude
lon (float): longitude
time (:obj:`datetime.datetime`): time
dt (float): time difference in seconds from the past point in the segment
should be computed
acc (float): accelaration in km^2/h, relative to the previous point in the segment
should be computed with compute_metrics method
vel (float): velocity in km/h, relative to the previous point in the segment
should be computed with compute_metrics method
"""
def __init__(self, lat, lon, time):
self.lon = lon
self.lat = lat
self.time = time
self.dt = .0 #pylint: disable=invalid-name
self.dx = .0 #pylint: disable=invalid-name
self.acc = .0
self.vel = .0
def get_timestamp(self):
""" Gets the timestamp of this point's time, seconds since 1970
Returns:
float: time since epoch, in seconds
"""
return (self.time - EPOCH).total_seconds()
def gen2arr(self):
""" Generate a location array
Returns:
:obj:`list` of float: List with longitude and latitude
"""
return [self.lon, self.lat]
def gen3arr(self):
""" Generate a time-location array
Returns:
:obj:`list` of float: List with longitude, latitude and timestamp
"""
return [self.lon, self.lat, self.get_timestamp()]
def distance(self, other):
""" Distance between points
Args:
other (:obj:`Point`)
Returns:
float: Distance in km
"""
return distance(self.lat, self.lon, None, other.lat, other.lon, None)
def time_difference(self, previous):
""" Calcultes the time difference against another point
Args:
previous (:obj:`Point`): Point before
Returns:
Time difference in seconds
"""
return abs((self.time - previous.time).total_seconds())
def compute_metrics(self, previous):
""" Computes the metrics of this point
Computes and updates the dt, vel and acc attributes.
Args:
previous (:obj:`Point`): Point before
Returns:
:obj:`Point`: Self
"""
delta_t = self.time_difference(previous)
delta_x = self.distance(previous)
vel = 0
delta_v = 0
acc = 0
if delta_t != 0:
vel = delta_x/delta_t
delta_v = vel - previous.vel
acc = delta_v/delta_t
self.dt = delta_t
self.dx = delta_x
self.acc = acc
self.vel = vel
return self
@staticmethod
def from_gpx(gpx_track_point):
""" Creates a point from GPX representation
Arguments:
gpx_track_point (:obj:`gpxpy.GPXTrackPoint`)
Returns:
:obj:`Point`
"""
return Point(
lat=gpx_track_point.latitude,
lon=gpx_track_point.longitude,
time=gpx_track_point.time
)
def to_json(self):
""" Creates a JSON serializable representation of this instance
Returns:
:obj:`dict`: For example,
{
"lat": 9.3470298,
"lon": 3.79274,
"time": "2016-07-15T15:27:53.574110"
}
"""
return {
'lat': self.lat,
'lon': self.lon,
'time': self.time.isoformat() if self.time is not None else None
}
@staticmethod
def from_json(json):
""" Creates Point instance from JSON representation
Args:
json (:obj:`dict`): Must have at least the following keys: lat (float), lon (float),
time (string in iso format). Example,
{
"lat": 9.3470298,
"lon": 3.79274,
"time": "2016-07-15T15:27:53.574110"
}
json: map representation of Point instance
Returns:
:obj:`Point`
"""
return Point(
lat=json['lat'],
lon=json['lon'],
time=isostr_to_datetime(json['time'])
)
ONE_DEGREE = 1000. * 10000.8 / 90.
EARTH_RADIUS = 6371 * 1000
def to_rad(number):
""" Degrees to rads """
return number / 180. * math.pi
def haversine_distance(latitude_1, longitude_1, latitude_2, longitude_2):
"""
Haversine distance between two points, expressed in meters.
Implemented from http://www.movable-type.co.uk/scripts/latlong.html
"""
d_lat = to_rad(latitude_1 - latitude_2)
d_lon = to_rad(longitude_1 - longitude_2)
lat1 = to_rad(latitude_1)
lat2 = to_rad(latitude_2)
#pylint: disable=invalid-name
a = math.sin(d_lat/2) * math.sin(d_lat/2) + \
math.sin(d_lon/2) * math.sin(d_lon/2) * math.cos(lat1) * math.cos(lat2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = EARTH_RADIUS * c
return d
#pylint: disable=too-many-arguments
def distance(latitude_1, longitude_1, elevation_1, latitude_2, longitude_2, elevation_2,
haversine=None):
""" Distance between two points """
# If points too distant -- compute haversine distance:
if haversine or (abs(latitude_1 - latitude_2) > .2 or abs(longitude_1 - longitude_2) > .2):
return haversine_distance(latitude_1, longitude_1, latitude_2, longitude_2)
coef = math.cos(latitude_1 / 180. * math.pi)
#pylint: disable=invalid-name
x = latitude_1 - latitude_2
y = (longitude_1 - longitude_2) * coef
distance_2d = math.sqrt(x * x + y * y) * ONE_DEGREE
if elevation_1 is None or elevation_2 is None or elevation_1 == elevation_2:
return distance_2d
return math.sqrt(distance_2d ** 2 + (elevation_1 - elevation_2) ** 2)