Stride Reference Manual  - generated for commit 9643b11
GeoGridProtoReader.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, 2019Jan Broeckhove and Bistromatics group.
14  */
15 
16 #include "GeoGridProtoReader.h"
17 
18 #include "geogrid.pb.h"
19 #include "geopop/GeoGrid.h"
20 #include "pop/Person.h"
21 #include "pop/Population.h"
22 
23 #include <iostream>
24 #include <stdexcept>
25 
26 namespace geopop {
27 
28 using namespace std;
29 using namespace stride::ContactType;
30 
31 GeoGridProtoReader::GeoGridProtoReader(unique_ptr<istream> inputStream, stride::Population* pop)
32  : GeoGridReader(move(inputStream), pop)
33 {
34 }
35 
37 {
38  proto::GeoGrid protoGrid;
39  if (!protoGrid.ParseFromIstream(m_inputStream.get())) {
40  throw runtime_error("Failed to parse Proto file");
41  }
42  auto& geoGrid = m_population->RefGeoGrid();
43 
44  for (int idx = 0; idx < protoGrid.persons_size(); idx++) {
45  const proto::GeoGrid_Person& protoPerson = protoGrid.persons(idx);
46  const auto person = ParsePerson(protoPerson);
47  m_people[person->GetId()] = person;
48  }
49 
50  for (int idx = 0; idx < protoGrid.locations_size(); idx++) {
51  const proto::GeoGrid_Location& protoLocation = protoGrid.locations(idx);
52  auto loc = ParseLocation(protoLocation);
53  geoGrid.AddLocation(move(loc));
54  }
55 
56  AddCommutes(geoGrid);
57  m_people.clear();
58  m_commutes.clear();
59 }
60 
61 void GeoGridProtoReader::ParseContactPools(shared_ptr<Location> loc,
62  const proto::GeoGrid_Location_ContactPools& protoContactPools)
63 {
64  const auto protoType = protoContactPools.type();
65 
66  static const map<proto::GeoGrid_Location_ContactPools_Type, Id> types = {
73 
74  const auto typeId = types.at(protoType);
75 
76  for (int idx = 0; idx < protoContactPools.pools_size(); idx++) {
77  const proto::GeoGrid_Location_ContactPools_ContactPool& protoContactPool = protoContactPools.pools(idx);
78  ParseContactPool(loc, protoContactPool, typeId);
79  }
80 }
81 
83 {
84  return {protoCoordinate.longitude(), protoCoordinate.latitude()};
85 }
86 
87 void GeoGridProtoReader::ParseContactPool(shared_ptr<Location> loc,
89  Id type)
90 {
91  // Don't use the id of the ContactPool but the let the Population create an id
92  auto result = m_population->RefPoolSys().CreateContactPool(type);
93  loc->RefPools(type).emplace_back(result);
94 
95  for (int idx = 0; idx < protoContactPool.people_size(); idx++) {
96  const auto person_id = static_cast<unsigned int>(protoContactPool.people(idx));
97  const auto person = m_people.at(person_id);
98  result->AddMember(person);
99  // Update original pool id with new pool id used in the population
100  person->SetPoolId(type, static_cast<unsigned int>(result->GetId()));
101  }
102 }
103 
104 shared_ptr<Location> GeoGridProtoReader::ParseLocation(const proto::GeoGrid_Location& protoLocation)
105 {
106  const auto id = protoLocation.id();
107  const auto& name = protoLocation.name();
108  const auto province = protoLocation.province();
109  const auto population = protoLocation.population();
110  const auto& coordinate = ParseCoordinate(protoLocation.coordinate());
111 
112  auto loc = make_shared<Location>(id, province, coordinate, name, population);
113 
114  for (int idx = 0; idx < protoLocation.contactpools_size(); idx++) {
115  const proto::GeoGrid_Location_ContactPools& protoPools = protoLocation.contactpools(idx);
116  ParseContactPools(loc, protoPools);
117  }
118 
119  for (int idx = 0; idx < protoLocation.commutes_size(); idx++) {
120  const proto::GeoGrid_Location_Commute& commute = protoLocation.commutes(idx);
121  m_commutes.emplace_back(make_tuple(id, commute.to(), commute.proportion()));
122  }
123 
124  return loc;
125 }
126 
128 {
129  const auto id = static_cast<unsigned int>(protoPerson.id());
130  return m_population->CreatePerson(id, protoPerson.age(), 0, 0, 0, 0, 0, 0);
131 }
132 
133 } // namespace geopop
Header file for the Person class.
void AddCommutes(GeoGrid &geoGrid)
Add the commutes that were found to their respective Locations symmetrically.
void ParseContactPools(std::shared_ptr< Location > loc, const proto::GeoGrid_Location_ContactPools &protoContactPools)
Create ContactPools based on protobuf ContactPools info.
::google::protobuf::int64 population() const
Definition: geogrid.pb.h:1349
::proto::GeoGrid_Location_ContactPools_Type type() const
Definition: geogrid.pb.h:1188
int contactpools_size() const
Definition: geogrid.pb.h:1411
stride::Population * m_population
Population to use in the GeoGrid may be nullptr.
Definition: GeoGridReader.h:67
void Read() override
Actually perform the read and return the GeoGrid.
int locations_size() const
Definition: geogrid.pb.h:1507
std::shared_ptr< Location > ParseLocation(const proto::GeoGrid_Location &protoLocation)
Create a Location based on protobuf Location info.
const ::proto::GeoGrid_Location & locations(int index) const
Definition: geogrid.pb.h:1522
::google::protobuf::int64 province() const
Definition: geogrid.pb.h:1335
stride::Person * ParsePerson(const proto::GeoGrid_Person &person)
Create a Person based on protobuf Person info.
::google::protobuf::int64 id() const
Definition: geogrid.pb.h:1478
geopop::GeoGrid & RefGeoGrid()
Reference the GeoGrid associated with this population (may be a nullptr).
Definition: Population.h:83
GeoGridProtoReader(std::unique_ptr< std::istream > inputStream, stride::Population *pop)
Construct the GeoGridJSONReader with the istream which contains the Protobuf info.
::google::protobuf::int64 age() const
Definition: geogrid.pb.h:1492
STL namespace.
ContactPoolSys & RefPoolSys()
Reference the ContactPoolSys of the Population.
Definition: Population.h:80
const ::proto::GeoGrid_Person & persons(int index) const
Definition: geogrid.pb.h:1552
void ParseContactPool(std::shared_ptr< Location > loc, const proto::GeoGrid_Location_ContactPools_ContactPool &protoContactPool, stride::ContactType::Id typeId)
Create a ContactPool based on the provided protobuf ContactPool.
Key Data structure: container for (a) all individuals in the population (b) the ContactPoolSys wchich...
Definition: Population.h:47
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
std::vector< std::tuple< unsigned int, unsigned int, double > > m_commutes
Definition: GeoGridReader.h:64
Person * CreatePerson(unsigned int id, double age, unsigned int householdId, unsigned int k12SchoolId, unsigned int collegeId, unsigned int workId, unsigned int primaryCommunityId, unsigned int secondaryCommunityId)
Create Person in the population.
Definition: Population.cpp:108
std::unique_ptr< std::istream > m_inputStream
File to read.
Definition: GeoGridReader.h:66
::google::protobuf::int64 people(int index) const
Definition: geogrid.pb.h:1143
An abstract base class for creating a GeoGrid that was read from a file, can be implemented using mul...
Definition: GeoGridReader.h:37
Coordinate ParseCoordinate(const proto::GeoGrid_Location_Coordinate &protoCoordinate)
Create a Coordinate based on the provided protobuf Coordinate.
int commutes_size() const
Definition: geogrid.pb.h:1441
const ::proto::GeoGrid_Location_ContactPools_ContactPool & pools(int index) const
Definition: geogrid.pb.h:1214
boost::geometry::model::point< double, 2, boost::geometry::cs::geographic< boost::geometry::degree >> Coordinate
Definition: Coordinate.h:23
int persons_size() const
Definition: geogrid.pb.h:1537
Header file for the core Population class.
std::map< unsigned int, stride::Person * > m_people
< Store the persons (id->person) that were found while loping over the ContactPools.
Definition: GeoGridReader.h:61
::google::protobuf::int64 id() const
Definition: geogrid.pb.h:1268
const ::proto::GeoGrid_Location_Coordinate & coordinate() const
Definition: geogrid.pb.h:1369
ContactPool * CreateContactPool(ContactType::Id typeId)
Create a new contact pool of a given type.
Store and handle person data.
Definition: Person.h:34
const ::proto::GeoGrid_Location_Commute & commutes(int index) const
Definition: geogrid.pb.h:1456
const ::proto::GeoGrid_Location_ContactPools & contactpools(int index) const
Definition: geogrid.pb.h:1426
const ::std::string & name() const
Definition: geogrid.pb.h:1282
::google::protobuf::int64 to() const
Definition: geogrid.pb.h:1236
Namespace to manage types of ContactPool.
Definition: ContactType.cpp:28