SUMO - Simulation of Urban MObility
MSVTypeProbe.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Writes positions of vehicles that have a certain (named) type
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
36 #include <microsim/MSNet.h>
37 #include <microsim/MSVehicle.h>
38 #include <microsim/MSLane.h>
41 
42 #include "MSVTypeProbe.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 MSVTypeProbe::MSVTypeProbe(const std::string& id,
53  const std::string& vType,
54  OutputDevice& od, SUMOTime frequency)
55  : Named(id), myVType(vType), myOutputDevice(od), myFrequency(frequency) {
57  myOutputDevice.writeXMLHeader("vehicle-type-probes");
58 }
59 
60 
62 }
63 
64 
67  myOutputDevice.openTag("timestep") << " time=\"" << time2string(currentTime) << "\" id=\"" << getID() << "\" vType=\"" << myVType << "\"";
69  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
70  const SUMOVehicle* veh = it->second;
71  const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh);
72  if (myVType == "" || myVType == veh->getVehicleType().getID()) {
73  if (veh->isOnRoad()) {
74  Position pos = veh->getPosition();
75  myOutputDevice.openTag("vehicle").writeAttr("id", veh->getID());
76  if (microVeh != 0) {
77  myOutputDevice.writeAttr("lane", microVeh->getLane()->getID());
78  }
80  myOutputDevice.writeAttr("x", pos.x()).writeAttr("y", pos.y());
84  myOutputDevice.writeAttr("lat", pos.y()).writeAttr("lon", pos.x());
86  }
87  myOutputDevice.writeAttr("speed", veh->getSpeed());
89  }
90  }
91  }
93  return myFrequency;
94 }
95 
96 
97 /****************************************************************************/
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
MSVTypeProbe(const std::string &id, const std::string &vType, OutputDevice &od, SUMOTime frequency)
Constructor.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
#define GEO_OUTPUT_ACCURACY
Definition: config.h:16
virtual SUMOReal getPositionOnLane() const =0
Get the vehicle's position along the lane.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
SUMOTime myFrequency
The frequency of reporting.
Definition: MSVTypeProbe.h:103
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
void setPrecision(unsigned int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
Representation of a vehicle.
Definition: SUMOVehicle.h:65
SUMOTime execute(SUMOTime currentTime)
Writes values into the given stream.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:288
OutputDevice & myOutputDevice
The device to write into.
Definition: MSVTypeProbe.h:100
virtual bool isOnRoad() const =0
Returns the information whether the vehicle is on a road (is simulated)
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
Base class for objects which have an id.
Definition: Named.h:45
MSEventControl * getEndOfTimestepEvents()
Returns the event control for events executed at the end of a time step.
Definition: MSNet.h:379
virtual SUMOReal getSpeed() const =0
Returns the vehicle's current speed.
std::string myVType
The id of the vehicle type vehicles must have to be reported.
Definition: MSVTypeProbe.h:97
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
virtual ~MSVTypeProbe()
Destructor.
virtual Position getPosition(const SUMOReal offset=0) const =0
Return current position (x/y, cartesian)
const std::string & getID() const
Returns the name of the vehicle type.
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
int SUMOTime
Definition: SUMOTime.h:43
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
Patch the time in a way that it is at least as high as the simulation begin time. ...
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
The class responsible for building and deletion of vehicles.
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:331
virtual const std::string & getID() const =0
Get the vehicle's ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.