alaCarte Maps
Renderer for OpenStreetMap tiles
job.cpp
Go to the documentation of this file.
1 
23 #include "server/job.hpp"
25 #include "server/cache.hpp"
26 #include "server/tile.hpp"
29 #include "server/stylesheet.hpp"
32 #include "server/http_request.hpp"
33 #include "general/geodata.hpp"
36 #include "utils/rect.hpp"
37 #include "utils/transform.hpp"
38 #include "utils/statistic.hpp"
39 
40 #define STAT_START(_X) Statistic::Get()->start(measurement, _X)
41 #define STAT_STOP(_X) Statistic::Get()->stop(measurement, _X)
42 #define STAT_STATS(_X, _Y, _Z) Statistic::Get()->setStats(measurement, _X, _Y, _Z)
43 #define STAT_WRITE() Statistic::Get()->finished(measurement)
44 
51 Job::Job(const shared_ptr<MetaIdentifier>& mid,
52  const shared_ptr<Configuration>& config,
53  const shared_ptr<RequestManager>& manager,
54  const shared_ptr<RenderCanvas>& canvas)
55  : manager(manager)
56  , config(config)
57  , mid(mid)
58  , canvas(canvas)
59  , measurement(Statistic::Get()->startNewMeasurement(mid->getStylesheetPath(), mid->getZoom()))
60 {
61 }
62 
68 FixedRect Job::computeRect(const shared_ptr<TileIdentifier>& ti)
69 {
70  int zoom = ti->getZoom();
71  coord_t x0, x1, y0, y1;
72  tileToMercator(ti->getX(), ti->getY(), zoom, x0, y0);
73  tileToMercator(ti->getX() + 1, ti->getY() + 1, zoom, x1, y1);
74 
75  coord_t minX = std::min(x0, x1);
76  coord_t minY = std::min(y0, y1);
77  coord_t maxX = std::max(x0, x1);
78  coord_t maxY = std::max(y0, y1);
79 
80  FixedRect tile(minX, minY, maxX, maxY);
81  return tile.grow(tile.getWidth() * TILE_OVERLAP,
82  tile.getHeight() * TILE_OVERLAP);
83 }
84 
90 FixedRect Job::computeRect(const shared_ptr<MetaIdentifier>& ti)
91 {
92  int zoom = ti->getIdentifiers()[0]->getZoom();
93  coord_t x0, x1, y0, y1;
94  tileToMercator(ti->getX(), ti->getY(), zoom, x0, y0);
95  tileToMercator(ti->getX() + ti->getWidth(),
96  ti->getY() + ti->getHeight(),
97  zoom, x1, y1);
98 
99  coord_t minX = std::min(x0, x1);
100  coord_t minY = std::min(y0, y1);
101  coord_t maxX = std::max(x0, x1);
102  coord_t maxY = std::max(y0, y1);
103 
104  FixedRect tile(minX, minY, maxX, maxY);
105  return tile.grow(tile.getWidth() * TILE_OVERLAP,
106  tile.getHeight() * TILE_OVERLAP);
107 }
108 
113 shared_ptr<Tile> Job::computeEmpty()
114 {
115  const string& path = mid->getStylesheetPath();
116  const TileIdentifier::Format format = mid->getImageFormat();
117  shared_ptr<Stylesheet> stylesheet = manager->getStylesheetManager()->getStylesheet(mid->getStylesheetPath());
118  shared_ptr<TileIdentifier> emptyID = TileIdentifier::CreateEmptyTID(path, format);
119  shared_ptr<Tile> tile = manager->getCache()->getTile(emptyID);
120 
121  if(!tile->isRendered()) {
122  shared_ptr<std::vector<NodeId>> nodeIDs = boost::make_shared< std::vector<NodeId>>();
123  shared_ptr<std::vector<WayId>> wayIDs = boost::make_shared< std::vector<WayId>>();
124  shared_ptr<std::vector<RelId>> relationIDs = boost::make_shared< std::vector<RelId>>();
125 
126  RenderAttributes renderAttributes;
127 
128  stylesheet->match(nodeIDs, wayIDs, relationIDs, mid, &renderAttributes);
129  manager->getRenderer()->renderEmptyTile(renderAttributes, canvas, tile);
130  }
131 
132  return tile;
133 }
134 
140 {
141  bool rendered = true;
142  for (auto& id : mid->getIdentifiers())
143  {
144  shared_ptr<Tile> tile = manager->getCache()->getTile(id);
145  rendered = rendered && tile->isRendered();
146  tiles.push_back(tile);
147  }
148 
149  return rendered;
150 }
151 
157 {
158  shared_ptr<Geodata> geodata = manager->getGeodata();
159 
160  FixedRect rect = computeRect(mid);
162  empty = !geodata->containsData(rect);
164 
165  if(empty) {
166  STAT_WRITE();
167  return;
168  }
169 
170  cached = initTiles();
171  if (cached) {
172  STAT_WRITE();
173  return;
174  }
175 
177  auto nodeIDs = geodata->getNodeIDs(rect);
179 
181  auto wayIDs = geodata->getWayIDs(rect);
183 
185  auto relationIDs = geodata->getRelationIDs(rect);
187 
188  STAT_STATS(nodeIDs->size(), wayIDs->size(), relationIDs->size());
189 
190  shared_ptr<Stylesheet> stylesheet = manager->getStylesheetManager()->getStylesheet(mid->getStylesheetPath());
191  RenderAttributes renderAttributes;
193  stylesheet->match(nodeIDs, wayIDs, relationIDs, mid, &renderAttributes);
195 
196  const shared_ptr<Renderer>& renderer = manager->getRenderer();
198  renderer->renderMetaTile(renderAttributes, canvas, mid);
200 }
201 
202 /*
203  * @brief answers the requets for the computed tiles. Called by RequestManager
204  */
206 {
207  if (empty)
208  {
209  shared_ptr<Tile> tile = computeEmpty();
210  for (auto& id : mid->getIdentifiers())
211  {
212  for (auto& req : requests[*id])
213  req->answer(tile);
214  }
215  } else {
216  const shared_ptr<Renderer>& renderer = manager->getRenderer();
218  for (auto& tile : tiles) {
219  if (!tile->isRendered())
220  renderer->sliceTile(canvas, mid, tile);
221 
222  for (auto& req : requests[*tile->getIdentifier()])
223  req->answer(tile);
224  }
225  if (!cached)
227  }
228 
229  STAT_WRITE();
230 }
231 
T getHeight() const
Definition: rect.hpp:158
#define STAT_STATS(_X, _Y, _Z)
Definition: job.cpp:42
void deliver()
Definition: job.cpp:205
A RenderAttributes object contains mappings from NodeIDs and WayIDs to Style objects which define how...
std::vector< shared_ptr< Tile > > tiles
initialized by initTiles
Definition: job.hpp:74
std::int32_t coord_t
Definition: settings.hpp:117
Format
Enumeration which contains all supported image formats.
T getWidth() const
Definition: rect.hpp:154
void tileToMercator(int tx, int ty, int zoom, coord_t &x, coord_t &y)
converts tile coordinates to north-west corner of the tile in the Mercator projection.
Definition: transform.cpp:52
#define STAT_STOP(_X)
Definition: job.cpp:41
TESTABLE FixedRect computeRect(const shared_ptr< TileIdentifier > &ti)
Computes an rectangle for the given TileIdentifier.
Definition: job.cpp:68
shared_ptr< RenderCanvas > canvas
supplied by worker thread
Definition: job.hpp:68
boost::unordered_map< TileIdentifier, std::list< shared_ptr< HttpRequest > > > requests
Definition: job.hpp:75
#define STAT_WRITE()
Definition: job.cpp:43
TESTABLE shared_ptr< Tile > computeEmpty()
Computes an empty Tile.
Definition: job.cpp:113
basic_rect< T > grow(T dx, T dy) const
Definition: rect.hpp:91
shared_ptr< MetaIdentifier > mid
Definition: job.hpp:70
static const char * config
Option to get the configuration filename (type: string)
#define STAT_START(_X)
This file is part of alaCarte.
Definition: job.cpp:40
static shared_ptr< TileIdentifier > CreateEmptyTID(const string &stylesheetPath, TileIdentifier::Format format)
Return the TileIdentifier used for Tiles without data.
void process()
Computes a all tiles contained in the MetaIdentifier.
Definition: job.cpp:156
bool empty
Definition: job.hpp:71
shared_ptr< RequestManager > manager
RequestManager which holds all important components.
Definition: job.hpp:66
#define TILE_OVERLAP
Definition: settings.hpp:130
bool cached
Definition: job.hpp:72
Job(const shared_ptr< MetaIdentifier > &mid, const shared_ptr< Configuration > &config, const shared_ptr< RequestManager > &manager, const shared_ptr< RenderCanvas > &canvas)
Creates a new Job and sets the RequestManager.
Definition: job.cpp:51
bool initTiles()
Inits the internal list of tiles that are part of the MetaTile.
Definition: job.cpp:139