-
Notifications
You must be signed in to change notification settings - Fork 0
/
topojson.py
60 lines (49 loc) · 1.93 KB
/
topojson.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
"""
adapted from http://github.com/sgillies/topojson)
"""
import numpy as np
import math
def lat_long(coordinates, scale, translate):
"""convert the topojson encoded coordinates into (lat, long)"""
return dict(
latitude=coordinates[1] * scale[1] + translate[1],
longitude=coordinates[0] * scale[0] + translate[0])
def properties(json, obj_name):
'''
convert topojson gemometries to a dataframe of the properties
and coordinates (converted to lat, long)
'''
properties = []
for obj in json['objects'][obj_name]['geometries']:
obj_prop = obj['properties'].copy()
if 'coordinates' in obj:
obj_prop.update(lat_long(obj['coordinates'], **json['transform']))
properties.append(obj_prop)
return properties
def get_linear_dist(df):
'''convert lat long to a linear distance travelled since start'''
LL = ['latitude', 'longitude']
latlng = df.reset_index()[LL].fillna(0).copy()
latlng['distance'] = 0.0
for i, coords in latlng.iterrows():
if i == 0: continue
latlng.ix[i, 'distance'] = haversine_distance(
latlng.ix[i - 1][LL].values,
coords[LL].values)
dists = latlng.distance.replace(0, np.nan)
dists.ix[0] = 0
if dists.isnull().any():
print('interpolating missing stop distances')
return dists.cumsum().interpolate().values
return dists.cumsum().values
def haversine_distance(origin, destination):
# Haversine formula for great circle distance
# platoscave.net/blog/2009/oct/5/calculate-distance-latitude-longitude-python/
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
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)
return 2 * radius * math.atan2(math.sqrt(a), math.sqrt(1-a))