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

Classes

struct  NodeData
 
struct  WayData
 
struct  Vertex
 
struct  Distance
 
struct  Routing
 

Macros

#define IMAGE_WIDTH   1200
 
#define IMAGE_HEIGHT   2000
 
#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   6.4
 
#define LON_BOUND_DIF   0.30
 
#define LAT_BOUND   51.1
 
#define LAT_BOUND_DIF   0.30
 
#define MAX_NODES   27672
 

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)
 
Color toColor (double id)
 
int main (int argc, char **argv)
 

Variables

Ways_tways
 
Nodes_tnodes
 
Vertices_tverticies
 
Distance_tdistances
 
Bmp bild
 
Color c
 
double pos = 0.0
 

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

◆ IMAGE_HEIGHT

#define IMAGE_HEIGHT   2000

◆ IMAGE_WIDTH

#define IMAGE_WIDTH   1200

◆ LAT_BOUND

#define LAT_BOUND   51.1

◆ LAT_BOUND_DIF

#define LAT_BOUND_DIF   0.30

◆ LON_BOUND

#define LON_BOUND   6.4

◆ LON_BOUND_DIF

#define LON_BOUND_DIF   0.30

◆ MAX_NODES

#define MAX_NODES   27672

◆ 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 
)
99  {
100  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)));
101  if (dist < 0) dist *=-1;
102  return dist;
103 }
#define TORADI(angleDegrees)
Definition: osm2graph_bitmap.cpp:23

◆ main()

