sumolib.shapes.poi

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2010-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    poi.py
14# @author  Daniel Krajzewicz
15# @author  Michael Behrisch
16# @author  Jakob Erdmann
17# @author  Melanie Knocke
18# @date    2010-02-18
19
20from __future__ import absolute_import
21
22from xml.sax import handler, parse
23from .. import color
24
25
26class PoI:
27
28    def __init__(self, id, type, layer, color, x, y, lane=None, pos=None, lonLat=False):
29        """interpret x,y as lon,lat if lonLat is True"""
30        self.id = id
31        self.type = type
32        self.color = color
33        self.layer = layer
34        self.x = x
35        self.y = y
36        self.lane = lane
37        self.pos = pos
38        self.attributes = {}
39        self.lonLat = lonLat
40
41    def toXML(self):
42        if self.lane:
43            ret = '<poi id="%s" type="%s" color="%s" layer="%s" lane="%s" pos="%s"' % (
44                self.id, self.type, self.color.toXML(), self.layer, self.lane, self.pos)
45        elif self.lonLat:
46            ret = '<poi id="%s" type="%s" color="%s" layer="%s" lon="%s" lat="%s"' % (
47                self.id, self.type, self.color.toXML(), self.layer, self.x, self.y)
48        else:
49            ret = '<poi id="%s" type="%s" color="%s" layer="%s" x="%s" y="%s"' % (
50                self.id, self.type, self.color.toXML(), self.layer, self.x, self.y)
51        if len(self.attributes) == 0:
52            ret += '/>'
53        else:
54            ret += '>'
55            for a in self.attributes:
56                ret += '<param key="%s" value="%s"/>' % (a, self.attributes[a])
57            ret += '</poi>'
58        return ret
59
60
61class PoIReader(handler.ContentHandler):
62
63    def __init__(self):
64        self._id2poi = {}
65        self._pois = []
66        self._lastPOI = None
67
68    def startElement(self, name, attrs):
69        if name == 'poi':
70            c = color.decodeXML(attrs['color'])
71            if 'lane' not in attrs:
72                if 'x' not in attrs:
73                    poi = PoI(attrs['id'], attrs['type'], float(attrs['layer']), c, float(
74                        attrs['lon']), float(attrs['lat']), lonLat=True)
75                else:
76                    poi = PoI(attrs['id'], attrs['type'], float(
77                        attrs['layer']), c, float(attrs['x']), float(attrs['y']))
78            else:
79                poi = PoI(attrs['id'], attrs['type'], float(
80                    attrs['layer']), c, None, None, attrs['lane'], float(attrs['pos']))
81            self._id2poi[poi.id] = poi
82            self._pois.append(poi)
83            self._lastPOI = poi
84        if name == 'param' and self._lastPOI is not None:
85            self._lastPOI.attributes[attrs['key']] = attrs['value']
86
87    def endElement(self, name):
88        if name == 'poi':
89            self._lastPOI = None
90
91
92def read(filename):
93    pois = PoIReader()
94    parse(filename, pois)
95    return pois._pois
class PoI:
27class PoI:
28
29    def __init__(self, id, type, layer, color, x, y, lane=None, pos=None, lonLat=False):
30        """interpret x,y as lon,lat if lonLat is True"""
31        self.id = id
32        self.type = type
33        self.color = color
34        self.layer = layer
35        self.x = x
36        self.y = y
37        self.lane = lane
38        self.pos = pos
39        self.attributes = {}
40        self.lonLat = lonLat
41
42    def toXML(self):
43        if self.lane:
44            ret = '<poi id="%s" type="%s" color="%s" layer="%s" lane="%s" pos="%s"' % (
45                self.id, self.type, self.color.toXML(), self.layer, self.lane, self.pos)
46        elif self.lonLat:
47            ret = '<poi id="%s" type="%s" color="%s" layer="%s" lon="%s" lat="%s"' % (
48                self.id, self.type, self.color.toXML(), self.layer, self.x, self.y)
49        else:
50            ret = '<poi id="%s" type="%s" color="%s" layer="%s" x="%s" y="%s"' % (
51                self.id, self.type, self.color.toXML(), self.layer, self.x, self.y)
52        if len(self.attributes) == 0:
53            ret += '/>'
54        else:
55            ret += '>'
56            for a in self.attributes:
57                ret += '<param key="%s" value="%s"/>' % (a, self.attributes[a])
58            ret += '</poi>'
59        return ret
PoI(id, type, layer, color, x, y, lane=None, pos=None, lonLat=False)
29    def __init__(self, id, type, layer, color, x, y, lane=None, pos=None, lonLat=False):
30        """interpret x,y as lon,lat if lonLat is True"""
31        self.id = id
32        self.type = type
33        self.color = color
34        self.layer = layer
35        self.x = x
36        self.y = y
37        self.lane = lane
38        self.pos = pos
39        self.attributes = {}
40        self.lonLat = lonLat

