Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/purgatory/districts2poly.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 districts2poly.py
15
# @author Jakob Erdmann
16
# @date 2015-07-31
17
18
"""
19
From a sumo network and a taz (district) file, this script colors each district
20
with a unique color (by creating a colored polygon for each edge in that
21
district)
22
These polygons can then be visualized in sumo-gui
23
"""
24
from __future__ import absolute_import
25
import sys
26
import os
27
import random
28
from optparse import OptionParser
29
sys.path.append(os.path.join(os.environ["SUMO_HOME"], 'tools'))
30
from sumolib.output import parse # noqa
31
from sumolib.net import readNet # noqa
32
from sumolib.miscutils import Colorgen # noqa
33
34
35
def parse_args():
36
USAGE = "Usage: " + sys.argv[0] + " <netfile> <routefile> [options]"
37
optParser = OptionParser()
38
optParser.add_option("-o", "--outfile", help="name of output file")
39
optParser.add_option("-u", "--hue", default="random",
40
help="hue for polygons (float from [0,1] or 'random')")
41
optParser.add_option("-s", "--saturation", default=1,
42
help="saturation for polygons (float from [0,1] or 'random')")
43
optParser.add_option("-b", "--brightness", default=1,
44
help="brightness for polygons (float from [0,1] or 'random')")
45
optParser.add_option(
46
"-l", "--layer", default=100, help="layer for generated polygons")
47
options, args = optParser.parse_args()
48
try:
49
options.net, options.routefile = args
50
options.colorgen = Colorgen(
51
(options.hue, options.saturation, options.brightness))
52
except Exception:
53
sys.exit(USAGE)
54
if options.outfile is None:
55
options.outfile = options.routefile + ".poly.xml"
56
return options
57
58
59
def generate_poly(net, id, color, layer, edges, outf):
60
for edge in edges:
61
shape = net.getEdge(edge).getLane(0).getShape()
62
shapeString = ' '.join('%s,%s' % (x, y) for x, y in shape)
63
outf.write('<poly id="%s_%s" color="%s" layer="%s" type="taz_edge" shape="%s"/>\n' % (
64
id, net.getEdge(edge).getID(), color, layer, shapeString))
65
66
67
def main():
68
random.seed(42)
69
options = parse_args()
70
net = readNet(options.net)
71
with open(options.outfile, 'w') as outf:
72
outf.write('<polygons>\n')
73
for taz in parse(options.routefile, 'taz'):
74
generate_poly(net, taz.id, options.colorgen(),
75
options.layer, taz.edges.split(), outf)
76
outf.write('</polygons>\n')
77
78
79
if __name__ == "__main__":
80
main()
81
82