Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/district/gridDistricts.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2007-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 gridDistricts.py
15
# @author Jakob Erdmann
16
# @author Mirko Barthauer
17
# @date 2019-01-02
18
19
"""
20
Generate a grid-based TAZ file
21
"""
22
from __future__ import absolute_import
23
from __future__ import print_function
24
import os
25
import sys
26
import random
27
SUMO_HOME = os.environ.get('SUMO_HOME',
28
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
29
sys.path.append(os.path.join(SUMO_HOME, 'tools'))
30
import sumolib # noqa
31
from sumolib.miscutils import Colorgen # noqa
32
from sumolib.options import ArgumentParser # noqa
33
34
35
class TAZ:
36
def __init__(self, id, shape, color):
37
self.id = id
38
self.shape = shape
39
self.color = color
40
self.edges = []
41
42
def write(self, outf):
43
outf.write(' <taz id="%s" shape="%s" color="%s" edges="%s"/>\n' % (
44
self.id, ' '.join(["%s,%s" % (x, y) for x, y in self.shape]),
45
self.color, ' '.join(self.edges)))
46
47
48
def getOptions():
49
ap = ArgumentParser()
50
ap.add_argument("-v", "--verbose", action="store_true", default=False,
51
help="tell me what you are doing")
52
ap.add_argument("-n", "--net-file", dest="netfile", category="input", type=ArgumentParser.net_file,
53
required=True, help="the network to read lane and edge permissions")
54
ap.add_argument("-o", "--output", category="output", type=ArgumentParser.file,
55
required=True, help="output taz file")
56
ap.add_argument("-w", "--grid-width", dest="gridWidth", type=float, default=100.0,
57
help="width of gride cells in m")
58
ap.add_argument("--vclass", type=str, help="Include only edges allowing VCLASS")
59
ap.add_argument("-u", "--hue", default="random", type=str,
60
help="hue for taz (float from [0,1] or 'random')")
61
ap.add_argument("-s", "--saturation", default=1, type=str,
62
help="saturation for taz (float from [0,1] or 'random')")
63
ap.add_argument("-b", "--brightness", default=1, type=str,
64
help="brightness for taz (float from [0,1] or 'random')")
65
ap.add_argument("--seed", type=int, default=42, help="random seed")
66
options = ap.parse_args()
67
if not options.netfile or not options.output:
68
ap.print_help()
69
ap.exit("Error! net-file and output file")
70
options.colorgen = Colorgen((options.hue, options.saturation, options.brightness))
71
return options
72
73
74
if __name__ == "__main__":
75
options = getOptions()
76
random.seed(options.seed)
77
if options.verbose:
78
print("Reading net")
79
net = sumolib.net.readNet(options.netfile)
80
xmin, ymin, xmax, ymax = net.getBoundary()
81
odpairs = {} # (x,y) -> TAZ
82
centerCoords = {} # edge -> center pos
83
w = options.gridWidth
84
w2 = w * 0.5 - 1
85
for edge in net.getEdges():
86
if options.vclass is not None and not edge.allows(options.vclass):
87
continue
88
x, y = sumolib.geomhelper.positionAtShapeOffset(edge.getShape(True), edge.getLength() / 2)
89
xIndex = int((x - xmin + w2) / w)
90
yIndex = int((y - ymin + w2) / w)
91
ii = (xIndex, yIndex)
92
x2 = xmin + xIndex * w
93
y2 = ymin + yIndex * w
94
if ii not in odpairs:
95
odpairs[ii] = TAZ("%s_%s" % (xIndex, yIndex),
96
[(x2 - w2, y2 - w2),
97
(x2 + w2, y2 - w2),
98
(x2 + w2, y2 + w2),
99
(x2 - w2, y2 + w2),
100
(x2 - w2, y2 - w2)],
101
options.colorgen())
102
odpairs[ii].edges.append(edge.getID())
103
104
with open(options.output, 'w') as outf:
105
sumolib.writeXMLHeader(outf, "$Id$", "additional", options=options)
106
for ii, taz in sorted(odpairs.items()):
107
taz.write(outf)
108
outf.write("</additional>\n")
109
110