tile_downloader.h
Go to the documentation of this file.
1 /* Copyright 2018-2019 TomTom N.V.
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 #include <functional>
16 
17 #include <QCryptographicHash>
18 #include <QDir>
19 #include <QImage>
20 #include <QImageReader>
21 #include <QStandardPaths>
22 #include <QString>
23 #include <QtCore>
24 #include <QtNetwork>
25 
26 #include <ros/ros.h>
27 
29 #include "tile_id.h"
30 
31 namespace detail
32 {
38 class TileDownloader : public QObject
39 {
40  Q_OBJECT
41  QNetworkAccessManager* manager;
42  std::function<void(TileId, QImage)> callback;
43 
44 public:
46 
47  TileDownloader(decltype(callback) callback) : manager(new QNetworkAccessManager(this)), callback(std::move(callback))
48  {
49  connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*)));
50 
51  QNetworkDiskCache* disk_cache = new QNetworkDiskCache(this);
52  QString const cache_path =
53  QDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)).filePath("rviz_satellite");
54  disk_cache->setCacheDirectory(cache_path);
55  // there is no option to disable maximum cache size
56  disk_cache->setMaximumCacheSize(std::numeric_limits<qint64>::max());
57  manager->setCache(disk_cache);
58  }
59 
66  void loadTile(TileId const& tile_id)
67  {
68  // see https://foundation.wikimedia.org/wiki/Maps_Terms_of_Use#Using_maps_in_third-party_services
69  auto const request_url = QUrl(QString::fromStdString(tileURL(tile_id)));
70  ROS_DEBUG_STREAM_NAMED("rviz_satellite", "Loading tile " << request_url.toString().toStdString());
71 
72  QNetworkRequest request(request_url);
73  char constexpr agent[] = "rviz_satellite/" RVIZ_SATELLITE_VERSION " (+https://github.com/gareth-cross/"
74  "rviz_satellite)";
75  request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, agent);
76  QVariant variant;
77  variant.setValue(tile_id);
78  request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::CacheLoadControl::PreferCache);
79  request.setAttribute(QNetworkRequest::User, variant);
80  manager->get(request);
81  }
82 
83 public slots:
84  void downloadFinished(QNetworkReply* reply)
85  {
86  QVariant const variant = reply->request().attribute(QNetworkRequest::User);
87  TileId const tile_id = variant.value<TileId>();
88 
89  QUrl const url = reply->url();
90  if (reply->error())
91  {
92  ROS_ERROR_STREAM("Got error when loading tile: " << reply->errorString().toStdString());
93  error_rates.issueError(tile_id.tile_server);
94  return;
95  }
96  else
97  {
98  error_rates.issueSuccess(tile_id.tile_server);
99  }
100 
101  // log if tile comes from cache or web
102  bool const from_cache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool();
103  if (from_cache)
104  {
105  ROS_DEBUG_STREAM_NAMED("rviz_satellite", "Loaded tile from cache " << url.toString().toStdString());
106  }
107  else
108  {
109  ROS_DEBUG_STREAM_NAMED("rviz_satellite", "Loaded tile from web " << url.toString().toStdString());
110  }
111 
112  QImageReader reader(reply);
113  if (!reader.canRead())
114  {
115  ROS_ERROR_STREAM("Unable to decode image at " << reply->request().url().toString().toLatin1().data());
116  return;
117  }
118 
119  callback(tile_id, reader.read());
120 
121  reply->deleteLater();
122  }
123 };
124 
125 } // namespace detail
TileDownloader(decltype(callback) callback)
std::string tileURL(TileId const &tile_id)
Definition: tile_id.cpp:35
#define ROS_DEBUG_STREAM_NAMED(name, args)
std::string tile_server
Definition: tile_id.h:33
detail::ErrorRateManager< std::string > error_rates
void downloadFinished(QNetworkReply *reply)
std::function< void(TileId, QImage)> callback
void issueError(T const &id)
Tile downloader.
void loadTile(TileId const &tile_id)
Load a specific tile.
QNetworkAccessManager * manager
void issueSuccess(T const &id)
#define ROS_ERROR_STREAM(args)
Definition: tile_id.h:31


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