7#include <config-kosmindoormap.h>
10#include "boundarysearch_p.h"
13#include "marblegeometryassembler_p.h"
14#include "tilecache_p.h"
16#include <osm/datatypes.h>
17#include <osm/datasetmergebuffer.h>
18#include <osm/element.h>
19#include <osm/o5mparser.h>
23#include <QElapsedTimer>
32inline void initResources()
35 Q_INIT_RESOURCE(assets);
40class MapLoaderPrivate {
44 MarbleGeometryAssembler m_marbleMerger;
46 TileCache m_tileCache;
49 std::vector<Tile> m_pendingTiles;
50 std::unique_ptr<BoundarySearch> m_boundarySearcher;
59MapLoader::MapLoader(
QObject *parent)
61 , d(new MapLoaderPrivate)
64 connect(&d->m_tileCache, &TileCache::tileLoaded,
this, &MapLoader::downloadFinished);
65 connect(&d->m_tileCache, &TileCache::tileError,
this, &MapLoader::downloadFailed);
66 d->m_tileCache.expire();
69MapLoader::~MapLoader() =
default;
76 d->m_errorMessage.clear();
79 qCritical() << f.
fileName() << f.errorString();
82 const auto data = f.map(0, f.size());
87 qCWarning(
Log) <<
"no file reader for" << fileName;
90 reader->read(data, f.size());
92 d->m_data.setDataSet(std::move(ds));
93 qCDebug(
Log) <<
"o5m loading took" << loadTime.
elapsed() <<
"ms";
106 d->m_pendingTiles.clear();
107 d->m_boundarySearcher = std::make_unique<BoundarySearch>();
109 d->m_errorMessage.clear();
110 d->m_marbleMerger.setDataSet(&d->m_dataSet);
113 auto tile = Tile::fromCoordinate(lat, lon, TileZoomLevel);
114 d->m_loadedTiles =
QRect(tile.x, tile.y, 1, 1);
115 d->m_pendingTiles.push_back(std::move(tile));
123 d->m_pendingTiles.clear();
124 d->m_errorMessage.clear();
125 d->m_marbleMerger.setDataSet(&d->m_dataSet);
128 const auto topLeftTile = Tile::fromCoordinate(box.min.latF(), box.min.lonF(), TileZoomLevel);
129 const auto bottomRightTile = Tile::fromCoordinate(box.max.latF(), box.max.lonF(), TileZoomLevel);
130 for (
auto x = topLeftTile.x; x <= bottomRightTile.x; ++x) {
131 for (
auto y = bottomRightTile.y; y <= topLeftTile.y; ++y) {
132 d->m_pendingTiles.push_back(makeTile(x, y));
141 d->m_tileBbox = tile.boundingBox();
142 d->m_pendingTiles.clear();
143 d->m_errorMessage.clear();
144 d->m_marbleMerger.setDataSet(&d->m_dataSet);
147 if (tile.z >= TileZoomLevel) {
148 d->m_pendingTiles.push_back(std::move(tile));
150 const auto start = tile.topLeftAtZ(TileZoomLevel);
151 const auto end = tile.bottomRightAtZ(TileZoomLevel);
152 for (
auto x =
start.x; x <= end.x; ++x) {
153 for (
auto y =
start.y; y <= end.y; ++y) {
154 d->m_pendingTiles.push_back(makeTile(x, y));
164 return std::move(d->m_data);
167void MapLoader::downloadTiles()
169 for (
const auto &tile : d->m_pendingTiles) {
170 d->m_tileCache.ensureCached(tile);
172 if (d->m_tileCache.pendingDownloads() == 0) {
178 Q_EMIT isLoadingChanged();
182void MapLoader::downloadFinished()
184 if (d->m_tileCache.pendingDownloads() > 0) {
190void MapLoader::loadTiles()
196 p.setMergeBuffer(&d->m_mergeBuffer);
197 for (
const auto &tile : d->m_pendingTiles) {
198 const auto fileName = d->m_tileCache.cachedTile(tile);
199 qCDebug(
Log) <<
"loading tile" << fileName;
202 qWarning() << f.fileName() << f.errorString();
205 const auto data = f.map(0, f.size());
206 p.read(data, f.size());
207 d->m_marbleMerger.merge(&d->m_mergeBuffer);
209 d->m_tileBbox = OSM::unite(d->m_tileBbox, tile.boundingBox());
211 d->m_pendingTiles.clear();
213 if (d->m_boundarySearcher) {
214 const auto bbox = d->m_boundarySearcher->boundingBox(d->m_dataSet);
215 qCDebug(
Log) <<
"needed bbox:" << bbox <<
"got:" << d->m_tileBbox << d->m_loadedTiles;
218 if (bbox.min.longitude < d->m_tileBbox.min.longitude) {
219 d->m_loadedTiles.setLeft(d->m_loadedTiles.left() - 1);
220 for (
int y = d->m_loadedTiles.top(); y <= d->m_loadedTiles.bottom(); ++y) {
221 d->m_pendingTiles.push_back(makeTile(d->m_loadedTiles.left(), y));
224 if (bbox.max.longitude > d->m_tileBbox.max.longitude) {
225 d->m_loadedTiles.setRight(d->m_loadedTiles.right() + 1);
226 for (
int y = d->m_loadedTiles.top(); y <= d->m_loadedTiles.bottom(); ++y) {
227 d->m_pendingTiles.push_back(makeTile(d->m_loadedTiles.right(), y));
232 if (bbox.max.latitude > d->m_tileBbox.max.latitude) {
233 d->m_loadedTiles.setTop(d->m_loadedTiles.top() - 1);
234 for (
int x = d->m_loadedTiles.left(); x <= d->m_loadedTiles.right(); ++x) {
235 d->m_pendingTiles.push_back(makeTile(x, d->m_loadedTiles.top()));
238 if (bbox.min.latitude < d->m_tileBbox.min.latitude) {
239 d->m_loadedTiles.setBottom(d->m_loadedTiles.bottom() + 1);
240 for (
int x = d->m_loadedTiles.left(); x <= d->m_loadedTiles.right(); ++x) {
241 d->m_pendingTiles.push_back(makeTile(x, d->m_loadedTiles.bottom()));
245 if (!d->m_pendingTiles.empty()) {
249 d->m_data.setBoundingBox(bbox);
252 d->m_marbleMerger.finalize();
253 d->m_data.setDataSet(std::move(d->m_dataSet));
254 d->m_boundarySearcher.reset();
256 qCDebug(
Log) <<
"o5m loading took" << loadTime.
elapsed() <<
"ms";
257 Q_EMIT isLoadingChanged();
261Tile MapLoader::makeTile(uint32_t x, uint32_t y)
const
263 auto tile = Tile(x, y, TileZoomLevel);
268void MapLoader::downloadFailed(Tile tile,
const QString& errorMessage)
272 d->m_tileCache.cancelPending();
273 Q_EMIT isLoadingChanged();
279 return d->m_tileCache.pendingDownloads() > 0;
282bool MapLoader::hasError()
const
284 return !d->m_errorMessage.isEmpty();
287QString MapLoader::errorMessage()
const
289 return d->m_errorMessage;
292#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.
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.
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 contains(QChar ch, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)