SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RODFDetFlowLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A loader for detector flows
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2015 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 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <fstream>
34 #include <sstream>
42 #include "RODFDetFlowLoader.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  RODFDetectorFlows& into,
54  SUMOTime startTime, SUMOTime endTime,
55  SUMOTime timeOffset, SUMOTime timeScale)
56  : myStorage(into), myTimeOffset(timeOffset), myTimeScale(timeScale),
57  myStartTime(startTime), myEndTime(endTime), myDetectorContainer(dets),
58  myHaveWarnedAboutOverridingBoundaries(false), myHaveWarnedAboutPartialDefs(false) {}
59 
60 
61 
63 
64 
65 void
66 RODFDetFlowLoader::read(const std::string& file) {
67  LineReader lr(file);
68  // parse first line
69  myLineHandler.reinit(lr.readLine(), ";", ";", true, true);
70  // parse values
71  while (lr.hasMore()) {
72  std::string line = lr.readLine();
73  if (line.find(';') == std::string::npos) {
74  continue;
75  }
77  try {
78  std::string detName = myLineHandler.get("detector");
79  if (!myDetectorContainer.knows(detName)) {
80  continue;
81  }
82  const SUMOReal parsedTime = TplConvert::_2SUMOReal((myLineHandler.get("time").c_str())) * myTimeScale - myTimeOffset;
83  // parsing as float to handle values which would cause int overflow
84  if (parsedTime < myStartTime || parsedTime >= myEndTime) {
87  WRITE_WARNING("At least one value lies beyond given time boundaries.");
88  }
89  continue;
90  }
91  const SUMOTime time = (SUMOTime)(parsedTime + .5);
92  FlowDef fd;
93  fd.isLKW = 0;
94  fd.qPKW = TplConvert::_2SUMOReal(myLineHandler.get("qpkw").c_str());
95  fd.vPKW = 0;
96  if (myLineHandler.know("vPKW")) {
97  fd.vPKW = TplConvert::_2SUMOReal(myLineHandler.get("vpkw").c_str());
98  }
99  fd.qLKW = 0;
100  if (myLineHandler.know("qLKW")) {
101  fd.qLKW = TplConvert::_2SUMOReal(myLineHandler.get("qlkw").c_str());
102  }
103  fd.vLKW = 0;
104  if (myLineHandler.know("vLKW")) {
105  fd.vLKW = TplConvert::_2SUMOReal(myLineHandler.get("vlkw").c_str());
106  }
107  if (fd.qLKW < 0) {
108  fd.qLKW = 0;
109  }
110  if (fd.qPKW < 0) {
111  fd.qPKW = 0;
112  }
113  myStorage.addFlow(detName, time, fd);
116  WRITE_WARNING("At least one line does not contain the correct number of columns.");
117  }
118  continue;
119  } catch (UnknownElement&) {} catch (OutOfBoundsException&) {} catch (NumberFormatException&) {}
120  throw ProcessError("The detector-flow-file '" + lr.getFileName() + "' is corrupt;\n"
121  + " The following values must be supplied : 'Detector', 'Time', 'qPKW'\n"
122  + " The according column names must be given in the first line of the file.");
123  }
124 }
125 
126 
127 /****************************************************************************/
128 
bool myHaveWarnedAboutOverridingBoundaries
Whether a warning about overriding boundaries was already written.
bool knows(const std::string &id) const
const SUMOTime myTimeOffset
The time offset to apply to read time values.
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
Definition: LineReader.cpp:80
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:58
const SUMOTime myEndTime
std::string get(const std::string &name, bool prune=false) const
Returns the named information.
const RODFDetectorCon & myDetectorContainer
Container holding known detectors.
SUMOReal isLKW
A container for flows.
A container for RODFDetectors.
Definition: RODFDetector.h:227
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
RODFDetectorFlows & myStorage
The container for read detector values.
void reinit(const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true)
Reinitialises the parser.
RODFDetFlowLoader(const RODFDetectorCon &dets, RODFDetectorFlows &into, SUMOTime startTime, SUMOTime endTime, SUMOTime timeOffset, int timeScale)
Constructor.
void read(const std::string &file)
Reads the given file assuming it contains detector values.
Definition of the traffic during a certain time containing the flows and speeds.
bool hasFullDefinition() const
Returns whether the number of named columns matches the actual number.
const SUMOTime myTimeScale
The time scale to apply to read time values.
bool myHaveWarnedAboutPartialDefs
Whether a warning about partial definitions was already written.
std::string getFileName() const
Returns the name of the used file.
Definition: LineReader.cpp:183
bool know(const std::string &name) const
Returns the information whether the named column is known.
SUMOReal vPKW
void addFlow(const std::string &detector_id, SUMOTime timestamp, const FlowDef &fd)
NamedColumnsParser myLineHandler
The value extractor.
int SUMOTime
Definition: SUMOTime.h:43
SUMOReal qPKW
~RODFDetFlowLoader()
Destructor.
#define SUMOReal
Definition: config.h:218
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:64
SUMOReal vLKW
SUMOReal qLKW
void parseLine(const std::string &line)
Parses the contents of the line.