Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/district/filterDistricts.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 filterDistricts.py
15
# @author Jakob Erdmann
16
# @author Mirko Barthauer
17
# @date 2017-10-05
18
19
"""
20
Filters a TAZ file for edges that exist in the given net
21
and optionally
22
- keep edges that permit a specific vehicle class
23
- remove specific edge ids
24
25
"""
26
from __future__ import absolute_import
27
from __future__ import print_function
28
import os
29
import sys
30
SUMO_HOME = os.environ.get('SUMO_HOME',
31
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
32
sys.path.append(os.path.join(SUMO_HOME, 'tools'))
33
import sumolib # noqa
34
from sumolib.options import ArgumentParser # noqa
35
36
37
def getOptions():
38
ap = ArgumentParser()
39
ap.add_argument(
40
"-v", "--verbose", action="store_true", default=False,
41
help="tell me what you are doing")
42
ap.add_argument("-n", "--net-file", dest="netfile", category="input", type=ArgumentParser.net_file,
43
required=True, help="the network to read lane and edge permissions")
44
ap.add_argument("-t", "--taz-file", dest="tazfile", category="input", type=ArgumentParser.file,
45
required=True, help="the district file to be filtered")
46
ap.add_argument("-o", "--output", default="taz_filtered.add.xml", category="output", type=ArgumentParser.file,
47
help="write filtered districts to FILE (default: %(default)s)", metavar="FILE")
48
ap.add_argument("--vclass", type=str, help="filter taz edges that allow the given vehicle class")
49
ap.add_argument("--remove-ids", category="processing", dest="removeIDs",
50
help="Remove the given ids from all TAZ")
51
ap.add_argument("--remove-ids-file", dest="removeIDsFile", category="processing", type=ap.additional_file,
52
help="Remove ids listed in FILE from all TAZ")
53
options = ap.parse_args()
54
if not options.netfile or not options.tazfile:
55
ap.print_help()
56
ap.exit(
57
"Error! net-file, taz-file and vclass are mandatory")
58
59
options.remove = set()
60
if options.removeIDs is not None:
61
options.remove.update(options.removeIDs.strip("'").split(','))
62
if options.removeIDsFile is not None:
63
with open(options.removeIDsFile) as idf:
64
for line in idf:
65
options.remove.update(line.strip().split(','))
66
67
return options
68
69
70
def keep(options, net, edge):
71
return (net.hasEdge(edge)
72
and (options.vclass is None or net.getEdge(edge).allows(options.vclass))
73
and edge not in options.remove)
74
75
76
if __name__ == "__main__":
77
options = getOptions()
78
if options.verbose:
79
print("Reading net")
80
net = sumolib.net.readNet(options.netfile)
81
with open(options.output, 'w') as outf:
82
sumolib.writeXMLHeader(outf, "$Id$", "additional", options=options)
83
for taz in sumolib.output.parse(options.tazfile, "taz"):
84
if taz.edges is not None:
85
taz.edges = " ".join(
86
[e for e in taz.edges.split() if keep(options, net, e)])
87
deleteSources = []
88
if taz.tazSink is not None:
89
taz.tazSink = [s for s in taz.tazSink if keep(options, net, s.id)]
90
if taz.tazSource is not None:
91
taz.tazSource = [s for s in taz.tazSource if keep(options, net, s.id)]
92
outf.write(taz.toXML(initialIndent=" " * 4))
93
outf.write("</additional>\n")
94
95