-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathQGVLayerTiles.cpp
More file actions
353 lines (310 loc) · 11.1 KB
/
QGVLayerTiles.cpp
File metadata and controls
353 lines (310 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2025 Andrey Yaroshenko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see https://www.gnu.org/licenses.
****************************************************************************/
#include "QGVLayerTiles.h"
#include "QGVDrawItem.h"
#include <QtMath>
QGVLayerTiles::QGVLayerTiles()
{
mCurZoom = -1;
mTileGeoOffset = QGV::GeoPos(0.0, 0.0);
sendToBack();
}
void QGVLayerTiles::setTilesMarginWithZoomChange(size_t value)
{
mPerfomanceProfile.TilesMarginWithZoomChange = value;
qgvDebug() << "TilesMarginWithZoomChange changed to" << value;
}
void QGVLayerTiles::setTilesMarginNoZoomChange(size_t value)
{
mPerfomanceProfile.TilesMarginNoZoomChange = value;
qgvDebug() << "TilesMarginNoZoomChange changed to" << value;
}
void QGVLayerTiles::setAnimationUpdateDelayMs(size_t value)
{
mPerfomanceProfile.AnimationUpdateDelayMs = value;
qgvDebug() << "AnimationUpdateDelayMs changed to" << value;
}
void QGVLayerTiles::setVisibleZoomLayersBelowCurrent(size_t value)
{
mPerfomanceProfile.VisibleZoomLayersBelowCurrent = value;
qgvDebug() << "VisibleZoomLayersBelowCurrent changed to" << value;
}
void QGVLayerTiles::setVisibleZoomLayersAboveCurrent(size_t value)
{
mPerfomanceProfile.VisibleZoomLayersAboveCurrent = value;
qgvDebug() << "VisibleZoomLayersAboveCurrent changed to" << value;
}
void QGVLayerTiles::setCameraUpdatesDuringAnimation(bool value)
{
mPerfomanceProfile.CameraUpdatesDuringAnimation = value;
qgvDebug() << "CameraUpdatesDuringAnimation changed to" << value;
}
QGV::GeoTilePos QGVLayerTiles::geoPosToTilePos(int zoom, const QGV::GeoPos& geoPos) const{
return QGV::GeoTilePos::geoToTilePos(
zoom,
QGV::GeoPos(
geoPos.latitude() + mTileGeoOffset.latitude(),
geoPos.longitude() + mTileGeoOffset.longitude())
);
}
QGV::GeoRect QGVLayerTiles::tilePosToGeoRect(const QGV::GeoTilePos& tilePos) const{
auto r = tilePos.toGeoRect();
return QGV::GeoRect(
r.latTop() - mTileGeoOffset.latitude(),
r.lonLeft() - mTileGeoOffset.longitude(),
r.latBottom() - mTileGeoOffset.latitude(),
r.lonRight() - mTileGeoOffset.longitude()
);
}
QGV::GeoPos QGVLayerTiles::tileGeoOffset() const{
return mTileGeoOffset;
}
void QGVLayerTiles::setTileGeoOffset(const QGV::GeoPos &offset){
mTileGeoOffset = offset;
}
void QGVLayerTiles::onProjection(QGVMap* geoMap)
{
QGVLayer::onProjection(geoMap);
}
void QGVLayerTiles::onCamera(const QGVCameraState& oldState, const QGVCameraState& newState)
{
QGVLayer::onCamera(oldState, newState);
if (oldState == newState) {
return;
}
bool needUpdate = true;
if (newState.animation()) {
if (!mPerfomanceProfile.CameraUpdatesDuringAnimation) {
needUpdate = false;
} else if (!mLastAnimation.isValid()) {
mLastAnimation.start();
} else if (mLastAnimation.elapsed() < static_cast<qint64>(mPerfomanceProfile.AnimationUpdateDelayMs)) {
needUpdate = false;
} else {
mLastAnimation.restart();
}
} else {
mLastAnimation.invalidate();
}
if (needUpdate) {
processCamera();
}
}
void QGVLayerTiles::onUpdate()
{
QGVLayer::onUpdate();
processCamera();
}
void QGVLayerTiles::onClean()
{
QGVLayer::onClean();
mCurZoom = -1;
mCurRect = {};
mIndex.clear();
deleteItems();
}
void QGVLayerTiles::onTile(const QGV::GeoTilePos& tilePos, QGVDrawItem* tileObj)
{
if (tilePos.zoom() != mCurZoom || !mCurRect.contains(tilePos.pos())) {
delete tileObj;
return;
}
addTile(tilePos, tileObj);
removeAllAbove(tilePos);
const int fromZoom = minZoomlevel();
const int toZoom = tilePos.zoom() - 1;
for (int zoom = fromZoom; zoom <= toZoom; ++zoom) {
for (const QGV::GeoTilePos& below : existingTiles(zoom)) {
if (below.contains(tilePos)) {
removeWhenCovered(below);
}
}
}
}
int QGVLayerTiles::scaleToZoom(double scale) const
{
const double scaleChange = 1 / scale;
const int newZoom = qRound((17.0 - qLn(scaleChange) * M_LOG2E));
return newZoom;
}
void QGVLayerTiles::processCamera()
{
if (getMap() == nullptr || !isVisible()) {
return;
}
const QGVProjection* projection = getMap()->getProjection();
const QGVCameraState camera = getMap()->getCamera();
const QRectF areaProjRect = camera.projRect().intersected(projection->boundaryProjRect());
const QGV::GeoRect areaGeoRect = projection->projToGeo(areaProjRect);
int originZoom = scaleToZoom(camera.scale());
int newZoom = qMin(maxZoomlevel(), qMax(minZoomlevel(), originZoom));
if (newZoom != originZoom) {
return;
}
const bool zoomChanged = (mCurZoom != newZoom);
mCurZoom = newZoom;
const int margin = (zoomChanged) ? static_cast<int>(mPerfomanceProfile.TilesMarginWithZoomChange)
: static_cast<int>(mPerfomanceProfile.TilesMarginNoZoomChange);
const int sizePerZoom = static_cast<int>(qPow(2, mCurZoom));
const QRect maxRect = QRect(QPoint(0, 0), QPoint(sizePerZoom, sizePerZoom));
const QPoint topLeft = geoPosToTilePos(mCurZoom, areaGeoRect.topLeft()).pos();
const QPoint bottomRight = geoPosToTilePos(mCurZoom, areaGeoRect.bottomRight()).pos();
QRect activeRect = QRect(topLeft, bottomRight);
activeRect = activeRect.adjusted(-margin, -margin, margin, margin);
activeRect = activeRect.intersected(maxRect);
const bool rectChanged = (!zoomChanged && (mCurRect != activeRect));
mCurRect = activeRect;
if (!zoomChanged && !rectChanged) {
return;
}
if (zoomChanged) {
qgvDebug() << "new active zoom" << mCurZoom;
const int fromZoom = minZoomlevel();
const int toZoom = maxZoomlevel();
for (int zoom = fromZoom; zoom <= toZoom; ++zoom) {
if (zoom == mCurZoom) {
for (const QGV::GeoTilePos& current : existingTiles(zoom)) {
removeAllAbove(current);
}
} else {
for (const QGV::GeoTilePos& nonCurrent : existingTiles(zoom)) {
if (!isTileFinished(nonCurrent)) {
qgvDebug() << "cancel non-finished" << nonCurrent;
removeTile(nonCurrent);
} else if (zoom < mCurZoom) {
removeWhenCovered(nonCurrent);
}
removeForPerfomance(nonCurrent);
}
}
}
}
if (rectChanged) {
qgvDebug() << "new active rect" << mCurRect.topLeft() << mCurRect.bottomRight();
for (const QGV::GeoTilePos& tilePos : existingTiles(mCurZoom)) {
if (!mCurRect.contains(tilePos.pos())) {
qgvDebug() << "delete out of boundary view" << tilePos;
removeTile(tilePos);
}
}
}
QMultiMap<qreal, QGV::GeoTilePos> missing;
for (int x = mCurRect.left(); x < mCurRect.right(); ++x) {
for (int y = mCurRect.top(); y < mCurRect.bottom(); ++y) {
const auto tilePos = QGV::GeoTilePos(mCurZoom, QPoint(x, y));
if (isTileExists(tilePos)) {
continue;
}
qreal radius = qSqrt(qPow(x - mCurRect.center().x(), 2) + qPow(y - mCurRect.center().y(), 2));
missing.insert(radius, tilePos);
}
}
for (const QGV::GeoTilePos& tilePos : missing) {
addTile(tilePos, nullptr);
}
}
void QGVLayerTiles::removeAllAbove(const QGV::GeoTilePos& tilePos)
{
const int fromZoom = tilePos.zoom() + 1;
const int toZoom = maxZoomlevel();
for (int zoom = fromZoom; zoom <= toZoom; ++zoom) {
for (const QGV::GeoTilePos& target : existingTiles(zoom)) {
if (!tilePos.contains(target)) {
continue;
}
qgvDebug() << "remove" << target << "above" << tilePos;
removeTile(target);
}
}
}
void QGVLayerTiles::removeWhenCovered(const QGV::GeoTilePos& tilePos)
{
const int zoomDelta = mCurZoom - tilePos.zoom() + 1;
const int neededCount = static_cast<int>(qPow(2, zoomDelta));
int count = neededCount;
for (const QGV::GeoTilePos& current : existingTiles(mCurZoom)) {
if (!tilePos.contains(current)) {
continue;
}
if (!isTileFinished(current)) {
break;
}
count--;
if (count == 0) {
break;
}
}
if (count == 0) {
qgvDebug() << tilePos << "deleted by 100% coverage";
removeTile(tilePos);
} else {
qgvDebug() << tilePos << "covered" << neededCount - count << "/" << neededCount;
}
}
void QGVLayerTiles::removeForPerfomance(const QGV::GeoTilePos& tilePos)
{
const auto minZoom = mCurZoom - static_cast<int>(mPerfomanceProfile.VisibleZoomLayersBelowCurrent);
const auto maxZoom = mCurZoom + static_cast<int>(mPerfomanceProfile.VisibleZoomLayersAboveCurrent);
if (tilePos.zoom() < minZoom || tilePos.zoom() > maxZoom) {
qgvDebug() << "delete because of performance request" << minZoom << maxZoom;
removeTile(tilePos);
}
}
void QGVLayerTiles::addTile(const QGV::GeoTilePos& tilePos, QGVDrawItem* tileObj)
{
if (isTileFinished(tilePos)) {
delete tileObj;
return;
}
if (tileObj == nullptr) {
qgvDebug() << "request tile" << tilePos;
mIndex[tilePos.zoom()][tilePos] = nullptr;
request(tilePos);
} else {
qgvDebug() << "add tile" << tilePos;
mIndex[tilePos.zoom()][tilePos] = tileObj;
tileObj->setZValue(static_cast<qint16>(tilePos.zoom()));
addItem(tileObj);
}
}
void QGVLayerTiles::removeTile(const QGV::GeoTilePos& tilePos)
{
const auto tile = mIndex[tilePos.zoom()].take(tilePos);
if (tile == nullptr) {
qgvDebug() << "cancel tile" << tilePos;
cancel(tilePos);
} else {
qgvDebug() << "remove tile" << tilePos;
delete tile;
}
}
bool QGVLayerTiles::isTileExists(const QGV::GeoTilePos& tilePos) const
{
return mIndex[tilePos.zoom()].contains(tilePos);
}
bool QGVLayerTiles::isTileFinished(const QGV::GeoTilePos& tilePos) const
{
if (!isTileExists(tilePos)) {
return false;
}
return mIndex[tilePos.zoom()][tilePos] != nullptr;
}
QList<QGV::GeoTilePos> QGVLayerTiles::existingTiles(int zoom) const
{
return mIndex[zoom].keys();
}