Path: blob/main/tools/district/countConnectionsInDistricts.py
169674 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2007-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file countConnectionsInDistricts.py14# @author Daniel Krajzewicz15# @author Michael Behrisch16# @author Mirko Barthauer17# @date 2007-07-261819from __future__ import absolute_import20from __future__ import print_function21import os22import sys23from xml.sax import make_parser, handler24SUMO_HOME = os.environ.get('SUMO_HOME',25os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))26sys.path.append(os.path.join(SUMO_HOME, 'tools'))27from sumolib.options import ArgumentParser # noqa2829# written into the net. All members are "private".303132class NetDistrictConnectionCountingHandler(handler.ContentHandler):3334def __init__(self):35self._currentID = ""36self._districtSinkNo = {}37self._districtSourceNo = {}3839def startElement(self, name, attrs):40if name == 'taz':41self._currentID = attrs['id']42elif name == 'tazSink':43if self._currentID in self._districtSinkNo:44self._districtSinkNo[self._currentID] = self._districtSinkNo[45self._currentID] + 146else:47self._districtSinkNo[self._currentID] = 148elif name == 'tazSource':49if self._currentID in self._districtSinkNo:50self._districtSourceNo[self._currentID] = self._districtSourceNo[51self._currentID] + 152else:53self._districtSourceNo[self._currentID] = 15455def writeResults(self, output):56fd = open(output, "w")57for district in self._districtSourceNo:58fd.write(district + ";" + str(self._districtSourceNo[district]) + ";" + str(59self._districtSinkNo[district]) + "\n")60fd.close()616263ap = ArgumentParser()64ap.add_argument("-v", "--verbose", action="store_true", dest="verbose",65default=False, help="tell me what you are doing")66ap.add_argument("-n", "--net-file", dest="netfile", category="input", type=ap.net_file, required=True,67help="read SUMO network(s) from FILE(s) (mandatory)", metavar="FILE")68ap.add_argument("-o", "--output", dest="output", category="output", type=ap.net_file, required=True,69help="read SUMO network(s) from FILE(s) (mandatory)", metavar="FILE")70options = ap.parse_args()7172parser = make_parser()73reader = NetDistrictConnectionCountingHandler()74parser.setContentHandler(reader)75if options.verbose:76print("Reading net '" + options.netfile + "'")77parser.parse(options.netfile)78reader.writeResults(options.output)798081