Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/driveways2poly.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2012-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 driveways2poly.py
15
# @author Jakob Erdmann
16
# @date 2024-06-14
17
18
"""
19
Visualize railsignal-block-output as polygons
20
which can be loaded with sumo-gui for visualization
21
"""
22
from __future__ import absolute_import
23
import sys
24
import os
25
import random
26
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
27
import sumolib # noqa
28
import route2poly # noqa
29
30
31
def parse_args(args=None):
32
optParser = sumolib.options.ArgumentParser()
33
optParser.add_option("-n", "--netfile", required=True, help="name of input network file")
34
optParser.add_option("-d", "--driveways", required=True, help="name of input railsignal block file")
35
optParser.add_option("-o", "--output", required=True, help="name of output file")
36
optParser.add_option("-u", "--hue", default="random",
37
help="hue for polygons (float from [0,1] or 'random')")
38
optParser.add_option("-s", "--saturation", default=1,
39
help="saturation for polygons (float from [0,1] or 'random')")
40
optParser.add_option("-b", "--brightness", default=1,
41
help="brightness for polygons (float from [0,1] or 'random')")
42
optParser.add_option("-l", "--layer", default=100, help="layer for generated polygons")
43
optParser.add_option("--geo", action="store_true",
44
default=False, help="write polygons with geo-coordinates")
45
optParser.add_option("--internal", action="store_true",
46
default=False, help="include internal edges in generated shapes")
47
optParser.add_option("--spread", type=float, help="spread polygons laterally to avoid overlap")
48
optParser.add_option("--blur", type=float,
49
default=0, help="maximum random disturbance to route geometry")
50
optParser.add_option("--scale-width", type=float, dest="scaleWidth",
51
help="group similar routes and scale width by " +
52
"group size multiplied with the given factor (in m)")
53
optParser.add_option("--filter-signals", dest="filterSignals",
54
help="only write output for driveways of the given signals")
55
optParser.add_option("--filter-driveways", dest="filterDriveways",
56
help="only write output for the given driveways")
57
optParser.add_option("--filter-foes", dest="filterFoes",
58
help="only write output for the foes of the given driveways")
59
optParser.add_option("--seed", type=int, help="random seed")
60
61
options = optParser.parse_args(args=args)
62
if options.seed:
63
random.seed(options.seed)
64
65
if options.filterSignals:
66
options.filterSignals = set(options.filterSignals.split(','))
67
if options.filterDriveways:
68
options.filterDriveways = set(options.filterDriveways.split(','))
69
if options.filterFoes:
70
options.filterFoes = set(options.filterFoes.split(','))
71
72
return options
73
74
75
def getDriveWays(fname):
76
for rs in sumolib.xml.parse(fname, "railSignal"):
77
for link in rs.link:
78
if not link.driveWay:
79
continue
80
for dw in link.driveWay:
81
yield rs.id, dw
82
for dj in sumolib.xml.parse(fname, "departJunction"):
83
for dw in dj.driveWay:
84
yield dj.id, dw
85
86
87
def main(options):
88
colorgen = sumolib.miscutils. Colorgen((options.hue, options.saturation, options.brightness))
89
net = sumolib.net.readNet(options.netfile, withInternal=options.internal)
90
91
permittedFoes = None
92
if options.filterFoes:
93
permittedFoes = set()
94
for signal, dw in getDriveWays(options.driveways):
95
if dw.id in options.filterFoes:
96
permittedFoes.update(dw.foes[0].driveWays.split())
97
98
with open(options.output, 'w') as outf:
99
sumolib.xml.writeHeader(outf, root='polygons', rootAttrs=None, options=options)
100
for signal, dw in getDriveWays(options.driveways):
101
if options.filterSignals and signal not in options.filterSignals:
102
continue
103
if options.filterDriveways and dw.id not in options.filterDriveways:
104
continue
105
if permittedFoes and dw.id not in permittedFoes:
106
continue
107
route2poly.generate_poly(options, net, dw.id, colorgen(), dw.edges.split(), outf)
108
outf.write('</polygons>\n')
109
110
111
if __name__ == "__main__":
112
main(parse_args())
113
114