SUMO - Simulation of Urban MObility
RODFDetectorHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A handler for loading detector descriptions
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
39 #include <utils/common/ToString.h>
42 #include "RODFDetectorHandler.h"
43 #include "RODFNet.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50  const std::string& file)
51  : SUMOSAXHandler(file),
52  myNet(optNet), myIgnoreErrors(ignoreErrors), myContainer(con) {}
53 
54 
56 
57 
58 void
60  const SUMOSAXAttributes& attrs) {
61  if (element == SUMO_TAG_DETECTOR_DEFINITION || element == SUMO_TAG_E1DETECTOR || element == SUMO_TAG_INDUCTION_LOOP) {
62  try {
63  bool ok = true;
64  // get the id, report an error if not given or empty...
65  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
66  if (!ok) {
67  throw ProcessError();
68  }
69  std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
70  if (!ok) {
71  throw ProcessError();
72  }
73  ROEdge* edge = myNet->getEdge(lane.substr(0, lane.rfind('_')));
74  int laneIndex = TplConvert::_2intSec(lane.substr(lane.rfind('_') + 1).c_str(), std::numeric_limits<int>::max());
75  if (edge == 0 || laneIndex >= edge->getNumLanes()) {
76  throw ProcessError("Unknown lane '" + lane + "' for detector '" + id + "' in '" + getFileName() + "'.");
77  }
78  double pos = attrs.get<double>(SUMO_ATTR_POSITION, id.c_str(), ok);
79  std::string mml_type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
80  if (!ok) {
81  throw ProcessError();
82  }
84  if (mml_type == "between") {
85  type = BETWEEN_DETECTOR;
86  } else if (mml_type == "source" || mml_type == "highway_source") { // !!! highway-source is legacy (removed accoring output on 06.08.2007)
87  type = SOURCE_DETECTOR;
88  } else if (mml_type == "sink") {
89  type = SINK_DETECTOR;
90  }
91  RODFDetector* detector = new RODFDetector(id, lane, pos, type);
92  if (!myContainer.addDetector(detector)) {
93  delete detector;
94  throw ProcessError("Could not add detector '" + id + "' (probably the id is already used).");
95  }
96  } catch (ProcessError& e) {
97  if (myIgnoreErrors) {
98  WRITE_WARNING(e.what());
99  } else {
100  throw e;
101  }
102  }
103  }
104 }
105 
106 
107 
108 /****************************************************************************/
109 
alternative tag for e1 detector
virtual ~RODFDetectorHandler()
Destructor.
int getNumLanes() const
Returns the number of lanes this edge has.
Definition: ROEdge.h:251
RODFDetectorType
Numerical representation of different detector types.
Definition: RODFDetector.h:65
RODFNet * myNet
the net
bool addDetector(RODFDetector *dfd)
A source detector.
Definition: RODFDetector.h:76
const std::string & getFileName() const
returns the current file name
SAX-handler base for SUMO-files.
A container for RODFDetectors.
Definition: RODFDetector.h:227
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
A not yet defined detector.
Definition: RODFDetector.h:67
bool myIgnoreErrors
whether to ignore errors on parsing
An in-between detector.
Definition: RODFDetector.h:73
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
A DFROUTER-network.
Definition: RODFNet.h:51
definition of a detector
RODFDetectorCon & myContainer
the container to put the detectors into
A basic edge for routing applications.
Definition: ROEdge.h:77
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
Class representing a detector within the DFROUTER.
Definition: RODFDetector.h:88
RODFDetectorHandler(RODFNet *optNet, bool ignoreErrors, RODFDetectorCon &con, const std::string &file)
Constructor.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:163
static int _2intSec(const E *const data, int def)
converts a 0-terminated char-type array into the integer value described by it
Definition: TplConvert.h:195