sumolib.output

  1# -*- coding: utf-8 -*-
  2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
  3# Copyright (C) 2011-2026 German Aerospace Center (DLR) and others.
  4# This program and the accompanying materials are made available under the
  5# terms of the Eclipse Public License 2.0 which is available at
  6# https://www.eclipse.org/legal/epl-2.0/
  7# This Source Code may also be made available under the following Secondary
  8# Licenses when the conditions for such availability set forth in the Eclipse
  9# Public License 2.0 are satisfied: GNU General Public License, version 2
 10# or later which is available at
 11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
 12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
 13
 14# @file    __init__.py
 15# @author  Michael Behrisch
 16# @author  Jakob Erdmann
 17# @date    2011-06-23
 18
 19from __future__ import print_function
 20from __future__ import absolute_import
 21from xml.sax import make_parser
 22from xml.sax.handler import ContentHandler
 23from . import dump, inductionloop, convert  # noqa
 24from sumolib.xml import *  # noqa
 25
 26
 27class AbstractHandler__byID(ContentHandler):
 28
 29    def __init__(self, element_name, idAttr, attributes):
 30        self._element_name = element_name
 31        self._attributes = attributes
 32        self._idAttr = idAttr
 33        self._values = {}
 34
 35    def startElement(self, name, attrs):
 36        if name != self._element_name:
 37            return
 38        cid = float(attrs[self._idAttr])
 39        self._values[cid] = {}
 40        if self._attributes:
 41            for a in self._attributes:
 42                self._values[cid][a] = float(attrs[a])
 43        else:
 44            for a in attrs.keys():
 45                if a != self._idAttr:
 46                    self._values[cid][a] = float(attrs[a])
 47
 48
 49class AbstractHandler__asList(ContentHandler):
 50
 51    def __init__(self, element_name, attributes):
 52        self._element_name = element_name
 53        self._attributes = attributes
 54        self._values = []
 55
 56    def startElement(self, name, attrs):
 57        if name != self._element_name:
 58            return
 59        tmp = {}
 60        if self._attributes:
 61            for a in self._attributes:
 62                try:
 63                    tmp[a] = float(attrs[a])
 64                except ValueError:
 65                    tmp[a] = attrs[a]
 66        else:
 67            for a in attrs.keys():
 68                try:
 69                    tmp[a] = float(attrs[a])
 70                except ValueError:
 71                    tmp[a] = attrs[a]
 72        self._values.append(tmp)
 73
 74
 75def parse_sax(xmlfile, handler):
 76    myparser = make_parser()
 77    myparser.setContentHandler(handler)
 78    myparser.parse(xmlfile)
 79
 80
 81def parse_sax__byID(xmlfile, element_name, idAttr, attrnames):
 82    h = AbstractHandler__byID(element_name, idAttr, attrnames)
 83    parse_sax(xmlfile, h)
 84    return h._values
 85
 86
 87def parse_sax__asList(xmlfile, element_name, attrnames):
 88    h = AbstractHandler__asList(element_name, attrnames)
 89    parse_sax(xmlfile, h)
 90    return h._values
 91
 92
 93def toList(mapList, attr):
 94    ret = []
 95    for a in mapList:
 96        ret.append(a[attr])
 97    return ret
 98
 99
100def prune(fv, minV, maxV):
101    if minV is not None:
102        for i, v in enumerate(fv):
103            fv[i] = max(v, minV)
104    if maxV is not None:
105        for i, v in enumerate(fv):
106            fv[i] = min(v, maxV)
class AbstractHandler__byID(xml.sax.handler.ContentHandler):
28class AbstractHandler__byID(ContentHandler):
29
30    def __init__(self, element_name, idAttr, attributes):
31        self._element_name = element_name
32        self._attributes = attributes
33        self._idAttr = idAttr
34        self._values = {}
35
36    def startElement(self, name, attrs):
37        if name != self._element_name:
38            return
39        cid = float(attrs[self._idAttr])
40        self._values[cid] = {}
41        if self._attributes:
42            for a in self._attributes:
43                self._values[cid][a] = float(attrs[a])
44        else:
45            for a in attrs.keys():
46                if a != self._idAttr:
47                    self._values[cid][a] = float(attrs[a])

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.

AbstractHandler__byID(element_name, idAttr, attributes)
30    def __init__(self, element_name, idAttr, attributes):
31        self._element_name = element_name
32        self._attributes = attributes
33        self._idAttr = idAttr
34        self._values = {}
def startElement(self, name, attrs):
36    def startElement(self, name, attrs):
37        if name != self._element_name:
38            return
39        cid = float(attrs[self._idAttr])
40        self._values[cid] = {}
41        if self._attributes:
42            for a in self._attributes:
43                self._values[cid][a] = float(attrs[a])
44        else:
45            for a in attrs.keys():
46                if a != self._idAttr:
47                    self._values[cid][a] = 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.

class AbstractHandler__asList(xml.sax.handler.ContentHandler):
50class AbstractHandler__asList(ContentHandler):
51
52    def __init__(self, element_name, attributes):
53        self._element_name = element_name
54        self._attributes = attributes
55        self._values = []
56
57    def startElement(self, name, attrs):
58        if name != self._element_name:
59            return
60        tmp = {}
61        if self._attributes:
62            for a in self._attributes:
63                try:
64                    tmp[a] = float(attrs[a])
65                except ValueError:
66                    tmp[a] = attrs[a]
67        else:
68            for a in attrs.keys():
69                try:
70                    tmp[a] = float(attrs[a])
71                except ValueError:
72                    tmp[a] = attrs[a]
73        self._values.append(tmp)

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.

AbstractHandler__asList(element_name, attributes)
52    def __init__(self, element_name, attributes):
53        self._element_name = element_name
54        self._attributes = attributes
55        self._values = []
def startElement(self, name, attrs):
57    def startElement(self, name, attrs):
58        if name != self._element_name:
59            return
60        tmp = {}
61        if self._attributes:
62            for a in self._attributes:
63                try:
64                    tmp[a] = float(attrs[a])
65                except ValueError:
66                    tmp[a] = attrs[a]
67        else:
68            for a in attrs.keys():
69                try:
70                    tmp[a] = float(attrs[a])
71                except ValueError:
72                    tmp[a] = attrs[a]
73        self._values.append(tmp)

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 parse_sax(xmlfile, handler):
76def parse_sax(xmlfile, handler):
77    myparser = make_parser()
78    myparser.setContentHandler(handler)
79    myparser.parse(xmlfile)
def parse_sax__byID(xmlfile, element_name, idAttr, attrnames):
82def parse_sax__byID(xmlfile, element_name, idAttr, attrnames):
83    h = AbstractHandler__byID(element_name, idAttr, attrnames)
84    parse_sax(xmlfile, h)
85    return h._values
def parse_sax__asList(xmlfile, element_name, attrnames):
88def parse_sax__asList(xmlfile, element_name, attrnames):
89    h = AbstractHandler__asList(element_name, attrnames)
90    parse_sax(xmlfile, h)
91    return h._values
def toList(mapList, attr):
94def toList(mapList, attr):
95    ret = []
96    for a in mapList:
97        ret.append(a[attr])
98    return ret
def prune(fv, minV, maxV):
101def prune(fv, minV, maxV):
102    if minV is not None:
103        for i, v in enumerate(fv):
104            fv[i] = max(v, minV)
105    if maxV is not None:
106        for i, v in enumerate(fv):
107            fv[i] = min(v, maxV)