sumolib.output.convert.phem
This module includes functions for converting SUMO's fcd-output into data files read by PHEM.
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 phem.py 14# @author Daniel Krajzewicz 15# @author Jakob Erdmann 16# @author Michael Behrisch 17# @date 2013-01-15 18 19""" 20This module includes functions for converting SUMO's fcd-output into 21data files read by PHEM. 22""" 23from __future__ import print_function 24from __future__ import absolute_import 25import math 26import sumolib 27 28 29def _convType(tID): 30 if tID: 31 if tID.lower().startswith("passenger") or tID.lower().startswith("pkw"): 32 # needed for V2X applications only 33 if tID.lower().startswith("passenger_equipped") or tID.lower().startswith("pkw_equipped"): 34 return "PKW_equipped" 35 return "PKW" 36 if tID.lower().startswith("bus"): 37 return "BUS" 38 if tID.lower().startswith("heavy") or tID.lower().startswith("lkw"): 39 return "LKW" 40 print("Could not convert the vehicle type properly") 41 return "unknown" 42 43 44def fcd2dri(inpFCD, outSTRM, ignored): 45 """ 46 Reformats the contents of the given fcd-output file into a .dri file, readable 47 by PHEM. The fcd-output "fcd" must be a valid file name of an fcd-output. 48 49 The following may be a matter of changes: 50 - the engine torque is not given 51 """ 52 # print >> outSTRM, "v1\n<t>,<v>,<grad>,<n>\n[s],[km/h],[%],[1/min]\n" 53 print("v1\n<t>,<v>,<grad>\n[s],[km/h],[%]", file=outSTRM) 54 for q in inpFCD: 55 if q.vehicle: 56 for v in q.vehicle: 57 percSlope = math.sin(float(v.slope)) * 100. 58 print("%s,%.3f,%s" % ( 59 sumolib._intTime(q.time), float(v.speed) * 3.6, percSlope), file=outSTRM) 60 61 62def net2str(net, outSTRM): 63 """ 64 Writes the network object given as "inpNET" as a .str file readable by PHEM. 65 Returns a map from the SUMO-road id to the generated numerical id used by PHEM. 66 67 The following may be a matter of changes: 68 - currently, only the positions of the start and the end nodes are written, 69 the geometry of the edge as defined in the SUMO-network is not exported. 70 A map between the edge id and a segment to a numerical id would be necessary 71 """ 72 if outSTRM is not None: 73 print("Str-Id,Sp,SegAnX,SegEnX,SegAnY,SegEnY", file=outSTRM) 74 sIDm = sumolib._Running() 75 for e in net._edges: 76 eid = sIDm.g(e._id) 77 if outSTRM is not None: 78 c1 = e._from._coord 79 c2 = e._to._coord 80 print("%s,%s,%s,%s,%s,%s" % 81 (eid, len(e._lanes), c1[0], c2[0], c1[1], c2[1]), file=outSTRM) 82 return sIDm 83 84 85def fcd2fzp(inpFCD, outSTRM, further): 86 """ 87 Reformats the contents of the given fcd-output file into a .fzp file, readable 88 by PHEM. The fcd-output "fcd" must be a valid file name of an fcd-output. 89 90 The "sIDm" parameter must be a map from SUMO-edge ids to their numerical 91 representation as generated by toSTR(inpNET, outSTRM). 92 Returns two maps, the first from vehicle ids to a numerical representation, 93 the second from vehicle type ids to a numerical representation. 94 """ 95 sIDm = further["phemStreetMap"] 96 if outSTRM is not None: 97 print("t,WeltX,WeltY,Veh. No,v,Gradient,veh.Typ-Id,Str-Id", file=outSTRM) 98 vIDm = sumolib._Running(further["orig-ids"], True) 99 vtIDm = sumolib._Running() 100 vtIDm.g("PKW") 101 vtIDm.g("PKW_equipped") 102 vtIDm.g("LKW") 103 vtIDm.g("BUS") 104 for q in inpFCD: 105 if q.vehicle: 106 for v in q.vehicle: 107 vid = vIDm.g(v.id) 108 aType = _convType(v.type) 109 vtid = vtIDm.g(aType) 110 sid = sIDm.g(v.edge) 111 percSlope = math.sin(float(v.slope)) * 100. 112 if outSTRM is not None: 113 print("%s,%s,%s,%s,%.3f,%s,%s,%s" % ( 114 sumolib._intTime(q.time), float(v.x), float(v.y), 115 vid, float(v.speed) * 3.6, percSlope, vtid, sid), file=outSTRM) 116 return vIDm, vtIDm 117 118 119def vehicleTypes2flt(outSTRM, vtIDm): 120 """ 121 Currently, rather a stub than an implementation. Writes the vehicle ids stored 122 in the given "vtIDm" map formatted as a .flt file readable by PHEM. 123 124 The following may be a matter of changes: 125 - A default map is assigned to all vehicle types with the same probability 126 """ 127 for q in sorted(vtIDm._m): 128 print("%s,%s,%s" % 129 (vtIDm.g(q), r"<VEHDIR>\PC\PC_%s.GEN" % q, 1.), file=outSTRM)
45def fcd2dri(inpFCD, outSTRM, ignored): 46 """ 47 Reformats the contents of the given fcd-output file into a .dri file, readable 48 by PHEM. The fcd-output "fcd" must be a valid file name of an fcd-output. 49 50 The following may be a matter of changes: 51 - the engine torque is not given 52 """ 53 # print >> outSTRM, "v1\n<t>,<v>,<grad>,<n>\n[s],[km/h],[%],[1/min]\n" 54 print("v1\n<t>,<v>,<grad>\n[s],[km/h],[%]", file=outSTRM) 55 for q in inpFCD: 56 if q.vehicle: 57 for v in q.vehicle: 58 percSlope = math.sin(float(v.slope)) * 100. 59 print("%s,%.3f,%s" % ( 60 sumolib._intTime(q.time), float(v.speed) * 3.6, percSlope), file=outSTRM)
Reformats the contents of the given fcd-output file into a .dri file, readable by PHEM. The fcd-output "fcd" must be a valid file name of an fcd-output.
The following may be a matter of changes:
- the engine torque is not given
63def net2str(net, outSTRM): 64 """ 65 Writes the network object given as "inpNET" as a .str file readable by PHEM. 66 Returns a map from the SUMO-road id to the generated numerical id used by PHEM. 67 68 The following may be a matter of changes: 69 - currently, only the positions of the start and the end nodes are written, 70 the geometry of the edge as defined in the SUMO-network is not exported. 71 A map between the edge id and a segment to a numerical id would be necessary 72 """ 73 if outSTRM is not None: 74 print("Str-Id,Sp,SegAnX,SegEnX,SegAnY,SegEnY", file=outSTRM) 75 sIDm = sumolib._Running() 76 for e in net._edges: 77 eid = sIDm.g(e._id) 78 if outSTRM is not None: 79 c1 = e._from._coord 80 c2 = e._to._coord 81 print("%s,%s,%s,%s,%s,%s" % 82 (eid, len(e._lanes), c1[0], c2[0], c1[1], c2[1]), file=outSTRM) 83 return sIDm
Writes the network object given as "inpNET" as a .str file readable by PHEM. Returns a map from the SUMO-road id to the generated numerical id used by PHEM.
The following may be a matter of changes:
- currently, only the positions of the start and the end nodes are written, the geometry of the edge as defined in the SUMO-network is not exported. A map between the edge id and a segment to a numerical id would be necessary
86def fcd2fzp(inpFCD, outSTRM, further): 87 """ 88 Reformats the contents of the given fcd-output file into a .fzp file, readable 89 by PHEM. The fcd-output "fcd" must be a valid file name of an fcd-output. 90 91 The "sIDm" parameter must be a map from SUMO-edge ids to their numerical 92 representation as generated by toSTR(inpNET, outSTRM). 93 Returns two maps, the first from vehicle ids to a numerical representation, 94 the second from vehicle type ids to a numerical representation. 95 """ 96 sIDm = further["phemStreetMap"] 97 if outSTRM is not None: 98 print("t,WeltX,WeltY,Veh. No,v,Gradient,veh.Typ-Id,Str-Id", file=outSTRM) 99 vIDm = sumolib._Running(further["orig-ids"], True) 100 vtIDm = sumolib._Running() 101 vtIDm.g("PKW") 102 vtIDm.g("PKW_equipped") 103 vtIDm.g("LKW") 104 vtIDm.g("BUS") 105 for q in inpFCD: 106 if q.vehicle: 107 for v in q.vehicle: 108 vid = vIDm.g(v.id) 109 aType = _convType(v.type) 110 vtid = vtIDm.g(aType) 111 sid = sIDm.g(v.edge) 112 percSlope = math.sin(float(v.slope)) * 100. 113 if outSTRM is not None: 114 print("%s,%s,%s,%s,%.3f,%s,%s,%s" % ( 115 sumolib._intTime(q.time), float(v.x), float(v.y), 116 vid, float(v.speed) * 3.6, percSlope, vtid, sid), file=outSTRM) 117 return vIDm, vtIDm
Reformats the contents of the given fcd-output file into a .fzp file, readable by PHEM. The fcd-output "fcd" must be a valid file name of an fcd-output.
The "sIDm" parameter must be a map from SUMO-edge ids to their numerical representation as generated by toSTR(inpNET, outSTRM). Returns two maps, the first from vehicle ids to a numerical representation, the second from vehicle type ids to a numerical representation.
120def vehicleTypes2flt(outSTRM, vtIDm): 121 """ 122 Currently, rather a stub than an implementation. Writes the vehicle ids stored 123 in the given "vtIDm" map formatted as a .flt file readable by PHEM. 124 125 The following may be a matter of changes: 126 - A default map is assigned to all vehicle types with the same probability 127 """ 128 for q in sorted(vtIDm._m): 129 print("%s,%s,%s" % 130 (vtIDm.g(q), r"<VEHDIR>\PC\PC_%s.GEN" % q, 1.), file=outSTRM)
Currently, rather a stub than an implementation. Writes the vehicle ids stored in the given "vtIDm" map formatted as a .flt file readable by PHEM.
The following may be a matter of changes:
- A default map is assigned to all vehicle types with the same probability