Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSVTypeProbe.cpp
15 : /// @author Tino Morenz
16 : /// @author Daniel Krajzewicz
17 : /// @author Jakob Erdmann
18 : /// @author Michael Behrisch
19 : /// @date Wed, 24.10.2007
20 : ///
21 : // Writes positions of vehicles that have a certain (named) type
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <string>
26 : #include <utils/common/WrappingCommand.h>
27 : #include <microsim/MSNet.h>
28 : #include <microsim/MSVehicle.h>
29 : #include <microsim/MSEventControl.h>
30 : #include <microsim/MSVehicleControl.h>
31 : #include <microsim/MSLane.h>
32 : #include <utils/iodevices/OutputDevice.h>
33 : #include <utils/geom/GeoConvHelper.h>
34 :
35 : #include "MSVTypeProbe.h"
36 :
37 :
38 : // ===========================================================================
39 : // method definitions
40 : // ===========================================================================
41 90 : MSVTypeProbe::MSVTypeProbe(const std::string& id,
42 : const std::string& vType,
43 90 : OutputDevice& od, SUMOTime frequency)
44 90 : : Named(id), myVType(vType), myOutputDevice(od), myFrequency(frequency) {
45 90 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
46 180 : myOutputDevice.writeXMLHeader("vehicle-type-probes", "vtypeprobe_file.xsd");
47 90 : }
48 :
49 :
50 180 : MSVTypeProbe::~MSVTypeProbe() {
51 180 : }
52 :
53 :
54 : SUMOTime
55 8852 : MSVTypeProbe::execute(SUMOTime currentTime) {
56 8852 : myOutputDevice.openTag(SUMO_TAG_TIMESTEP);
57 8852 : myOutputDevice.writeAttr(SUMO_ATTR_TIME, time2string(currentTime));
58 8852 : myOutputDevice.writeAttr(SUMO_ATTR_ID, getID());
59 8852 : myOutputDevice.writeAttr("vType", myVType);
60 8852 : MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
61 19942 : for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
62 11090 : const SUMOVehicle* veh = it->second;
63 11090 : const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh);
64 11090 : if (myVType == "" || myVType == veh->getVehicleType().getID()) {
65 10712 : if (veh->isOnRoad()) {
66 10624 : Position pos = veh->getPosition();
67 10624 : myOutputDevice.openTag(SUMO_TAG_VEHICLE);
68 10624 : myOutputDevice.writeAttr(SUMO_ATTR_ID, veh->getID());
69 10624 : if (microVeh != nullptr) {
70 7072 : myOutputDevice.writeAttr(SUMO_ATTR_LANE, microVeh->getLane()->getID());
71 : }
72 10624 : myOutputDevice.writeAttr(SUMO_ATTR_POSITION, veh->getPositionOnLane());
73 10624 : myOutputDevice.writeAttr(SUMO_ATTR_X, pos.x());
74 10624 : myOutputDevice.writeAttr(SUMO_ATTR_Y, pos.y());
75 10624 : if (MSNet::getInstance()->hasElevation()) {
76 72 : myOutputDevice.writeAttr(SUMO_ATTR_Z, pos.z());
77 : }
78 10624 : if (GeoConvHelper::getFinal().usingGeoProjection()) {
79 8706 : GeoConvHelper::getFinal().cartesian2geo(pos);
80 8706 : myOutputDevice.setPrecision(gPrecisionGeo);
81 8706 : myOutputDevice.writeAttr(SUMO_ATTR_LAT, pos.y());
82 8706 : myOutputDevice.writeAttr(SUMO_ATTR_LON, pos.x());
83 8706 : myOutputDevice.setPrecision();
84 : }
85 10624 : myOutputDevice.writeAttr(SUMO_ATTR_SPEED, veh->getSpeed());
86 21248 : myOutputDevice.closeTag();
87 : }
88 : }
89 : }
90 8852 : myOutputDevice.closeTag();
91 8852 : return myFrequency;
92 : }
93 :
94 :
95 : /****************************************************************************/
|