-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmapdatadisk.cpp
More file actions
150 lines (144 loc) · 5.25 KB
/
Copy pathmapdatadisk.cpp
File metadata and controls
150 lines (144 loc) · 5.25 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
#include "mapdatadisk.h"
#include <QDir>
#include <QDebug>
#include <QRegExp>
#include <QTime>
#include "json.h"
#include "geocoord.h"
MapDataDisk::MapDataDisk(const QString &pathToTiles, QObject *parent) :
IMapData(parent),
m_pathToTiles(pathToTiles)
{
QDir path(pathToTiles);
if (path.exists()){
parseDir(pathToTiles);
} else {
QFile file(pathToTiles);
if (file.exists()) {
file.open(QIODevice::ReadOnly);
QString jsonData = QString::fromUtf8(file.readAll());
file.close();
bool ok;
QtJson::JsonObject result = QtJson::parse(jsonData, ok).toMap();
if (ok) {
int minZoom = result.value("minzoom", -1).toInt();
int maxZoom = result.value("maxzoom", -1).toInt();
qreal topX;
qreal topY;
qreal bottomX;
qreal bottomY;
ok = result.contains("bounds");
if (ok) {
QtJson::JsonArray bounds = result.value("bounds").toList();
if (bounds.size() == 4) {
while (true) {
topX = bounds.value(0).toDouble(&ok);
if (!ok) break;
topY = bounds.value(1).toDouble(&ok);
if (!ok) break;
bottomX = bounds.value(2).toDouble(&ok);
if (!ok) break;
bottomY = bounds.value(3).toDouble(&ok);
break;
}
} else {
ok = false;
}
}
if (ok && minZoom >= 0 && maxZoom >= 0) {
for (int i = minZoom; i <= maxZoom; ++i) {
GeoCoord coordTop = GeoCoord(topY, topX);
GeoCoord coordBottom = GeoCoord(bottomY, bottomX);
ZoomData zd;
zd.zoomLvl = i;
QPoint delataTile;
zd.minX = coordTop.getTilePosition(i, delataTile).x();
zd.minY = coordTop.getTilePosition(i, delataTile).y();
zd.maxX = coordBottom.getTilePosition(i, delataTile).x();
zd.maxY = coordBottom.getTilePosition(i, delataTile).y();
addZoomLevel(zd);
}
}
}
}
}
}
QPixmap MapDataDisk::getTile(int x, int y)
{
if (x < minX() || x > maxX() || y < minY() || y > maxY() ) {
return defaultBackground();
}
QPixmap tile = getImageFromCache(x, y);
if (tile.isNull()) {
QString filename = QString("%1/%2/%3.png")
.arg(zoomLvl())
.arg(x)
.arg(y);
QString filepath = pathAppend(m_pathToTiles, filename);
tile.load(filepath);
} else {
return tile;
}
if (tile.isNull() || tile.height() != 0x100 || tile.width() != 0x100) {
return defaultBackground();
}
putImageInCache(x, y, tile);
return tile;
}
QString MapDataDisk::pathAppend(const QString& path1, const QString& path2)
{
return QDir::cleanPath(path1 + QDir::separator() + path2);
}
void MapDataDisk::parseDir(const QString &pathToTiles)
{
QDir path(pathToTiles);
QRegExp regExpForYCoord("^(\\d+)\\.png$");
for (const QString zoomLvlStr : path.entryList(QDir::Dirs)) {
bool ok;
int zoomLvl = zoomLvlStr.toInt(&ok);
if (ok) {
QDir pathToXCoord(pathAppend(pathToTiles, zoomLvlStr));
ZoomData zd;
zd.zoomLvl = zoomLvl;
zd.maxX = INT_MIN;
zd.minX = INT_MAX;
zd.maxY = INT_MIN;
zd.minY = INT_MAX;
bool firstEntry = true;
for (const QString &xStr : pathToXCoord.entryList(QDir::Dirs)) {
int x = xStr.toInt(&ok);
if (ok) {
if (x < zd.minX) {
zd.minX = x;
}
if (x > zd.maxX) {
zd.maxX = x;
}
}
if (firstEntry && ok) {
firstEntry = false;
QDir pathToYCoord(pathAppend(pathToXCoord.absolutePath(), xStr));
for (const QString &yStr : pathToYCoord.entryList({QString("*.png")}, QDir::Files)) {
if (regExpForYCoord.indexIn(yStr) >= 0) {
int y = regExpForYCoord.cap(1).toInt(&ok);
if (ok) {
if (y < zd.minY) {
zd.minY = y;
}
if (y > zd.maxY) {
zd.maxY = y;
}
}
}
}
}
}
if (zd.maxX != INT_MIN && zd.maxY != INT_MIN && zd.minX != INT_MAX && zd.minY != INT_MAX) {
if (!isValidData()) {
setDataValid(true);
}
addZoomLevel(zd);
}
}
}
}