-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_extract.py
132 lines (105 loc) · 4.16 KB
/
data_extract.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
import pandas as pd
__author__ = 'jeromethai'
begin = 'var geojson_features = [{\n'
def begin_feature(type):
string = ' "type": "Feature",\n "geometry": {\n'
begin_coord = ' "coordinates": [\n'
return string + ' "type": "{}",\n'.format(type) + begin_coord
def coord(lat,lon,type):
if type == "LineString": return ' [{}, {}],\n'.format(lon,lat)
if type == "Point": return ' [{}, {}]'.format(lon,lat)
begin_prop = ' ]},\n "properties": {\n'
def end_prop(next):
if next: return ' }},{\n'
return ' }}];\n'
def prop(name, value):
return ' "{}": "{}",\n'.format(name, value)
def extract_all_trajectories(df):
"""create geojson LineString features from trajectories pandas dataframe
generated in data_import.py"""
out = ''
out += begin
for user_id in df.index.levels[0][:-1]:
out += extract_one_trajectory(df, user_id, True)
out += end_prop(True)
out += extract_one_trajectory(df, df.index.levels[0][-1], True)
out += end_prop(False)
return out
def extract_one_trajectory(df, user_id, called_by_extract_all_trajectories=False):
"""create geojson LineString features from trajectories pandas dataframe
generated in data_import.py"""
print 'convert trajectory for user {} to geoJson'.format(user_id)
out = ''
if not called_by_extract_all_trajectories: out += begin
out += begin_feature("LineString")
length, duration = 0., 0.
for i in range(len(df.loc[user_id])):
row = df.loc[user_id].loc[i]
if i > 0:
length += row['dist_to_prev']
duration += row['time_to_prev']
out += coord(row['lat'], row['lon'], "LineString")
out += begin_prop
out += prop('distance', length)
out += prop('duration', duration)
out += prop('user_id', user_id)
out += prop('number_points', len(df.loc[user_id]))
if not called_by_extract_all_trajectories: out += end_prop(False)
return out
def to_geoJson_traj():
df = pd.load('data/filtered_stem_LA_sample_users_all_type_6_0902.pkl')
out = extract_all_trajectories(df)
out += '\n'
out += 'var lat_center_map = 33.982075\n'
out += 'var lon_center_map = -118.28104\n'
with open('visualization_data/all_trajectories.js', 'w') as f:
f.write(out)
def to_geoJson_link():
df = pd.load('data/OSM_LA.pkl')
type = 'LineString'
out = begin
for i in range(len(df)):
out += begin_feature(type)
out += coord(df.loc[i]['lat1'], df.loc[i]['lon1'], type)
out += coord(df.loc[i]['lat2'], df.loc[i]['lon2'], type)
out += begin_prop
out += prop('flow', df.loc[i]['flow'])
out += prop('flow_over_capacity', df.loc[i]['flow/capacity'])
out += prop('tt_over_fftt', df.loc[i]['travel_time/fftt'])
out += prop('capacity', df.loc[i]['capacity'])
out += prop('freespeed', df.loc[i]['freespeed'])
out += prop('length', df.loc[i]['length'])
out += prop('fftt', df.loc[i]['fftt'])
if i < len(df)-1:
out += end_prop(next = True)
else:
out += end_prop(next = False)
out += '\n'
out += 'var lat_center_map = 33.982075\n'
out += 'var lon_center_map = -118.28104\n'
with open('visualization_data/OSM_LA.js', 'w') as f:
f.write(out)
def example():
type = 'LineString'
with open('visualization_data/dummy.js', 'w') as f:
f.write(begin + begin_feature(type))
f.write(coord(33.974394, -118.308914, type))
f.write(coord(33.974465, -118.300266, type))
f.write(coord(33.974465, -118.291554, type))
f.write(begin_prop)
f.write(prop('name1', 'value1'))
f.write(prop('name2', 'value2'))
f.write(end_prop(next = True))
f.write(begin_feature(type))
f.write(coord(33.974394, -118.308914, type))
f.write(coord(33.974465, -118.300266, type))
f.write(coord(33.974465, -118.291554, type))
f.write(begin_prop)
f.write(prop('name1', 'value1'))
f.write(prop('name2', 'value2'))
f.write(end_prop(next = False))
def main():
#to_geoJson_traj()
to_geoJson_link()
if __name__ == "__main__":
main()