int main ( int  argc,
char **  argv 
)
271  {
272  Routing routing;
273  ways = new Ways_t(1);
274  nodes = new Nodes_t(2);
275 
276  verticies = new Vertices_t(3);
277  distances = new Distance_t(4);
278 
279  read_osm_pbf(argv[1], routing, true); // ways
280 
281  read_osm_pbf(argv[1], routing, false); // nodes und bedingungen
282 
283  // create verticies -------------------------------------------------------------------------------------
284 
285  Ways_t::Data w;
286  ways->each(w, [](Key wayosmid, Ways_t::Data & way) {
287  bool firstItem = true;
288  Key last;
289  double lastLon = 0.0;
290  double lastLat = 0.0;
291  Key dist = 0;
292 
293  // create verticies from refs: a->b->c->d->e
294  // d link to e(=last)
295  // c link to d(=last)
296  // ...
297  for (unsigned i=0; i < way.second.size(); ++i) {
298  Key ref = way.second[way.second.size()-1 - i]; // add actually ref to the last ref (from backward)
299  Nodes_t::Data * nptr = nodes->get(ref);
300 
301  if (nptr == nullptr) continue;
302  if (nptr->first._lon == 0 && nptr->first._lat == 0) continue;
303 
304  if (nptr->first._used == 0) {
305  if (firstItem == false) {
306  dist += calcDist(nptr->first._lon, nptr->first._lat, lastLon, lastLat);
307  lastLon = nptr->first._lon;
308  lastLat = nptr->first._lat;
309  }
310  continue;
311  }
312 
313  Vertices_t::Data * vptr = verticies->get(ref);
314  Distance_t::Data * dptr = distances->get(ref);
315 
316  if (vptr == nullptr) {
317  Vertices_t::Data vertex;
318  Distance_t::Data distance;
319  vertex.first._way = wayosmid;
320  vertex.first._lon = nptr->first._lon;
321  vertex.first._lat = nptr->first._lat;
322  if (firstItem) {
323  firstItem = false;
324  } else {
325  dist += calcDist(vertex.first._lon, vertex.first._lat, lastLon, lastLat);
326  vertex.second.push_back(last);
327  distance.second.push_back(dist);
328  }
329  lastLon = nptr->first._lon;
330  lastLat = nptr->first._lat;
331  verticies->set(ref, vertex.first, vertex.second);
332  distances->set(ref, distance.first, distance.second);
333  } else {
334  if (firstItem) {
335  firstItem = false;
336  } else {
337  dist += calcDist(vptr->first._lon, vptr->first._lat, lastLon, lastLat);
338  vptr->second.push_back(last);
339  dptr->second.push_back(dist);
340  }
341  lastLon = nptr->first._lon;
342  lastLat = nptr->first._lat;
343  verticies->set(ref, vptr->first, vptr->second);
344  distances->set(ref, dptr->first, dptr->second);
345  }
346  dist = 0;
347  last = ref;
348  }
349 
350  // Not a oneway. create verticies from refs in the direction: a<-b<-c<-d<-e
351  // b link to a(=last)
352  // c link to b(=last)
353  // ...
354  firstItem = true;
355  lastLon = 0.0;
356  lastLat = 0.0;
357  dist = 0;
358  if (way.first._oneway == false) {
359  for (unsigned i=0; i < way.second.size(); ++i) {
360  Key ref = way.second[i];
361  Nodes_t::Data * nptr = nodes->get(ref);
362 
363  if (nptr == nullptr) continue;
364  if (nptr->first._lon == 0 && nptr->first._lat == 0) continue;
365 
366  if (nptr->first._used == 0) {
367  if (firstItem == false) {
368  dist += calcDist(nptr->first._lon, nptr->first._lat, lastLon, lastLat);
369  lastLon = nptr->first._lon;
370  lastLat = nptr->first._lat;
371  }
372  continue;
373  }
374 
375  Vertices_t::Data * vptr = verticies->get(ref);
376  Distance_t::Data * dptr = distances->get(ref);
377 
378  if (vptr == nullptr) {
379  Vertices_t::Data vertex;
380  Distance_t::Data distance;
381  vertex.first._way = wayosmid;
382  vertex.first._lon = nptr->first._lon;
383  vertex.first._lat = nptr->first._lat;
384  if (firstItem) {
385  firstItem = false;
386  } else {
387  dist += calcDist(vertex.first._lon, vertex.first._lat, lastLon, lastLat);
388  vertex.second.push_back(last);
389  distance.second.push_back(dist);
390  }
391  lastLon = nptr->first._lon;
392  lastLat = nptr->first._lat;
393  verticies->set(ref, vertex.first, vertex.second);
394  distances->set(ref, distance.first, distance.second);
395  } else {
396  if (firstItem) {
397  firstItem = false;
398  } else {
399  dist += calcDist(vptr->first._lon, vptr->first._lat, lastLon, lastLat);
400  vptr->second.push_back(last);
401  dptr->second.push_back(dist);
402  }
403  lastLon = nptr->first._lon;
404  lastLat = nptr->first._lat;
405  verticies->set(ref, vptr->first, vptr->second);
406  distances->set(ref, dptr->first, dptr->second);
407  }
408  dist = 0;
409  last = ref;
410  }
411  }
412  // all items, no stop
413  return false;
414  });
415 
416  // print verticies ------------------------------------------------
417 
419 
420  Vertices_t::Data dummy;
421  verticies->each(dummy, [](Key id, Vertices_t::Data & v) {
422  pos += 1.0;
423 
424  // europa: lon -10..25
425  // lat 35..70
426  // projektion: 1000px to 35 grad
427 
428  double lon = v.first._lon - LON_BOUND;
429  double lat = v.first._lat - LAT_BOUND;
430  int x = IMAGE_WIDTH*(lon/LON_BOUND_DIF);
431  int y = IMAGE_HEIGHT - IMAGE_HEIGHT*(lat/LAT_BOUND_DIF);
432  c = bild.get(x, y);
433 
434  // set color only if nothing set there before
435  if (c.red == 0 && c.green ==0 && c.blue == 0) {
436  bild.get(x, y) = toColor(pos);
437  }
438 
439  // all items, no stop
440  return false;
441  });
442 
443  bild.write("graphnodes.bmp");
444 
445  delete distances;
446  delete verticies;
447  delete nodes;
448  delete ways;
449 
450  return 0;
451 }
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
Color c
Definition: osm2graph_bitmap.cpp:123
Ways_t * ways
Definition: osm2graph_bitmap.cpp:86
Color toColor(double id)
Definition: osm2graph_bitmap.cpp:127
SwappyItems< Key, Vertex, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Vertices_t
Definition: osm2graph_bitmap.cpp:90
#define LAT_BOUND
Definition: osm2graph_bitmap.cpp:42
#define IMAGE_HEIGHT
Definition: osm2graph_bitmap.cpp:15
#define LON_BOUND_DIF
Definition: osm2graph_bitmap.cpp:41
SwappyItems< Key, Distance, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Distance_t
Definition: osm2graph_bitmap.cpp:92
#define LON_BOUND
Definition: osm2graph_bitmap.cpp:40
double pos
Definition: osm2graph_bitmap.cpp:125
SwappyItems< Key, WayData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Ways_t
Definition: osm2graph_bitmap.cpp:84
Nodes_t * nodes
Definition: osm2graph_bitmap.cpp:87
#define IMAGE_WIDTH
Definition: osm2graph_bitmap.cpp:14
Distance_t * distances
Definition: osm2graph_bitmap.cpp:93
SwappyItems< Key, NodeData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > Nodes_t
Definition: osm2graph_bitmap.cpp:85
Key calcDist(double lon1, double lat1, double lon2, double lat2)
Definition: osm2graph_bitmap.cpp:99
Bmp bild
Definition: osm2graph_bitmap.cpp:122
Vertices_t * verticies
Definition: osm2graph_bitmap.cpp:91
#define LAT_BOUND_DIF
Definition: osm2graph_bitmap.cpp:43
void init(int pwidth, int pheight)
Definition: Bmp.hpp:60
Color & get(int x, int y)
Definition: Bmp.hpp:34
void write(const char *filename)
Definition: Bmp.hpp:97
BMP_byte blue
Definition: Bmp.hpp:12
BMP_byte red
Definition: Bmp.hpp:14
BMP_byte green
Definition: Bmp.hpp:13
Definition: osm2graph.cpp:120

