SUMO - Simulation of Urban MObility
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.dlr.de/
13 // Copyright (C) 2001-2016 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 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51  const std::string& file)
52  : SUMOSAXHandler(file),
53  myNet(optNet), myIgnoreErrors(ignoreErrors), myContainer(con) {}
54 
55 
57 
58 
59 void
61  const SUMOSAXAttributes& attrs) {
62  if (element == SUMO_TAG_DETECTOR_DEFINITION || element == SUMO_TAG_E1DETECTOR || element == SUMO_TAG_INDUCTION_LOOP) {
63  try {
64  bool ok = true;
65  // get the id, report an error if not given or empty...
66  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
67  if (!ok) {
68  throw ProcessError();
69  }
70  std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
71  if (!ok) {
72  throw ProcessError();
73  }
74  ROEdge* edge = myNet->getEdge(lane.substr(0, lane.rfind('_')));
75  int laneIndex = TplConvert::_2intSec(lane.substr(lane.rfind('_') + 1).c_str(), INT_MAX);
76  if (edge == 0 || laneIndex >= edge->getLaneNo()) {
77  throw ProcessError("Unknown lane '" + lane + "' for detector '" + id + "' in '" + getFileName() + "'.");
78  }
79  double pos = attrs.get<double>(SUMO_ATTR_POSITION, id.c_str(), ok);
80  std::string mml_type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
81  if (!ok) {
82  throw ProcessError();
83  }
85  if (mml_type == "between") {
86  type = BETWEEN_DETECTOR;
87  } else if (mml_type == "source" || mml_type == "highway_source") { // !!! highway-source is legacy (removed accoring output on 06.08.2007)
88  type = SOURCE_DETECTOR;
89  } else if (mml_type == "sink") {
90  type = SINK_DETECTOR;
91  }
92  RODFDetector* detector = new RODFDetector(id, lane, pos, type);
93  if (!myContainer.addDetector(detector)) {
94  delete detector;
95  throw ProcessError("Could not add detector '" + id + "' (probably the id is already used).");
96  }
97  } catch (ProcessError& e) {
98  if (myIgnoreErrors) {
99  WRITE_WARNING(e.what());
100  } else {
101  throw e;
102  }
103  }
104  }
105 }
106 
107 
108 
109 /****************************************************************************/
110 
alternative tag for e1 detector
virtual ~RODFDetectorHandler()
Destructor.
RODFDetectorType
Numerical representation of different detector types.
Definition: RODFDetector.h:66
RODFNet * myNet
the net
bool addDetector(RODFDetector *dfd)
A source detector.
Definition: RODFDetector.h:77
const std::string & getFileName() const
returns the current file name
SAX-handler base for SUMO-files.
A container for RODFDetectors.
Definition: RODFDetector.h:228
#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:68
bool myIgnoreErrors
whether to ignore errors on parsing
An in-between detector.
Definition: RODFDetector.h:74
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:52
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:89
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:165
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:186
int getLaneNo() const
Returns the number of lanes this edge has.
Definition: ROEdge.h:245