sumolib.output.convert.omnet

This module includes functions for converting SUMO's fcd-output into data files read by OMNET.

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2013-2026 German Aerospace Center (DLR) and others.
 3# This program and the accompanying materials are made available under the
 4# terms of the Eclipse Public License 2.0 which is available at
 5# https://www.eclipse.org/legal/epl-2.0/
 6# This Source Code may also be made available under the following Secondary
 7# Licenses when the conditions for such availability set forth in the Eclipse
 8# Public License 2.0 are satisfied: GNU General Public License, version 2
 9# or later which is available at
10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13# @file    omnet.py
14# @author  Daniel Krajzewicz
15# @author  Michael Behrisch
16# @date    2013-01-15
17
18"""
19This module includes functions for converting SUMO's fcd-output into
20data files read by OMNET.
21"""
22from __future__ import print_function
23from __future__ import absolute_import
24import datetime
25import sumolib.output
26import sumolib.net
27
28
29def fcd2omnet(inpFCD, outSTRM, further):
30    print('<?xml version="1.0" encoding="utf-8"?>', file=outSTRM)
31    print(
32        '<xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mobility_trace.xsd">',
33        file=outSTRM)
34    print('<!-- generated on %s by %s -->\n' %
35          (datetime.datetime.now(), further["app"]), file=outSTRM)
36    print('<mobility_trace>', file=outSTRM)
37    vIDm = sumolib._Running(further["orig-ids"], True)
38    checkGaps = not further["ignore-gaps"]
39    for timestep in inpFCD:
40        seen = set()
41        if not timestep.vehicle and checkGaps:
42            _writeMissing(outSTRM, timestep.time, vIDm, seen)
43            continue
44        for v in timestep.vehicle:
45            seen.add(v.id)
46            # create if not given
47            if not vIDm.k(v.id):
48                nid = vIDm.g(v.id)
49                print(("""  <create><nodeid>%s</nodeid><time>%s</time>\
50<type>SimpleNode</type><location><xpos>%s</xpos><ypos>%s</ypos></location></create>""") % (
51                    nid, timestep.time, v.x, v.y), file=outSTRM)
52            else:
53                nid = vIDm.g(v.id)
54                print(("""  <waypoint><nodeid>%s</nodeid><time>%s</time>\
55<destination><xpos>%s</xpos><ypos>%s</ypos></destination><speed>%s</speed></waypoint>""") % (
56                    nid, timestep.time, v.x, v.y, v.speed), file=outSTRM)
57        if checkGaps:
58            _writeMissing(outSTRM, timestep.time, vIDm, seen)
59    _writeMissing(outSTRM, timestep.time, vIDm, seen)
60    print('</mobility_trace>', file=outSTRM)
61
62
63def _writeMissing(outSTRM, t, vIDm, seen):
64    toDel = []
65    for v in sorted(vIDm._m):
66        if v in seen:
67            continue
68        nid = vIDm.g(v)
69        print("""  <destroy><time>%s</time><nodeid>%s</nodeid></destroy>""" %
70              (t, nid), file=outSTRM)
71        toDel.append(v)
72    for v in toDel:
73        vIDm.d(v)
def fcd2omnet(inpFCD, outSTRM, further):
30def fcd2omnet(inpFCD, outSTRM, further):
31    print('<?xml version="1.0" encoding="utf-8"?>', file=outSTRM)
32    print(
33        '<xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mobility_trace.xsd">',
34        file=outSTRM)
35    print('<!-- generated on %s by %s -->\n' %
36          (datetime.datetime.now(), further["app"]), file=outSTRM)
37    print('<mobility_trace>', file=outSTRM)
38    vIDm = sumolib._Running(further["orig-ids"], True)
39    checkGaps = not further["ignore-gaps"]
40    for timestep in inpFCD:
41        seen = set()
42        if not timestep.vehicle and checkGaps:
43            _writeMissing(outSTRM, timestep.time, vIDm, seen)
44            continue
45        for v in timestep.vehicle:
46            seen.add(v.id)
47            # create if not given
48            if not vIDm.k(v.id):
49                nid = vIDm.g(v.id)
50                print(("""  <create><nodeid>%s</nodeid><time>%s</time>\
51<type>SimpleNode</type><location><xpos>%s</xpos><ypos>%s</ypos></location></create>""") % (
52                    nid, timestep.time, v.x, v.y), file=outSTRM)
53            else:
54                nid = vIDm.g(v.id)
55                print(("""  <waypoint><nodeid>%s</nodeid><time>%s</time>\
56<destination><xpos>%s</xpos><ypos>%s</ypos></destination><speed>%s</speed></waypoint>""") % (
57                    nid, timestep.time, v.x, v.y, v.speed), file=outSTRM)
58        if checkGaps:
59            _writeMissing(outSTRM, timestep.time, vIDm, seen)
60    _writeMissing(outSTRM, timestep.time, vIDm, seen)
61    print('</mobility_trace>', file=outSTRM)