Eclipse SUMO - Simulation of Urban MObility
MSFCDExport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Realises dumping Floating Car Data (FCD) Data
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
30 #include <utils/geom/GeomHelper.h>
33 #include <microsim/MSEdgeControl.h>
34 #include <microsim/MSEdge.h>
35 #include <microsim/MSLane.h>
36 #include <microsim/MSGlobals.h>
37 #include <microsim/MSNet.h>
38 #include <microsim/MSVehicle.h>
41 #include <microsim/MSContainer.h>
43 #include "MSFCDExport.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 void
50 MSFCDExport::write(OutputDevice& of, SUMOTime timestep, bool elevation) {
51  const bool useGeo = OptionsCont::getOptions().getBool("fcd-output.geo");
52  const bool signals = OptionsCont::getOptions().getBool("fcd-output.signals");
53  const bool writeDistance = OptionsCont::getOptions().getBool("fcd-output.distance");
54  const SUMOTime period = string2time(OptionsCont::getOptions().getString("device.fcd.period"));
55  if (period > 0 && timestep % period != 0) {
56  return;
57  }
58  of.openTag("timestep").writeAttr(SUMO_ATTR_TIME, time2string(timestep));
60  const bool filter = MSDevice_FCD::getEdgeFilter().size() > 0;
61  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
62  const SUMOVehicle* veh = it->second;
63  const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh);
64  if ((veh->isOnRoad() || veh->isParking() || veh->isRemoteControlled())
65  && veh->getDevice(typeid(MSDevice_FCD)) != nullptr
66  // only filter on normal edges
67  && (!filter || MSDevice_FCD::getEdgeFilter().count(veh->getEdge()) > 0)) {
68  Position pos = veh->getPosition();
69  if (useGeo) {
72  }
74  of.writeAttr(SUMO_ATTR_ID, veh->getID());
75  of.writeAttr(SUMO_ATTR_X, pos.x());
76  of.writeAttr(SUMO_ATTR_Y, pos.y());
77  if (elevation) {
78  of.writeAttr(SUMO_ATTR_Z, pos.z());
79  }
84  if (microVeh != nullptr) {
85  of.writeAttr(SUMO_ATTR_LANE, microVeh->getLane()->getID());
86  }
88  if (microVeh != nullptr) {
89  if (signals) {
90  of.writeAttr("signals", toString(microVeh->getSignals()));
91  }
92  if (writeDistance) {
93  double distance = microVeh->getEdge()->getDistance();
94  if (microVeh->getLane()->isInternal()) {
95  distance += microVeh->getRoute().getDistanceBetween(0, microVeh->getPositionOnLane(),
96  microVeh->getEdge(), &microVeh->getLane()->getEdge(), true, microVeh->getRoutePosition());
97  } else {
98  distance += microVeh->getPositionOnLane();
99  }
100  // if the kilometrage runs counter to the edge direction edge->getDistance() is negative
101  of.writeAttr("distance", fabs(distance));
102  }
103  }
104  of.closeTag();
105  // write persons and containers
106  const MSEdge* edge = microVeh == nullptr ? veh->getEdge() : &veh->getLane()->getEdge();
107 
108  const std::vector<MSTransportable*>& persons = veh->getPersons();
109  for (std::vector<MSTransportable*>::const_iterator it_p = persons.begin(); it_p != persons.end(); ++it_p) {
110  writeTransportable(of, edge, *it_p, SUMO_TAG_PERSON, useGeo, elevation);
111  }
112  const std::vector<MSTransportable*>& containers = veh->getContainers();
113  for (std::vector<MSTransportable*>::const_iterator it_c = containers.begin(); it_c != containers.end(); ++it_c) {
114  writeTransportable(of, edge, *it_c, SUMO_TAG_CONTAINER, useGeo, elevation);
115  }
116  }
117  }
118  if (MSNet::getInstance()->getPersonControl().hasTransportables()) {
119  // write persons
121  const MSEdgeVector& edges = ec.getEdges();
122  for (MSEdgeVector::const_iterator e = edges.begin(); e != edges.end(); ++e) {
123  if (filter && MSDevice_FCD::getEdgeFilter().count(*e) == 0) {
124  continue;
125  }
126  const std::vector<MSTransportable*>& persons = (*e)->getSortedPersons(timestep);
127  for (std::vector<MSTransportable*>::const_iterator it_p = persons.begin(); it_p != persons.end(); ++it_p) {
128  writeTransportable(of, *e, *it_p, SUMO_TAG_PERSON, useGeo, elevation);
129  }
130  }
131  }
132  if (MSNet::getInstance()->getContainerControl().hasTransportables()) {
133  // write containers
135  const std::vector<MSEdge*>& edges = ec.getEdges();
136  for (std::vector<MSEdge*>::const_iterator e = edges.begin(); e != edges.end(); ++e) {
137  if (filter && MSDevice_FCD::getEdgeFilter().count(*e) == 0) {
138  continue;
139  }
140  const std::vector<MSTransportable*>& containers = (*e)->getSortedContainers(timestep);
141  for (std::vector<MSTransportable*>::const_iterator it_c = containers.begin(); it_c != containers.end(); ++it_c) {
142  writeTransportable(of, *e, *it_c, SUMO_TAG_CONTAINER, useGeo, elevation);
143  }
144  }
145  }
146  of.closeTag();
147 }
148 
149 
150 void
151 MSFCDExport::writeTransportable(OutputDevice& of, const MSEdge* e, MSTransportable* p, SumoXMLTag tag, bool useGeo, bool elevation) {
152  if (p->getDevice(typeid(MSTransportableDevice_FCD)) == nullptr) {
153  return;
154  }
155  Position pos = p->getPosition();
156  if (useGeo) {
159  }
160  of.openTag(tag);
161  of.writeAttr(SUMO_ATTR_ID, p->getID());
162  of.writeAttr(SUMO_ATTR_X, pos.x());
163  of.writeAttr(SUMO_ATTR_Y, pos.y());
164  if (elevation) {
165  of.writeAttr(SUMO_ATTR_Z, pos.z());
166  }
170  of.writeAttr(SUMO_ATTR_EDGE, e->getID());
171  of.writeAttr(SUMO_ATTR_SLOPE, e->getLanes()[0]->getShape().slopeDegreeAtOffset(p->getEdgePos()));
172  of.closeTag();
173 }
174 /****************************************************************************/
int getRoutePosition() const
Definition: MSVehicle.cpp:1180
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
static const std::set< const MSEdge * > & getEdgeFilter()
Definition: MSDevice_FCD.h:85
SumoXMLTag
Numbers representing SUMO-XML - element names.
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:670
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
long long int SUMOTime
Definition: SUMOTime.h:35
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
double z() const
Returns the z-position.
Definition: Position.h:67
virtual const std::vector< MSTransportable * > & getPersons() const =0
retrieve riding persons
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
virtual MSVehicleDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:561
virtual double getEdgePos() const
Return the position on the edge.
double y() const
Returns the y-position.
Definition: Position.h:62
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:397
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
double x() const
Returns the x-position.
Definition: Position.h:57
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:165
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
const MSRoute & getRoute() const
Returns the current route.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
int getSignals() const
Returns the signals.
Definition: MSVehicle.h:1249
const std::string & getID() const
Returns the id.
Definition: Named.h:77
static void writeTransportable(OutputDevice &of, const MSEdge *e, MSTransportable *p, SumoXMLTag tag, bool useGeo, bool elevation)
write transportable
virtual double getSpeed() const
the current speed of the transportable
virtual bool isParking() const =0
Returns the information whether the vehicle is parked.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
static void write(OutputDevice &of, SUMOTime timestep, bool elevation)
Writes the position and the angle of each vehicle into the given device.
Definition: MSFCDExport.cpp:50
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
bool isInternal() const
Definition: MSLane.cpp:1999
A road/street connecting two junctions.
Definition: MSEdge.h:76
static double naviDegree(const double angle)
Definition: GeomHelper.cpp:194
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Representation of a vehicle.
Definition: SUMOVehicle.h:61
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
int gPrecisionGeo
Definition: StdDefs.cpp:28
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
virtual double getAngle() const
return the current angle of the transportable
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:337
Stores edges and lanes, performs moving of vehicle.
Definition: MSEdgeControl.h:73
virtual bool isRemoteControlled() const =0
Returns the information whether the vehicle is fully controlled via TraCI.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
const std::string & getID() const
returns the id of the transportable
virtual double getSlope() const =0
Returns the slope of the road at vehicle&#39;s position.
virtual bool isOnRoad() const =0
Returns the information whether the vehicle is on a road (is simulated)
virtual const std::vector< MSTransportable * > & getContainers() const =0
retrieve riding containers
trigger: the time of the step
virtual double getAngle() const =0
Get the vehicle&#39;s angle.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
Definition: MSDevice_FCD.h:49
MSTransportableDevice * getDevice(const std::type_info &type) const
Returns a device of the given type if it exists or 0.
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:94
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
const MSEdgeVector & getEdges() const
Returns loaded edges.
description of a vehicle
virtual double getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
Position getPosition(const double) const
Return current position (x/y, cartesian)
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
MSEdgeControl & getEdgeControl()
Returns the edge control.
Definition: MSNet.h:380
The class responsible for building and deletion of vehicles.
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:72
double getDistance() const
Returns the kilometrage/mileage at the start of the edge.
Definition: MSEdge.h:288
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
double getDistanceBetween(double fromPos, double toPos, const MSEdge *fromEdge, const MSEdge *toEdge, bool includeInternal=true, int routePosition=0) const
Compute the distance between 2 given edges on this route, including the length of internal lanes...
Definition: MSRoute.cpp:277