1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
10 /// @file    MSVTypeProbe.cpp
11 /// @author  Tino Morenz
12 /// @author  Daniel Krajzewicz
13 /// @author  Jakob Erdmann
14 /// @author  Michael Behrisch
15 /// @date    Wed, 24.10.2007
16 /// @version $Id$
17 ///
18 // Writes positions of vehicles that have a certain (named) type
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <utils/common/WrappingCommand.h>
29 #include <microsim/MSNet.h>
30 #include <microsim/MSVehicle.h>
31 #include <microsim/MSEventControl.h>
32 #include <microsim/MSVehicleControl.h>
33 #include <microsim/MSLane.h>
34 #include <utils/iodevices/OutputDevice.h>
35 #include <utils/geom/GeoConvHelper.h>
36 
37 #include "MSVTypeProbe.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
MSVTypeProbe(const std::string & id,const std::string & vType,OutputDevice & od,SUMOTime frequency)43 MSVTypeProbe::MSVTypeProbe(const std::string& id,
44                            const std::string& vType,
45                            OutputDevice& od, SUMOTime frequency)
46     : Named(id), myVType(vType), myOutputDevice(od), myFrequency(frequency) {
47     MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
48     myOutputDevice.writeXMLHeader("vehicle-type-probes", "vtypeprobe_file.xsd");
49 }
50 
51 
~MSVTypeProbe()52 MSVTypeProbe::~MSVTypeProbe() {
53 }
54 
55 
56 SUMOTime
execute(SUMOTime currentTime)57 MSVTypeProbe::execute(SUMOTime currentTime) {
58     myOutputDevice.openTag(SUMO_TAG_TIMESTEP);
59     myOutputDevice.writeAttr(SUMO_ATTR_TIME, time2string(currentTime));
60     myOutputDevice.writeAttr(SUMO_ATTR_ID, getID());
61     myOutputDevice.writeAttr("vType", myVType);
62     MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
63     for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
64         const SUMOVehicle* veh = it->second;
65         const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh);
66         if (myVType == "" || myVType == veh->getVehicleType().getID()) {
67             if (veh->isOnRoad()) {
68                 Position pos = veh->getPosition();
69                 myOutputDevice.openTag(SUMO_TAG_VEHICLE);
70                 myOutputDevice.writeAttr(SUMO_ATTR_ID, veh->getID());
71                 if (microVeh != nullptr) {
72                     myOutputDevice.writeAttr(SUMO_ATTR_LANE, microVeh->getLane()->getID());
73                 }
74                 myOutputDevice.writeAttr(SUMO_ATTR_POSITION, veh->getPositionOnLane());
75                 myOutputDevice.writeAttr(SUMO_ATTR_X, pos.x());
76                 myOutputDevice.writeAttr(SUMO_ATTR_Y, pos.y());
77                 if (MSNet::getInstance()->hasElevation()) {
78                     myOutputDevice.writeAttr(SUMO_ATTR_Z, pos.z());
79                 }
80                 if (GeoConvHelper::getFinal().usingGeoProjection()) {
81                     GeoConvHelper::getFinal().cartesian2geo(pos);
82                     myOutputDevice.setPrecision(gPrecisionGeo);
83                     myOutputDevice.writeAttr(SUMO_ATTR_LAT, pos.y());
84                     myOutputDevice.writeAttr(SUMO_ATTR_LON, pos.x());
85                     myOutputDevice.setPrecision();
86                 }
87                 myOutputDevice.writeAttr(SUMO_ATTR_SPEED, veh->getSpeed());
88                 myOutputDevice.closeTag();
89             }
90         }
91     }
92     myOutputDevice.closeTag();
93     return myFrequency;
94 }
95 
96 
97 /****************************************************************************/
98