interpret x,y as lon,lat if lonLat is True

id
type
color
layer
x
y
lane
pos
attributes
lonLat
def toXML(self):
42    def toXML(self):
43        if self.lane:
44            ret = '<poi id="%s" type="%s" color="%s" layer="%s" lane="%s" pos="%s"' % (
45                self.id, self.type, self.color.toXML(), self.layer, self.lane, self.pos)
46        elif self.lonLat:
47            ret = '<poi id="%s" type="%s" color="%s" layer="%s" lon="%s" lat="%s"' % (
48                self.id, self.type, self.color.toXML(), self.layer, self.x, self.y)
49        else:
50            ret = '<poi id="%s" type="%s" color="%s" layer="%s" x="%s" y="%s"' % (
51                self.id, self.type, self.color.toXML(), self.layer, self.x, self.y)
52        if len(self.attributes) == 0:
53            ret += '/>'
54        else:
55            ret += '>'
56            for a in self.attributes:
57                ret += '<param key="%s" value="%s"/>' % (a, self.attributes[a])
58            ret += '</poi>'
59        return ret
class PoIReader(xml.sax.handler.ContentHandler):
62class PoIReader(handler.ContentHandler):
63
64    def __init__(self):
65        self._id2poi = {}
66        self._pois = []
67        self._lastPOI = None
68
69    def startElement(self, name, attrs):
70        if name == 'poi':
71            c = color.decodeXML(attrs['color'])
72            if 'lane' not in attrs:
73                if 'x' not in attrs:
74                    poi = PoI(attrs['id'], attrs['type'], float(attrs['layer']), c, float(
75                        attrs['lon']), float(attrs['lat']), lonLat=True)
76                else:
77                    poi = PoI(attrs['id'], attrs['type'], float(
78                        attrs['layer']), c, float(attrs['x']), float(attrs['y']))
79            else:
80                poi = PoI(attrs['id'], attrs['type'], float(
81                    attrs['layer']), c, None, None, attrs['lane'], float(attrs['pos']))
82            self._id2poi[poi.id] = poi
83            self._pois.append(poi)
84            self._lastPOI = poi
85        if name == 'param' and self._lastPOI is not None:
86            self._lastPOI.attributes[attrs['key']] = attrs['value']
87
88    def endElement(self, name):
89        if name == 'poi':
90            self._lastPOI = None

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.

def startElement(self, name, attrs):
69    def startElement(self, name, attrs):
70        if name == 'poi':
71            c = color.decodeXML(attrs['color'])
72            if 'lane' not in attrs:
73                if 'x' not in attrs:
74                    poi = PoI(attrs['id'], attrs['type'], float(attrs['layer']), c, float(
75                        attrs['lon']), float(attrs['lat']), lonLat=True)
76                else:
77                    poi = PoI(attrs['id'], attrs['type'], float(
78                        attrs['layer']), c, float(attrs['x']), float(attrs['y']))
79            else:
80                poi = PoI(attrs['id'], attrs['type'], float(
81                    attrs['layer']), c, None, None, attrs['lane'], float(attrs['pos']))
82            self._id2poi[poi.id] = poi
83            self._pois.append(poi)
84            self._lastPOI = poi
85        if name == 'param' and self._lastPOI is not None:
86            self._lastPOI.attributes[attrs['key']] = attrs['value']

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 endElement(self, name):
88    def endElement(self, name):
89        if name == 'poi':
90            self._lastPOI = None

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

The name parameter contains the name of the element type, just as with the startElement event.

def read(filename):
93def read(filename):
94    pois = PoIReader()
95    parse(filename, pois)
96    return pois._pois