Path: blob/main/tests/tools/sumolib/patch_network/runner.py
428384 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2026 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file runner.py14# @author Jakob Erdmann15# @date1617# test for patching network with random node position offset18from __future__ import absolute_import19from __future__ import print_function2021import os22import sys23import random24from subprocess import call25if 'SUMO_HOME' in os.environ:26tools = os.path.join(os.environ['SUMO_HOME'], 'tools')27sys.path.append(tools)28else:29sys.exit("please declare environment variable 'SUMO_HOME'")30import sumolib.net # noqa3132random.seed(42)33netfile = sys.argv[1]34patchfile = 'nodes.nod.xml'35netconvert = sumolib.checkBinary('netconvert')36# disaggregate37call([netconvert, '-s', netfile, '--plain-output-prefix', 'plain'])38# create patch39with open(patchfile, 'w') as outf:40# setting attrs is optional, it results in a cleaner patch file41attrs = {'node': ['id', 'x', 'y']} # other attrs are not needed for patching42# parse always returns a generator but there is only one root element43nodes = list(sumolib.xml.parse('plain.nod.xml', 'nodes', attrs))[0]44for node in nodes.node:45node.addChild("param", {"key": "origPos", "value": "%s %s" % (node.x, node.y)})46node.x = "%.2f" % (float(node.x) + random.random() * 40. - 20.)47node.y = "%.2f" % (float(node.y) + random.random() * 40. - 20.)48outf.write(nodes.toXML())4950# rebuild51call([netconvert, '-s', netfile, '-n', patchfile, '-o', 'net.net.xml'])525354