Stride Reference Manual  - generated for commit 9643b11
CommutesCSVReader.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #include "CommutesCSVReader.h"
17 
18 #include "geopop/GeoGrid.h"
19 #include "geopop/Location.h"
20 #include "util/CSV.h"
21 #include "util/Exception.h"
22 
23 #include <map>
24 
25 namespace geopop {
26 
27 using namespace std;
28 using namespace stride::util;
29 
30 CommutesCSVReader::CommutesCSVReader(unique_ptr<istream> inputStream) : CommutesReader(move(inputStream)) {}
31 
33 {
34  // flanders_commuting format
35  // kolom: stad van vertrek (headers = id)
36  // rij: stad van aankomst (volgorde = volgorde van kolommen = id).
37  CSV reader(*(m_inputStream.get()));
38 
39  // represents the location id for column x
40  vector<unsigned int> header;
41 
42  for (const string& label : reader.GetLabels()) {
43  header.push_back(static_cast<unsigned int>(stoi(label.substr(3))));
44  }
45 
46  const auto columnCount = static_cast<unsigned int>(reader.GetColumnCount());
47  map<unsigned int, unsigned int> sizes; // indexed by header/row id
48 
49  // Since columns represent the "from city" and the proportion is calculated using the from city,
50  // the total population of a city is calculated using the values found in the columns.
51  for (const CSVRow& row : reader) {
52  for (unsigned int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
53  sizes[columnIndex] += row.GetValue<int>(columnIndex);
54  }
55  }
56 
57  auto rowIndex = 0U;
58  for (const CSVRow& row : reader) {
59  for (auto columnIndex = 0U; columnIndex < columnCount; columnIndex++) {
60  auto abs = row.GetValue<double>(columnIndex);
61  if (abs != 0 && columnIndex != rowIndex) {
62  const auto& locFrom = geoGrid.GetById(header[columnIndex]);
63  const auto& locTo = geoGrid.GetById(header[rowIndex]);
64  const auto& total = sizes[columnIndex];
65  double proportion = abs / total;
66 
67  if (proportion < 0 || proportion > 1) {
68  throw Exception("Proportion of commutes from " + to_string(locFrom->GetID()) +
69  " to " + to_string(locTo->GetID()) +
70  " is invalid (0 <= proportion <= 1)");
71  }
72  locFrom->AddOutgoingCommute(locTo, proportion);
73  locTo->AddIncomingCommute(locFrom, proportion);
74  }
75  }
76  rowIndex++;
77  }
78 }
79 
80 } // namespace geopop
Row in CSV file.
Definition: CSVRow.h:36
const std::vector< std::string > & GetLabels() const
Definition: CSV.cpp:163
Utilities for the project.
size_t GetColumnCount() const
Number of columns in the CSV.
Definition: CSV.h:101
CommutesCSVReader(std::unique_ptr< std::istream > istream)
Construct the CommutesCSVReader with an istream containing the CSV data.
STL namespace.
A Geographic grid of simulation region contains Locations that in turn contain an index to the Contac...
Definition: GeoGrid.h:41
Basic exception class: needed to prevent clang-tidy warning "thrown exception type is not nothrow cop...
Definition: Exception.h:28
void FillGeoGrid(GeoGrid &geoGrid) const override
Add the found Commutes to the provided GeoGrid.
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
std::shared_ptr< Location > GetById(unsigned int id) const
Gets a Location by Id and check if the Id exists.
Definition: GeoGrid.h:60
A collection of CSVRow elements.
Definition: CSV.h:46
Header file of base class for config that needs to be read from a file.
Create an abstract Reader that fills a GeoGrid with the commute info from file.
std::unique_ptr< std::istream > m_inputStream
The istream with the file content.