Bridges-C++ 3.5.0-dev2-1-ge3e57bf
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#include <algorithm>
9
10//should be defined in math.h but VS2017 has a weird behavior here.
11#ifndef M_PI
12#define M_PI 3.1415926535897
13#endif
14
15#include <GraphAdjList.h>
16
17#include "OSMVertex.h"
18#include "OSMEdge.h"
19
20namespace bridges {
21 namespace dataset {
22 using namespace bridges::datastructure;
38 class OSMData {
39 private:
40
41 bool debug() const {
42 return false;
43 }
44
45 string name = "";
46 // data range
47 double latitude_range[2] = {0.0, 0.0},
48 longitude_range[2] = {0.0, 0.0};
49 double cartesian_range_x[2] = {0.0, 0.0},
50 cartesian_range_y[2] = {0.0, 0.0};
51 // vertices
52 vector<OSMVertex> vertices;
53 // edges
54 vector<OSMEdge> edges;
55
56 static double degreeToRadians(double deg) {
57 return deg * M_PI / 180.;
58 }
59
60 void recomputeCartesianRange() {
61 cartesian_range_x[0] = 1000000.;
62 cartesian_range_x[1] = -1000000.;
63 cartesian_range_y[0] = 1000000.;
64 cartesian_range_y[1] = -1000000.;
65
66 OSMVertex v1 (0, latitude_range[0], longitude_range[0]);
67 OSMVertex v2 (1, latitude_range[1], longitude_range[1]);
68 OSMVertex v3 (2, latitude_range[0], longitude_range[1]);
69 OSMVertex v4 (3, latitude_range[1], longitude_range[0]);
70
71 //the cartesian range will always come from one of the extremities
72 double coords[2];
73 v1.getCartesianCoords(coords);
74
75 cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
76 cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
77 cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
78 cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
79
80 v2.getCartesianCoords(coords);
81
82 cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
83 cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
84 cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
85 cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
86
87 v3.getCartesianCoords(coords);
88
89 cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
90 cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
91 cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
92 cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
93
94 v4.getCartesianCoords(coords);
95
96 cartesian_range_x[0] = (std::min)(cartesian_range_x[0], coords[0]);
97 cartesian_range_x[1] = (std::max)(cartesian_range_x[1], coords[0]);
98 cartesian_range_y[0] = (std::min)(cartesian_range_y[0], coords[1]);
99 cartesian_range_y[1] = (std::max)(cartesian_range_y[1], coords[1]);
100
101 }
102
103 public:
104
111 void setLatLongRange (double *lat_range, double *longit_range) {
112 latitude_range[0] = lat_range[0];
113 latitude_range[1] = lat_range[1];
114 longitude_range[0] = longit_range[0];
115 longitude_range[1] = longit_range[1];
116 recomputeCartesianRange();
117 }
124 void setLatLongRange (double lat_min, double lat_max,
125 double long_min, double long_max) {
126 latitude_range[0] = lat_min;
127 latitude_range[1] = lat_max;
128 longitude_range[0] = long_min;
129 longitude_range[1] = long_max;
130 recomputeCartesianRange();
131 }
132
147 // vector<OSMVertex> vertices = osm_data->getVertices();
148 // vector<OSMEdge> edges = osm_data->getEdges();
149
150 int k = 0;
151 double coords[2];
152
153 double xrange[2], yrange[2];
154 this->getCartesianCoordsRange(xrange, yrange);
155 if (debug()) {
156 cout << "Range(Cartesian):" << xrange[0] << "," << xrange[1] <<
157 yrange[0] << "," << yrange[1] << endl;
158 }
159
160 double ll[2], ur[2];
161 // translation
162 double tx = xrange[0];
163 double ty = yrange[0];
164
165 // scale factor -- assume a display of 1000 pixels wide
166 double sx = 1000. / (xrange[1] - xrange[0]);
167 double sy = 1000. / (yrange[1] - yrange[0]);
168
169 if (debug()) {
170 cout << "Translate:" << tx << "," << ty << endl;
171 cout << "Scale:" << sx << "," << sy << endl;
172 }
173
174 std::unordered_map<OSMVertex::OSMVertexID, int> vert_map;
175
176 k = 0;
177 for (int k = 0; k < vertices.size(); k++) {
178 // Preventing multiple vertex inclusion.
179 // Not sure why that would happen, but being safe.
180 if (vert_map.find(vertices[k].getVertexID()) == vert_map.end()) {
181
182 vert_map[vertices[k].getVertexID()] = k;
183 gr->addVertex(k, vertices[k]);
184
185 vertices[k].getCartesianCoords(coords);
186 //coords[1] = yrange[1] - (coords[1] - yrange[0]);
187 //double x = (coords[0]-tx)*sx, y = (coords[1]-ty)*sy;
188 double x = coords[0];
189 double y = coords[1];
190 gr->getVertex(k)->getVisualizer()->setLocation( x, y);
191 gr->getVertex(k)->getVisualizer()->setColor(Color("green"));
192 }
193 }
194 for (int k = 0; k < edges.size(); k++) {
195 // std::cout<<edges[k].getEdgeLength()<<std::endl;
196
197 gr->addEdge(vert_map[edges[k].getSourceVertex()], vert_map[edges[k].getDestinationVertex()],
198 edges[k].getEdgeLength() );
199 }
200
201 if (debug()) {
202 cout << "Num vertices, Edges: " << vertices.size() << "," << edges.size() << endl;
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
#define M_PI
Definition: OSMData.h:12
Class that hold Open Street Map Data.
Definition: OSMData.h:38
void setEdges(const vector< OSMEdge > &e)
set edges
Definition: OSMData.h:307
void getGraph(GraphAdjList< int, OSMVertex, double > *gr) const
Definition: OSMData.h:146
const string & getName() const
get the name of the dataset
Definition: OSMData.h:215
static void toCartesianCoords(double lat, double longit, double *coords)
Definition: OSMData.h:317
OSMData()
Definition: OSMData.h:207
const vector< OSMVertex > & getVertices() const
Definition: OSMData.h:259
void setVertices(const vector< OSMVertex > &verts)
replace the vertices stored by this new set.
Definition: OSMData.h:269
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
void setLatLongRange(double lat_min, double lat_max, double long_min, double long_max)
set the range of the dataset
Definition: OSMData.h:124
void setLatLongRange(double *lat_range, double *longit_range)
set the latitude and longitude range of the dataset
Definition: OSMData.h:111
const vector< OSMEdge > & getEdges() const
get edges.
Definition: OSMData.h:296
void getLatLongRange(double *lat_range, double *longit_range) const
Definition: OSMData.h:234
Class that hold Open Street Map vertices.
Definition: OSMVertex.h:33
void getCartesianCoords(double *coords) const
Definition: OSMVertex.h:123
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:50
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:141
void setColor(const Color &col)
Set the color to "col".
Definition: ElementVisualizer.h:94
void setLocation(const double &locX, const double &locY)
Definition: ElementVisualizer.h:157
This class provides methods to represent adjacency list based graphs.
Definition: GraphAdjList.h:110
void addVertex(const K &k, const E1 &e=E1())
Adds a vertex to the graph.
Definition: GraphAdjList.h:175
void addEdge(const K &src, const K &dest, const E2 &data=E2())
Definition: GraphAdjList.h:198
const Element< E1 > * getVertex(const K &key) const
Return the vertex corresponding to a key.
Definition: GraphAdjList.h:405
Definition: Array.h:9
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4