The SwappyItems Key-Values Store
Classes | Macros | Typedefs | Functions | Variables
osm2graph_baremetal.cpp File Reference
#include <cstdio>
#include <utility>
#include <inttypes.h>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include "SwappyItems.hpp"
#include "osmpbfreader.hpp"

Classes

struct  NodeData
 
struct  WayData
 
struct  Vertex
 
struct  Distance
 
struct  Routing
 

Macros

#define FILE_ITEMS   ( 7*1024)
 
#define FILE_MULTI   6
 
#define RAM_MULTI   9
 
#define BBITS   5
 
#define BMASK   ((BBITS+4)* FILE_ITEMS)
 
#define TORADI(angleDegrees)   ((angleDegrees) * M_PI / 180.0)
 
#define LON_BOUND   5.85
 
#define LON_BOUND_DIF   3.62
 
#define LAT_BOUND   50.32
 
#define LAT_BOUND_DIF   2.2
 

Typedefs

typedef uint64_t Key
 
typedef SwappyItems< Key, WayData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASKWays_t
 
typedef SwappyItems< Key, NodeData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASKNodes_t
 
typedef SwappyItems< Key, Vertex, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASKVertices_t
 
typedef SwappyItems< Key, Distance, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASKDistance_t
 

Functions

Key calcDist (double lon1, double lat1, double lon2, double lat2)
 
uint8_t relevance (const std::string &wt)
 
int main (int argc, char **argv)
 

Variables

Ways_tways
 
Nodes_tnodes
 
Vertices_tverticies
 
Distance_tdistances
 

Macro Definition Documentation

◆ BBITS

#define BBITS   5

◆ BMASK

#define BMASK   ((BBITS+4)* FILE_ITEMS)

◆ FILE_ITEMS

#define FILE_ITEMS   ( 7*1024)

◆ FILE_MULTI

#define FILE_MULTI   6

◆ LAT_BOUND

#define LAT_BOUND   50.32

◆ LAT_BOUND_DIF

#define LAT_BOUND_DIF   2.2

◆ LON_BOUND

#define LON_BOUND   5.85

◆ LON_BOUND_DIF

#define LON_BOUND_DIF   3.62

◆ RAM_MULTI

#define RAM_MULTI   9

◆ TORADI

#define TORADI (   angleDegrees)    ((angleDegrees) * M_PI / 180.0)

Typedef Documentation

◆ Distance_t

◆ Key

typedef uint64_t Key

◆ Nodes_t

◆ Vertices_t

◆ Ways_t

Function Documentation

◆ calcDist()

Key calcDist ( double  lon1,
double  lat1,
double  lon2,
double  lat2 
)
82  {
83  double dist = 6378388.0 * std::acos(std::sin(TORADI(lat1)) * std::sin(TORADI(lat2)) + std::cos(TORADI(lat1)) * std::cos(TORADI(lat2)) * std::cos(TORADI(lon2 - lon1)));
84  if (dist < 0) dist *=-1;
85  return dist;
86 }
#define TORADI(angleDegrees)
Definition: osm2graph_baremetal.cpp:19

◆ main()

