tile_cache.h
Go to the documentation of this file.
1 /* Copyright 2018-2019 TomTom N.V., 2014 Gareth Cross
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7 http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License. */
14 
15 #pragma once
16 
17 #include <functional>
18 #include <limits>
19 #include <mutex>
20 #include <unordered_map>
21 #include <utility>
22 
23 #include <QImage>
24 
25 #include <boost/optional.hpp>
26 
27 #include "area.h"
28 #include "detail/tile_downloader.h"
29 #include "tile_id.h"
30 
35 {
36  std::mutex& mutex;
37 
38 public:
39  template <typename TileCache>
40  TileCacheGuard(TileCache const& cache_) : mutex(cache_.cachedTilesLock)
41  {
42  mutex.lock();
43  }
44 
46  {
47  mutex.unlock();
48  }
49 };
50 
60 template <typename Tile>
61 class TileCache
62 {
64  std::unordered_map<TileId, Tile> cached_tiles;
65  std::mutex mutable cachedTilesLock;
67 
71  void loadedTile(TileId tile_id, QImage image)
72  {
73  TileCacheGuard guard(*this);
74 
75  if (cached_tiles.find(tile_id) == cached_tiles.end())
76  {
77  cached_tiles.emplace(std::make_pair(tile_id, std::move(image)));
78  }
79  }
80 
81 public:
83  : downloader([this](TileId tile_id, QImage image) { loadedTile(std::move(tile_id), std::move(image)); }){};
84 
90  void request(Area const& area)
91  {
92  TileCacheGuard guard(*this);
93 
94  for (int x = area.left_top.x; x <= area.right_bottom.x; ++x)
95  {
96  for (int y = area.left_top.y; y <= area.right_bottom.y; ++y)
97  {
98  TileId const to_find{ area.center.tile_server, { x, y }, area.center.zoom };
99 
100  if (cached_tiles.find(to_find) == cached_tiles.end())
101  {
102  downloader.loadTile(to_find);
103  }
104  }
105  }
106  }
107 
112  Tile const* ready(TileId const& to_find) const
113  {
114  auto const it = cached_tiles.find(to_find);
115 
116  if (it == cached_tiles.cend())
117  {
118  return nullptr;
119  }
120 
121  return &it->second;
122  }
123 
128  void purge(Area const& area)
129  {
130  for (auto it = cached_tiles.begin(); it != cached_tiles.end();)
131  {
132  if (!areaContainsTile(area, it->first))
133  {
134  it = cached_tiles.erase(it);
135  }
136  else
137  {
138  ++it;
139  }
140  }
141  }
142 
148  float getTileServerErrorRate(std::string const& tile_server) const
149  {
150  return downloader.error_rates.calculate(tile_server);
151  }
152 
153 protected:
158  bool isAreaReady(Area const& area) const
159  {
160  for (int xx = area.left_top.x; xx <= area.right_bottom.x; ++xx)
161  {
162  for (int yy = area.left_top.y; yy <= area.right_bottom.y; ++yy)
163  {
164  TileId const to_find{ area.center.tile_server, { xx, yy }, area.center.zoom };
165 
166  if (cached_tiles.find(to_find) == cached_tiles.end())
167  {
168  return false;
169  }
170  }
171  }
172  return true;
173  }
174 };
bool areaContainsTile(Area const &haystack, TileId const &needle)
Definition: area.h:75
void request(Area const &area)
Definition: tile_cache.h:90
float calculate(T const &id) const
std::unordered_map< TileId, Tile > cached_tiles
Definition: tile_cache.h:64
std::string tile_server
Definition: tile_id.h:33
detail::ErrorRateManager< std::string > error_rates
friend TileCacheGuard
Definition: tile_cache.h:63
A cache for tiles.
Definition: tile_cache.h:61
TileId center
Definition: area.h:35
int zoom
Definition: tile_id.h:35
Tile downloader.
std::mutex & mutex
Definition: tile_cache.h:36
void loadTile(TileId const &tile_id)
Load a specific tile.
Definition: area.h:31
float getTileServerErrorRate(std::string const &tile_server) const
Calculate the error rate of a tile server.
Definition: tile_cache.h:148
detail::TileDownloader downloader
Definition: tile_cache.h:66
TileCacheGuard(TileCache const &cache_)
Definition: tile_cache.h:40
TileCoordinate right_bottom
Definition: area.h:34
void loadedTile(TileId tile_id, QImage image)
Definition: tile_cache.h:71
bool isAreaReady(Area const &area) const
Definition: tile_cache.h:158
TileCoordinate left_top
Definition: area.h:33
std::mutex cachedTilesLock
Definition: tile_cache.h:65
Definition: tile_id.h:31
void purge(Area const &area)
Definition: tile_cache.h:128
Tile const * ready(TileId const &to_find) const
Definition: tile_cache.h:112


rviz_satellite
Author(s): Gareth Cross , Andre Schröder
autogenerated on Thu May 4 2023 02:31:43