Bridges-C++  3.2.0
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 
144  // vector<OSMVertex> vertices = osm_data->getVertices();
145  // vector<OSMEdge> edges = osm_data->getEdges();
146 
147  int k = 0;
148  double coords[2];
149 
150  double xrange[2], yrange[2];
151  this->getCartesianCoordsRange(xrange, yrange);
152  if (debug()) {
153  cout << "Range(Cartesian):" << xrange[0] << "," << xrange[1] <<
154  yrange[0] << "," << yrange[1] << endl;
155  }
156 
157 
158  double ll[2], ur[2];
159  // translation
160  double tx = xrange[0];
161  double ty = yrange[0];
162 
163  // scale factor -- assume a display of 1000 pixels wide
164  double sx = 1000. / (xrange[1] - xrange[0]);
165  double sy = 1000. / (yrange[1] - yrange[0]);
166 
167  if (debug()) {
168  cout << "Translate:" << tx << "," << ty << endl;
169  cout << "Scale:" << sx << "," << sy << endl;
170  }
171 
172  std::unordered_map<OSMVertex::OSMVertexID, int> vert_map;
173 
174 
175  k = 0;
176  for (int k = 0; k < vertices.size(); k++) {
177  // Preventing multiple vertex inclusion.
178  // Not sure why that would happen, but being safe.
179  if (vert_map.find(vertices[k].getVertexID()) == vert_map.end()) {
180 
181  vert_map[vertices[k].getVertexID()] = k;
182  gr->addVertex(k, vertices[k]);
183 
184  vertices[k].getCartesianCoords(coords);
185  //coords[1] = yrange[1] - (coords[1] - yrange[0]);
186  //double x = (coords[0]-tx)*sx, y = (coords[1]-ty)*sy;
187  double x = coords[0];
188  double y = coords[1];
189  gr->getVertex(k)->getVisualizer()->setLocation( x, y);
190  gr->getVertex(k)->getVisualizer()->setColor(Color("green"));
191  }
192  }
193  for (int k = 0; k < edges.size(); k++) {
194  // std::cout<<edges[k].getEdgeLength()<<std::endl;
195 
196  gr->addEdge(vert_map[edges[k].getSourceVertex()], vert_map[edges[k].getDestinationVertex()],
197  edges[k].getEdgeLength() );
198  }
199 
200  if (debug()) {
201  cout << "Num vertices, Edges: " << vertices.size() << "," << edges.size() << endl;
202  }
203 
204  }
205 
206 
208  }
209 
215  const string& getName() const {
216  return name;
217  }
224  void setName(const string& n) {
225  name = n;
226  }
227 
234  void getLatLongRange (double *lat_range, double *longit_range) const {
235  lat_range[0] = latitude_range[0];
236  lat_range[1] = latitude_range[1];
237  longit_range[0] = longitude_range[0];
238  longit_range[1] = longitude_range[1];
239  }
246  void getCartesianCoordsRange (double *xrange, double *yrange) const {
247  xrange[0] = cartesian_range_x[0];
248  xrange[1] = cartesian_range_x[1];
249  yrange[0] = cartesian_range_y[0];
250  yrange[1] = cartesian_range_y[1];
251  }
252 
259  const vector<OSMVertex>& getVertices () const {
260  return vertices;
261  }
269  void setVertices (const vector<OSMVertex>& verts) {
270  vertices = verts;
271  // update the ranges for lat/long and cartesian equivalent
272  latitude_range[0] = 1000000.;
273  latitude_range[1] = -1000000.;
274  longitude_range[0] = 1000000.;
275  longitude_range[1] = -1000000.;
276  double lat, longit, cart_coords[2];
277  for (auto& v : vertices) {
278  lat = v.getLatitude();
279  longit = v.getLongitude();
280  latitude_range[0] = (std::min)(latitude_range[0], lat); //The parenthesis around std::min are needed to workaround a VS2017 bug
281 
282  latitude_range[1] = (std::max)(latitude_range[0], lat);
283  longitude_range[0] = (std::min)(longitude_range[1], longit);
284  longitude_range[1] = (std::max)(longitude_range[1], longit);
285  }
286  recomputeCartesianRange();
287  }
288 
296  const vector<OSMEdge>& getEdges () const {
297  return edges;
298  }
299 
307  void setEdges (const vector<OSMEdge>& e) {
308  edges = e;
309  }
317  static void toCartesianCoords(double lat, double longit, double *coords) {
318  const double R = 6378.; // Radius of the earth in km
319  double lat_rad = degreeToRadians(lat);
320  double longit_rad = degreeToRadians(longit);
321  coords[0] = R * cos(lat_rad) * cos (longit_rad);
322  coords[1] = R * cos(lat_rad) * sin (longit_rad);
323  }
324  };
325  }
326 } // end namespace bridges
327 
328 #endif
const vector< OSMEdge > & getEdges() const
get edges.
Definition: OSMData.h:296
static void toCartesianCoords(double lat, double longit, double *coords)
Definition: OSMData.h:317
const vector< OSMVertex > & getVertices() const
Definition: OSMData.h:259
#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:246
void setName(const string &n)
change the name of the dataset
Definition: OSMData.h:224
const Element< E1 > * getVertex(const K &key) const
Return the vertex corresponding to a key.
Definition: GraphAdjList.h:383
OSMData()
Definition: OSMData.h:207
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:174
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:269
const string & getName() const
get the name of the dataset
Definition: OSMData.h:215
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:143
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 Data.
Definition: OSMData.h:34
void getLatLongRange(double *lat_range, double *longit_range) const
Definition: OSMData.h:234
void getCartesianCoords(double *coords) const
Definition: OSMVertex.h:119
void setEdges(const vector< OSMEdge > &e)
set edges
Definition: OSMData.h:307
Definition: Array.h:9
void addEdge(const K &src, const K &dest, const E2 &data=E2())
Add an edge with data.
Definition: GraphAdjList.h:197
Class that hold Open Street Map vertices.
Definition: OSMVertex.h:28