Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/district/countConnectionsInDistricts.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 countConnectionsInDistricts.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @author Mirko Barthauer
18
# @date 2007-07-26
19
20
from __future__ import absolute_import
21
from __future__ import print_function
22
import os
23
import sys
24
from xml.sax import make_parser, handler
25
SUMO_HOME = os.environ.get('SUMO_HOME',
26
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
27
sys.path.append(os.path.join(SUMO_HOME, 'tools'))
28
from sumolib.options import ArgumentParser # noqa
29
30
# written into the net. All members are "private".
31
32
33
class NetDistrictConnectionCountingHandler(handler.ContentHandler):
34
35
def __init__(self):
36
self._currentID = ""
37
self._districtSinkNo = {}
38
self._districtSourceNo = {}
39
40
def startElement(self, name, attrs):
41
if name == 'taz':
42
self._currentID = attrs['id']
43
elif name == 'tazSink':
44
if self._currentID in self._districtSinkNo:
45
self._districtSinkNo[self._currentID] = self._districtSinkNo[
46
self._currentID] + 1
47
else:
48
self._districtSinkNo[self._currentID] = 1
49
elif name == 'tazSource':
50
if self._currentID in self._districtSinkNo:
51
self._districtSourceNo[self._currentID] = self._districtSourceNo[
52
self._currentID] + 1
53
else:
54
self._districtSourceNo[self._currentID] = 1
55
56
def writeResults(self, output):
57
fd = open(output, "w")
58
for district in self._districtSourceNo:
59
fd.write(district + ";" + str(self._districtSourceNo[district]) + ";" + str(
60
self._districtSinkNo[district]) + "\n")
61
fd.close()
62
63
64
ap = ArgumentParser()
65
ap.add_argument("-v", "--verbose", action="store_true", dest="verbose",
66
default=False, help="tell me what you are doing")
67
ap.add_argument("-n", "--net-file", dest="netfile", category="input", type=ap.net_file, required=True,
68
help="read SUMO network(s) from FILE(s) (mandatory)", metavar="FILE")
69
ap.add_argument("-o", "--output", dest="output", category="output", type=ap.net_file, required=True,
70
help="read SUMO network(s) from FILE(s) (mandatory)", metavar="FILE")
71
options = ap.parse_args()
72
73
parser = make_parser()
74
reader = NetDistrictConnectionCountingHandler()
75
parser.setContentHandler(reader)
76
if options.verbose:
77
print("Reading net '" + options.netfile + "'")
78
parser.parse(options.netfile)
79
reader.writeResults(options.output)
80
81