sumolib.output.inductionloop

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2008-2026 German Aerospace Center (DLR) and others.
 3# This program and the accompanying materials are made available under the
 4# terms of the Eclipse Public License 2.0 which is available at
 5# https://www.eclipse.org/legal/epl-2.0/
 6# This Source Code may also be made available under the following Secondary
 7# Licenses when the conditions for such availability set forth in the Eclipse
 8# Public License 2.0 are satisfied: GNU General Public License, version 2
 9# or later which is available at
10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13# @file    inductionloop.py
14# @author  Daniel Krajzewicz
15# @author  Michael Behrisch
16# @date    2011-06-15
17
18from __future__ import absolute_import
19
20from xml.sax import make_parser, handler
21
22
23class InductLoopReader(handler.ContentHandler):
24
25    def __init__(self, toCollect):
26        self._values = {}
27        self._toCollect = toCollect
28        self._intervalBegins = []
29        self._beginTime = None
30        for a in self._toCollect:
31            self._values[a] = []
32
33    def startElement(self, name, attrs):
34        if name == 'interval':
35            if self._beginTime != float(attrs['begin']):
36                self._beginTime = float(attrs['begin'])
37                for a in self._toCollect:
38                    self._values[a].append({})
39            self._intervalBegins.append(self._beginTime)
40            id = attrs['id']
41            for a in attrs.keys():
42                if a not in self._toCollect:
43                    continue
44                self._values[a][-1][id] = float(attrs[a])
45
46    def join(self, what, how):
47        for a in what:
48            self._singleJoin(a, how)
49
50    def get(self, what):
51        return self._values[what]
52
53    def _singleJoin(self, what, how):
54        ret = {}
55        no = {}
56        for i in self._values[what]:
57            for e in i:
58                if e not in ret:
59                    ret[e] = 0
60                    no[e] = 0
61                ret[e] = ret[e] + i[e]
62                no[e] = no[e] + 1
63        if how == "sum":
64            return ret
65        elif how == "average":
66            for e in i:
67                ret[e] = ret[e] / float(no[e])
68        self._values[what] = [ret]
69
70
71def readInductLoop(file, toCollect):
72    parser = make_parser()
73    il = InductLoopReader(toCollect)
74    parser.setContentHandler(il)
75    parser.parse(file)
76    return il
class InductLoopReader(xml.sax.handler.ContentHandler):
24class InductLoopReader(handler.ContentHandler):
25
26    def __init__(self, toCollect):
27        self._values = {}
28        self._toCollect = toCollect
29        self._intervalBegins = []
30        self._beginTime = None
31        for a in self._toCollect:
32            self._values[a] = []
33
34    def startElement(self, name, attrs):
35        if name == 'interval':
36            if self._beginTime != float(attrs['begin']):
37                self._beginTime = float(attrs['begin'])
38                for a in self._toCollect:
39                    self._values[a].append({})
40            self._intervalBegins.append(self._beginTime)
41            id = attrs['id']
42            for a in attrs.keys():
43                if a not in self._toCollect:
44                    continue
45                self._values[a][-1][id] = float(attrs[a])
46
47    def join(self, what, how):
48        for a in what:
49            self._singleJoin(a, how)
50
51    def get(self, what):
52        return self._values[what]
53
54    def _singleJoin(self, what, how):
55        ret = {}
56        no = {}
57        for i in self._values[what]:
58            for e in i:
59                if e not in ret:
60                    ret[e] = 0
61                    no[e] = 0
62                ret[e] = ret[e] + i[e]
63                no[e] = no[e] + 1
64        if how == "sum":
65            return ret
66        elif how == "average":
67            for e in i:
68                ret[e] = ret[e] / float(no[e])
69        self._values[what] = [ret]

Interface for receiving logical document content events.

This is the main callback interface in SAX, and the one most important to applications. The order of events in this interface mirrors the order of the information in the document.

InductLoopReader(toCollect)
26    def __init__(self, toCollect):
27        self._values = {}
28        self._toCollect = toCollect
29        self._intervalBegins = []
30        self._beginTime = None
31        for a in self._toCollect:
32            self._values[a] = []
def startElement(self, name, attrs):
34    def startElement(self, name, attrs):
35        if name == 'interval':
36            if self._beginTime != float(attrs['begin']):
37                self._beginTime = float(attrs['begin'])
38                for a in self._toCollect:
39                    self._values[a].append({})
40            self._intervalBegins.append(self._beginTime)
41            id = attrs['id']
42            for a in attrs.keys():
43                if a not in self._toCollect:
44                    continue
45                self._values[a][-1][id] = float(attrs[a])

Signals the start of an element in non-namespace mode.

The name parameter contains the raw XML 1.0 name of the element type as a string and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.

def join(self, what, how):
47    def join(self, what, how):
48        for a in what:
49            self._singleJoin(a, how)
def get(self, what):
51    def get(self, what):
52        return self._values[what]
def readInductLoop(file, toCollect):
72def readInductLoop(file, toCollect):
73    parser = make_parser()
74    il = InductLoopReader(toCollect)
75    parser.setContentHandler(il)
76    parser.parse(file)
77    return il