Stride Reference Manual  - generated for commit 9643b11
GeoGridJSONWriter.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, 2019, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #include "GeoGridJSONWriter.h"
17 
18 #include "contact/ContactPool.h"
19 #include "geopop/ContactCenter.h"
20 #include "geopop/GeoGrid.h"
21 #include "pop/Person.h"
22 
23 #include <boost/property_tree/json_parser.hpp>
24 #include <iostream>
25 #include <omp.h>
26 
27 namespace geopop {
28 
29 using namespace std;
30 using namespace stride;
31 using namespace stride::ContactType;
32 using namespace boost::property_tree;
33 
34 GeoGridJSONWriter::GeoGridJSONWriter() : m_persons_found() {}
35 
36 void GeoGridJSONWriter::Write(GeoGrid& geoGrid, ostream& stream)
37 {
38  ptree root;
39  ptree locations;
40  ptree persons;
41 
42  for (const auto& location : geoGrid) {
43  pair<string, ptree> child;
44  child = make_pair("", WriteLocation(*location));
45  locations.push_back(move(child));
46  }
47  root.add_child("locations", locations);
48 
49  for (const auto& person : m_persons_found) {
50  pair<string, ptree> child;
51  child = make_pair("", WritePerson(person));
52  persons.push_back(move(child));
53  }
54  root.add_child("persons", persons);
55 
56  m_persons_found.clear();
57  write_json(stream, root);
58 }
59 
60 ptree GeoGridJSONWriter::WriteContactCenter(shared_ptr<ContactCenter> contactCenter)
61 {
62  ptree contactCenter_root;
63  contactCenter_root.put("id", contactCenter->GetId());
64  contactCenter_root.put("type", ToString(contactCenter->GetContactPoolType()));
65  ptree pools;
66 
67  for (const auto& pool : *contactCenter) {
68  pair<string, ptree> child;
69  child = make_pair("", WriteContactPool(pool));
70  pools.push_back(move(child));
71  }
72 
73  contactCenter_root.add_child("pools", pools);
74  return contactCenter_root;
75 }
76 
78 {
79  ptree contactPool_root;
80  contactPool_root.put("id", contactPool->GetId());
81  ptree people;
82  for (auto person : *contactPool) {
83  ptree person_root;
84  m_persons_found.insert(person);
85  person_root.put("", person->GetId());
86  people.push_back(make_pair("", person_root));
87  }
88  contactPool_root.add_child("people", people);
89  return contactPool_root;
90 }
91 
93 {
94  ptree coordinate_root;
95  coordinate_root.put("longitude", boost::geometry::get<0>(coordinate));
96  coordinate_root.put("latitude", boost::geometry::get<1>(coordinate));
97  return coordinate_root;
98 }
99 
101 {
102  ptree location_root;
103  location_root.put("id", location.GetID());
104  location_root.put("name", location.GetName());
105  location_root.put("province", location.GetProvince());
106  location_root.put("population", location.GetPopCount());
107  location_root.add_child("coordinate", WriteCoordinate(location.GetCoordinate()));
108 
109  auto commutes = location.CRefOutgoingCommutes();
110  if (!commutes.empty()) {
111  ptree commutes_root;
112  for (auto commute_pair : commutes) {
113  commutes_root.put(to_string(commute_pair.first->GetID()), commute_pair.second);
114  }
115  location_root.add_child("commutes", commutes_root);
116  }
117 
118  ptree contactCenters;
119  for (Id typ : IdList) {
120  for (const auto& c : location.CRefCenters(typ)) {
121  pair<string, ptree> child;
122  child = make_pair("", WriteContactCenter(c));
123  contactCenters.push_back(move(child));
124  }
125  }
126  location_root.add_child("contactCenters", contactCenters);
127 
128  return location_root;
129 }
130 
132 {
133  ptree person_root;
134  person_root.put("id", person->GetId());
135  person_root.put("age", person->GetAge());
136  person_root.put("K12School", person->GetPoolId(Id::K12School));
137  person_root.put("College", person->GetPoolId(Id::College));
138  person_root.put("Household", person->GetPoolId(Id::Household));
139  person_root.put("Workplace", person->GetPoolId(Id::Workplace));
140  person_root.put("PrimaryCommunity", person->GetPoolId(Id::PrimaryCommunity));
141  person_root.put("SecondaryCommunity", person->GetPoolId(Id::SecondaryCommunity));
142  return person_root;
143 }
144 
145 } // namespace geopop
unsigned int GetID() const
Gets ID of this Location.
Definition: Location.h:63
boost::property_tree::ptree WriteLocation(const Location &location)
Create a Boost Property Tree containing all info needed to reconstruct a Location.
std::string GetName() const
Gets the name.
Definition: Location.h:72
Header file for the Person class.
A group of Persons that potentially have contacts with one another.
Definition: ContactPool.h:38
Header for the core ContactPool class.
constexpr std::initializer_list< Id > IdList
To allow iteration over the type ids.
Definition: ContactType.h:75
std::set< stride::Person * > m_persons_found
The persons found when looping over the ContactPools.
unsigned int GetPoolId(const ContactType::Id &poolType) const
Get ID of contactpool_type.
Definition: Person.h:65
string ToString(Id l)
Converts a LogMode value to corresponding name.
unsigned int GetProvince() const
Gets the province.
Definition: Location.h:81
unsigned int GetId() const
Get the id.
Definition: Person.h:62
boost::property_tree::ptree WritePerson(stride::Person *person)
Create a Boost Property Tree containing all info needed to reconstruct a Person.
unsigned int GetId() const
Get the pool id.
Definition: ContactPool.h:51
STL namespace.
float GetAge() const
Get the age.
Definition: Person.h:53
boost::property_tree::ptree WriteContactCenter(std::shared_ptr< ContactCenter > contactCenter)
Create a Boost Property Tree containing all info needed to reconstruct a ContactCenter.
A Geographic grid of simulation region contains Locations that in turn contain an index to the Contac...
Definition: GeoGrid.h:41
boost::property_tree::ptree WriteCoordinate(const Coordinate &coordinate)
Create a Boost Property Tree containing all info needed to reconstruct a Coordinate.
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
const Coordinate GetCoordinate() const
Gets the Coordinate of this Location.
Definition: Location.h:60
void Write(GeoGrid &geoGrid, std::ostream &stream) override
Write the provided GeoGrid to the proved ostream in JSON format.
boost::geometry::model::point< double, 2, boost::geometry::cs::geographic< boost::geometry::degree >> Coordinate
Definition: Coordinate.h:23
unsigned int GetPopCount() const
Gets the absolute population.
Definition: Location.h:78
boost::property_tree::ptree WriteContactPool(stride::ContactPool *contactPool)
Create a Boost Property Tree containing all info needed to reconstruct a ContactPool.
GeoGridJSONWriter()
Construct the GeoGridJSONWriter.
Location for use within the GeoGrid, contains Coordinate and index to ContactPools.
Definition: Location.h:41
Store and handle person data.
Definition: Person.h:34
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Namespace to manage types of ContactPool.
Definition: ContactType.cpp:28
const std::vector< std::pair< Location *, double > > & CRefOutgoingCommutes() const
References outgoing commute Locations + fraction of commutes to that Location.
Definition: Location.h:149