-
Notifications
You must be signed in to change notification settings - Fork 0
/
uber_map.py
152 lines (130 loc) · 4.54 KB
/
uber_map.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
#!/usr/bin/python3
"""
MIT License
Copyright (c) 2019 Guiral Lacotte
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# imports
import re
import glob
import argparse
import webbrowser
from folium import Map, PolyLine
from folium.plugins import HeatMap
# constants
# functions
def build_map(trackpoint, trips):
''' Build and display Folium map '''
html_file = args.output
# color map for heatmap plugin
heatmap_grad = \
{0.0: '#000004',
0.1: '#160b39',
0.2: '#420a68',
0.3: '#6a176e',
0.4: '#932667',
0.5: '#bc3754',
0.6: '#dd513a',
0.7: '#f37819',
0.8: '#fca50a',
0.9: '#f6d746',
1.0: '#fcffa4'}
fmap = Map(tiles='CartoDB dark_matter', prefer_canvas=True, max_zoom=16)
# Individual coordonate as heatmap
HeatMap(
trackpoint,
radius=5,
blur=5,
gradient=heatmap_grad,
max_zoom=19).add_to(fmap)
# Trace the trip as polyline
for trip in trips:
PolyLine(
locations=trip,
weight=5,
color='#FFC300',
opacity=0.6).add_to(fmap)
# Set map bounds
fmap.fit_bounds(fmap.get_bounds())
# save map to HTML file and open with browser
print('writing ' + html_file + '...')
fmap.save(html_file)
webbrowser.open(html_file, new=2, autoraise=True)
print('done')
def open_csv_files(files):
"""
open CSV files and extract tracks
Done with RegEx, dirty but 3 times faster than Lxml and CSV File lib
"""
trackpoint = []
trips = []
for file in files:
print('reading :' + file + '...')
with open(file, 'r') as filehandler:
# Open a file
for line in filehandler:
# Search for coordonates
tmp = re.findall('[-]*[0-9]{1,2}[.][0-9]{4,}', line)
if tmp:
if len(tmp) == 2: # 1 coordonate it is trackpoint.
trackpoint.append([float(tmp[0]), float(tmp[1])])
if len(tmp) == 4: # 2 coordonates it is a trip.
trips.append(
[[float(tmp[0]), float(tmp[1])],
[float(tmp[2]), float(tmp[3])]])
print('Trackpoints:', len(trackpoint), 'trackpoints')
print('trips:', len(trips), 'trips')
return(trackpoint, trips)
def main(args):
'''arguments'''
csv_dir = args.dir
csv_filter = args.filter
html_file = args.output
# output file must be html
if not html_file[-5:] == '.html':
print('ERROR output file must be .html')
quit()
# parse CSV and Exiffiles
csv_files = glob.glob(csv_dir + '/' + csv_filter)
if not csv_files:
print('ERROR no CSV files in ' + csv_dir)
quit()
# We should be good with user input, now
trackpoint, trips = open_csv_files(csv_files)
build_map(trackpoint, trips)
if __name__ == '__main__':
'''command line parameters'''
parser = argparse.ArgumentParser(
description='Generate a map based on Uber csv files')
parser.add_argument(
'--csv-dir',
dest='dir',
default='uber',
help='directory containing the csv files (default: uber)')
parser.add_argument(
'--csv-filter',
dest='filter',
default='*.csv',
help='glob filter for the csv files (default: *.csv)')
parser.add_argument(
'--output',
dest='output',
default='uber_map.html',
help='output HTML file (default: uber_map.html)')
args = parser.parse_args()
'''Main function'''
main(args)