forked from AJ92/TileDownloader
/
tiledownloader.cpp
418 lines (334 loc) · 12.3 KB
/
tiledownloader.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "tiledownloader.h"
#include "ui_tiledownloader.h"
#include <QMessageBox>
#include <QDir>
#include <QString>
#include <QStringList>
#include <QFileDialog>
#include <QTreeView>
#include "maputils.h"
#include <QDebug>
#include "filedownloader.h"
#include <QFileInfo>
#include <QPainter>
TileDownloader::TileDownloader(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TileDownloader)
{
ui->setupUi(this);
//////////////////////////////////////////////////
//DOWNLOADING STUFF
//keep 32 images in ram and store once full or done
buffer_size = 1024;
connect(ui->pushButtonCancel,SIGNAL(clicked()),this,SLOT(cancelDownload()));
connect(ui->pushButtonBrowse,SIGNAL(clicked()),this,SLOT(setOutputPath()));
connect(ui->pushButtonDownload,SIGNAL(clicked()),this,SLOT(downloadTiles()));
connect(ui->pushButtonStich,SIGNAL(clicked()),this,SLOT(stichDownloadedTiles()));
fdl_1 = new FileDownloader(this);
connect(fdl_1, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_1()));
fdl_2 = new FileDownloader(this);
connect(fdl_2, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_2()));
fdl_3 = new FileDownloader(this);
connect(fdl_3, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_3()));
fdl_4 = new FileDownloader(this);
connect(fdl_4, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_4()));
//////////////////////////////////////////////////
//STICHING STUFF
m_stichthread = new Stichthread();
// Worker thread
connect( m_stichthread, SIGNAL( progress(const QString&) ),
this, SLOT( threadStatus(const QString&) ) );
/*
connect( m_stichthread, SIGNAL( ready(bool) ),
goButton, SLOT( setEnabled(bool) ) );
*/
connect( m_stichthread, SIGNAL( results( const QString& ) ),
this, SLOT( threadDone( const QString& ) ) );
// Launch worker thread
m_stichthread->start();
}
TileDownloader::~TileDownloader()
{
m_stichthread->quit();
m_stichthread->wait();
delete m_stichthread;
delete ui;
}
void TileDownloader::setProgress(int done, int from){
ui->progressBar->setValue((int)(100.0 * ((double)done/(double)from)));
ui->labelProgress->setText(QString::number(done) + "/" + QString::number(from));
}
void TileDownloader::setOutputPath(){
QFileDialog *fd = new QFileDialog;
fd->setFileMode(QFileDialog::Directory);
fd->setOption(QFileDialog::ShowDirsOnly);
int result = fd->exec();
QString directory;
if (result)
{
directory = fd->selectedFiles()[0];
ui->lineEditOutputPath->setText(directory);
}
}
void TileDownloader::downloadTiles(){
//get all the values:
double longLeft = ui->doubleSpinBoxLeftLong->value();
double longRight = ui->doubleSpinBoxRightLong->value();
double latTop = ui->doubleSpinBoxTopLat->value();
double latBottom = ui->doubleSpinBoxBottomLat->value();
int level = ui->spinBoxLevel->value();
QString outputPath = ui->lineEditOutputPath->text();
if(longLeft >= longRight){
QMessageBox msgBox;
msgBox.setWindowTitle("Tile Downloader");
msgBox.setText("Error.");
msgBox.setIcon(QMessageBox::Critical);
msgBox.setInformativeText("Left Long is bigger than Right Long (or equal).");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
return;
}
if(latBottom >= latTop){
QMessageBox msgBox;
msgBox.setWindowTitle("Tile Downloader");
msgBox.setText("Error.");
msgBox.setIcon(QMessageBox::Critical);
msgBox.setInformativeText("Bottom Lat is bigger than Top Lat (or equal).");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
return;
}
//try to create the output path if not existing
QDir dir;
if(!dir.mkpath(outputPath)){
QMessageBox msgBox;
msgBox.setWindowTitle("Tile Downloader");
msgBox.setText("Error.");
msgBox.setIcon(QMessageBox::Critical);
msgBox.setInformativeText("Couldn't create output path.");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
return;
}
/////////////////////////////////////////////////////////////////////////////////////
//work it
tiles_to_load.clear();
current_tile_index = 0;
current_tile_index_1 = 0;
current_tile_index_2 = 0;
current_tile_index_3 = 0;
current_tile_index_4 = 0;
current_outputPath = outputPath;
current_level = level;
image_buffer.clear();
path_buffer.clear();
//get topLeft tile
QVector2D topLeftTile;
{
int pixelX;
int pixelY;
int tileX;
int tileY;
MapUtils::LatLongToPixelXY(latTop,longLeft,level,pixelX,pixelY);
MapUtils::PixelXYToTileXY(pixelX,pixelY,tileX,tileY);
topLeftTile.setX(tileX);
topLeftTile.setY(tileY);
}
//get bottomRight tile
QVector2D bottomRightTile;
{
int pixelX;
int pixelY;
int tileX;
int tileY;
MapUtils::LatLongToPixelXY(latBottom,longRight,level,pixelX,pixelY);
MapUtils::PixelXYToTileXY(pixelX,pixelY,tileX,tileY);
bottomRightTile.setX(tileX);
bottomRightTile.setY(tileY);
}
qDebug() << topLeftTile;
qDebug() << MapUtils::TileXYToQuadKey(topLeftTile.x(),topLeftTile.y(),level);
qDebug() << bottomRightTile;
qDebug() << MapUtils::TileXYToQuadKey(bottomRightTile.x(),bottomRightTile.y(),level);
//fill the list with all TILES TO LOAD
for(int x_coord = topLeftTile.x(); x_coord <= bottomRightTile.x(); x_coord++){
for(int y_coord = topLeftTile.y(); y_coord <= bottomRightTile.y(); y_coord++){
tiles_to_load.append(QVector2D(x_coord,y_coord));
}
}
qDebug() << tiles_to_load.size();
startImgDownloader_1();
startImgDownloader_2();
startImgDownloader_3();
startImgDownloader_4();
}
void TileDownloader::startImgDownloader_1(){
setProgress(current_tile_index,tiles_to_load.size());
if(image_buffer.size() >= buffer_size){
writeImagesToDisk();
}
disconnect(fdl_1, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_1()));
if(current_tile_index < tiles_to_load.size()){
current_tile_index_1 = current_tile_index;
current_tile_index += 1;
connect(fdl_1, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_1()));
fdl_1->download(getnextUrl(current_tile_index_1));
}
else{
downloadDone();
}
}
void TileDownloader::startImgDownloader_2(){
setProgress(current_tile_index,tiles_to_load.size());
if(image_buffer.size() >= buffer_size){
writeImagesToDisk();
}
disconnect(fdl_2, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_2()));
if(current_tile_index < tiles_to_load.size()){
current_tile_index_2 = current_tile_index;
current_tile_index += 1;
connect(fdl_2, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_2()));
fdl_2->download(getnextUrl(current_tile_index_2));
}
else{
downloadDone();
}
}
void TileDownloader::startImgDownloader_3(){
setProgress(current_tile_index,tiles_to_load.size());
if(image_buffer.size() >= buffer_size){
writeImagesToDisk();
}
disconnect(fdl_3, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_3()));
if(current_tile_index < tiles_to_load.size()){
current_tile_index_3 = current_tile_index;
current_tile_index += 1;
connect(fdl_3, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_3()));
fdl_3->download(getnextUrl(current_tile_index_3));
}
else{
downloadDone();
}
}
void TileDownloader::startImgDownloader_4(){
setProgress(current_tile_index,tiles_to_load.size());
if(image_buffer.size() >= buffer_size){
writeImagesToDisk();
}
disconnect(fdl_4, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_4()));
if(current_tile_index < tiles_to_load.size()){
current_tile_index_4 = current_tile_index;
current_tile_index += 1;
connect(fdl_4, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_4()));
fdl_4->download(getnextUrl(current_tile_index_4));
}
else{
downloadDone();
}
}
QUrl TileDownloader::getnextUrl(int tileIndex){
//build url...
//bing has multiple servers to balance loads so lets use them...
//random number between 0 and 3
int randServer = rand()%(3-0 + 1) + 0;
//https://t3.ssl.ak.tiles.virtualearth.net/tiles/a122100100223310303.jpeg?g=3675&n=z
//https://t1.ssl.ak.tiles.virtualearth.net/tiles/a120212020102130102.jpeg?g=3986&n=z
return QUrl("https://t" +
QString::number(randServer) +
".ssl.ak.tiles.virtualearth.net/tiles/a" +
MapUtils::TileXYToQuadKey(tiles_to_load[tileIndex].x(),
tiles_to_load[tileIndex].y(),
current_level)+
".jpeg?g=3986&n=z");
}
void TileDownloader::loadAndSaveImage_1(){
QImage img;
img.loadFromData(fdl_1->downloadedData());
storeDownloadedDiskInBuffer(img,current_tile_index_1);
//current_tile_index += 1;
startImgDownloader_1();
}
void TileDownloader::loadAndSaveImage_2(){
QImage img;
img.loadFromData(fdl_2->downloadedData());
storeDownloadedDiskInBuffer(img,current_tile_index_2);
//current_tile_index += 1;
startImgDownloader_2();
}
void TileDownloader::loadAndSaveImage_3(){
QImage img;
img.loadFromData(fdl_3->downloadedData());
storeDownloadedDiskInBuffer(img,current_tile_index_3);
//current_tile_index += 1;
startImgDownloader_3();
}
void TileDownloader::loadAndSaveImage_4(){
QImage img;
img.loadFromData(fdl_4->downloadedData());
storeDownloadedDiskInBuffer(img,current_tile_index_4);
//current_tile_index += 1;
startImgDownloader_4();
}
void TileDownloader::storeDownloadedDiskInBuffer(QImage &img, int tileIndex){
ui->imgPreview->setPixmap(QPixmap::fromImage(img));
image_buffer.append(img);
path_buffer.append(current_outputPath + "/" +
QString::number(tiles_to_load[tileIndex].x()) + "-" +
QString::number(tiles_to_load[tileIndex].y()) + ".png");
}
void TileDownloader::downloadDone(){
writeImagesToDisk();
QApplication::beep();
/*
QMessageBox msgBox;
msgBox.setWindowTitle("Tile Downloader");
msgBox.setText("Done.");
msgBox.setIcon(QMessageBox::Information);
msgBox.setInformativeText("Downloaded all images!");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
*/
}
void TileDownloader::cancelDownload(){
disconnect(fdl_1, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_1()));
disconnect(fdl_2, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_2()));
disconnect(fdl_3, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_3()));
disconnect(fdl_4, SIGNAL (downloaded()), this, SLOT (loadAndSaveImage_4()));
ui->progressBar->setValue(0);
QMessageBox msgBox;
msgBox.setWindowTitle("Tile Downloader");
msgBox.setText("Canceled.");
msgBox.setIcon(QMessageBox::Information);
msgBox.setInformativeText("Canceled all jobs!");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
void TileDownloader::writeImagesToDisk(){
for(int i = 0; i < image_buffer.size(); i++){
image_buffer.at(i).save(path_buffer.at(i));
}
image_buffer.clear();
path_buffer.clear();
}
//STICH
void TileDownloader::stichDownloadedTiles(){
m_stichthread->execute( ui->lineEditOutputPath->text() );
}
void TileDownloader::threadStatus(const QString& message){
ui->statusBar->showMessage( "Stiching: " + message, 3000);
}
void TileDownloader::threadDone(const QString &message){
QMessageBox msgBox;
msgBox.setWindowTitle("Tile Downloader");
msgBox.setText("Stiching stopped");
msgBox.setIcon(QMessageBox::Information);
msgBox.setInformativeText(message);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}