-
Notifications
You must be signed in to change notification settings - Fork 2
/
geoengine.cpp
359 lines (320 loc) · 12.4 KB
/
geoengine.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
#include "geoengine.h"
#include <math.h>
#include <QDebug>
#include <QDirIterator>
#include <QTimer>
#include <QDomDocument>
#include <QDir>
GeoEngine::GeoEngine(GeoEngineMode m)
{
manager = new QNetworkAccessManager(this);
cache = new QNetworkDiskCache();
mode = m;
originXY = QPoint(-20037508,20037508);
tilesDir = QDir::currentPath()+QString("/tiles");
//http://gpp3-wxs.ign.fr/tyujsdxmzox31ituc2uw0qwl/geoportail/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=CADASTRALPARCELS.PARCELS&STYLE=bdparcellaire&FORMAT=image/png&TILEMATRIXSET=PM&TILEMATRIX=10&TILEROW=354&TILECOL=531&extParamId=aHR0cDovL3d3dy5nZW9wb3J0YWlsLmdvdXYuZnIvZG9ubmVlcw==
//GET /tyujsdxmzox31ituc2uw0qwl/geoportail/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=ORTHOIMAGERY.ORTHOPHOTOS&STYLE=normal&FORMAT=image/jpeg&TILEMATRIXSET=PM&TILEMATRIX=10&TILEROW=353&TILECOL=530&extParamId=aHR0cDovL3d3dy5nZW9wb3J0YWlsLmdvdXYuZnIvZG9ubmVlcw== HTTP/1.1
//http://gpp3-wxs.ign.fr/tyujsdxmzox31ituc2uw0qwl/geoportail/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=GEOGRAPHICALGRIDSYSTEMS.MAPS&STYLE=normal&FORMAT=image/jpeg&TILEMATRIXSET=PM&TILEMATRIX=12&TILEROW=1424&TILECOL=2081
// Les constantes pour les differents types d'images
csteCouche.resize(10);
formatCouche.resize(10);
csteCouche[CARTE_IGN] = QString("GEOGRAPHICALGRIDSYSTEMS.MAPS");
formatCouche[CARTE_IGN] = QString("image/jpeg");
csteCouche[PHOTOS] = QString("ORTHOIMAGERY.ORTHOPHOTOS");
formatCouche[PHOTOS] = QString("image/jpeg");
//GET /tyujsdxmzox31ituc2uw0qwl/geoportail/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=GEOGRAPHICALGRIDSYSTEMS.CASSINI&STYLE=normal&FORMAT=image/jpeg&TILEMATRIXSET=PM&TILEMATRIX=12&TILEROW=1407&TILECOL=2116&
csteCouche[CASSINI] = QString("GEOGRAPHICALGRIDSYSTEMS.CASSINI");// Cassini
formatCouche[CASSINI] = QString("image/jpeg");
csteCouche[CADASTRE] = QString("CADASTRALPARCELS.PARCELS");
formatCouche[CADASTRE] = QString("image/png");
csteCouche[TEST] = QString("8fG");
formatCouche[TEST] = QString(".png");
my_tabencryptxy.resize(62);
for(int i =0; i<62; i++)
my_tabencryptxy[i] = tabencryptxy[i];
my_tabencryptnbr62x.resize(25);
for(int i=0; i<25;i++)
my_tabencryptnbr62x[i] = tabencryptnbr62x[i];
my_tabencryptsignes.resize(4);
for(int i=0;i<4;i++)
my_tabencryptsignes[i] = tabencryptsignes[i];
initialized = false;
}
void GeoEngine::saveCachedTiles(QProgressBar* progressbar)
{
QNetworkDiskCache* cache = (QNetworkDiskCache*)(manager->cache());
QDirIterator it1(cache->cacheDirectory()+QString("http/"));
int nbFiles = 0;
while(it1.hasNext())
{
nbFiles++;
it1.next();
}
QDirIterator it(cache->cacheDirectory()+QString("http/"));
progressbar->setMaximum(nbFiles);
progressbar->setMinimum(0);
progressbar->show();
progressbar->raise();
int numFile=0;
while(it.hasNext())
{
progressbar->setValue(numFile++);
it.next();
if(it.fileInfo().isFile())
{
QNetworkCacheMetaData metaData = cache->fileMetaData(it.filePath());
QIODevice* data = cache->data(metaData.url());
if(data)
{
saveTileToDisk(metaData.url(),data->readAll());
delete data;
}
}
}
progressbar->hide();
}
void GeoEngine::saveTileToDisk(QUrl url, QByteArray dat)
{
//ne sauve pas une image vide
if(dat.size()==0)
return;
TuileParams params = extractParamsFromUrl(url.toString());
QFileInfo fileinfo = convertTileToFileInfo(params);
QDir dir(fileinfo.absolutePath());
if(!dir.exists())
dir.mkpath(dir.path());
QFile cacheFile(fileinfo.filePath());
cacheFile.open(QIODevice::WriteOnly);
cacheFile.write(dat);
cacheFile.close();
}
TuileParams GeoEngine::extractParamsFromUrl(QString filename)
{
TuileParams params;
QRegExp rx("http:.*LAYER=([^&]*).*TILEMATRIX=([^&]*)&TILEROW=([^&]*)&TILECOL=([^&]*)$");
if(rx.exactMatch(filename))
{
if(rx.cap(1)== QString("GEOGRAPHICALGRIDSYSTEMS.MAPS"))
params.couche = CARTE_IGN;
else if(rx.cap(1)==QString("ORTHOIMAGERY.ORTHOPHOTOS"))
params.couche = PHOTOS;
else if(rx.cap(1)== QString("GEOGRAPHICALGRIDSYSTEMS.CASSINI"))
params.couche = CASSINI;
else if(rx.cap(1)==QString("CADASTRALPARCELS.PARCELS"))
params.couche = CADASTRE;
params.zoomlevel = rx.cap(2).toInt();
params.y = rx.cap(3).toInt();
params.x = rx.cap(4).toInt();
}
return params;
}
QFileInfo GeoEngine::convertTileToFileInfo(TuileParams params)
{
QString path=tilesDir+QString("/");
switch(params.couche)
{
case CARTE_IGN:
path += QString("CARTE_IGN/");
break;
case PHOTOS:
path+= QString("PHOTOS/");
break;
case CASSINI :
path+= QString("CASSINI/");
break;
case CADASTRE :
path+=QString("CADASTRE/");
break;
default :
return QString("E");
}
path.append(QString("%1/").arg(QString().sprintf("%02d",params.zoomlevel)));
path.append(QString("%1/").arg(QString().sprintf("%03d",int(params.x/100))));
path.append(QString("%1/").arg(QString().sprintf("%03d",params.x%100)));
path.append(QString("%1/").arg(QString().sprintf("%03d",int(params.y/100))));
path.append(QString("img%1-%2.%3").arg(params.x).arg(params.y).arg(QString(formatCouche[params.couche]).remove(0,6)));
return QFileInfo(path);
}
// projection de long/lat vers mercator
QPoint GeoEngine::convertLongLatToXY(double longi, double lati)
{
int x = longi*pi/180*r;
int y = log(tan((90 + lati) * pi / 360.0 )) * r;
return QPoint(x,y);
}
// projection inverse mercator -> longlat
double GeoEngine::convertToLongitude(double x)
{
return x/r*180/pi;
}
double GeoEngine::convertToLatitude(double x)
{
double lat = x/r*180/pi;
return 180/pi*(2*atan(exp(lat*pi/180))-pi/2);
}
QPoint GeoEngine::convertXYToNumTile(QPoint xy, int zoomLevel)
{
return QPoint(floor((xy.x()-originXY.x())/resolutions[zoomLevel]/tileSize),
-ceil((xy.y()-originXY.y())/resolutions[zoomLevel]/tileSize));
}
QPoint GeoEngine::convertNumTileToXY(QPoint xy, int zoomLevel)
{
return originXY+QPoint(floor(xy.x()*tileSize*resolutions[zoomLevel]),
-ceil(xy.y()*tileSize*resolutions[zoomLevel]));
}
QPoint GeoEngine::convertPixToMapXY(QPoint pix,int zoomLevel)
{
return pix*resolutions[zoomLevel];
}
QPoint GeoEngine::convertMapXYToPix(QPoint xy,int zoomLevel)
{
return xy/resolutions[zoomLevel];
}
void GeoEngine::init()
{
switch(mode)
{
case CONNECTED:
cache->setCacheDirectory(QString(".cache"));
manager->setCache(cache);
connect(manager,SIGNAL(finished(QNetworkReply*)),
this, SLOT(requestFinished(QNetworkReply*)));
initialized = false;
initReply = manager->get(QNetworkRequest(QUrl("http://www.geoportail.fr")));
qDebug()<<"Mode connecté";
break;
case OFFLINE:
qDebug()<<"Mode off-line";
compteur = 0;
break;
}
}
QUrl GeoEngine::genereUrl(Couche couche, int x, int y, int zoomLevel)
{
QString url("http://gpp3-wxs.ign.fr/tyujsdxmzox31ituc2uw0qwl/geoportail/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile");
url.append(QString("&LAYER=%1").arg(csteCouche[couche]));
url.append(QString("&STYLE=normal"));
url.append(QString("&FORMAT=%1").arg(formatCouche[couche]));
url.append(QString("&TILEMATRIXSET=PM"));
url.append(QString("&TILEMATRIX=%1").arg(zoomLevel));
url.append(QString("&TILEROW=%1").arg(y));
url.append(QString("&TILECOL=%1").arg(x));
return QUrl(url);
}
QUrl GeoEngine::redirectUrl(const QUrl& possibleRedirectUrl,
const QUrl& oldRedirectUrl) const {
QUrl redirectUrl;
/*
* Check if the URL is empty and
* that we aren't being fooled into a infinite redirect loop.
* We could also keep track of how many redirects we have been to
* and set a limit to it, but we'll leave that to you.
*/
if(!possibleRedirectUrl.isEmpty() &&
possibleRedirectUrl != oldRedirectUrl) {
redirectUrl = possibleRedirectUrl;
}
return redirectUrl;
}
void GeoEngine::requestFinished(QNetworkReply* reply)
{
/*
* Reply is finished!
* We'll ask for the reply about the Redirection attribute
* http://doc.trolltech.com/qnetworkrequest.html#Attribute-enum
*/
QVariant possibleRedirectUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
/* We'll deduct if the redirection is valid in the redirectUrl function */
_urlRedirectedTo = this->redirectUrl(possibleRedirectUrl.toUrl(),
_urlRedirectedTo);
/* If the URL is not empty, we're being redirected. */
if(!_urlRedirectedTo.isEmpty())
{
/* We'll do another request to the redirection url. */
if(reply == initReply)
initReply = manager->get(QNetworkRequest(_urlRedirectedTo));
else
manager->get(QNetworkRequest(_urlRedirectedTo));
}
else
{ /*
* We weren't redirected anymore
* so we arrived to the final destination...
*/
/* ...so this can be cleared. */
_urlRedirectedTo.clear();
if((reply == initReply) && !initialized)
{
initialized = true;
emit(ready());
}
if(imageRequests.contains(reply))
{
QByteArray tileData = reply->readAll();
if(autoSave)
{
saveTileToDisk(reply->url(),tileData);
}
emit(dataReady(tileData,int(reply)));
}
if(reply == geocodeReply)
{
QByteArray temp = reply->readAll();
QDomDocument kmlDoc;
kmlDoc.setContent(temp);
QDomElement n = kmlDoc.documentElement().firstChildElement(QString("Response")).
firstChildElement(QString("Placemark")).
firstChildElement(QString("Point")).firstChildElement(QString("coordinates"));
QString coordStr = n.firstChild().nodeValue();
QStringList coordsStr = coordStr.split(',');
if(coordsStr.size()>1)
{
QPointF coords = QPointF(coordsStr.at(0).toFloat(),coordsStr.at(1).toFloat());
emit(geocodeReceived(coords));
}
}
}
/* Clean up. */
reply->deleteLater();
}
int GeoEngine::downloadImage(Couche couche, int x, int y, int zoomLevel,bool forceDl)
{
TuileParams params;
params.couche=couche;
params.x = x;
params.y = y;
params.zoomlevel = zoomLevel;
QFileInfo info = convertTileToFileInfo(params);
if(!info.exists() || ((mode==CONNECTED) && forceDl))
{
if(mode==OFFLINE)
info = QFileInfo(QString("noImg.jpg"));
else if (mode==CONNECTED)
{
QNetworkRequest request(genereUrl(couche, x, y, zoomLevel));
request.setRawHeader("Referer", "http://www.geoportail.gouv.fr/swf/geoportal-visu-1.1.3.swf");
request.setRawHeader("Host", "gpp3-wxs.ign.fr");
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
QNetworkRequest::PreferCache);
imageRequests.append(manager->get(request));
return int(imageRequests.back());
}
}
QFile fich(info.filePath());
fich.open(QIODevice::ReadOnly);
toSendId.enqueue(compteur++);
toSend.enqueue(fich.readAll());
fich.close();
QTimer::singleShot(10,this,SLOT(sendFirstData()));
return toSendId.back();
}
void GeoEngine::sendFirstData()
{
emit(dataReady(toSend.dequeue(),toSendId.dequeue()));
}
void GeoEngine::getCoord(QString address)
{
QNetworkRequest request(QString("http://maps.google.com/maps/geo?output=kml&q=%1").arg(address));
geocodeReply = manager->get(request);
}