SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RODFDetectorHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A handler for loading detector descriptions
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
40 #include <utils/common/ToString.h>
43 #include "RODFDetectorHandler.h"
44 #include "RODFNet.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  const std::string& file)
56  : SUMOSAXHandler(file),
57  myNet(optNet), myIgnoreErrors(ignoreErrors), myContainer(con) {}
58 
59 
61 
62 
63 void
65  const SUMOSAXAttributes& attrs) {
66  if (element == SUMO_TAG_DETECTOR_DEFINITION) {
67  try {
68  bool ok = true;
69  // get the id, report an error if not given or empty...
70  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
71  if (!ok) {
72  throw ProcessError();
73  }
74  std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
75  if (!ok) {
76  throw ProcessError();
77  }
78  ROEdge* edge = myNet->getEdge(lane.substr(0, lane.rfind('_')));
79  unsigned int laneIndex = TplConvert::_2intSec(lane.substr(lane.rfind('_') + 1).c_str(), INT_MAX);
80  if (edge == 0 || laneIndex >= edge->getLaneNo()) {
81  throw ProcessError("Unknown lane '" + lane + "' for detector '" + id + "' in '" + getFileName() + "'.");
82  }
83  SUMOReal pos = attrs.get<SUMOReal>(SUMO_ATTR_POSITION, id.c_str(), ok);
84  std::string mml_type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
85  if (!ok) {
86  throw ProcessError();
87  }
89  if (mml_type == "between") {
90  type = BETWEEN_DETECTOR;
91  } else if (mml_type == "source" || mml_type == "highway_source") { // !!! highway-source is legacy (removed accoring output on 06.08.2007)
92  type = SOURCE_DETECTOR;
93  } else if (mml_type == "sink") {
94  type = SINK_DETECTOR;
95  }
96  RODFDetector* detector = new RODFDetector(id, lane, pos, type);
97  if (!myContainer.addDetector(detector)) {
98  delete detector;
99  throw ProcessError("Could not add detector '" + id + "' (probably the id is already used).");
100  }
101  } catch (ProcessError& e) {
102  if (myIgnoreErrors) {
103  WRITE_WARNING(e.what());
104  } else {
105  throw e;
106  }
107  }
108  }
109 }
110 
111 
112 
113 /****************************************************************************/
114 
virtual ~RODFDetectorHandler()
Destructor.
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
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:102
unsigned int getLaneNo() const
Returns the number of lanes this edge has.
Definition: ROEdge.h:203
SAX-handler base for SUMO-files.
A container for RODFDetectors.
Definition: RODFDetector.h:227
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
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
const std::string & getFileName() const
returns the current file name
An in-between detector.
Definition: RODFDetector.h:73
Encapsulated SAX-Attributes.
A DFROUTER-network.
Definition: RODFNet.h:51
RODFDetectorCon & myContainer
the container to put the detectors into
A basic edge for routing applications.
Definition: ROEdge.h:69
Class representing a detector within the DFROUTER.
Definition: RODFDetector.h:88
RODFDetectorHandler(RODFNet *optNet, bool ignoreErrors, RODFDetectorCon &con, const std::string &file)
Constructor.
#define SUMOReal
Definition: config.h:215
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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
static int _2intSec(const E *const data, int def)
Definition: TplConvert.h:312