-
Notifications
You must be signed in to change notification settings - Fork 0
/
matsim.py
35 lines (28 loc) · 1.01 KB
/
matsim.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
import gzip
import xml.sax
class PopulationReader(xml.sax.handler.ContentHandler):
def __init__(self):
self.population = []
self.person = None
self.process_activities = False
def startElement(self, name, attributes):
if name == "person":
self.person = { "id": attributes["id"], "activities": [] }
self.process_activities = False
elif name == "plan":
self.process_activities = attributes["selected"] == "yes"
elif name == "activity":
self.person["activities"].append({
"purpose": attributes["type"],
"x": float(attributes["x"]),
"y": float(attributes["y"])
})
def endElement(self, name):
if name == "person":
self.population.append(self.person)
self.person = None
def read_population(path):
with gzip.open(path) as f:
reader = PopulationReader()
xml.sax.parse(f, reader)
return reader.population