The SwappyItems Key-Values Store
Classes | Macros | Typedefs | Functions | Variables
osm2graph.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   ( 2*1024)
 
#define FILE_MULTI   4
 
#define RAM_MULTI   8
 
#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
 
#define DEFAULTATTR   " version='1' timestamp='2019-12-20T14:59:00Z' visible='true'"
 build a map file with osmosis: ../osmosis/bin/osmosis –read-xml file=graph.osm –mw file=krefeld_mg.map zoom-interval-conf=7,5,8,11,9,13 threads=2 More...
 

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)
 
int 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)

◆ DEFAULTATTR

#define DEFAULTATTR   " version='1' timestamp='2019-12-20T14:59:00Z' visible='true'"

build a map file with osmosis: ../osmosis/bin/osmosis –read-xml file=graph.osm –mw file=krefeld_mg.map zoom-interval-conf=7,5,8,11,9,13 threads=2

Examples
osm2graph.cpp.

◆ FILE_ITEMS

#define FILE_ITEMS   ( 2*1024)

◆ FILE_MULTI

#define FILE_MULTI   4

◆ LAT_BOUND

#define LAT_BOUND   50.32
Examples
osm2graph.cpp.

◆ LAT_BOUND_DIF

#define LAT_BOUND_DIF   2.2
Examples
osm2graph.cpp.

◆ LON_BOUND

#define LON_BOUND   5.85
Examples
osm2graph.cpp.

◆ LON_BOUND_DIF

#define LON_BOUND_DIF   3.62
Examples
osm2graph.cpp.

◆ RAM_MULTI

#define RAM_MULTI   8

◆ TORADI

#define TORADI (   angleDegrees)    ((angleDegrees) * M_PI / 180.0)
Examples
osm2graph.cpp.

Typedef Documentation

◆ Distance_t

Examples
osm2graph.cpp.

◆ Key

typedef uint64_t Key
Examples
osm2graph.cpp.

◆ Nodes_t

Examples
osm2graph.cpp.

◆ Vertices_t

Examples
osm2graph.cpp.

◆ Ways_t

Examples
osm2graph.cpp.

Function Documentation

◆ calcDist()

Key calcDist ( double  lon1,
double  lat1,
double  lon2,
double  lat2 
)
Examples
osm2graph.cpp.
97  {
98  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)));
99  if (dist < 0) dist *=-1;
100  return dist;
101 }
#define TORADI(angleDegrees)
Definition: osm2graph.cpp:19

◆ main()

