-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcacamap.cpp
658 lines (611 loc) · 16.1 KB
/
cacamap.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
Copyright 2010 Jean Fairlie [email protected]
This file is part of CacaMap
CacaMap is free software: you can redistribute it and/or modify
it under the terms of the GNU 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.
*/
#include "cacamap.h"
using namespace std;
/**
* constructor
*/
longPoint::longPoint(quint32 _x, quint32 _y)
{
x = _x;
y = _y;
}
/**
*empty constructor
*/
longPoint::longPoint()
{
x = 0;
y = 0;
}
/**
* Converts a geo coordinate to map pixels
* @param geocoord has the longitude and latitude in degrees.
* @param zoom is the zoom level, ranges from cacaMap::minZoom (out) to maxZoom (in).
* @param tilesize the width/height in px of the square %tile (e.g 256).
* @return a longpoint struct containing the x and y px coordinates
* in the map for the given geocoordinates and zoom level.
*/
longPoint myMercator::geoCoordToPixel(QPointF const &geocoord, int zoom, int tilesize)
{
qreal longitude = geocoord.x();
qreal latitude = geocoord.y();
//height, width of the whole map,this is, all tiles for a given zoom level put together
quint32 mapsize = (1<<zoom)*tilesize;
qreal latitude_m = atanh(sin(latitude/180.0*M_PI))*180.0/M_PI;
quint32 x = mapsize*(longitude + 180.0)/360.0;
quint32 y = mapsize*(180.0 - latitude_m)/360.0;
return longPoint(x,y);
}
/**
* Converts map pixels to geo coordinates in degrees
* @param pixelcoord has the x and y px coordinates.
* @param zoom is the zoom level, ranges from 0(out) to 18(in).
* @param tilesize the width/height in px of the square %tile (e.g 256).
* @return a QPointF object containing the latitude and longitude of
* of the given location.
*/
QPointF myMercator::pixelToGeoCoord(longPoint const &pixelcoord, int zoom, int tilesize)
{
long x= pixelcoord.x;
long y= pixelcoord.y;
//height, width of the whole map,this is, all tiles for a given zoom level put together
quint32 mapsize = (1<<zoom)*tilesize;
qreal longitude = x*360.0/mapsize - 180.0;
qreal latitude_m = 180.0 - y*360.0/mapsize;
qreal latitude = asin(tanh(latitude_m*M_PI/180.0))*180/M_PI;
return QPointF(longitude,latitude);
}
/**
* constructor
*/
cacaMap::cacaMap(QWidget* parent):QWidget(parent)
{
cout<<"cacamap constructor"<<endl;
if (!servermgr.loadConfigFile("tileservers.xml"))
{
std::cout<<"error loading server file."<<std::endl;
}
cacheSize = 0;
maxZoom = 18;
minZoom = 0;
folder = QDir::currentPath();
loadCache();
geocoords = QPointF(23.8564,61.4667);
downloading = false;
tileSize = 256;
zoom = 14;
manager = new QNetworkAccessManager(this);
loadingAnim.setFileName("loading.gif");
loadingAnim.setScaledSize(QSize(tileSize,tileSize));
loadingAnim.start();
notAvailableTile.load("notavailable.jpeg");
imgBuffer = new QPixmap(size());
buffzoomrate = 1.0;
}
/**
Sets the latitude and longitude to the coords in newcoords
@param newcoords the new coordinates.
*/
void cacaMap::setGeoCoords(QPointF newcoords)
{
//geocoords.setX(newcoords.x());
//geocoords.setY(newcoords.y());
geocoords = newcoords;
}
/**
* zooms in one level
* @return true if it zoomed succesfully, false otherwise (if maxZoom has been reached)
*/
bool cacaMap::zoomIn()
{
if (zoom < maxZoom)
{
zoom++;
downloadQueue.clear();
updateContent();
return true;
}
return false;
}
/**
* zooms out one level
* @return true if it zoomed succesfully, false otherwise (if minZoom has been reached)
*/
bool cacaMap::zoomOut()
{
if (zoom > minZoom)
{
zoom--;
downloadQueue.clear();
updateContent();
return true;
}
return false;
}
/**
* zooms to a specific level
* @return true if it is a valid level, false otherwise (if level is outside the valid range)
*/
bool cacaMap::setZoom(int level)
{
if (level>= minZoom && level <= maxZoom)
{
zoom = level;
downloadQueue.clear();
updateContent();
return true;
}
return false;
}
/**
* @return current geocoords
*/
QPointF cacaMap::getGeoCoords()
{
return geocoords;
}
/**
*@return list of available tile server names
*/
QStringList cacaMap::getServerNames()
{
return servermgr.getServerNames();
}
/**
* Change tile server to the one in index
*/
void cacaMap::setServer(int index)
{
servermgr.selectServer(index);
downloadQueue.clear();
loadCache();
downloading = false;
updateContent();
update();
}
/**
* @return current zoom level
*/
int cacaMap::getZoom()
{
return zoom;
}
/**
*@param zoom zoom level
*@param x tile x column
*@return string with the path to the folder containing the tiles in
*for zoom level and x column
*/
QString cacaMap::getTilePath(int zoom,qint32 x)
{
return "cache/"+servermgr.tileCacheFolder()+servermgr.filePath(zoom,x);
}
/**
* @return image for temporarily replacing a tile that is downloading and currently unavailable
* The 'patch' is a subsection of an available tile from a lower zoom level.
* The algorithm tries to find a suitable tile starting from level zoom -1, until level 0.
* The higher the zoom level difference the more pixelated the patch will be.
*/
QPixmap cacaMap::getTilePatch(int zoom, quint32 x, quint32 y, int offx, int offy, int tsize)
{
//dont go beyond level 0 and
//dont use patches smaller than 16 px. They are unintelligible anyways.
if (zoom>0 && tsize>=16*2)
{
int parentx, parenty, offsetx, offsety;
QString tileid;
QPixmap patch;
QString sz,sx,sy;
parentx = x/2;
parenty = y/2;
sz.setNum(zoom-1);
sx.setNum(parentx);
sy.setNum(parenty);
offsetx = offx/2 + (x%2)*tileSize/2;
offsety = offy/2 + (y%2)*tileSize/2;
tileid = sz+"."+sx+"."+sy;
if (tileCache.contains(tileid))
{
//render the tile
QDir::setCurrent(folder);
QString path= getTilePath(zoom-1,parentx) ;
QString fileName = servermgr.fileName(parenty);
QDir::setCurrent(path);
QFile f(fileName);
if (f.open(QIODevice::ReadOnly))
{
patch.loadFromData(f.readAll());
f.close();
return patch.copy(offsetx,offsety,tsize/2,tsize/2).scaledToHeight(tileSize);
}
else
{
cout<<"no file found "<<path.toStdString()<<fileName.toStdString()<<endl;
}
}
else
{
return getTilePatch(zoom-1,parentx,parenty,offsetx,offsety,tsize/2);
}
}
return loadingAnim.currentPixmap();
}
/**
Starts downloading the next %tile in the queue
@see cacaMap::downloadQueue
*/
void cacaMap::downloadPicture()
{
//check if there isnt an active download already
if (!downloading)
{
//there are items in the queue
if (downloadQueue.size())
{
downloading = true;
QHash<QString,tile>::const_iterator i;
i = downloadQueue.constBegin();
tile nextItem = i.value();
QString surl = nextItem.url;
QNetworkRequest request;
request.setUrl(QUrl(surl));
QNetworkReply *reply = manager->get(request);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(slotDownloadReady(QNetworkReply*)));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),this, SLOT(slotDownloadProgress(qint64, qint64)));
}
else
{
// cout<<"no items in the queue"<<endl;
}
}
else
{
cout<<"another download is already in progress... "<<endl;
}
}
/**
Populates the cache list by checking the existing files on the cache folder
*/
void cacaMap::loadCache()
{
cacheSize=0;
unavailableTiles.clear();
tileCache.clear();
QDir::setCurrent(folder);
QDir dir;
if (dir.cd("cache"))
{
if (dir.cd(servermgr.tileCacheFolder()))
{
QStringList zoom = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
QString zoomLevel;
for(int i=0; i< zoom.size(); i++)
{
zoomLevel = zoom.at(i);
dir.cd(zoomLevel);
QStringList longitudes = dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
QString lon;
for(int j=0; j< longitudes.size(); j++)
{
lon = longitudes.at(j);
dir.cd(lon);
QFileInfoList latitudes = dir.entryInfoList(QDir::Files|QDir::NoDotAndDotDot);
QString lat;
for(int k=0; k< latitudes.size(); k++)
{
lat = latitudes.at(k).baseName();
cacheSize+= latitudes.at(k).size();
QString name = zoomLevel+"."+lon+"."+lat;
tileCache.insert(name,1);
}
dir.cdUp();//go back to zoom level folder
}
dir.cdUp();//go back to tile folder
}
}
QDir::setCurrent(folder);
cout<<"cache size "<<(float)cacheSize/1024/1024<<" MB"<<endl;
}
}
/**
Slot to keep track of download progress
*/
void cacaMap::slotDownloadProgress(qint64 _bytesReceived, qint64 _bytesTotal)
{
}
/**
* Slot that gets called everytime a %tile download request finishes
* Saves image file to HDD, takes out item from download queue, and adds item to cache list
*/
void cacaMap::slotDownloadReady(QNetworkReply * _reply)
{
QNetworkReply::NetworkError error = _reply->error();
//get url of original request
QNetworkRequest req = _reply->request();
QUrl url = req.url();
QString surl = url.toString();
bool found = false;
QHash<QString,tile>::const_iterator i;
i = downloadQueue.constBegin();
for( i; i!=downloadQueue.constEnd();i++)
{
if (i.value().url == surl)
{
found = true;
break;
}
}
if (error == QNetworkReply::NoError)
{
qint64 bytes = _reply->bytesAvailable();
if (bytes)
{
//get url of original request
QNetworkRequest req = _reply->request();
QUrl url = req.url();
QString surl = url.toString();
cacheSize+=bytes;
//get image data
QByteArray data = _reply->readAll();
if (found)
{
tile nextItem = i.value();
QString kk = i.key();
QString zdir = QString().setNum(nextItem.zoom);
QString xdir = QString().setNum(nextItem.x);
QString tilefile = servermgr.fileName(nextItem.y);
QDir::setCurrent(folder);
QDir dir;
if (!dir.exists("cache"))
{
dir.mkdir("cache");
}
dir.cd("cache");
if (!dir.exists(servermgr.tileCacheFolder()))
{
dir.mkdir(servermgr.tileCacheFolder());
}
dir.cd(servermgr.tileCacheFolder());
if(!dir.exists(zdir))
{
dir.mkdir(zdir);
}
dir.cd(zdir);
if(!dir.exists(xdir))
{
dir.mkdir(xdir);
}
dir.cd(xdir);
QDir::setCurrent(dir.path());
QFile f(tilefile);
f.open(QIODevice::WriteOnly);
quint64 byteswritten = f.write(data);
if (byteswritten <= 0)
{
cout<<"error writing to file "<<f.fileName().toStdString()<<endl;
}
f.close();
//remove item from download queue
downloadQueue.remove(i.key());
//add it to cache
tileCache.insert(kk,1);
//update with new tile
updateBuffer();
update();
}
else
{
cout<<"downloaded tile "<<surl.toStdString()<<" was not in Download queue. Data ignored"<<endl;
}
downloading = false;
downloadPicture();
}
else
{
//cout<<"no data"<<endl;
}
update();
}
else
{
cout<<"network error: ("<<error<<") "<<_reply->errorString().toStdString()<<endl;
if(found)
{
//if content is not available we dont want to keep requesting it
if (error == QNetworkReply::ContentNotFoundError)
{
unavailableTiles.insert(i.key(),1);
}
//remove the item from queue and try again
downloadQueue.remove(i.key());
downloading = false;
downloadPicture();
}
}
_reply->deleteLater();
}
/**
Slot that gets called when theres is an network error
*/
void cacaMap::slotError(QNetworkReply::NetworkError _code)
{
cout<<"some error "<<_code<<endl;
}
/**
Widget resize event handler
*/
void cacaMap::resizeEvent(QResizeEvent* event)
{
delete imgBuffer;
imgBuffer = new QPixmap(size());
updateContent();
}
/**
* Blits buffer to widget
*/
void cacaMap::renderMap(QPainter &p)
{
//QRect dest(QPoint(0,0), size());
if (buffzoomrate<1.0)
{
int ox = width()*(1-buffzoomrate)/2;
int oy = height()*(1-buffzoomrate)/2;
QRect src(QPoint(ox,oy),size()*buffzoomrate);
tmpbuff= imgBuffer->copy(src).scaled(size(),Qt::KeepAspectRatio);
p.drawPixmap(0,0,tmpbuff);
//p.drawPixmap(dest, *imgBuffer,src);//this is shorter but slower
}
else
{
p.drawPixmap(0,0,*imgBuffer);
}
}
/**
Paint even handler
*/
void cacaMap::paintEvent(QPaintEvent *event)
{
QPainter p(this);
renderMap(p);
}
/**
destructor
*/
cacaMap::~cacaMap()
{
delete manager;
delete imgBuffer;
}
/**
Figures out which tiles are visible
Based on the current size of the widget, the %tile size, current coordinates and zoom level
*/
void cacaMap::updateTilesToRender()
{
longPoint pixelCoords = myMercator::geoCoordToPixel(geocoords,zoom,tileSize);
//central tile coords
qint32 xtile = pixelCoords.x/tileSize;
qint32 ytile = pixelCoords.y/tileSize;
//offset of central tile respect to the center of the widget
int offsetx = pixelCoords.x % tileSize;
int offsety = pixelCoords.y % tileSize;
//num columns of tiles that fit left of the central tile
float tilesleft = (float)(this->width()/2 - offsetx)/tileSize;
//how many pixels overflow from the leftmost tiles
//second %tileSize is to take into account negative tilesLeft
int globaloffsetx = (tileSize - (this->width()/2 - offsetx) % tileSize)%tileSize;
//num rows of tiles that fit above the central tile
float tilesup = (float)(this->height()/2 - offsety)/tileSize;
//how many pixels overflow from top tiles
int globaloffsety = (tileSize - (this->height()/2 - offsety) % tileSize)%tileSize;
//num columns of tiles that fit right of central tile
float tilesright = (float)(this->width()/2 + offsetx - tileSize)/tileSize;
//num rows of tiles that fit under central tile
float tilesbottom = (float)(this->height()/2 + offsety - tileSize)/tileSize;
tilesToRender.left = xtile - ceil(tilesleft);
tilesToRender.right = xtile + ceil(tilesright);
tilesToRender.top =ytile - ceil(tilesup);
tilesToRender.bottom = ytile + ceil(tilesbottom);
tilesToRender.offsetx = globaloffsetx;
tilesToRender.offsety = globaloffsety;
tilesToRender.zoom = zoom;
}
/**
* Blits visible tiles buffer
*/
void cacaMap::updateBuffer()
{
QPainter p(imgBuffer);
imgBuffer->fill(Qt::gray);
for (qint32 i= tilesToRender.left;i<= tilesToRender.right; i++)
{
for (qint32 j=tilesToRender.top ; j<= tilesToRender.bottom; j++)
{
QString x;
QString y;
//wrap around the tiles horizontally if i is outside [0,2^zoom]
qint32 numtiles = 1<<tilesToRender.zoom;
qint32 valx =((i<0)*numtiles + i%numtiles)%numtiles;
x.setNum(valx);
QPixmap image;
int posx = (i-tilesToRender.left)*tileSize - tilesToRender.offsetx;
int posy = (j-tilesToRender.top)*tileSize - tilesToRender.offsety;
//dont try to render tiles with y coords outside range
//cause we cant do vertical wrapping!
if (j>=0 && j<numtiles)
{
QString tileid = QString().setNum(tilesToRender.zoom) +"."+x+"."+QString().setNum(j);
if (tileCache.contains(tileid))
{
//render the tile
QDir::setCurrent(folder);
//check path format (windows?)
QString path= getTilePath(tilesToRender.zoom,valx) ;
QString fileName = servermgr.fileName(j);
QDir::setCurrent(path);
QFile f(fileName);
if (f.open(QIODevice::ReadOnly))
{
image.loadFromData(f.readAll());
f.close();
}
else
{
cout<<"no file found "<<path.toStdString()<<fileName.toStdString()<<endl;
}
}
//check if it's in the list of unavailable tiles
else if (unavailableTiles.contains(tileid))
{
image = notAvailableTile;
}
//the tile is not cached so download it
else
{
//check that the image hasnt been queued already
if (!downloadQueue.contains(tileid))
{
tile t;
t.zoom = tilesToRender.zoom;
t.x = valx;
t.y = j;
t.url = servermgr.getTileUrl(tilesToRender.zoom,valx,j);
//queue the image for download
downloadQueue.insert(tileid,t);
}
//crop a tile from a lower zoom level and use it as a patch(a la google maps)
//while the tile is downloading
image = getTilePatch(tilesToRender.zoom,valx,j,0,0,tileSize);
}
p.drawPixmap(posx,posy,image);
}
}
}
p.drawRect(0,0,width()-1, height()-1);
if (!downloading)
{
downloadPicture();
}
}
/**
* calls the following two functions
* @see cacaMap::updateTilesToRender
* @see cacaMap::updateBuffer
*/
void cacaMap::updateContent()
{
updateTilesToRender();
updateBuffer();
}