-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsmap.cpp
349 lines (264 loc) · 7.68 KB
/
qgsmap.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
#include "qgsmap.h"
#include <QGLWidget>
#include <QtDebug>
#include <QApplication>
#include <QDir>
QGSMap::QGSMap(QWidget *parent) :
QGraphicsView(parent)
{
#ifndef QT_NO_OPENGL
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
#endif
//nulls and spells
serverSettings = NULL;
//built-in options
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setDragMode(QGraphicsView::ScrollHandDrag); //reimplementation inevitable
}
QList<QGSLayer*> QGSMap::getLayers()
{
layers.clear();
for(int i=0;i<items().count();i++)
{
QGSLayer *itm = (QGSLayer*)items().at(i);
if(itm->topLevelItem() == itm)
layers.append(itm);
}
return layers;
}
QGSLayer* QGSMap::getLayer(int layerId)
{
foreach(QGSLayer *lyr, getLayers())
{
if(lyr->getId() == layerId)
{
return lyr;
break;
}
}
return 0;
}
QGSLayer* QGSMap::getLayer(QString layerName)
{
foreach(QGSLayer *lyr, getLayers())
{
if(lyr->getName() == layerName)
{
return lyr;
break;
}
}
return 0;
}
QGSLayer* QGSMap::addLayer(int layerId, QString layerName)
{
QGSLayer *lyr = new QGSLayer(layerId, layerName);
scene()->addItem(lyr);
lyr->setZValue(0);
return lyr;
}
bool QGSMap::deleteLayer(int layerId)
{
QGSLayer *lyr = getLayer(layerId);
delete lyr;
if(getLayer(layerId) == NULL)
return true;
else
return false;
}
bool QGSMap::deleteLayer(QString layerName)
{
QGSLayer *lyr = getLayer(layerName);
delete lyr;
if(getLayer(layerName) == NULL)
return true;
else
return false;
}
QGraphicsScene* QGSMap::loadMap(QString mapName)
{
if(serverSettings != NULL)
{
setMapInfo(getServerSettings()->getMapInfo(mapName));
if(scene() != NULL)
{
scene()->clear();
}
if(getMapInfo() != NULL)
{
QGraphicsScene *scene = new QGraphicsScene;
featureFactory = new QGSFeatueFactory(scene);
setScene(scene);
setMapCanvas();
setCurrentZoom(3);
setCacheDir(".");
setCacheDir("cache/" + QString::number(getMapInfo()->getMapSrs()) + "/" + getMapInfo()->getMapName() + "/" + QString::number(getCurrentZoom()));
paintMap();
return scene;
}
return NULL;
}
else
{
qDebug() << "Server settings are not defined!";
return NULL;
}
}
QGSSettings* QGSMap::getServerSettings()
{
return this->serverSettings;
}
bool QGSMap::setServerSettings(QString serverHost, int EPSG, int serverPort)
{
serverSettings = new QGSSettings(serverHost, EPSG, serverPort);
if(serverSettings->getConnectionState() == QGSSettings::Available)
{
return true;
}
else
{
serverSettings = NULL;
qDebug() << "Unable to connect";
return false;
}
}
void QGSMap::setMapInfo(QGSMapInfo *mapInfo)
{
this->mapInfo = mapInfo;
}
QGSMapInfo* QGSMap::getMapInfo()
{
return this->mapInfo;
}
void QGSMap::paintMap(bool reloadMap)
{
QGSCoordinateTransform ct;
QPointF centerPoint = getMapInfo()->getBoundingBox().center();
QPoint ptZero = ct.metersToTile(centerPoint, getCurrentZoom());
QPoint ptPxZero = ct.metersToPixels(centerPoint, getCurrentZoom());
QGSRect bbox = ct.getTileBounds(ptZero.x(), ptZero.y(), getCurrentZoom());
//init the coordinate transform
float L = bbox.getMaxX().toFloat() - bbox.getMinX().toFloat();
float x = centerPoint.x() - bbox.getMinX().toFloat();
float l = getMapInfo()->getTileWidth();
float x1 = (x/L)*l;
int hTiles = ((float)width() / 256) + 1;
int vTiles = ((float)height() / 256) + 1;
int si = 0;
int sj = 0;
for(int i=(ptZero.x()-1);i<(ptZero.x()+hTiles);i++)
{
for(int j=(ptZero.y()-1);j<(ptZero.y()+vTiles);j++)
{
requestImageFile(i, j, si, sj);
si++;
}
sj++;
}
QGSLayer *can = getMapCanvas();
can->setPos((-ptPxZero.x()+width()/2), (-ptPxZero.y()+height()/2));
}
void QGSMap::requestImageFile(int xTile, int yTile, int xNum, int yNum)
{
QGSCoordinateTransform ct;
QGSRect bbox = ct.getTileBounds(xTile, yTile, getCurrentZoom());
bbox.clearPlus();
//to complex! maybe variables for bbox and stuff?
QString urlBuffer;
urlBuffer.append( "http://" );
urlBuffer.append( getServerSettings()->getServerHost() );
urlBuffer.append( ":" );
urlBuffer.append( QString::number(getServerSettings()->getServerPort()) );
urlBuffer.append( "/geoserver/gwc/service/wms/" );
urlBuffer.append( "?LAYERS=" ).append( getMapInfo()->getMapName() );
urlBuffer.append( "&FORMAT=").append("image/png").append("&SERVICE=WMS&VERSION=1.1.1&" );
urlBuffer.append( "REQUEST=GetMap&STYLES=&EXCEPTIONS=application/vnd.ogc.se_inimage&" );
urlBuffer.append( "SRS=EPSG:").append(QString::number(getMapInfo()->getMapSrs())).append("&BBOX=" );
urlBuffer.append( bbox.getMinX() ).append( "," );
urlBuffer.append( bbox.getMinY() ).append( "," );
urlBuffer.append( bbox.getMaxX() ).append( "," );
urlBuffer.append( bbox.getMaxY() );
urlBuffer.append( "&WIDTH=").append(QString::number(getMapInfo()->getTileWidth())).append("&HEIGHT=").append(QString::number(getMapInfo()->getTileHeight()));
QGSImageLoader *iLoader = new QGSImageLoader(urlBuffer, xTile, yTile, xNum, yNum, this);
imageLoaders.append(iLoader);
connect(iLoader, SIGNAL(imageLoaded(QString,int)), this, SLOT(recieveImageFile(QString,int)));
}
void QGSMap::setCurrentZoom(int zoom)
{
this->currentZoom = zoom;
}
int QGSMap::getCurrentZoom()
{
return this->currentZoom;
}
QDir QGSMap::setCacheDir(QString cachePath)
{
if(!cacheDir.exists(cachePath))
cacheDir.mkpath(cachePath);
cacheDir = QDir(cachePath);
return cacheDir;
}
QDir QGSMap::getCacheDir()
{
return cacheDir;
}
void QGSMap::recieveImageFile(QString fileName, int loaderId)
{
QPixmap imagePixmap = QPixmap(fileName);
QGraphicsPixmapItem *imgItem = new QGraphicsPixmapItem(imagePixmap);
QGSImageLoader *imgLoad = getLoaderById(loaderId);
int tx = imgLoad->getTileNum(X, false)*256;
int ty = imgLoad->getTileNum(Y, false)*256;
imgItem->setPos(tx, ty);
imgItem->setParentItem(getMapCanvas());
scene()->addItem(imgItem);
for(int i=0;i<imageLoaders.count();i++)
{
QGSImageLoader *iLoader = imageLoaders.at(i);
if(iLoader->getLoaderId() == loaderId)
{
imageLoaders.removeAt(i);
disconnect(iLoader, SIGNAL(imageLoaded(QString,int)), this, SLOT(recieveImageFile(QString,int)));
//delete iLoader;
break;
}
}
}
QGSLayer* QGSMap::getMapCanvas()
{
return mapCanvas;
}
void QGSMap::setMapCanvas()
{
if(mapCanvas != NULL)
clearMapCanvas();
mapCanvas = new QGSLayer(13116, "bckgrnd_original");
}
void QGSMap::clearMapCanvas()
{
mapCanvas = NULL;
delete mapCanvas;
}
QGSImageLoader* QGSMap::getLoaderById(int loaderId)
{
for(int i=0;i<imageLoaders.count();i++)
{
QGSImageLoader *iLoader = imageLoaders.at(i);
if(iLoader->getLoaderId() == loaderId)
return iLoader;
}
return NULL;
}
QTransform QGSMap::getWorldToScreen()
{
QTransform tr;
double scale = 1;
tr.translate(0, 0);
tr.scale(scale, -(scale));
if(getMapInfo() != NULL)
{
tr.translate(-getMapInfo()->getBoundingBox().getMinX().toDouble(), -getMapInfo()->getBoundingBox().getMaxY().toDouble());
}
return tr;
}