Eclipse SUMO - Simulation of Urban MObility
RODFDetFlowLoader.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 /****************************************************************************/
17 // A loader for detector flows
18 /****************************************************************************/
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <string>
25 #include <fstream>
26 #include <sstream>
34 #include "RODFDetFlowLoader.h"
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
41  RODFDetectorFlows& into,
42  SUMOTime startTime, SUMOTime endTime,
43  SUMOTime timeOffset, SUMOTime timeScale)
44  : myStorage(into), myTimeOffset(timeOffset), myTimeScale(timeScale),
45  myStartTime(startTime), myEndTime(endTime), myDetectorContainer(dets),
46  myHaveWarnedAboutOverridingBoundaries(false), myHaveWarnedAboutPartialDefs(false) {}
47 
48 
49 
51 
52 
53 void
54 RODFDetFlowLoader::read(const std::string& file) {
55  LineReader lr(file);
56  // parse first line
57  myLineHandler.reinit(lr.readLine(), ";", ";", true, true);
58  // parse values
59  while (lr.hasMore()) {
60  std::string line = lr.readLine();
61  if (line.find(';') == std::string::npos) {
62  continue;
63  }
65  try {
66  std::string detName = myLineHandler.get("detector");
67  if (!myDetectorContainer.knows(detName)) {
68  continue;
69  }
70  const double parsedTime = StringUtils::toDouble((myLineHandler.get("time"))) * myTimeScale - myTimeOffset;
71  // parsing as float to handle values which would cause int overflow
72  if (parsedTime < myStartTime || parsedTime >= myEndTime) {
75  WRITE_WARNING("At least one value lies beyond given time boundaries.");
76  }
77  continue;
78  }
79  const SUMOTime time = (SUMOTime)(parsedTime + .5);
80  FlowDef fd;
81  fd.isLKW = 0;
82  fd.qPKW = StringUtils::toDouble(myLineHandler.get("qpkw"));
83  fd.vPKW = 0;
84  if (myLineHandler.know("vPKW")) {
85  fd.vPKW = StringUtils::toDouble(myLineHandler.get("vpkw"));
86  }
87  fd.qLKW = 0;
88  if (myLineHandler.know("qLKW")) {
89  fd.qLKW = StringUtils::toDouble(myLineHandler.get("qlkw"));
90  }
91  fd.vLKW = 0;
92  if (myLineHandler.know("vLKW")) {
93  fd.vLKW = StringUtils::toDouble(myLineHandler.get("vlkw"));
94  }
95  if (fd.qLKW < 0) {
96  fd.qLKW = 0;
97  }
98  if (fd.qPKW < 0) {
99  fd.qPKW = 0;
100  }
101  myStorage.addFlow(detName, time, fd);
104  WRITE_WARNING("At least one line does not contain the correct number of columns.");
105  }
106  continue;
107  } catch (UnknownElement&) {} catch (OutOfBoundsException&) {} catch (NumberFormatException&) {}
108  throw ProcessError("The detector-flow-file '" + lr.getFileName() + "' is corrupt;\n"
109  + " The following values must be supplied : 'Detector', 'Time', 'qPKW'\n"
110  + " The according column names must be given in the first line of the file.");
111  }
112 }
113 
114 
115 /****************************************************************************/
116 
RODFDetectorFlows
A container for flows.
Definition: RODFDetectorFlow.h:67
LineReader.h
NamedColumnsParser::hasFullDefinition
bool hasFullDefinition() const
Returns whether the number of named columns matches the actual number.
Definition: NamedColumnsParser.cpp:104
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
RODFDetectorCon::knows
bool knows(const std::string &id) const
Definition: RODFDetector.cpp:619
LineReader::readLine
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
Definition: LineReader.cpp:68
OptionsCont.h
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
MsgHandler.h
FileHelpers.h
RODFDetFlowLoader::myEndTime
const SUMOTime myEndTime
Definition: RODFDetFlowLoader.h:89
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
RODFDetectorCon
A container for RODFDetectors.
Definition: RODFDetector.h:220
RODFDetFlowLoader::myDetectorContainer
const RODFDetectorCon & myDetectorContainer
Container holding known detectors.
Definition: RODFDetFlowLoader.h:95
RODFDetFlowLoader::~RODFDetFlowLoader
~RODFDetFlowLoader()
Destructor.
Definition: RODFDetFlowLoader.cpp:50
NamedColumnsParser::parseLine
void parseLine(const std::string &line)
Parses the contents of the line.
Definition: NamedColumnsParser.cpp:61
RODFDetFlowLoader::myTimeScale
const SUMOTime myTimeScale
The time scale to apply to read time values.
Definition: RODFDetFlowLoader.h:86
fd
static double fd[10]
Definition: odrSpiral.cpp:94
NumberFormatException
Definition: UtilExceptions.h:95
NamedColumnsParser::get
std::string get(const std::string &name, bool prune=false) const
Returns the named information.
Definition: NamedColumnsParser.cpp:67
FlowDef
Definition of the traffic during a certain time containing the flows and speeds.
Definition: RODFDetectorFlow.h:40
LineReader::getFileName
std::string getFileName() const
Returns the name of the used file.
Definition: LineReader.cpp:171
ProcessError
Definition: UtilExceptions.h:39
OutOfBoundsException
Definition: UtilExceptions.h:134
UtilExceptions.h
LineReader::hasMore
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:52
NamedColumnsParser::reinit
void reinit(const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true)
Reinitialises the parser.
Definition: NamedColumnsParser.cpp:50
LineReader
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:50
NamedColumnsParser::know
bool know(const std::string &name) const
Returns the information whether the named column is known.
Definition: NamedColumnsParser.cpp:88
RODFDetFlowLoader::RODFDetFlowLoader
RODFDetFlowLoader(const RODFDetectorCon &dets, RODFDetectorFlows &into, SUMOTime startTime, SUMOTime endTime, SUMOTime timeOffset, SUMOTime timeScale)
Constructor.
Definition: RODFDetFlowLoader.cpp:40
StringUtils.h
UnknownElement
Definition: UtilExceptions.h:147
RODFDetFlowLoader.h
RODFDetFlowLoader::myTimeOffset
const SUMOTime myTimeOffset
The time offset to apply to read time values.
Definition: RODFDetFlowLoader.h:83
config.h
StringTokenizer.h
RODFDetectorFlows::addFlow
void addFlow(const std::string &detector_id, SUMOTime timestamp, const FlowDef &fd)
Definition: RODFDetectorFlow.cpp:44
RODFDetFlowLoader::myHaveWarnedAboutPartialDefs
bool myHaveWarnedAboutPartialDefs
Whether a warning about partial definitions was already written.
Definition: RODFDetFlowLoader.h:101
RODFDetFlowLoader::myLineHandler
NamedColumnsParser myLineHandler
The value extractor.
Definition: RODFDetFlowLoader.h:92
RODFDetFlowLoader::myHaveWarnedAboutOverridingBoundaries
bool myHaveWarnedAboutOverridingBoundaries
Whether a warning about overriding boundaries was already written.
Definition: RODFDetFlowLoader.h:98
RODFDetFlowLoader::myStorage
RODFDetectorFlows & myStorage
The container for read detector values.
Definition: RODFDetFlowLoader.h:80
RODFDetFlowLoader::read
void read(const std::string &file)
Reads the given file assuming it contains detector values.
Definition: RODFDetFlowLoader.cpp:54