int main ( int  argc,
char **  argv 
)
204  {
205  Routing routing;
206  ways = new Ways_t(1);
207  nodes = new Nodes_t(2);
208 
209  verticies = new Vertices_t(3);
210  distances = new Distance_t(4);
211 
212  read_osm_pbf(argv[1], routing, true); // ways
213 
214  read_osm_pbf(argv[1], routing, false); // nodes und bedingungen
215 
216  // create verticies -------------------------------------------------------------------------------------
217 
218  Ways_t::Data w;
219  ways->each(w, [](Key wayosmid, Ways_t::Data & way) {
220  bool firstItem = true;
221  Key last;
222  double lastLon = 0.0;
223  double lastLat = 0.0;
224  Key dist = 0;
225 
226  // create verticies from refs: a->b->c->d->e
227  // d link to e(=last)
228  // c link to d(=last)
229  // ...
230  for (unsigned i=0; i < way.second.size(); ++i) {
231  Key ref = way.second[way.second.size()-1 - i]; // add actually ref to the last ref (from backward)
232  Nodes_t::Data * nptr = nodes->get(ref);
233 
234  if (nptr == nullptr) continue;
235  if (nptr->first._lon == 0 && nptr->first._lat == 0) continue;
236 
237  if (nptr->first._used == 0) {
238  if (firstItem == false) {
239  dist += calcDist(nptr->first._lon, nptr->first._lat, lastLon, lastLat);
240  lastLon = nptr->first._lon;
241  lastLat = nptr->first._lat;
242  }
243  continue;
244  }
245 
246  Vertices_t::Data * vptr = verticies->get(ref);
247  Distance_t::Data * dptr = distances->get(ref);
248 
249  if (vptr == nullptr) {
250  Vertices_t::Data vertex;
251  Distance_t::Data distance;
252  vertex.first._way = wayosmid;
253  vertex.first._lon = nptr->first._lon;
254  vertex.first._lat = nptr->first._lat;
255  if (firstItem) {
256  firstItem = false;
257  } else {
258  dist += calcDist(vertex.first._lon, vertex.first._lat, lastLon, lastLat);
259  vertex.second.push_back(last);
260  distance.second.push_back(dist);
261  }
262  lastLon = nptr->first._lon;
263  lastLat = nptr->first._lat;
264  verticies->set(ref, vertex.first, vertex.second);
265  distances->set(ref, distance.first, distance.second);
266  } else {
267  if (firstItem) {
268  firstItem = false;
269  } else {
270  dist += calcDist(vptr->first._lon, vptr->first._lat, lastLon, lastLat);
271  vptr->second.push_back(last);
272  dptr->second.push_back(dist);
273  }
274  lastLon = nptr->first._lon;
275  lastLat = nptr->first._lat;
276  verticies->set(ref, vptr->first, vptr->second);
277  distances->set(ref, dptr->first, dptr->second);
278  }
279  dist = 0;
280  last = ref;
281  }
282 
283  // Not a oneway. create verticies from refs in the direction: a<-b<-c<-d<-e
284  // b link to a(=last)
285  // c link to b(=last)
286  // ...
287  firstItem = true;
288  lastLon = 0.0;
289  lastLat = 0.0;
290  dist = 0;
291  if (way.first._oneway == false) {
292  for (unsigned i=0; i < way.second.size(); ++i) {
293  Key ref = way.second[i];
294  Nodes_t::Data * nptr = nodes->get(ref);
295 
296  if (nptr == nullptr) continue;
297  if (nptr->first._lon == 0 && nptr->first._lat == 0) continue;
298 
299  if (nptr->first._used == 0) {
300  if (firstItem == false) {
301  dist += calcDist(nptr->first._lon, nptr->first._lat, lastLon, lastLat);
302  lastLon = nptr->first._lon;
303  lastLat = nptr->first._lat;
304  }
305  continue;
306  }
307 
308  Vertices_t::Data * vptr = verticies->get(ref);
309  Distance_t::Data * dptr = distances->get(ref);
310 
311  if (vptr == nullptr) {
312  Vertices_t::Data vertex;
313  Distance_t::Data distance;
314  vertex.first._way = wayosmid;
315  vertex.first._lon = nptr->first._lon;
316  vertex.first._lat = nptr->first._lat;
317  if (firstItem) {
318  firstItem = false;
319  } else {
320  dist += calcDist(vertex.first._lon, vertex.first._lat, lastLon, lastLat);
321  vertex.second.push_back(last);
322  distance.second.push_back(dist);
323  }
324  lastLon = nptr->first._lon;
325  lastLat = nptr->first._lat;
326  verticies->set(ref, vertex.first, vertex.second);
327  distances->set(ref, distance.first, distance.second);
328  } else {
329  if (firstItem) {
330  firstItem = false;
331  } else {
332  dist += calcDist(vptr->first._lon, vptr->first._lat, lastLon, lastLat);
333  vptr->second.push_back(last);
334  dptr->second.push_back(dist);
335  }
336  lastLon = nptr->first._lon;
337  lastLat = nptr->first._lat;
338  verticies->set(ref, vptr->first, vptr->second);
339  distances->set(ref, dptr->first, dptr->second);
340  }
341  dist = 0;
342  last = ref;
343  }
344  }
345  // all items, no stop
346  return false;
347  });
348 
349  // print verticies ------------------------------------------------
350 
351  Vertices_t::Data dummy;
352  verticies->each(dummy, [](Key id, Vertices_t::Data & v) {
353  printf("%ld (%f,%f) part of way %ld\n", id, v.first._lon, v.first._lat, v.first._way);
354  for (Key r : v.second) {
355  printf(" %" PRId64, r);
356  }
357  printf("\n");
358  Distance_t::Data * dptr = distances->get(id);
359  for (Key r : dptr->second) {
360  printf(" %" PRId64, r);
361  }
362  printf("\n");
363  // all items, no stop
364  return false;
365  });
366 
367  delete distances;
368  delete verticies;
369  delete nodes;
370  delete ways;
371 
372  return 0;
373 }
std::pair< TVALUE, std::vector< TKEY > > Data
Definition: SwappyItems.hpp:52
Data * get(const TKEY &key)
Definition: SwappyItems.hpp:188
bool each(Data &back, std::function< bool(TKEY, Data &)> foo)
Definition: SwappyItems.hpp:790
bool set(TKEY key, TVALUE value)
Definition: SwappyItems.hpp:123
void read_osm_pbf(const std::string &filename, Visitor &visitor, bool wayOnly)
Definition: osmpbfreader.hpp:359
uint64_t Key
Definition: osm2graph.cpp:48
Ways_t * ways
Definition: osm2graph_baremetal.cpp:72
SwappyItems< Key, Vertex, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Vertices_t
Definition: osm2graph_baremetal.cpp:76
SwappyItems< Key, Distance, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Distance_t
Definition: osm2graph_baremetal.cpp:78
SwappyItems< Key, WayData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Ways_t
Definition: osm2graph_baremetal.cpp:70
Nodes_t * nodes
Definition: osm2graph_baremetal.cpp:73
Distance_t * distances
Definition: osm2graph_baremetal.cpp:79
SwappyItems< Key, NodeData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Nodes_t
Definition: osm2graph_baremetal.cpp:71
Key calcDist(double lon1, double lat1, double lon2, double lat2)
Definition: osm2graph_baremetal.cpp:82
Vertices_t * verticies
Definition: osm2graph_baremetal.cpp:77
Definition: osm2graph.cpp:120

◆ relevance()

uint8_t relevance ( const std::string &  wt)
88  {
89  if (wt.compare("motorway") == 0) return 1;
90  if (wt.compare("motorway_link") == 0) return 2;
91  if (wt.compare("trunk") == 0) return 1;
92  if (wt.compare("trunk_link") == 0) return 2;
93  if (wt.compare("primary") == 0) return 1;
94  if (wt.compare("primary_link") == 0) return 2;
95  if (wt.compare("secondary") == 0) return 1;
96  if (wt.compare("secondary_link") == 0) return 2;
97  if (wt.compare("unclassified") == 0) return 1;
98  if (wt.compare("tertiary") == 0) return 1;
99  if (wt.compare("tertiary_link") == 0) return 2;
100  if (wt.compare("residential") == 0) return 1;
101  return 0;
102 }

Variable Documentation

◆ distances

Distance_t* distances

◆ nodes

Nodes_t* nodes

◆ verticies

Vertices_t* verticies

◆ ways

Ways_t* ways