◆ relevance()

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

◆ toColor()

Color toColor ( double  id)
127  {
128  Color col;
129  col.red = 0; col.green = 0; col.blue = 0;
130  if( id >= MAX_NODES ) return col;
131  double hi,q,t,coef;
132 
133  coef = 7.0 * (id/MAX_NODES);
134  hi = floor(coef);
135  t = coef - hi;
136  q = 1 - t;
137 
138  if (hi == 0.0) {
139  col.red = 0;
140  col.green = t*255; //immer mehr green und blau -> dunkelblau zu cyan
141  col.blue = t*127 + 128;
142  } else if (hi == 1.0) {
143  col.red = t*255; //immer mehr rot -> cyan zu weiss
144  col.green = 255;
145  col.blue = 255;
146  } else if (hi == 2.0) {
147  col.red = 255;
148  col.green = 255;
149  col.blue = q*255; // immer weniger blau -> weiss zu gelb
150  } else if (hi == 3.0) {
151  col.red = 255;
152  col.green = q*127 + 128; // immer weniger green -> gelb zu orange
153  col.blue = 0;
154  } else if (hi == 4.0) {
155  col.red = q*127 + 128; // orange wird dunkler -> orange zu braun
156  col.green = q*63 + 64;
157  col.blue = 0;
158  } else if (hi == 5.0) {
159  col.red = 128;
160  col.green = 64;
161  col.blue = t*128; // mehr blau -> braun zu violett
162  } else if (hi == 6.0) {
163  col.red = t*127 + 128; // weniger blau und green, mehr rot -> violett wird rot
164  col.green = q*64;
165  col.blue = q*128;
166  }
167  return col;
168 }
#define MAX_NODES
Definition: osm2graph_bitmap.cpp:46
Definition: Bmp.hpp:11

Variable Documentation

◆ bild

Bmp bild

◆ c

Color c

◆ distances

Distance_t* distances

◆ nodes

Nodes_t* nodes

◆ pos

double pos = 0.0

◆ verticies

Vertices_t* verticies

◆ ways

Ways_t* ways