SUMO - Simulation of Urban MObility

MSVTypeProbe.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Writes positions of vehicles that have a certain (named) type
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <string>
00031 #include <utils/common/WrappingCommand.h>
00032 #include <microsim/MSLane.h>
00033 #include <utils/iodevices/OutputDevice.h>
00034 #include <utils/geom/GeoConvHelper.h>
00035 
00036 #include "MSVTypeProbe.h"
00037 
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041 
00042 
00043 // ===========================================================================
00044 // method definitions
00045 // ===========================================================================
00046 MSVTypeProbe::MSVTypeProbe(const std::string& id,
00047                            const std::string& vType,
00048                            OutputDevice& od, SUMOTime frequency)
00049     : Named(id), myVType(vType), myOutputDevice(od), myFrequency(frequency) {
00050     MSNet::getInstance()->getEndOfTimestepEvents().addEvent(this, 0, MSEventControl::ADAPT_AFTER_EXECUTION);
00051     myOutputDevice.writeXMLHeader("vehicle-type-probes");
00052 }
00053 
00054 
00055 MSVTypeProbe::~MSVTypeProbe() {
00056 }
00057 
00058 
00059 SUMOTime
00060 MSVTypeProbe::execute(SUMOTime currentTime) {
00061     const std::string indent("    ");
00062     myOutputDevice << indent << "<timestep time=\"" <<time2string(currentTime)<< "\" id=\"" << getID() << "\" vType=\"" << myVType << "\">" << "\n";
00063     MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
00064     MSVehicleControl::constVehIt it = vc.loadedVehBegin();
00065     MSVehicleControl::constVehIt end = vc.loadedVehEnd();
00066     for (; it != end; ++it) {
00067         const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
00068         if (myVType=="" || myVType==veh->getVehicleType().getID()) {
00069             if (!veh->isOnRoad()) {
00070                 continue;
00071             }
00072             Position pos = veh->getLane()->getShape().positionAtLengthPosition(veh->getPositionOnLane());
00073             myOutputDevice << indent << indent
00074                            << "<vehicle id=\"" << veh->getID()
00075                            << "\" lane=\"" << veh->getLane()->getID()
00076                            << "\" pos=\"" << veh->getPositionOnLane()
00077                            << "\" x=\"" << pos.x()
00078                            << "\" y=\"" << pos.y();
00079             if (GeoConvHelper::getDefaultInstance().usingGeoProjection()) {
00080                 GeoConvHelper::getDefaultInstance().cartesian2geo(pos);
00081                 myOutputDevice.setPrecision(GEO_OUTPUT_ACCURACY);
00082                 myOutputDevice << "\" lat=\"" << pos.y() << "\" lon=\"" << pos.x();
00083                 myOutputDevice.setPrecision();
00084             }
00085             myOutputDevice << "\" speed=\"" << veh->getSpeed() << "\"/>" << "\n";
00086         }
00087 
00088     }
00089     myOutputDevice << indent << "</timestep>" << "\n";
00090     return myFrequency;
00091 }
00092 
00093 
00094 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines