Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/netconvert/osm_roundtrip/runner.py
169685 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file runner.py
16
# @author Jakob Erdmann
17
# @author Laura Bieker
18
# @author Michael Behrisch
19
# @date 2011-05-27
20
21
"""
22
import osm network
23
then import the built network again and check for idempotency
24
"""
25
from __future__ import absolute_import
26
27
import sys
28
import os
29
import subprocess
30
import difflib
31
if "SUMO_HOME" in os.environ:
32
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
33
import sumolib # noqa
34
35
osm_input = 'osm.xml'
36
net_output = 'from_osm'
37
net_output2 = 'net'
38
39
netconvert = sumolib.checkBinary('netconvert')
40
41
# filter header and projection clause
42
43
44
def filtered(file_name, start_element):
45
skippedHeader = False
46
with open(file_name) as f:
47
for line in f:
48
if start_element in line:
49
skippedHeader = True
50
if not skippedHeader:
51
continue
52
if '<location' in line:
53
continue
54
if '<projection' in line:
55
continue
56
yield line
57
58
59
def get_filtered_lines(prefix):
60
result = []
61
for suffix, start_element in [
62
('.net.xml', '<net '),
63
('.nod.xml', '<nodes '),
64
('.edg.xml', '<edges '),
65
('.con.xml', '<connections '),
66
('.tll.xml', '<tlLogics ')]:
67
result += list(filtered(prefix + suffix, start_element))
68
return result
69
70
71
args1 = [netconvert,
72
'--no-internal-links',
73
'--osm-files', osm_input, '--proj.utm',
74
'-R', '--ramps.guess',
75
'--tls.guess', '--tls.join',
76
'--junctions.join',
77
'--plain-output-prefix', net_output,
78
'--output', net_output + '.net.xml']
79
80
args2 = [netconvert,
81
'--sumo-net-file', net_output + '.net.xml',
82
'--no-internal-links',
83
'--offset.disable-normalization',
84
'--plain-output-prefix', net_output2,
85
'--output', net_output2 + '.net.xml']
86
87
subprocess.call(args1)
88
subprocess.call(args2)
89
90
fromlines = get_filtered_lines(net_output)
91
tolines = get_filtered_lines(net_output2)
92
# with open('fromlines','w') as f: f.write('\n'.join(fromlines))
93
# with open('tolines','w') as f: f.write('\n'.join(tolines))
94
sys.stderr.writelines(difflib.unified_diff(fromlines, sumolib.fpdiff.fpfilter(fromlines, tolines, 0.0201)))
95
96