Eclipse 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-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 /****************************************************************************/
18 // A handler for loading detector descriptions
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
33 #include <utils/common/ToString.h>
36 #include "RODFDetectorHandler.h"
37 #include "RODFNet.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44  const std::string& file)
45  : SUMOSAXHandler(file),
46  myNet(optNet), myIgnoreErrors(ignoreErrors), myContainer(con) {}
47 
48 
50 
51 
52 void
54  const SUMOSAXAttributes& attrs) {
55  if (element == SUMO_TAG_DETECTOR_DEFINITION || element == SUMO_TAG_E1DETECTOR || element == SUMO_TAG_INDUCTION_LOOP) {
56  try {
57  bool ok = true;
58  // get the id, report an error if not given or empty...
59  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
60  if (!ok) {
61  throw ProcessError();
62  }
63  std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
64  if (!ok) {
65  throw ProcessError();
66  }
67  ROEdge* edge = myNet->getEdge(lane.substr(0, lane.rfind('_')));
68  int laneIndex = StringUtils::toIntSecure(lane.substr(lane.rfind('_') + 1), std::numeric_limits<int>::max());
69  if (edge == nullptr || laneIndex >= edge->getNumLanes()) {
70  throw ProcessError("Unknown lane '" + lane + "' for detector '" + id + "' in '" + getFileName() + "'.");
71  }
72  double pos = attrs.get<double>(SUMO_ATTR_POSITION, id.c_str(), ok);
73  std::string mml_type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
74  if (!ok) {
75  throw ProcessError();
76  }
78  if (mml_type == "between") {
79  type = BETWEEN_DETECTOR;
80  } else if (mml_type == "source" || mml_type == "highway_source") { // !!! highway-source is legacy (removed accoring output on 06.08.2007)
81  type = SOURCE_DETECTOR;
82  } else if (mml_type == "sink") {
83  type = SINK_DETECTOR;
84  }
85  RODFDetector* detector = new RODFDetector(id, lane, pos, type);
86  if (!myContainer.addDetector(detector)) {
87  delete detector;
88  throw ProcessError("Could not add detector '" + id + "' (probably the id is already used).");
89  }
90  } catch (ProcessError& e) {
91  if (myIgnoreErrors) {
92  WRITE_WARNING(e.what());
93  } else {
94  throw e;
95  }
96  }
97  }
98 }
99 
100 
101 
102 /****************************************************************************/
103 
alternative tag for e1 detector
virtual ~RODFDetectorHandler()
Destructor.
int getNumLanes() const
Returns the number of lanes this edge has.
Definition: ROEdge.h:244
RODFDetectorType
Numerical representation of different detector types.
Definition: RODFDetector.h:59
RODFNet * myNet
the net
bool addDetector(RODFDetector *dfd)
static int toIntSecure(const std::string &sData, int def)
converts a string into the integer value described by it
A source detector.
Definition: RODFDetector.h:70
const std::string & getFileName() const
returns the current file name
SAX-handler base for SUMO-files.
A container for RODFDetectors.
Definition: RODFDetector.h:221
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
A not yet defined detector.
Definition: RODFDetector.h:61
bool myIgnoreErrors
whether to ignore errors on parsing
An in-between detector.
Definition: RODFDetector.h:67
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:45
definition of a detector
RODFDetectorCon & myContainer
the container to put the detectors into
A basic edge for routing applications.
Definition: ROEdge.h:73
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:82
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:153