-
Notifications
You must be signed in to change notification settings - Fork 25
/
generateAmitranFromTAZWeights.py
executable file
·132 lines (114 loc) · 4.16 KB
/
generateAmitranFromTAZWeights.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
#!/usr/bin/env python3
""" Generate the default Amitran OD-matrix from TAZ weights.
Author: Lara CODECA
This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0.
"""
import argparse
import csv
import sys
def get_options(cmd_args=None):
"""Argument Parser"""
parser = argparse.ArgumentParser(
prog="generateAmitranFromTAZWeights.py",
usage="%(prog)s [options]",
description="Generate the default Amitran OD-matrix from TAZ weights.",
)
parser.add_argument(
"--taz-weights",
type=str,
dest="taz_file",
required=True,
help="Weighted TAZ file (CSV).",
)
parser.add_argument(
"--out",
type=str,
dest="output",
required=True,
help="OD matrix in Amitran format.",
)
parser.add_argument(
"--density",
type=float,
dest="density",
default=3000.0,
help="Average population density in square kilometers.",
)
return parser.parse_args(cmd_args)
class AmitranFromTAZWeightsGenerator:
"""Generate the default Amitran OD-matrix from TAZ weights."""
def __init__(self, options):
self._options = options
self._taz_weights = dict()
self._odpairs = list()
self._load_weights_from_csv()
self._generate_odpairs_from_taz()
def _load_weights_from_csv(self):
"""Load the TAZ weight from a CSV file."""
with open(self._options.taz_file, "r") as csvfile:
weightreader = csv.reader(csvfile)
header = []
for row in weightreader:
if not header:
header = row
elif row: # ignoring empty lines
self._taz_weights[row[0]] = {
header[0]: row[0],
header[1]: row[1],
header[2]: int(row[2]),
header[3]: float(row[3]),
}
def _generate_odpairs_from_taz(self):
"""Generate all the possible OD pairs."""
_single_taz = len(self._taz_weights) == 1
for origin, taz_orig in self._taz_weights.items():
for destination, _ in self._taz_weights.items():
if origin == destination and not _single_taz:
continue
amount = round(
self._options.density
* taz_orig["Area"]
/ 1e6, # from mq to square kmq
0,
)
if amount <= 0:
continue
self._odpairs.append(
{
"origin": origin,
"destination": destination,
"amount": amount,
}
)
AMITRAN_TPL = """<demand xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://sumo.dlr.de/xsd/amitran/od.xsd">
<actorConfig id="0">
<timeSlice duration="86400000" startTime="0">{odpair}
</timeSlice>
</actorConfig>
</demand>
"""
ODPAIR_TPL = """
<odPair amount="{amount}" destination="{dest}" origin="{orig}"/>"""
def save_odmatrix_to_file(self, filename):
"""Save the OD-matric in Amitran format."""
print("Creation of {}".format(filename))
with open(filename, "w") as outfile:
list_of_odpairs = ""
for pair in self._odpairs:
list_of_odpairs += self.ODPAIR_TPL.format(
amount=int(pair["amount"]),
dest=pair["destination"],
orig=pair["origin"],
)
outfile.write(self.AMITRAN_TPL.format(odpair=list_of_odpairs))
print("{} created.".format(filename))
def main(cmd_args):
"""Generate the default Amitran OD-matrix from TAZ weights."""
options = get_options(cmd_args)
odmatrix = AmitranFromTAZWeightsGenerator(options)
odmatrix.save_odmatrix_to_file(options.output)
print("Done.")
if __name__ == "__main__":
main(sys.argv[1:])