int main ( int  argc,
char **  argv 
)
Examples
osm2graph.cpp.
220  {
221  Routing routing;
222  ways = new Ways_t(1);
223  nodes = new Nodes_t(2);
224 
225  verticies = new Vertices_t(3);
226  distances = new Distance_t(4);
227 
228  read_osm_pbf(argv[1], routing, true); // ways
229 
230  read_osm_pbf(argv[1], routing, false); // nodes und bedingungen
231 
232  // create verticies -------------------------------------------------------------------------------------
233 
234  Ways_t::Data w;
235  ways->each(w, [](Key wayosmid, Ways_t::Data & way) {
236  bool firstItem = true;
237  Key last;
238  double lastLon = 0.0;
239  double lastLat = 0.0;
240  Key dist = 0;
241 
242  // create verticies from refs: a->b->c->d->e
243  // d link to e(=last)
244  // c link to d(=last)
245  // ...
246  for (unsigned i=0; i < way.second.size(); ++i) {
247  Key ref = way.second[way.second.size()-1 - i]; // add actually ref to the last ref (from backward)
248  Nodes_t::Data * nptr = nodes->get(ref);
249 
250  if (nptr == nullptr) continue;
251  if (nptr->first._lon == 0 && nptr->first._lat == 0) continue;
252 
253  // with reduction of nodes
254  if (nptr->first._used == 0) {
255  if (firstItem == false) {
256  dist += calcDist(nptr->first._lon, nptr->first._lat, lastLon, lastLat);
257  lastLon = nptr->first._lon;
258  lastLat = nptr->first._lat;
259  }
260  continue;
261  }
262 
263  Vertices_t::Data * vptr = verticies->get(ref);
264  Distance_t::Data * dptr = distances->get(ref);
265 
266  if (vptr == nullptr) {
267  Vertices_t::Data vertex;
268  Distance_t::Data distance;
269  vertex.first._way = wayosmid;
270  vertex.first._lon = nptr->first._lon;
271  vertex.first._lat = nptr->first._lat;
272  if (firstItem) {
273  firstItem = false;
274  } else {
275  dist += calcDist(vertex.first._lon, vertex.first._lat, lastLon, lastLat);
276  vertex.second.push_back(last);
277  distance.second.push_back(dist);
278  }
279  lastLon = nptr->first._lon;
280  lastLat = nptr->first._lat;
281  verticies->set(ref, vertex.first, vertex.second);
282  distances->set(ref, distance.first, distance.second);
283  } else {
284  if (firstItem) {
285  firstItem = false;
286  } else {
287  dist += calcDist(vptr->first._lon, vptr->first._lat, lastLon, lastLat);
288  vptr->second.push_back(last);
289  dptr->second.push_back(dist);
290  }
291  lastLon = nptr->first._lon;
292  lastLat = nptr->first._lat;
293  verticies->set(ref, vptr->first, vptr->second);
294  distances->set(ref, dptr->first, dptr->second);
295  }
296  dist = 0;
297  last = ref;
298  }
299 
300  // Not a oneway. create verticies from refs in the direction: a<-b<-c<-d<-e
301  // b link to a(=last)
302  // c link to b(=last)
303  // ...
304  firstItem = true;
305  lastLon = 0.0;
306  lastLat = 0.0;
307  dist = 0;
308  if (way.first._oneway == false) {
309  for (unsigned i=0; i < way.second.size(); ++i) {
310  Key ref = way.second[i];
311  Nodes_t::Data * nptr = nodes->get(ref);
312 
313  if (nptr == nullptr) continue;
314  if (nptr->first._lon == 0 && nptr->first._lat == 0) continue;
315 
316  // with reduction of nodes
317  if (nptr->first._used == 0) {
318  if (firstItem == false) {
319  dist += calcDist(nptr->first._lon, nptr->first._lat, lastLon, lastLat);
320  lastLon = nptr->first._lon;
321  lastLat = nptr->first._lat;
322  }
323  continue;
324  }
325 
326  Vertices_t::Data * vptr = verticies->get(ref);
327  Distance_t::Data * dptr = distances->get(ref);
328 
329  if (vptr == nullptr) {
330  Vertices_t::Data vertex;
331  Distance_t::Data distance;
332  vertex.first._way = wayosmid;
333  vertex.first._lon = nptr->first._lon;
334  vertex.first._lat = nptr->first._lat;
335  if (firstItem) {
336  firstItem = false;
337  } else {
338  dist += calcDist(vertex.first._lon, vertex.first._lat, lastLon, lastLat);
339  vertex.second.push_back(last);
340  distance.second.push_back(dist);
341  }
342  lastLon = nptr->first._lon;
343  lastLat = nptr->first._lat;
344  verticies->set(ref, vertex.first, vertex.second);
345  distances->set(ref, distance.first, distance.second);
346  } else {
347  if (firstItem) {
348  firstItem = false;
349  } else {
350  dist += calcDist(vptr->first._lon, vptr->first._lat, lastLon, lastLat);
351  vptr->second.push_back(last);
352  dptr->second.push_back(dist);
353  }
354  lastLon = nptr->first._lon;
355  lastLat = nptr->first._lat;
356  verticies->set(ref, vptr->first, vptr->second);
357  distances->set(ref, dptr->first, dptr->second);
358  }
359  dist = 0;
360  last = ref;
361  }
362  }
363 
364  // all items, no stop
365  return false;
366  });
367 
368  // verticies to osm ------------------------------------------------
369 
370  uint64_t wayOsmId = 1;
371  FILE * pFile;
372  pFile = fopen ("graph.osm","w");
373 
374  fprintf (pFile,
375  "<?xml version='1.0' encoding='UTF-8'?>\n"
376  "<osm version='0.6'>\n"
377  "<bounds minlat='%f' minlon='%f' maxlat='%f' maxlon='%f'/>\n",
380  );
381 
382 
383  Vertices_t::Data dummy;
384 
385  // all nodes to the beginning!!!
386  verticies->each(dummy, [&wayOsmId, &pFile](Key id, Vertices_t::Data & v) {
387  //if (v.second.size() > 0) { // no. it could be a single point, where many oneways result in!
388  fprintf (pFile,
389  "<node id='%" PRIu64 "'" DEFAULTATTR " lat='%f' lon='%f'/>\n",
390  id, v.first._lat, v.first._lon
391  );
392  // all items, no stop
393  return false;
394  });
395 
396  // create the new ways
397  verticies->each(dummy, [&wayOsmId, &pFile](Key id, Vertices_t::Data & v) {
398  Distance_t::Data * dptr = distances->get(id);
399  for (unsigned i=0; i < v.second.size(); ++i) {
400  fprintf (pFile,
401  "<way id='%" PRIu64 "'" DEFAULTATTR ">"
402  "<nd ref='%" PRIu64 "'/>"
403  "<nd ref='%" PRIu64 "'/>"
404  "<tag k='highway' v='secondary'/>"
405  "<tag k='oneway' v='yes'/>"
406  "<tag k='name' v=\"%" PRIu64 "\"/></way>\n",
407  wayOsmId,
408  id,
409  v.second[i],
410  dptr->second[i]
411  );
412  ++wayOsmId;
413  }
414 
415  // all items, no stop
416  return false;
417  });
418 
419 
420  fprintf (pFile, "</osm>\n");
421  fclose (pFile);
422 
423  delete distances;
424  delete verticies;
425  delete nodes;
426  delete ways;
427 
428  return 0;
429 }
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.cpp:84
SwappyItems< Key, Vertex, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Vertices_t
Definition: osm2graph.cpp:88
#define LAT_BOUND
Definition: osm2graph.cpp:32
#define LON_BOUND_DIF
Definition: osm2graph.cpp:31
SwappyItems< Key, Distance, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Distance_t
Definition: osm2graph.cpp:90
#define LON_BOUND
Definition: osm2graph.cpp:30
SwappyItems< Key, WayData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Ways_t
Definition: osm2graph.cpp:82
Nodes_t * nodes
Definition: osm2graph.cpp:85
#define DEFAULTATTR
build a map file with osmosis: ../osmosis/bin/osmosis –read-xml file=graph.osm –mw file=krefeld_mg....
Definition: osm2graph.cpp:45
Distance_t * distances
Definition: osm2graph.cpp:91
SwappyItems< Key, NodeData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Nodes_t
Definition: osm2graph.cpp:83
Key calcDist(double lon1, double lat1, double lon2, double lat2)
Definition: osm2graph.cpp:97
Vertices_t * verticies
Definition: osm2graph.cpp:89
#define LAT_BOUND_DIF
Definition: osm2graph.cpp:33
Definition: osm2graph.cpp:120

◆ relevance()

int relevance ( const std::string &  wt)
Examples
osm2graph.cpp.
103  {
104  if (wt.compare("motorway") == 0) return 1;
105  if (wt.compare("motorway_link") == 0) return 2;
106  if (wt.compare("trunk") == 0) return 1;
107  if (wt.compare("trunk_link") == 0) return 2;
108  if (wt.compare("primary") == 0) return 1;
109  if (wt.compare("primary_link") == 0) return 2;
110  if (wt.compare("secondary") == 0) return 1;
111  if (wt.compare("secondary_link") == 0) return 2;
112  if (wt.compare("unclassified") == 0) return 1;
113  if (wt.compare("tertiary") == 0) return 1;
114  if (wt.compare("tertiary_link") == 0) return 2;
115  if (wt.compare("residential") == 0) return 1;
116  return 0;
117 }

Variable Documentation

◆ distances

Distance_t* distances
Examples
osm2graph.cpp.

◆ nodes

Nodes_t* nodes
Examples
osm2graph.cpp.

◆ verticies

Vertices_t* verticies
Examples
osm2graph.cpp.

◆ ways

Ways_t* ways
Examples
osm2graph.cpp.