Stride Reference Manual  - generated for commit 9643b11
LocationsCSVReader.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 "LocationsCSVReader.h"
17 
18 #include "util/CSV.h"
19 #include "util/CSVRow.h"
20 
21 namespace geopop {
22 
23 using namespace std;
24 using namespace stride::util;
25 
26 LocationsCSVReader::LocationsCSVReader(unique_ptr<istream> inputStream) : LocationsReader(move(inputStream)) {}
27 
29 {
30 
31  vector<pair<shared_ptr<Location>, int>> locations;
32 
33  CSV reader(*(m_inputStream.get()));
34  auto totalPopulation = 0U;
35 
36  for (const CSVRow& row : reader) {
37  // In file: id,province,population,x_coord,y_coord,latitude,longitude,name
38  // Ignore x and y, we do not use them,
39  // In Coordinate constructor switch order of latitude and longitude
40  const auto loc = make_shared<Location>(row.GetValue<int>(0), row.GetValue<int>(1),
41  Coordinate(row.GetValue<double>(6), row.GetValue<double>(5)),
42  row.GetValue(7));
43  geoGrid.AddLocation(loc);
44  locations.emplace_back(loc, row.GetValue<int>(2));
45  totalPopulation += row.GetValue<int>(2);
46  }
47 
48  for (const auto& l : locations) {
49  l.first->SetPopFraction(static_cast<double>(l.second) / static_cast<double>(totalPopulation));
50  }
51 }
52 
53 } // namespace geopop
Row in CSV file.
Definition: CSVRow.h:36
Create an abstract Reader which fills a GeoGrid with the cities found in file.
Utilities for the project.
void AddLocation(std::shared_ptr< Location > location)
Adds a location to this GeoGrid.
Definition: GeoGrid.cpp:39
void FillGeoGrid(GeoGrid &geoGrid) const override
Add the locations read to the GeoGrid.
STL namespace.
A Geographic grid of simulation region contains Locations that in turn contain an index to the Contac...
Definition: GeoGrid.h:41
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
boost::geometry::model::point< double, 2, boost::geometry::cs::geographic< boost::geometry::degree >> Coordinate
Definition: Coordinate.h:23
std::unique_ptr< std::istream > m_inputStream
The istream with the file content.
A collection of CSVRow elements.
Definition: CSV.h:46
Header file of base class for config that needs to be read from a file.
Header file of base class for config that needs to be read from a file.
LocationsCSVReader(std::unique_ptr< std::istream > inputStream)
Construct the CitiesCSVReader that reads istream CSV data.