/
qgspointdialog.cpp
461 lines (390 loc) · 12.8 KB
/
qgspointdialog.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
#include <cmath>
#include <QPushButton>
#include <QComboBox>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QLayout>
#include <QLineEdit>
#include <QMessageBox>
#include <QTextStream>
#include <QToolButton>
#include "qgisiface.h"
#include "qgsapplication.h"
#include "qgspointdialog.h"
#include "mapcoordsdialog.h"
#include "qgsleastsquares.h"
#include "qgsimagewarper.h"
#include "qgsgeorefwarpoptionsdialog.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaptoolzoom.h"
#include "qgsmaptoolpan.h"
#include "qgsgeorefdatapoint.h"
class QgsGeorefTool : public QgsMapTool
{
public:
QgsGeorefTool(QgsMapCanvas* canvas, QgsPointDialog* dlg, bool addPoint)
: QgsMapTool(canvas), mDlg(dlg), mAddPoint(addPoint)
{
}
//! Mouse press event for overriding
virtual void canvasPressEvent(QMouseEvent * e)
{
QgsPoint pnt = toMapCoords(e->pos());
if (mAddPoint)
mDlg->showCoordDialog(pnt);
else
mDlg->deleteDataPoint(pnt);
}
virtual void canvasMoveEvent(QMouseEvent * e) { }
virtual void canvasReleaseEvent(QMouseEvent * e) { }
private:
QgsPointDialog* mDlg;
bool mAddPoint;
};
QgsPointDialog::QgsPointDialog(QString layerPath, QgisIface* theQgisInterface,
QWidget* parent, Qt::WFlags fl)
: QDialog(parent, fl), mIface(theQgisInterface)
{
setupUi(this);
QString myIconPath = QgsApplication::themePath();
// setup actions
//
mActionZoomIn= new QAction(QIcon(myIconPath+"/mActionZoomIn.png"), tr("Zoom In"), this);
mActionZoomIn->setShortcut(tr("z"));
mActionZoomIn->setStatusTip(tr("Zoom In"));
connect(mActionZoomIn, SIGNAL(triggered()), this, SLOT(zoomIn()));
//
mActionZoomOut= new QAction(QIcon(myIconPath+"/mActionZoomOut.png"), tr("Zoom Out"), this);
mActionZoomOut->setShortcut(tr("Z"));
mActionZoomOut->setStatusTip(tr("Zoom Out"));
connect(mActionZoomOut, SIGNAL(triggered()), this, SLOT(zoomOut()));
//
mActionZoomToLayer= new QAction(QIcon(myIconPath+"/mActionZoomToLayer.png"), tr("Zoom To Layer"), this);
//mActionZoomToLayer->setShortcut(tr("Ctrl+O"));
mActionZoomToLayer->setStatusTip(tr("Zoom to Layer"));
connect(mActionZoomToLayer, SIGNAL(triggered()), this, SLOT(zoomToLayer()));
//
mActionPan= new QAction(QIcon(myIconPath+"/mActionPan.png"), tr("Pan Map"), this);
mActionPan->setStatusTip(tr("Pan the map"));
connect(mActionPan, SIGNAL(triggered()), this, SLOT(pan()));
//
mActionAddPoint= new QAction(QIcon(myIconPath+"/mActionCapturePoint.png"), tr("Add Point"), this);
mActionAddPoint->setShortcut(tr("."));
mActionAddPoint->setStatusTip(tr("Capture Points"));
connect(mActionAddPoint, SIGNAL(triggered()), this, SLOT(addPoint()));
//
mActionDeletePoint = new QAction(QIcon(myIconPath+"/mActionDeleteSelected.png"), tr("Delete Point"), this);
mActionDeletePoint->setStatusTip(tr("Delete Selected"));
connect(mActionDeletePoint, SIGNAL(triggered()), this, SLOT(deletePoint()));
// Map Tool Group
mMapToolGroup = new QActionGroup(this);
mActionPan->setCheckable(true);
mMapToolGroup->addAction(mActionPan);
mActionZoomIn->setCheckable(true);
mMapToolGroup->addAction(mActionZoomIn);
mActionZoomOut->setCheckable(true);
mMapToolGroup->addAction(mActionZoomOut);
mMapToolGroup->addAction(mActionZoomToLayer);
mActionAddPoint->setCheckable(true);
mMapToolGroup->addAction(mActionAddPoint);
mActionDeletePoint->setCheckable(true);
mMapToolGroup->addAction(mActionDeletePoint);
// set appopriate actions for tool buttons
tbnZoomIn->setDefaultAction(mActionZoomIn);
tbnZoomOut->setDefaultAction(mActionZoomOut);
tbnZoomToLayer->setDefaultAction(mActionZoomToLayer);
tbnPan->setDefaultAction(mActionPan);
tbnAddPoint->setDefaultAction(mActionAddPoint);
tbnDeletePoint->setDefaultAction(mActionDeletePoint);
// set up the canvas
QHBoxLayout* layout = new QHBoxLayout(canvasFrame);
layout->setAutoAdd(true);
mCanvas = new QgsMapCanvas(canvasFrame, "georefCanvas");
mCanvas->setBackgroundColor(Qt::white);
mCanvas->setMinimumWidth(400);
mCanvas->freeze(true);
// open raster layer
QgsRasterLayer* layer = new QgsRasterLayer(layerPath, "Raster");
mLayer = layer;
// add to map layer registry, do not signal addition
// so layer is not added to legend
QgsMapLayerRegistry* registry = QgsMapLayerRegistry::instance();
registry->addMapLayer(layer, FALSE);
// add layer to map canvas
std::deque<QString> layers;
layers.push_back(layer->getLayerID());
mCanvas->setLayerSet(layers);
// load previously added points
QFile pointFile(mLayer->source() + ".points");
if (pointFile.open(QIODevice::ReadOnly)) {
QTextStream points(&pointFile);
QString tmp;
points>>tmp>>tmp>>tmp>>tmp;
while (!points.atEnd()) {
double mapX, mapY, pixelX, pixelY;
points>>mapX>>mapY>>pixelX>>pixelY;
QgsPoint mapCoords(mapX, mapY);
QgsPoint pixelCoords(pixelX, pixelY);
addPoint(pixelCoords, mapCoords);
}
}
mCanvas->setExtent(mLayer->extent());
mCanvas->freeze(false);
leSelectWorldFile->setText(guessWorldFileName(mLayer->source()));
// set adding points as default tool
addPoint();
mCanvas->refresh();
}
QgsPointDialog::~QgsPointDialog()
{
// delete layer (and don't signal it as it's our private layer)
if (mLayer)
{
QgsMapLayerRegistry::instance()->removeMapLayer(mLayer->getLayerID(), FALSE);
}
}
void QgsPointDialog::addPoint(const QgsPoint& pixelCoords, const QgsPoint& mapCoords)
{
static int acetateCounter = 0;
QgsGeorefDataPoint* pnt = new QgsGeorefDataPoint(mCanvas,
acetateCounter++, pixelCoords, mapCoords);
pnt->show();
mPoints.push_back(pnt);
mCanvas->refresh();
}
void QgsPointDialog::on_pbnCancel_clicked()
{
reject();
}
void QgsPointDialog::on_pbnGenerateWorldFile_clicked()
{
if (generateWorldFile())
{
accept();
}
}
void QgsPointDialog::on_pbnGenerateAndLoad_clicked()
{
if (generateWorldFile())
{
QString source = mLayer->source();
// delete layer before it's loaded again (otherwise it segfaults)
QgsMapLayerRegistry::instance()->removeMapLayer(mLayer->getLayerID(), FALSE);
mLayer = 0;
// load raster to the main map canvas of QGIS
if (cmbTransformType->currentItem() == 0)
mIface->addRasterLayer(source);
else
mIface->addRasterLayer(leSelectModifiedRaster->text());
accept();
}
}
void QgsPointDialog::on_pbnSelectWorldFile_clicked()
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Choose a name for the world file"), ".");
leSelectWorldFile->setText(filename);
}
void QgsPointDialog::on_pbnSelectModifiedRaster_clicked()
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Choose a name for the world file"), ".");
if (filename.right(4) != ".tif")
filename += ".tif";
leSelectModifiedRaster->setText(filename);
leSelectWorldFile->setText(guessWorldFileName(filename));
}
bool QgsPointDialog::generateWorldFile()
{
QgsPoint origin(0, 0);
double pixelSize = 1;
double rotation = 0;
// create arrays with points from mPoints
std::vector<QgsPoint> pixelCoords, mapCoords;
for (unsigned int i = 0; i < mPoints.size(); i++)
{
QgsGeorefDataPoint* pt = mPoints[i];
pixelCoords.push_back(pt->pixelCoords());
mapCoords.push_back(pt->mapCoords());
}
// compute the parameters using the least squares method
// (might throw std::domain_error)
try
{
if (cmbTransformType->currentItem() == 0)
{
QgsLeastSquares::linear(mapCoords, pixelCoords, origin, pixelSize);
}
else if (cmbTransformType->currentItem() == 1)
{
int res = QMessageBox::warning(this, tr("Warning"),
tr("A Helmert transform requires modifications in "
"the raster layer.\nThe modifed raster will be "
"saved in a new file and a world file will be "
"generated for this new file instead.\nAre you "
"sure that this is what you want?"),
QMessageBox::No, QMessageBox::Yes);
if (res == QMessageBox::No)
return false;
QgsLeastSquares::helmert(mapCoords, pixelCoords, origin, pixelSize, rotation);
}
else if (cmbTransformType->currentItem() == 2)
{
QMessageBox::critical(this, tr("Not implemented!"),
tr("An affine transform requires changing the "
"original raster file. This is not yet "
"supported."));
return false;
}
}
catch (std::domain_error& e)
{
QMessageBox::critical(this, tr("Error"), QString(e.what()));
return false;
}
// warp the raster if needed
double xOffset, yOffset;
if (rotation != 0)
{
QgsGeorefWarpOptionsDialog d(this);
d.exec();
bool useZeroForTrans;
QgsImageWarper::ResamplingMethod resampling;
QgsImageWarper warper(-rotation);
d.getWarpOptions(resampling, useZeroForTrans);
warper.warp(mLayer->source(), leSelectModifiedRaster->text(),
xOffset, yOffset, resampling, useZeroForTrans);
}
// write the world file
QFile file(leSelectWorldFile->text());
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this, tr("Error"),
tr("Could not write to ") + leSelectWorldFile->text());
return false;
}
QTextStream stream(&file);
stream<<pixelSize<<endl
<<0<<endl
<<0<<endl
<<-pixelSize<<endl
<<(origin.x() - xOffset * pixelSize)<<endl
<<(origin.y() + yOffset * pixelSize)<<endl;
// write the data points in case we need them later
QFile pointFile(mLayer->source() + ".points");
if (pointFile.open(QIODevice::WriteOnly))
{
QTextStream points(&pointFile);
points<<"mapX\tmapY\tpixelX\tpixelY"<<endl;
for (unsigned int i = 0; i < mapCoords.size(); ++i)
{
points<<(QString("%1\t%2\t%3\t%4").
arg(mapCoords[i].x()).arg(mapCoords[i].y()).
arg(pixelCoords[i].x()).arg(pixelCoords[i].y()))<<endl;
}
}
return true;
}
void QgsPointDialog::zoomIn()
{
QgsMapToolZoom* tool = new QgsMapToolZoom(mCanvas, FALSE /* zoomOut */);
tool->setAction(mActionZoomIn);
mCanvas->setMapTool(tool);
}
void QgsPointDialog::zoomOut()
{
QgsMapToolZoom* tool = new QgsMapToolZoom(mCanvas, TRUE /* zoomOut */);
tool->setAction(mActionZoomOut);
mCanvas->setMapTool(tool);
}
void QgsPointDialog::zoomToLayer()
{
mCanvas->setExtent(mLayer->extent());
mCanvas->refresh();
}
void QgsPointDialog::pan()
{
QgsMapToolPan* tool = new QgsMapToolPan(mCanvas);
tool->setAction(mActionPan);
mCanvas->setMapTool(tool);
}
void QgsPointDialog::addPoint()
{
QgsGeorefTool* tool = new QgsGeorefTool(mCanvas, this, TRUE /* addPoint */);
tool->setAction(mActionAddPoint);
mCanvas->setMapTool(tool);
}
void QgsPointDialog::deletePoint()
{
QgsGeorefTool* tool = new QgsGeorefTool(mCanvas, this, FALSE /* addPoint */);
tool->setAction(mActionDeletePoint);
mCanvas->setMapTool(tool);
}
void QgsPointDialog::showCoordDialog(QgsPoint& pixelCoords)
{
MapCoordsDialog* mcd = new MapCoordsDialog(pixelCoords, this);
connect(mcd, SIGNAL(pointAdded(const QgsPoint&, const QgsPoint&)),
this, SLOT(addPoint(const QgsPoint&, const QgsPoint&)));
mcd->show();
}
void QgsPointDialog::deleteDataPoint(QgsPoint& coords)
{
std::vector<QgsGeorefDataPoint*>::iterator it = mPoints.begin();
double maxDistSqr = (5 * mCanvas->mupp())*(5 * mCanvas->mupp());
//#ifdef QGISDEBUG
std::cout << "deleteDataPoint! maxDistSqr: " << maxDistSqr << std::endl;
//#endif
for ( ; it != mPoints.end(); it++)
{
QgsGeorefDataPoint* pt = *it;
double x = pt->pixelCoords().x() - coords.x();
double y = pt->pixelCoords().y() - coords.y();
//#ifdef QGISDEBUG
std::cout << "deleteDataPoint! test: " << (x*x+y*y) << std::endl;
//#endif
if ((x*x + y*y) < maxDistSqr)
{
mPoints.erase(it);
mCanvas->refresh();
break;
}
}
}
void QgsPointDialog::enableRelevantControls()
{
if (cmbTransformType->currentItem() == 0)
{
leSelectModifiedRaster->setEnabled(false);
pbnSelectModifiedRaster->setEnabled(false);
}
else
{
leSelectModifiedRaster->setEnabled(true);
pbnSelectModifiedRaster->setEnabled(true);
}
if ((cmbTransformType->currentItem() == 0 &&
!leSelectWorldFile->text().isEmpty()) ||
(!leSelectWorldFile->text().isEmpty() &&
!leSelectModifiedRaster->text().isEmpty()))
{
pbnGenerateWorldFile->setEnabled(true);
pbnGenerateAndLoad->setEnabled(true);
}
else
{
pbnGenerateWorldFile->setEnabled(false);
pbnGenerateAndLoad->setEnabled(false);
}
}
QString QgsPointDialog::guessWorldFileName(const QString& raster)
{
int point = raster.findRev('.');
QString worldfile = "";
if (point != -1 && point != raster.length() - 1) {
worldfile = raster.left(point + 1);
worldfile += raster.at(point + 1);
worldfile += raster.at(raster.length() - 1);
worldfile += 'w';
}
return worldfile;
}