Bridges-C++  3.4.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;
40  class OSMData {
41  private:
42 
43  bool debug() const {
44  return false;
45  }
46 
47  string name = "";
48  // data range
49  double latitude_range[2] = {0.0, 0.0},
50  longitude_range[2] = {0.0, 0.0};
51  double cartesian_range_x[2] = {0.0, 0.0},
52  cartesian_range_y[2] = {0.0, 0.0};
53  // vertices
54  vector<OSMVertex> vertices;
55  // edges
56  vector<OSMEdge> edges;
57 
58  static double degreeToRadians(double deg) {
59  return deg * M_PI / 180.;
60  }
61 
62 
63  void recomputeCartesianRange() {
64  cartesian_range_x[0] = 1000000.;
65  cartesian_range_x[1] = -1000000.;
66  cartesian_range_y[0] = 1000000.;
67  cartesian_range_y[1] = -1000000.;
68 
69  OSMVertex v1 (0, latitude_range[0], longitude_range[0]);
70  OSMVertex v2 (1, latitude_range[1], longitude_range[1]);
71  OSMVertex v3 (2, latitude_range[0], longitude_range[1]);
72  OSMVertex v4 (3, latitude_range[1], longitude_range[0]);
73 
74  //the cartesian range will always come from one of the extremities
75  double coords[2];
76  v1.getCartesianCoords(coords);
77 
78  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
79  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
80  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
81  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
82 
83  v2.getCartesianCoords(coords);
84 
85  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
86  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
87  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
88  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
89 
90  v3.getCartesianCoords(coords);
91 
92  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
93  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
94  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
95  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
96 
97  v4.getCartesianCoords(coords);
98 
99  cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
100  cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
101  cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
102  cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
103 
104  }
105 
106  public:
107 
114  void setLatLongRange (double *lat_range, double *longit_range) {
115  latitude_range[0] = lat_range[0];
116  latitude_range[1] = lat_range[1];
117  longitude_range[0] = longit_range[0];
118  longitude_range[1] = longit_range[1];
119  recomputeCartesianRange();
120  }
127  void setLatLongRange (double lat_min, double lat_max,
128  double long_min, double long_max) {
129  latitude_range[0] = lat_min;
130  latitude_range[1] = lat_max;
131  longitude_range[0] = long_min;
132  longitude_range[1] = long_max;
133  recomputeCartesianRange();
134  }
135 
150  // vector<OSMVertex> vertices = osm_data->getVertices();
151  // vector<OSMEdge> edges = osm_data->getEdges();
152 
153  int k = 0;
154  double coords[2];
155 
156  double xrange[2], yrange[2];
157  this->getCartesianCoordsRange(xrange, yrange);
158  if (debug()) {
159  cout << "Range(Cartesian):" << xrange[0] << "," << xrange[1] <<
160  yrange[0] << "," << yrange[1] << endl;
161  }
162 
163 
164  double ll[2], ur[2];
165  // translation
166  double tx = xrange[0];
167  double ty = yrange[0];
168 
169  // scale factor -- assume a display of 1000 pixels wide
170  double sx = 1000. / (xrange[1] - xrange[0]);
171  double sy = 1000. / (yrange[1] - yrange[0]);
172 
173  if (debug()) {
174  cout << "Translate:" << tx << "," << ty << endl;
175  cout << "Scale:" << sx << "," << sy << endl;
176  }
177 
178  std::unordered_map<OSMVertex::OSMVertexID, int> vert_map;
179 
180 
181  k = 0;
182  for (int k = 0; k < vertices.size(); k++) {
183  // Preventing multiple vertex inclusion.
184  // Not sure why that would happen, but being safe.
185  if (vert_map.find(vertices[k].getVertexID()) == vert_map.end()) {
186 
187  vert_map[vertices[k].getVertexID()] = k;
188  gr->addVertex(k, vertices[k]);
189 
190  vertices[k].getCartesianCoords(coords);
191  //coords[1] = yrange[1] - (coords[1] - yrange[0]);
192  //double x = (coords[0]-tx)*sx, y = (coords[1]-ty)*sy;
193  double x = coords[0];
194  double y = coords[1];
195  gr->getVertex(k)->getVisualizer()->setLocation( x, y);
196  gr->getVertex(k)->getVisualizer()->setColor(Color("green"));
197  }
198  }
199  for (int k = 0; k < edges.size(); k++) {
200  // std::cout<<edges[k].getEdgeLength()<<std::endl;
201 
202  gr->addEdge(vert_map[edges[k].getSourceVertex()], vert_map[edges[k].getDestinationVertex()],
203  edges[k].getEdgeLength() );
204  }
205 
206  if (debug()) {
207  cout << "Num vertices, Edges: " << vertices.size() << "," << edges.size() << endl;
208  }
209 
210  }
211 
212 
214  }
215 
221  const string& getName() const {
222  return name;
223  }
230  void setName(const string& n) {
231  name = n;
232  }
233 
240  void getLatLongRange (double *lat_range, double *longit_range) const {
241  lat_range[0] = latitude_range[0];
242  lat_range[1] = latitude_range[1];
243  longit_range[0] = longitude_range[0];
244  longit_range[1] = longitude_range[1];
245  }
252  void getCartesianCoordsRange (double *xrange, double *yrange) const {
253  xrange[0] = cartesian_range_x[0];
254  xrange[1] = cartesian_range_x[1];
255  yrange[0] = cartesian_range_y[0];
256  yrange[1] = cartesian_range_y[1];
257  }
258 
265  const vector<OSMVertex>& getVertices () const {
266  return vertices;
267  }
275  void setVertices (const vector<OSMVertex>& verts) {
276  vertices = verts;
277  // update the ranges for lat/long and cartesian equivalent
278  latitude_range[0] = 1000000.;
279  latitude_range[1] = -1000000.;
280  longitude_range[0] = 1000000.;
281  longitude_range[1] = -1000000.;
282  double lat, longit, cart_coords[2];
283  for (auto& v : vertices) {
284  lat = v.getLatitude();
285  longit = v.getLongitude();
286  latitude_range[0] = (std::min)(latitude_range[0], lat); //The parenthesis around std::min are needed to workaround a VS2017 bug
287 
288  latitude_range[1] = (std::max)(latitude_range[0], lat);
289  longitude_range[0] = (std::min)(longitude_range[1], longit);
290  longitude_range[1] = (std::max)(longitude_range[1], longit);
291  }
292  recomputeCartesianRange();
293  }
294 
302  const vector<OSMEdge>& getEdges () const {
303  return edges;
304  }
305 
313  void setEdges (const vector<OSMEdge>& e) {
314  edges = e;
315  }
323  static void toCartesianCoords(double lat, double longit, double *coords) {
324  const double R = 6378.; // Radius of the earth in km
325  double lat_rad = degreeToRadians(lat);
326  double longit_rad = degreeToRadians(longit);
327  coords[0] = R * cos(lat_rad) * cos (longit_rad);
328  coords[1] = R * cos(lat_rad) * sin (longit_rad);
329  }
330  };
331  }
332 } // end namespace bridges
333 
334 #endif
const vector< OSMEdge > & getEdges() const
get edges.
Definition: OSMData.h:302
static void toCartesianCoords(double lat, double longit, double *coords)
Definition: OSMData.h:323
const vector< OSMVertex > & getVertices() const
Definition: OSMData.h:265
#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:252
void setName(const string &n)
change the name of the dataset
Definition: OSMData.h:230
const Element< E1 > * getVertex(const K &key) const
Return the vertex corresponding to a key.
Definition: GraphAdjList.h:408
OSMData()
Definition: OSMData.h:213
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())
Adds a vertex to the graph.
Definition: GraphAdjList.h:177
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:275
const string & getName() const
get the name of the dataset
Definition: OSMData.h:221
void setLatLongRange(double *lat_range, double *longit_range)
set the latitude and longitude range of the dataset
Definition: OSMData.h:114
void getGraph(GraphAdjList< int, OSMVertex, double > *gr) const
Definition: OSMData.h:149
void setLatLongRange(double lat_min, double lat_max, double long_min, double long_max)
set the range of the dataset
Definition: OSMData.h:127
Class that hold Open Street Map Data.
Definition: OSMData.h:40
void getLatLongRange(double *lat_range, double *longit_range) const
Definition: OSMData.h:240
void getCartesianCoords(double *coords) const
Definition: OSMVertex.h:125
void setEdges(const vector< OSMEdge > &e)
set edges
Definition: OSMData.h:313
Definition: Array.h:9
void addEdge(const K &src, const K &dest, const E2 &data=E2())
Definition: GraphAdjList.h:200
Class that hold Open Street Map vertices.
Definition: OSMVertex.h:34