7#include <config-kosmindoormap.h>
10#include "boundarysearch_p.h"
13#include "marblegeometryassembler_p.h"
14#include "tilecache_p.h"
16#include "network/useragent_p.h"
18#include <osm/datatypes.h>
19#include <osm/datasetmergebuffer.h>
20#include <osm/element.h>
21#include <osm/o5mparser.h>
25#include <QElapsedTimer>
27#include <QNetworkReply>
28#include <QNetworkRequest>
40inline void initResources()
43 Q_INIT_RESOURCE(assets);
48class MapLoaderPrivate {
53 MarbleGeometryAssembler m_marbleMerger;
55 TileCache m_tileCache{m_nam};
59 std::vector<Tile> m_pendingTiles;
60 std::unique_ptr<BoundarySearch> m_boundarySearcher;
62 std::deque<QUrl> m_pendingChangeSets;
70MapLoader::MapLoader(
QObject *parent)
72 , d(new MapLoaderPrivate)
75 connect(&d->m_tileCache, &TileCache::tileLoaded,
this, &MapLoader::downloadFinished);
76 connect(&d->m_tileCache, &TileCache::tileError,
this, &MapLoader::downloadFailed);
77 d->m_tileCache.expire();
80MapLoader::~MapLoader() =
default;
87 d->m_errorMessage.clear();
93 const auto data = f.
map(0, f.
size());
97 qCWarning(
Log) <<
"no file reader for" << fileName;
100 reader->read(data, f.
size());
102 qCDebug(
Log) <<
"o5m loading took" << loadTime.
elapsed() <<
"ms";
115 d->m_targetBbox = {};
116 d->m_pendingTiles.clear();
117 d->m_boundarySearcher = std::make_unique<BoundarySearch>();
119 d->m_errorMessage.clear();
120 d->m_marbleMerger.setDataSet(&d->m_dataSet);
123 auto tile = Tile::fromCoordinate(lat, lon, TileZoomLevel);
124 d->m_loadedTiles =
QRect(tile.x, tile.y, 1, 1);
125 d->m_pendingTiles.push_back(std::move(tile));
133 d->m_targetBbox = box;
134 d->m_pendingTiles.clear();
135 d->m_errorMessage.clear();
136 d->m_marbleMerger.setDataSet(&d->m_dataSet);
139 const auto topLeftTile = Tile::fromCoordinate(box.min.latF(), box.min.lonF(), TileZoomLevel);
140 const auto bottomRightTile = Tile::fromCoordinate(box.max.latF(), box.max.lonF(), TileZoomLevel);
141 for (
auto x = topLeftTile.x; x <= bottomRightTile.x; ++x) {
142 for (
auto y = bottomRightTile.y; y <= topLeftTile.y; ++y) {
143 d->m_pendingTiles.push_back(makeTile(x, y));
157 d->m_tileBbox = tile.boundingBox();
158 d->m_targetBbox = {};
159 d->m_pendingTiles.clear();
160 d->m_errorMessage.clear();
161 d->m_marbleMerger.setDataSet(&d->m_dataSet);
164 if (tile.z >= TileZoomLevel) {
165 d->m_pendingTiles.push_back(std::move(tile));
167 const auto start = tile.topLeftAtZ(TileZoomLevel);
168 const auto end = tile.bottomRightAtZ(TileZoomLevel);
169 for (
auto x =
start.x; x <= end.x; ++x) {
170 for (
auto y =
start.y; y <= end.y; ++y) {
171 d->m_pendingTiles.push_back(makeTile(x, y));
181 d->m_pendingChangeSets.push_back(url);
186 return std::move(d->m_data);
189void MapLoader::downloadTiles()
191 for (
const auto &tile : d->m_pendingTiles) {
192 d->m_tileCache.ensureCached(tile);
194 if (d->m_tileCache.pendingDownloads() == 0) {
200 Q_EMIT isLoadingChanged();
204void MapLoader::downloadFinished()
206 if (d->m_tileCache.pendingDownloads() > 0) {
212void MapLoader::loadTiles()
218 p.setMergeBuffer(&d->m_mergeBuffer);
219 for (
const auto &tile : d->m_pendingTiles) {
220 const auto fileName = d->m_tileCache.cachedTile(tile);
221 qCDebug(
Log) <<
"loading tile" << fileName;
224 qWarning() <<
"Failed to open tile!" << f.fileName() << f.errorString();
228 const auto data = f.map(0, f.size());
230 qCritical() <<
"Failed to mmap tile!" << f.fileName() << f.size() << f.errorString();
234 p.read(data, f.size());
235 d->m_marbleMerger.merge(&d->m_mergeBuffer);
237 d->m_tileBbox = OSM::unite(d->m_tileBbox, tile.boundingBox());
239 d->m_pendingTiles.clear();
241 if (d->m_boundarySearcher) {
242 const auto bbox = d->m_boundarySearcher->boundingBox(d->m_dataSet);
243 qCDebug(
Log) <<
"needed bbox:" << bbox <<
"got:" << d->m_tileBbox << d->m_loadedTiles;
246 if (bbox.min.longitude < d->m_tileBbox.min.longitude) {
247 d->m_loadedTiles.setLeft(d->m_loadedTiles.left() - 1);
248 for (
int y = d->m_loadedTiles.top(); y <= d->m_loadedTiles.bottom(); ++y) {
249 d->m_pendingTiles.push_back(makeTile(d->m_loadedTiles.left(), y));
252 if (bbox.max.longitude > d->m_tileBbox.max.longitude) {
253 d->m_loadedTiles.setRight(d->m_loadedTiles.right() + 1);
254 for (
int y = d->m_loadedTiles.top(); y <= d->m_loadedTiles.bottom(); ++y) {
255 d->m_pendingTiles.push_back(makeTile(d->m_loadedTiles.right(), y));
260 if (bbox.max.latitude > d->m_tileBbox.max.latitude) {
261 d->m_loadedTiles.setTop(d->m_loadedTiles.top() - 1);
262 for (
int x = d->m_loadedTiles.left(); x <= d->m_loadedTiles.right(); ++x) {
263 d->m_pendingTiles.push_back(makeTile(x, d->m_loadedTiles.top()));
266 if (bbox.min.latitude < d->m_tileBbox.min.latitude) {
267 d->m_loadedTiles.setBottom(d->m_loadedTiles.bottom() + 1);
268 for (
int x = d->m_loadedTiles.left(); x <= d->m_loadedTiles.right(); ++x) {
269 d->m_pendingTiles.push_back(makeTile(x, d->m_loadedTiles.bottom()));
273 if (!d->m_pendingTiles.empty()) {
277 d->m_targetBbox = bbox;
280 d->m_marbleMerger.finalize();
281 d->m_boundarySearcher.reset();
283 qCDebug(
Log) <<
"o5m loading took" << loadTime.
elapsed() <<
"ms";
284 applyNextChangeSet();
287Tile MapLoader::makeTile(uint32_t x, uint32_t y)
const
289 auto tile = Tile(x, y, TileZoomLevel);
294void MapLoader::downloadFailed(Tile tile,
const QString& errorMessage)
298 d->m_tileCache.cancelPending();
299 Q_EMIT isLoadingChanged();
305 return d->m_tileCache.pendingDownloads() > 0 || !d->m_pendingChangeSets.empty();
308bool MapLoader::hasError()
const
310 return !d->m_errorMessage.isEmpty();
313QString MapLoader::errorMessage()
const
315 return d->m_errorMessage;
318void MapLoader::applyNextChangeSet()
320 if (d->m_pendingChangeSets.empty() || hasError()) {
321 d->m_data.setDataSet(std::move(d->m_dataSet));
322 if (d->m_targetBbox.isValid()) {
323 d->m_data.setBoundingBox(d->m_targetBbox);
326 Q_EMIT isLoadingChanged();
331 const auto &url = d->m_pendingChangeSets.front();
332 if (url.isLocalFile()) {
333 QFile f(url.toLocalFile());
335 qCWarning(
Log) << f.fileName() << f.errorString();
336 d->m_errorMessage = f.errorString();
338 applyChangeSet(url, &f);
340 }
else if (url.scheme() ==
"https"_L1) {
343 auto reply = d->m_nam()->get(req);
345 reply->deleteLater();
348 d->m_errorMessage = reply->errorString();
350 applyChangeSet(url, reply);
353 d->m_pendingChangeSets.pop_front();
354 applyNextChangeSet();
359 d->m_pendingChangeSets.pop_front();
360 applyNextChangeSet();
363void MapLoader::applyChangeSet(
const QUrl &url,
QIODevice *io)
367 qCWarning(
Log) <<
"unable to find reader for" << url;
372 if (reader->hasError()) {
373 d->m_errorMessage = reader->errorString();
377#include "moc_maploader.cpp"
Raw OSM map data, separated by levels.
Q_INVOKABLE void loadFromFile(const QString &fileName)
Load a single O5M or OSM PBF file.
void done()
Emitted when the requested data has been loaded.
bool isLoading
Indicates we are downloading content.
void loadForBoundingBox(OSM::BoundingBox box)
Load map data for the given bounding box, without applying the boundary search.
Q_INVOKABLE void loadForCoordinate(double lat, double lon)
Load map for the given coordinates.
Q_INVOKABLE void addChangeSet(const QUrl &url)
Add a changeset to be applied on top of the data loaded by any of the load() methods.
MapData && takeData()
Take out the completely loaded result.
void loadForTile(Tile tile)
Load map data for the given tile.
Coordinate, stored as 1e7 * degree to avoid floating point precision issues, and offset to unsigned v...
Holds OSM elements produced by a parser prior to merging into OSM::DataSet.
A set of nodes, ways and relations.
Zero-copy parser of O5M binary files.
Q_SCRIPTABLE Q_NOREPLY void start()
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
OSM-based multi-floor indoor maps for buildings.
KOSMINDOORMAP_EXPORT QNetworkAccessManager * defaultNetworkAccessManagerFactory()
Default implementation if not using an application-specific QNetworkAccessManager instance.
KOSM_EXPORT std::unique_ptr< AbstractReader > readerForFileName(QStringView fileName, OSM::DataSet *dataSet)
Returns a suitable reader for the given file name.
qint64 elapsed() const const
virtual QString fileName() const const override
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
virtual qint64 size() const const override
uchar * map(qint64 offset, qint64 size, MemoryMapFlags flags)
QString errorString() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString fileName(ComponentFormattingOptions options) const const