Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/network_statistics.py
169673 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file network_statistics.py
16
# @author Ronald Nippold
17
# @author Michael Behrisch
18
# @author Daniel Krajzewicz
19
# @author Matthias Schwamborn
20
# @date 2009-10-06
21
22
"""
23
Usage: python network_statistics.py network-file-to-be-analyzed.net.xml
24
25
Prints some information about a given network
26
"""
27
from __future__ import absolute_import
28
from __future__ import print_function
29
30
import os
31
import sys
32
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
33
import sumolib.net # noqa
34
35
36
def parse_args():
37
USAGE = "Usage: " + sys.argv[0] + " -n <net-file> <options>"
38
argParser = sumolib.options.ArgumentParser(usage=USAGE)
39
argParser.add_argument("-n", "--net-file", dest="netFile", help="The .net.xml file to read")
40
argParser.add_argument("-w", "--write-html", action="store_true", default=True,
41
help="Write statistics to HTML output file")
42
argParser.add_argument("-p", "--show-png", action="store_true", default=True,
43
help="Render PNG barplot with edge statistics")
44
options = argParser.parse_args()
45
if not options.netFile:
46
print("Missing arguments")
47
argParser.print_help()
48
exit()
49
return options
50
51
52
def renderHTML(values, filePrefix):
53
fd = open(filePrefix + ".html", "w")
54
fd.write("<html><body>")
55
fd.write("<h1>" + values["netname"] + "</h1></br>")
56
# network
57
fd.write("<h2>Network</h2></br>")
58
# edges
59
fd.write("<h2>Edges</h2></br>")
60
fd.write("Edge number: " + str(values["edgeNumber"]) + "</br>")
61
fd.write("Edgelength sum: " + str(values["edgeLengthSum"]) + "</br>")
62
fd.write("Lanelength sum: " + str(values["laneLengthSum"]) + "</br>")
63
# nodes
64
fd.write("<h2>Nodes</h2></br>")
65
fd.write("Node number: " + str(values["nodeNumber"]) + "</br>")
66
fd.write("</body></html>")
67
fd.close()
68
69
70
def renderPNG(values):
71
from pylab import bar, show
72
bar([0], [values["edgeNumber"]], 1, color='r')
73
show()
74
75
76
def render(net, filePrefix):
77
values = {}
78
values["netname"] = "hallo"
79
values["edgesPerLaneNumber"] = {}
80
values["edgeLengthSum"] = 0
81
values["laneLengthSum"] = 0
82
values["edgeNumber"] = len(net._edges)
83
values["nodeNumber"] = len(net._nodes)
84
for e in net._edges:
85
values["edgeLengthSum"] = values["edgeLengthSum"] + e._length
86
values["laneLengthSum"] = values["laneLengthSum"] + \
87
(e._length * float(len(e._lanes)))
88
if len(e._lanes) not in values["edgesPerLaneNumber"]:
89
values["edgesPerLaneNumber"][len(e._lanes)] = 0
90
values["edgesPerLaneNumber"][
91
len(e._lanes)] = values["edgesPerLaneNumber"][len(e._lanes)] + 1
92
93
if options.write_html:
94
renderHTML(values, filePrefix)
95
if options.show_png:
96
renderPNG(values)
97
98
99
if __name__ == "__main__":
100
options = parse_args()
101
net = sumolib.net.readNet(options.netFile)
102
103
# ----------
104
narea = (net._ranges[0][1] - net._ranges[0][0]) * \
105
(net._ranges[1][1] - net._ranges[1][0])
106
if narea < 1000 * 1000:
107
print("area of network:\t" + str(round(narea, 2)) + " m²" + "\n")
108
else:
109
print("area of network:\t" + str(round(narea / 1000000, 2)) + " km²" + "\n")
110
print("total number of edges:\t" + str(len(net._edges)) + "\n")
111
# ----------
112
lengthOfEdgeSum = .0
113
lane_1 = 0
114
lane_2 = 0
115
lane_3 = 0
116
lane_4 = 0
117
for n in net._edges:
118
lengthOfEdgeSum = lengthOfEdgeSum + n._length
119
if len(n._lanes) == 1:
120
lane_1 = lane_1 + 1
121
elif len(n._lanes) == 2:
122
lane_2 = lane_2 + 1
123
elif len(n._lanes) == 3:
124
lane_3 = lane_3 + 1
125
else:
126
lane_4 = lane_4 + 1
127
if lengthOfEdgeSum < 10000:
128
print("total length of all edges:\t" +
129
str(round(lengthOfEdgeSum, 2)) + " m" + "\n")
130
else:
131
print("total length of all edges:\t" +
132
str(round(lengthOfEdgeSum / 1000, 2)) + " km" + "\n")
133
print("number of edges with 1 lane:\t" + str(lane_1) + "\n")
134
print("number of edges with 2 lanes:\t" + str(lane_2) + "\n")
135
print("number of edges with 3 lanes:\t" + str(lane_3) + "\n")
136
print("number of edges with 4 or more lanes:\t" + str(lane_4) + "\n")
137
print("total number of nodes:\t" + str(len(net._nodes)) + "\n")
138
139
if options.write_html or options.show_png:
140
filePrefix = options.netFile[:options.netFile.rfind(".net.xml")]
141
if options.netFile.rfind(".net.xml") == -1:
142
filePrefix = options.netFile
143
render(net, filePrefix)
144
145