Bridges-C++  3.4.5-dev1-6-g935685a
Bridges(C++ API)
USMap.h
Go to the documentation of this file.
1 #ifndef US_MAP_H
2 
3 #define US_MAP_H
4 
5 #include <math.h>
6 #include <cmath>
7 
8 #include <algorithm>
9 
10 #include <string>
11 #include <vector>
12 
13 using std::string;
14 using std::vector;
15 
16 #include "DataStructure.h"
17 #include "Map.h"
18 #include "./data_src/USState.h"
19 #include "./data_src/USCounty.h"
20 #include <JSONutil.h>
21 
22 #include <rapidjson/document.h>
23 #include <rapidjson/stringbuffer.h>
24 #include <rapidjson/writer.h>
25 
26 namespace bridges {
27  namespace datastructure {
28 
29  using namespace bridges::datastructure;
31 
52  class USMap : public Map, public DataStructure {
53  private:
54  vector<string> state_names;
55  vector<USState> state_data;
56 
57  virtual const string getDataStructureRepresentation ()
58  const override {
59  // virtual const rapidjson::Document getDataStructureRepresentation ()
60  // const override {
61  // using namespace rapidjson;
63 
64  // Document d;
65  // Value key, value;
66  // d.setObject();
67  // key.SetString("mapdummy");
68  // value.SetBool(true);
69  // d.AddMember(key, value, d.GetAllocator());
70 
71  // return d;
72  return JSONencode("mapdummy") + COLON + JSONencode(true) + CLOSE_CURLY;
73  }
74  public:
80  const string getProjection() const override {
81  return "albersusa";
82  }
88  const bool getOverlay() const override {
89  return true;
90  }
91 
97  virtual const string getMapRepresentation () const override {
98  // generates a JSON of the states with county information
99  string map_str = OPEN_BOX;
101  for (auto& st : state_data) {
102  map_str += OPEN_CURLY +
103  QUOTE + "_state_name" + QUOTE + COLON +
104  JSONencode(st.getStateName()) + COMMA +
105  QUOTE + "_stroke_color" + QUOTE + COLON
106  + st.getStrokeColor().getCSSRepresentation() + COMMA +
107  QUOTE + "_stroke_width" + QUOTE + COLON +
108  JSONencode(st.getStrokeWidth()) + COMMA +
109  QUOTE + "_fill_color" + QUOTE + COLON +
110  st.getFillColor().getCSSRepresentation() + COMMA +
111  QUOTE + "_view_counties" + QUOTE + COLON +
112  JSONencode(st.getViewCountiesFlag()) + COMMA +
113  QUOTE + "_counties" + QUOTE + COLON;
114 
115  // get all the counties
116  map_str += OPEN_BOX; // array of counties
117  for (auto& c : st.getCounties()) {
118  map_str += OPEN_CURLY +
119  QUOTE + "_geoid" + QUOTE + COLON +
120  JSONencode(c.second.getGeoId()) + COMMA +
121  QUOTE + "_fips_code" + QUOTE + COLON +
122  JSONencode(c.second.getFipsCode()) + COMMA +
123  QUOTE + "_county_name" + QUOTE + COLON +
124  JSONencode(c.second.getCountyName()) + COMMA +
125  QUOTE + "_state_name" + QUOTE + COLON +
126  JSONencode(c.second.getStateName()) + COMMA +
127  QUOTE + "_stroke_color" + QUOTE + COLON +
128  c.second.getStrokeColor().getCSSRepresentation() + COMMA +
129  QUOTE + "_stroke_width" + QUOTE + COLON +
130  JSONencode(c.second.getStrokeWidth()) + COMMA +
131  QUOTE + "_fill_color" + QUOTE + COLON +
132  c.second.getFillColor().getCSSRepresentation() + COMMA +
133  QUOTE + "_hide" + QUOTE + COLON +
134  JSONencode(c.second.getHideFlag()) +
135  CLOSE_CURLY + COMMA;
136  }
137  // remove last comma
138  if (st.getCounties().size()) // case where counties are on
139  map_str = map_str.substr(0, map_str.size() - 1);
140  map_str += CLOSE_BOX + CLOSE_CURLY + COMMA;
141  }
142  // close the states array
143  map_str = map_str.substr(0, map_str.size() - 1) + CLOSE_BOX;
144  return map_str;
145  }
146  /*
147  virtual const string getMapRepresentation () const override{
148  using namespace rapidjson;
149  StringBuffer sb;
150  Writer<StringBuffer> writer(sb);
151  writer.StartArray(); // start of states --array
152  for (auto& st : state_data) {
153  writer.StartObject(); // start of this state
154  writer.Key("_state_name"); writer.String(st.getStateName().c_str());
155  writer.Key("_stroke_color"); writer.String(st.getStrokeColor().c_str());
156  writer.Key("_fill_color"); writer.String(st.getFillColor().c_str());
157  writer.Key("_stroke_width"); writer.Double(st.getStrokeWidth());
158  writer.Key("_view_counties"); writer.Bool(st.getViewCountiesFlag());
159  writer.Key("_counties");
160  // get all the counties
161  writer.StartArray(); // start of counties for this state
162  for (auto& c : st.getCounties()) {
163  writer.StartObject(); // start of this county
164  writer.Key("_geoid"); writer.String(c.second.getGeoId().c_str());
165  writer.Key("_fips_code"); writer.String(c.second.getFipsCode().c_str());
166  writer.Key("_county_name"); writer.String(c.second.getCountyName().c_str());
167  writer.Key("_state_name"); writer.String(c.second.getStateName().c_str());
168  writer.Key("_stroke_color"); writer.String(c.second.getStrokeColor().c_str());
169  writer.Key("_stroke_width"); writer.Double(c.second.getStrokeWidth());
170  writer.Key("_fill_color"); writer.String(c.second.getFillColor().c_str());
171  writer.Key("_hide"); writer.Bool(c.second.getHideFlag());
172  writer.EndObject(); // end of this county
173  }
174  writer.EndArray(); // end of counties for this state
175  writer.EndObject(); // end of this state
176  }
177  writer.EndArray(); // end of states
178 
179  return sb.GetString();
180  }
181  virtual const string getMapRepresentation () const override{
182  using namespace rapidjson;
183  Document d;
184  d.SetObject();
185  Document::AllocatorType& allocator = d.GetAllocator();
186  Value key, value;
187  Value st_array(kArrayType);
188  for (auto& st : state_data) {
189  Value st_obj;
190  st_obj.SetObject();
191  key.SetString("_state_name", allocator);
192  value.SetString(st.getStateName().c_str(), allocator);
193  st_obj.AddMember(key, value, allocator);
194 
195  key.SetString("_stroke_color", allocator);
196  value.SetString(st.getStrokeColor().c_str(), allocator);
197  st_obj.AddMember(key, value, allocator);
198 
199  key.SetString("_fill_color", allocator);
200  value.SetString(st.getFillColor().c_str(), allocator);
201  st_obj.AddMember(key, value, allocator);
202 
203  key.SetString("stroke_width", allocator);
204  value.SetDouble(st.getStrokeWidth());
205  st_obj.AddMember(key, value, allocator);
206 
207  key.SetString("_view_counties", allocator);
208  value.SetBool(st.getViewCountiesFlag());
209  st_obj.AddMember(key, value, allocator);
210 
211  // put counties into an array
212  Value ct_array(kArrayType);
213  for (auto& c : st.getCounties()) {
214  Value c_obj;
215  c_obj.SetObject();
216 
217  key.SetString("_geoid", allocator);
218  value.SetString(c.second.getGeoId().c_str(), allocator);
219  c_obj.AddMember(key, value, allocator);
220 
221  key.SetString("_fips_code", allocator);
222  value.SetString(c.second.getFipsCode().c_str(), allocator);
223  c_obj.AddMember(key, value, allocator);
224 
225  key.SetString("_county_name", allocator);
226  value.SetString(c.second.getCountyName().c_str(), allocator);
227  c_obj.AddMember(key, value, allocator);
228 
229  key.SetString("_state_name", allocator);
230  value.SetString(c.second.getStateName().c_str(), allocator);
231  c_obj.AddMember(key, value, allocator);
232 
233  key.SetString("_stroke_color", allocator);
234  value.SetString(c.second.getStrokeColor().c_str(), allocator);
235  c_obj.AddMember(key, value, allocator);
236 
237  key.SetString("_stroke_width", allocator);
238  value.SetDouble(c.second.getStrokeWidth());
239  c_obj.AddMember(key, value, allocator);
240 
241  key.SetString("_fill_color", allocator);
242  value.SetString(c.second.getFillColor().c_str(), allocator);
243  c_obj.AddMember(key, value, allocator);
244 
245  key.SetString("_hide", allocator);
246  value.SetBool(c.second.getHideFlag());
247  c_obj.AddMember(key, value, allocator);
248 
249  ct_array.PushBack(c_obj, allocator);
250  }
251  st_obj.AddMember("counties", ct_array, allocator);
252  st_array.PushBack(st_obj, allocator);
253  d.AddMember("states", st_array, allocator);
254  }
255 
256  // convert to string
257  StringBuffer sb;
258  Writer<StringBuffer> writer(sb);
259  d["states"].Accept(writer);
260 
261 
262  string s = sb.GetString();
263  string s2 = s.substr(10, s.size()-11);
264  cout << "Map String(JSON) " << s << endl;
265 
266  return s;
267  }
268  */
269  public:
270  /*
271  * @brief Constructs a US Map object with map data
272  *
273  * @param st_data data containg state/county information
274  */
275  USMap(vector<USState> st_data) {
276  state_data = st_data;
277  }
278 
279  /*
280  * @brief This function returns the current state/county data
281  * in the US map object.
282  *
283  * @return list of state information
284  */
285  vector<USState>& getMapData() {
286  return state_data;
287  }
288 
289  /*
290  * @brief This function sets the state/county data
291  * in the US map object.
292  *
293  * @param list of state information
294  */
295  void setStateData(vector<USState> st_data) {
296  state_data = st_data;
297  }
298 
299 
300  /*
301  *
302  * @brief This function gets the data structure type for
303  * the US map,
304  *
305  */
306  virtual const string getDStype() const override {
307  return "us_map";
308  }
309  };
310  }
311 }
312 #endif
Definition: USState.h:26
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
Abstract class for Map API.
Definition: Map.h:16
This class provides an API to building, displaying and manipulating US maps and counties in BRIDGES.
Definition: USMap.h:52
virtual const string getMapRepresentation() const override
Generates the JSON representation of the US map.
Definition: USMap.h:97
void setStateData(vector< USState > st_data)
Definition: USMap.h:295
vector< USState > & getMapData()
Definition: USMap.h:285
virtual const string getDStype() const override
Definition: USMap.h:306
const string getProjection() const override
Gets the type of map projection. For US map we currently use albersusa.
Definition: USMap.h:80
USMap(vector< USState > st_data)
Definition: USMap.h:275
const bool getOverlay() const override
Gets the map overlay flag.
Definition: USMap.h:88
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
Definition: Array.h:9
Support for drawing Bar charts.
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string OPEN_CURLY
Definition: DataStructure.h:53
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50