-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
132 lines (116 loc) · 4.76 KB
/
main.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
"""
Run full workflow from here. Choose which city to run and all data
will be downloaded, calculations performed, and stressmap plotted.
Intermediary files will be saved to make subsequent runs faster.
Just delete the file you want to start from and everything after
will be recreated.
"""
import sys
import argparse
import constants
def plot_func(args, cities=None):
import LTS_plot # imported directly in the command to improve argparse performance
if args.city:
print(f'Plotting {args.city}')
LTS_plot.main(args.city, args.format)
else:
for city in cities:
try:
print(f'Plotting {city}')
LTS_plot.main(city, args.format)
except FileNotFoundError as e:
print(f'\t{e}')
continue
def combine_func(args):
import LTS_OSM # imported directly in the command to improve argparse performance
LTS_OSM.combine_data('GreaterBoston', args.cities.split(','))
class StressMapCli(object):
def __init__(self):
parser = argparse.ArgumentParser(
description='StressMap LTS tool for calculating and plotting bike '
'stress',
usage=
'''
main.py <command> [<args>]
The most commonly used stressmap commands are:
process Record changes to the repository
plot Plot a single city, a list of cities, or a whole region
combine Create a combined map from all cities analyzed
help Show this help message
'''
)
parser.add_argument('command', help='Subcommand to run')
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
print('Unrecognized command')
parser.print_help()
exit(1)
# use dispatch pattern to invoke method with same name
getattr(StressMapCli, args.command)()
@staticmethod
def process():
parser = argparse.ArgumentParser(
description='Fetch and process OSM data into LTS')
parser.add_argument("-cities", type=str,
help="Comma-separated list of cities")
parser.add_argument("-city", type=str,
help="Single city to ")
parser.add_argument("--rebuild", action="store_true",
help="Rebuild underlying data")
parser.add_argument("--combine", action="store_true",
help="Combine directly after processing")
parser.add_argument("--plot", action="store_true",
help="Plot directly after processing")
args = parser.parse_args(sys.argv[2:])
cities = constants.CITIES
if args.cities and args.city:
raise "Cannot specify both cities and city"
import LTS_OSM # imported directly in the command to improve argparse performance
if args.cities:
for city in args.cities.split(','):
LTS_OSM.main(city,
cities[city]['key'],
cities[city]['value'],
args.rebuild)
else:
LTS_OSM.main(args.city,
cities[args.city]['key'],
cities[args.city]['value'],
args.rebuild)
if args.combine:
combine_func(args)
if args.plot:
args.format = 'json'
args.city = 'GreaterBoston'
plot_func(args)
elif args.plot:
args.format = 'json'
if args.cities:
plot_func(args, cities)
else:
plot_func(args)
@staticmethod
def plot():
parser = argparse.ArgumentParser(
description='Plot existing local LTS data to either HTML or GeoJson')
parser.add_argument("-city", type=str,
help="Single city to ")
parser.add_argument("--format",
choices=["json"], default="json",
help="Format for plotting")
args = parser.parse_args(sys.argv[2:])
cities = constants.CITIES
if hasattr(args, 'cities'):
plot_func(args, cities)
else:
plot_func(args)
@staticmethod
def combine():
parser = argparse.ArgumentParser(
description='Download objects and refs from another repository')
parser.add_argument("-cities", type=str,
help="Comma-separated list of cities")
args = parser.parse_args(sys.argv[2:])
combine_func(args)
if __name__ == '__main__':
StressMapCli()