Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/netmatch.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-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 netmatch.py
15
# @author Marek Heinrich
16
# @author Michael Behrisch
17
# @date 2013-10-22
18
19
20
from __future__ import print_function
21
from __future__ import absolute_import
22
import os
23
import sys
24
25
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
26
import sumolib # noqa
27
from sumolib.geomhelper import distance # noqa
28
29
30
ap = sumolib.options.ArgumentParser()
31
ap.add_argument("-v", "--verbose", action="store_true", dest="verbose",
32
default=False, help="tell me what you are doing")
33
ap.add_argument("-1", "--net1", dest="net1", required=True, category="input", type=ap.net_file,
34
help="first SUMO network to use (mandatory)", metavar="FILE")
35
ap.add_argument("-2", "--net2", dest="net2", required=True, category="input", type=ap.net_file,
36
help="second SUMO network to use (mandatory)", metavar="FILE")
37
# ap.add_argument("-t", "--trips", dest="trips",
38
# category="input", type=ap.route_file, help="Trips to remap (mandatory)", metavar="FILE")
39
# ap.add_argument("-a", "--nodes1", dest="nodes1",
40
# category="input", type=ap.file, help="The first matching nodes", metavar="NODELIST")
41
# ap.add_argument("-b", "--nodes2", dest="nodes2",
42
# category="input", type=ap.file, help="The second matching nodes", metavar="NODELIST")
43
# ap.add_argument("-d", "--dump", dest="dump",
44
# category="output", type=ap.file, help="dump file to use", metavar="FILE")
45
ap.add_argument("-d", "--delta", default=1,
46
type=float, help="maximum distance between end points")
47
ap.add_argument("-o", "--output", dest="output", required=True,
48
category="output", type=ap.file, help="(base) name for the output", metavar="FILE")
49
ap.add_argument("--edges1", category="output", type=ap.file,
50
help="matched edges in net 1", metavar="FILE")
51
ap.add_argument("--edges2", category="output", type=ap.file,
52
help="matched edges in net 2", metavar="FILE")
53
options = ap.parse_args()
54
55
56
# read networks
57
if options.verbose:
58
print("Reading net#1...")
59
net1 = sumolib.net.readNet(options.net1)
60
net1.move(-net1.getLocationOffset()[0], -net1.getLocationOffset()[1])
61
62
if options.verbose:
63
print("Reading net#2...")
64
net2 = sumolib.net.readNet(options.net2)
65
net2.move(-net2.getLocationOffset()[0], -net2.getLocationOffset()[1])
66
67
68
f = open(options.output, "w")
69
70
matchedEdges1 = set()
71
matchedEdges2 = set()
72
73
for e1 in net1.getEdges():
74
from1 = e1.getFromNode().getCoord()
75
to1 = e1.getToNode().getCoord()
76
for e2, d in net2.getNeighboringEdges(from1[0], from1[1], options.delta):
77
if distance(from1, e2.getFromNode().getCoord()) < options.delta \
78
and distance(to1, e2.getToNode().getCoord()) < options.delta \
79
and e2 not in matchedEdges2:
80
print(e1.getID(), e2.getID(), file=f)
81
matchedEdges1.add(e1)
82
matchedEdges2.add(e2)
83
break
84
85
f.close()
86
87
print("matched", len(matchedEdges1), "out of",
88
len(net1.getEdges()), "in", options.net1)
89
print("matched", len(matchedEdges2), "out of",
90
len(net2.getEdges()), "in", options.net2)
91
92
if options.edges1:
93
print("\n".join(["edge:%s" % e.getID()
94
for e in matchedEdges1]), file=open(options.edges1, "w"))
95
if options.edges2:
96
print("\n".join(["edge:%s" % e.getID()
97
for e in matchedEdges2]), file=open(options.edges2, "w"))
98
99
# adaptor = sumolib.net.netshiftadaptor.NetShiftAdaptor(
100
# net1, net2, options.nodes1.split(","), options.nodes2.split(","))
101
# adaptor.reproject(options.verbose)
102
103