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
|
import os
import json
import getpass
from datetime import datetime
import itertools
import numpy
import data
class NumpyEncoder(json.JSONEncoder):
def default(self, o):
if type(o).__module__ == numpy.__name__:
return o.item()
super(NumpyEncoder, self).default(o)
class EGJ(object):
def save(self, path=getpass.getuser(), append=False):
path = os.path.join(data.path, 'visualizer', path)
if append:
if not os.path.isdir(path):
raise ValueError("Can't append to the given directory")
name = str(1+max(map(int, filter(str.isdigit, os.listdir(path)))+[-1]))
path = os.path.join(path, name)
else:
while os.path.isdir(path):
path = os.path.join(path, '0')
with open(path, 'w') as f:
self.write(f)
def write(self, file):
file.write(json.dumps(self.object(), cls=NumpyEncoder))
def type(self):
return 'raw'
def options(self):
return []
def object(self):
return {
'type': self.type(),
'data': {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'
}
},
'features': self.features()
}
}
class Point(EGJ):
def __init__(self, latitude, longitude, info=None):
self.latitude = latitude
self.longitude = longitude
self.info = info
def features(self):
d = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [self.longitude, self.latitude]
}
}
if self.info is not None:
d['properties'] = { 'info': self.info }
return [d]
class Path(EGJ):
def __init__(self, path, info=''):
self.path = path
self.info = info
def features(self):
info = self.info + '''trip_id: %(trip_id)s<br>
call_type: %(call_type_f)s<br>
origin_call: %(origin_call)d<br>
origin_stand: %(origin_stand)d<br>
taxi_id: %(taxi_id)d<br>
timestamp: %(timestamp_f)s<br>
day_type: %(day_type_f)s<br>
missing_data: %(missing_data)d<br>''' \
% dict(self.path,
call_type_f = ['central', 'stand', 'street'][self.path['call_type']],
timestamp_f = datetime.fromtimestamp(self.path['timestamp']).strftime('%c'),
day_type_f = ['normal', 'holiday', 'holiday eve'][self.path['day_type']])
return [{
'type': 'Feature',
'properties': {
'info': info,
'display': 'path',
'timestamp': self.path['timestamp']
},
'geometry': {
'type': 'LineString',
'coordinates': [[lon, lat] for (lat, lon) in zip(self.path['latitude'], self.path['longitude'])]
}
}]
class Vlist(EGJ, list):
def __init__(self, cluster=False, heatmap=False, *args):
list.__init__(self, *args)
self.cluster = cluster
self.heatmap = heatmap
def type(self):
if self.cluster or self.heatmap:
if all(isinstance(c, Point) for c in self):
if self.cluster:
return 'cluster'
elif self.heatmap:
return 'heatmap'
else:
raise ValueError('Building a %s with something that is not a Point' % ('cluster' if self.cluster else 'heatmap'))
else:
return 'raw'
def features(self):
return list(itertools.chain.from_iterable(p.features() for p in self))
|