Bridges-C++  3.1.1
Bridges(C++API)
OSMData.h
Go to the documentation of this file.
1 #ifndef OSM_DATA
2 
3 #define OSM_DATA
4 
5 #include <math.h>
6 #include <cmath>
7 
8 
9 #include <algorithm>
10 
11 //should be defined in math.h but VS2017 has a weird behavior here.
12 #ifndef M_PI
13 #define M_PI 3.1415926535897
14 #endif
15 
16 
17 #include <GraphAdjList.h>
18 
19 #include "OSMVertex.h"
20 #include "OSMEdge.h"
21 
22 namespace bridges {
23  namespace dataset {
24  using namespace bridges::datastructure;
34  class OSMData {
35  private:
36 
37  bool debug() const {
38  return false;
39  }
40 
41  string name = "";
42  // data range
43  double latitude_range[2] = {0.0, 0.0},
44  longitude_range[2] = {0.0, 0.0};
45  double cartesian_range_x[2] = {0.0, 0.0},
46  cartesian_range_y[2] = {0.0, 0.0};
47  // vertices
48  vector<OSMVertex> vertices;
49  // edges
50  vector<OSMEdge> edges;
51 
52  static double degreeToRadians(double deg) {
53  return deg * M_PI / 180.;
54  }
55 
56 
57  void recomputeCartesianRange() {
58  cartesian_range_x[0] = 1000000.;
59  cartesian_range_x[1] = -1000000.;
60  cartesian_range_y[0] = 1000000.;
61  cartesian_range_y[1] = -1000000.;
62 
63  OSMVertex v1 (0, latitude_range[0], longitude_range[0]);
64  OSMVertex v2 (1, latitude_range[1], longitude_range[1]);
65  OSMVertex v3 (2, latitude_range[0], longitude_range[1]);
66  OSMVertex v4 (3, latitude_range[1], longitude_range[0]);
67 
68  //the cartesian range will always come from one of the extremities
69  double coords[2];
70  v1.getCartesianCoords(coords);
71 
72  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
73  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
74  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
75  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
76 
77  v2.getCartesianCoords(coords);
78 
79  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
80  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
81  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
82  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
83 
84  v3.getCartesianCoords(coords);
85 
86  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
87  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
88  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
89  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
90 
91  v4.getCartesianCoords(coords);
92 
93  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
94  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
95  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
96  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
97 
98  }
99 
100  public:
101 
108  void setLatLongRange (double *lat_range, double *longit_range) {
109  latitude_range[0] = lat_range[0];
110  latitude_range[1] = lat_range[1];
111  longitude_range[0] = longit_range[0];
112  longitude_range[1] = longit_range[1];
113  recomputeCartesianRange();
114  }
121  void setLatLongRange (double lat_min, double lat_max,
122  double long_min, double long_max) {
123  latitude_range[0] = lat_min;
124  latitude_range[1] = lat_max;
125  longitude_range[0] = long_min;
126  longitude_range[1] = long_max;
127  recomputeCartesianRange();
128  }
129 
142  // vector<OSMVertex> vertices = osm_data->getVertices();
143  // vector<OSMEdge> edges = osm_data->getEdges();
144 
145  int k = 0;
146  double coords[2];
147 
148  double xrange[2], yrange[2];
149  this->getCartesianCoordsRange(xrange, yrange);
150  if (debug()) {
151  cout << "Range(Cartesian):" << xrange[0] << "," << xrange[1] <<
152  yrange[0] << "," << yrange[1] << endl;
153  }
154 
155 
156  double ll[2], ur[2];
157  // translation
158  double tx = xrange[0];
159  double ty = yrange[0];
160 
161  // scale factor -- assume a display of 1000 pixels wide
162  double sx = 1000. / (xrange[1] - xrange[0]);
163  double sy = 1000. / (yrange[1] - yrange[0]);
164 
165  if (debug()) {
166  cout << "Translate:" << tx << "," << ty << endl;
167  cout << "Scale:" << sx << "," << sy << endl;
168  }
169 
170  std::unordered_map<OSMVertex::OSMVertexID, int> vert_map;
171 
172 
173  k = 0;
174  for (int k = 0; k < vertices.size(); k++) {
175  // Preventing multiple vertex inclusion.
176  // Not sure why that would happen, but being safe.
177  if (vert_map.find(vertices[k].getVertexID()) == vert_map.end()) {
178 
179  vert_map[vertices[k].getVertexID()] = k;
180  gr->addVertex(k, vertices[k]);
181 
182  vertices[k].getCartesianCoords(coords);
183  //coords[1] = yrange[1] - (coords[1] - yrange[0]);
184  //double x = (coords[0]-tx)*sx, y = (coords[1]-ty)*sy;
185  double x = coords[0];
186  double y = coords[1];
187  gr->getVertex(k)->getVisualizer()->setLocation( x, y);
188  gr->getVertex(k)->getVisualizer()->setColor(Color("green"));
189  }
190  }
191  for (int k = 0; k < edges.size(); k++) {
192  // std::cout<<edges[k].getEdgeLength()<<std::endl;
193 
194  gr->addEdge(vert_map[edges[k].getSourceVertex()], vert_map[edges[k].getDestinationVertex()],
195  edges[k].getEdgeLength() );
196  }
197 
198  if (debug()) {
199  cout << "Num vertices, Edges: " << vertices.size() << "," << edges.size() << endl;
200  }
201 
202  }
203 
204 
206  }
207 
213  const string& getName() const {
214  return name;
215  }
222  void setName(const string& n) {
223  name = n;
224  }
225 
232  void getLatLongRange (double *lat_range, double *longit_range) const {
233  lat_range[0] = latitude_range[0];
234  lat_range[1] = latitude_range[1];
235  longit_range[0] = longitude_range[0];
236  longit_range[1] = longitude_range[1];
237  }
244  void getCartesianCoordsRange (double *xrange, double *yrange) const {
245  xrange[0] = cartesian_range_x[0];
246  xrange[1] = cartesian_range_x[1];
247  yrange[0] = cartesian_range_y[0];
248  yrange[1] = cartesian_range_y[1];
249  }
250 
257  const vector<OSMVertex>& getVertices () const {
258  return vertices;
259  }
267  void setVertices (const vector<OSMVertex>& verts) {
268  vertices = verts;
269  // update the ranges for lat/long and cartesian equivalent
270  latitude_range[0] = 1000000.;
271  latitude_range[1] = -1000000.;
272  longitude_range[0] = 1000000.;
273  longitude_range[1] = -1000000.;
274  double lat, longit, cart_coords[2];
275  for (auto& v : vertices) {
276  lat = v.getLatitude();
277  longit = v.getLongitude();
278  latitude_range[0] = (std::min)(latitude_range[0], lat); //The parenthesis around std::min are needed to workaround a VS2017 bug
279 
280  latitude_range[1] = (std::max)(latitude_range[0], lat);
281  longitude_range[0] = (std::min)(longitude_range[1], longit);
282  longitude_range[1] = (std::max)(longitude_range[1], longit);
283  }
284  recomputeCartesianRange();
285  }
286 
294  const vector<OSMEdge>& getEdges () const {
295  return edges;
296  }
297 
305  void setEdges (const vector<OSMEdge>& e) {
306  edges = e;
307  }
312  static void toCartesianCoords(double lat, double longit, double *coords) {
313  const double R = 6378.; // Radius of the earth in km
314  double lat_rad = degreeToRadians(lat);
315  double longit_rad = degreeToRadians(longit);
316  coords[0] = R * cos(lat_rad) * cos (longit_rad);
317  coords[1] = R * cos(lat_rad) * sin (longit_rad);
318  }
319  };
320  }
321 } // end namespace bridges
322 
323 #endif
const vector< OSMEdge > & getEdges() const
get edges.
Definition: OSMData.h:294
static void toCartesianCoords(double lat, double longit, double *coords)
Definition: OSMData.h:312
const vector< OSMVertex > & getVertices() const
Definition: OSMData.h:257
#define M_PI
Definition: OSMData.h:13
This class provides methods to represent adjacency list based graphs.
Definition: Element.h:19
void getCartesianCoordsRange(double *xrange, double *yrange) const
get the range of dataset in Cartesian coords
Definition: OSMData.h:244
void setName(const string &n)
change the name of the dataset
Definition: OSMData.h:222
const Element< E1 > * getVertex(const K &key) const
Definition: GraphAdjList.h:305
OSMData()
Definition: OSMData.h:205
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:51
void addVertex(const K &k, const E1 &e=E1())
Definition: GraphAdjList.h:96
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void setVertices(const vector< OSMVertex > &verts)
replace the vertices stored by this new set.
Definition: OSMData.h:267
const string & getName() const
get the name of the dataset
Definition: OSMData.h:213
void setLatLongRange(double *lat_range, double *longit_range)
set the latitude and longitude range of the dataset
Definition: OSMData.h:108
void getGraph(GraphAdjList< int, OSMVertex, double > *gr) const
Definition: OSMData.h:141
void setLatLongRange(double lat_min, double lat_max, double long_min, double long_max)
set the range of the dataset
Definition: OSMData.h:121
Class that hold Open Street Map vertices.
Definition: OSMData.h:34
void getLatLongRange(double *lat_range, double *longit_range) const
Definition: OSMData.h:232
void getCartesianCoords(double *coords) const
Definition: OSMVertex.h:105
void setEdges(const vector< OSMEdge > &e)
set edges
Definition: OSMData.h:305
Definition: Array.h:9
void addEdge(const K &src, const K &dest, const E2 &data=E2())
add an edge with data.
Definition: GraphAdjList.h:119
Class that hold Open Street Map vertices.
Definition: OSMVertex.h:28