SUMO - Simulation of Urban MObility
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-2017 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 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
49  RODFDetectorFlows& into,
50  SUMOTime startTime, SUMOTime endTime,
51  SUMOTime timeOffset, SUMOTime timeScale)
52  : myStorage(into), myTimeOffset(timeOffset), myTimeScale(timeScale),
53  myStartTime(startTime), myEndTime(endTime), myDetectorContainer(dets),
54  myHaveWarnedAboutOverridingBoundaries(false), myHaveWarnedAboutPartialDefs(false) {}
55 
56 
57 
59 
60 
61 void
62 RODFDetFlowLoader::read(const std::string& file) {
63  LineReader lr(file);
64  // parse first line
65  myLineHandler.reinit(lr.readLine(), ";", ";", true, true);
66  // parse values
67  while (lr.hasMore()) {
68  std::string line = lr.readLine();
69  if (line.find(';') == std::string::npos) {
70  continue;
71  }
73  try {
74  std::string detName = myLineHandler.get("detector");
75  if (!myDetectorContainer.knows(detName)) {
76  continue;
77  }
78  const double parsedTime = TplConvert::_2double((myLineHandler.get("time").c_str())) * myTimeScale - myTimeOffset;
79  // parsing as float to handle values which would cause int overflow
80  if (parsedTime < myStartTime || parsedTime >= myEndTime) {
83  WRITE_WARNING("At least one value lies beyond given time boundaries.");
84  }
85  continue;
86  }
87  const SUMOTime time = (SUMOTime)(parsedTime + .5);
88  FlowDef fd;
89  fd.isLKW = 0;
90  fd.qPKW = TplConvert::_2double(myLineHandler.get("qpkw").c_str());
91  fd.vPKW = 0;
92  if (myLineHandler.know("vPKW")) {
93  fd.vPKW = TplConvert::_2double(myLineHandler.get("vpkw").c_str());
94  }
95  fd.qLKW = 0;
96  if (myLineHandler.know("qLKW")) {
97  fd.qLKW = TplConvert::_2double(myLineHandler.get("qlkw").c_str());
98  }
99  fd.vLKW = 0;
100  if (myLineHandler.know("vLKW")) {
101  fd.vLKW = TplConvert::_2double(myLineHandler.get("vlkw").c_str());
102  }
103  if (fd.qLKW < 0) {
104  fd.qLKW = 0;
105  }
106  if (fd.qPKW < 0) {
107  fd.qPKW = 0;
108  }
109  myStorage.addFlow(detName, time, fd);
112  WRITE_WARNING("At least one line does not contain the correct number of columns.");
113  }
114  continue;
115  } catch (UnknownElement&) {} catch (OutOfBoundsException&) {} catch (NumberFormatException&) {}
116  throw ProcessError("The detector-flow-file '" + lr.getFileName() + "' is corrupt;\n"
117  + " The following values must be supplied : 'Detector', 'Time', 'qPKW'\n"
118  + " The according column names must be given in the first line of the file.");
119  }
120 }
121 
122 
123 /****************************************************************************/
124 
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:76
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.
RODFDetFlowLoader(const RODFDetectorCon &dets, RODFDetectorFlows &into, SUMOTime startTime, SUMOTime endTime, SUMOTime timeOffset, SUMOTime timeScale)
Constructor.
A container for flows.
A container for RODFDetectors.
Definition: RODFDetector.h:228
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
double vPKW
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.
double vLKW
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.
double qPKW
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:179
bool know(const std::string &name) const
Returns the information whether the named column is known.
void addFlow(const std::string &detector_id, SUMOTime timestamp, const FlowDef &fd)
NamedColumnsParser myLineHandler
The value extractor.
double isLKW
double qLKW
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
~RODFDetFlowLoader()
Destructor.
long long int SUMOTime
Definition: TraCIDefs.h:52
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:60
void parseLine(const std::string &line)
Parses the contents of the line.