SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-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 // 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 SUMOTime time = TplConvert::_2int((myLineHandler.get("time").c_str())) * myTimeScale - myTimeOffset;
83  if (time < myStartTime || time >= myEndTime) {
86  WRITE_WARNING("At least one value lies beyond given time boundaries.");
87  }
88  continue;
89  }
90  FlowDef fd;
91  fd.isLKW = 0;
92  fd.qPKW = TplConvert::_2SUMOReal(myLineHandler.get("qpkw").c_str());
93  fd.vPKW = 0;
94  if (myLineHandler.know("vPKW")) {
95  fd.vPKW = TplConvert::_2SUMOReal(myLineHandler.get("vpkw").c_str());
96  }
97  fd.qLKW = 0;
98  if (myLineHandler.know("qLKW")) {
99  fd.qLKW = TplConvert::_2SUMOReal(myLineHandler.get("qlkw").c_str());
100  }
101  fd.vLKW = 0;
102  if (myLineHandler.know("vLKW")) {
103  fd.vLKW = TplConvert::_2SUMOReal(myLineHandler.get("vlkw").c_str());
104  }
105  if (fd.qLKW < 0) {
106  fd.qLKW = 0;
107  }
108  if (fd.qPKW < 0) {
109  fd.qPKW = 0;
110  }
111  myStorage.addFlow(detName, time, fd);
114  WRITE_WARNING("At least one line does not contain the correct number of columns.");
115  }
116  continue;
117  } catch (UnknownElement&) {} catch (OutOfBoundsException&) {} catch (NumberFormatException&) {}
118  throw ProcessError("The detector-flow-file '" + lr.getFileName() + "' is corrupt;\n"
119  + " The following values must be supplied : 'Detector', 'Time', 'qPKW'\n"
120  + " The according column names must be given in the first line of the file.");
121  }
122 }
123 
124 
125 /****************************************************************************/
126 
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:196
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
static int _2int(const E *const data)
Definition: TplConvert.h:114
void addFlow(const std::string &detector_id, SUMOTime timestamp, const FlowDef &fd)
NamedColumnsParser myLineHandler
The value extractor.
SUMOReal qPKW
~RODFDetFlowLoader()
Destructor.
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.