Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/xmlnodes_applyOffset.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-2025 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 xmlnodes_applyOffset.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2009-08-01
18
19
"""
20
Applies a given offset to edges given in an xml-node-file.
21
The results are written into <XMLNODES>.mod.xml.
22
Call: xmlnodes_applyOffset.py <XMLNODES> <X-OFFSET> <Y-OFFSET>
23
"""
24
from __future__ import absolute_import
25
from __future__ import print_function
26
27
import sys
28
from xml.sax import saxutils, make_parser, handler
29
30
31
class XMLNodesReader(handler.ContentHandler):
32
33
def __init__(self, outFileName, xoff, yoff):
34
self._out = open(outFileName, 'w')
35
self._xoff = xoff
36
self._yoff = yoff
37
38
def endDocument(self):
39
self._out.close()
40
41
def startElement(self, name, attrs):
42
self._out.write('<' + name)
43
for (key, value) in attrs.items():
44
if key == "x":
45
self._out.write(
46
' %s="%s"' % (key, saxutils.escape(str(float(value) + self._xoff))))
47
elif key == "y":
48
self._out.write(
49
' %s="%s"' % (key, saxutils.escape(str(float(value) + self._yoff))))
50
else:
51
self._out.write(' %s="%s"' % (key, saxutils.escape(value)))
52
self._out.write('>')
53
54
def endElement(self, name):
55
self._out.write('</' + name + '>')
56
57
def characters(self, content):
58
self._out.write(saxutils.escape(content))
59
60
def ignorableWhitespace(self, content):
61
self._out.write(content)
62
63
def processingInstruction(self, target, data):
64
self._out.write('<?%s %s?>' % (target, data))
65
66
67
if len(sys.argv) < 4:
68
print("Usage: " + sys.argv[0] + " <XMLNODES> <X-OFFSET> <Y-OFFSET>")
69
sys.exit()
70
parser = make_parser()
71
reader = XMLNodesReader(
72
sys.argv[1] + ".mod.xml", float(sys.argv[2]), float(sys.argv[3]))
73
parser.setContentHandler(reader)
74
parser.parse(sys.argv[1])
75
76