Skip to content

Commit fc7c89d

Browse files
author
homann
committedFeb 4, 2009
New georeferencer from GIS-Lab thanks to Maxim Dubinin!
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10107 c8812cc2-4d05-0410-92ff-de0c093fc19c

16 files changed

+946
-829
lines changed
 

‎src/plugins/georeferencer/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
21
########################################################
32
# Files
43

54
SET (GEOREF_SRCS
65
plugin.cpp
76
qgsgeorefdatapoint.cpp
8-
plugingui.cpp
97
qgsimagewarper.cpp
108
qgsleastsquares.cpp
119
qgspointdialog.cpp
@@ -15,7 +13,6 @@ SET (GEOREF_SRCS
1513
)
1614

1715
SET (GEOREF_UIS
18-
pluginguibase.ui
1916
qgspointdialogbase.ui
2017
mapcoordsdialogbase.ui
2118
qgsgeorefdescriptiondialogbase.ui
@@ -24,7 +21,6 @@ SET (GEOREF_UIS
2421

2522
SET (GEOREF_MOC_HDRS
2623
plugin.h
27-
plugingui.h
2824
qgspointdialog.h
2925
mapcoordsdialog.h
3026
qgsgeorefwarpoptionsdialog.h
@@ -51,7 +47,9 @@ INCLUDE_DIRECTORIES(
5147
../../gui
5248
..
5349
${GSL_INCLUDE_DIR}
50+
${QGIS_INCLUDE_DIR}
5451
${GDAL_INCLUDE_DIR}
52+
${QT_QTXML_INCLUDE_DIR}
5553
)
5654

5755
TARGET_LINK_LIBRARIES(georefplugin
@@ -60,7 +58,6 @@ TARGET_LINK_LIBRARIES(georefplugin
6058
${GSL_LIBRARIES}
6159
)
6260

63-
6461
########################################################
6562
# Install
6663

‎src/plugins/georeferencer/affine.jpg

-66.7 KB
Binary file not shown.

‎src/plugins/georeferencer/helmert.jpg

-92.2 KB
Binary file not shown.

‎src/plugins/georeferencer/mapcoordsdialog.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
MapCoordsDialog::MapCoordsDialog()
2323
{
24-
24+
this->setWindowFlags(!Qt::Dialog);
25+
this->setWindowFlags(Qt::WindowSystemMenuHint);
26+
this->setWindowFlags(Qt::WindowMinimizeButtonHint);
27+
this->setWindowFlags(Qt::WindowMaximizeButtonHint);
2528
}
2629

2730

‎src/plugins/georeferencer/plugin.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@
5050
//
5151
//the gui subclass
5252
//
53-
#include "plugingui.h"
54-
53+
//#include "plugingui.h"
54+
#include "qgspointdialog.h"
55+
#include "qgsgeorefdescriptiondialog.h"
5556

5657
static const char * const sIdent = "$Id$";
57-
static const QString sName = QObject::tr( "Georeferencer" );
58-
static const QString sDescription = QObject::tr( "Adding projection info to rasters" );
58+
static const QString sName = QObject::tr( "Georeferencer GDAL" );
59+
static const QString sDescription = QObject::tr( "Adding projection info to rasters using GDAL" );
5960
static const QString sPluginVersion = QObject::tr( "Version 0.1" );
6061
static const QgisPlugin::PLUGINTYPE sPluginType = QgisPlugin::UI;
6162

@@ -97,19 +98,53 @@ void QgsGeorefPlugin::initGui()
9798
mQGisIface->addToolBarIcon( mQActionPointer );
9899
mQGisIface->addPluginToMenu( tr( "&Georeferencer" ), mQActionPointer );
99100

101+
mQActionPointer = new QAction( QIcon( ":/about.png" ), tr( "&Georeferencer" ), this );
102+
mQActionPointer = new QAction("About", this);
103+
connect(mQActionPointer, SIGNAL(triggered()), SLOT(about()));
104+
mQGisIface->addPluginToMenu(tr ("&Georeferencer"), mQActionPointer);
105+
106+
mQActionPointer = new QAction( QIcon( ":/help.png" ), tr( "&Georeferencer" ), this );
107+
mQActionPointer = new QAction("Help", this);
108+
connect(mQActionPointer, SIGNAL(triggered()), SLOT(help()));
109+
mQGisIface->addPluginToMenu(tr ("&Georeferencer"), mQActionPointer);
100110
}
101111
//method defined in interface
102112
void QgsGeorefPlugin::help()
103113
{
104-
//implement me!
114+
QgsGeorefDescriptionDialog dlg( mQGisIface->mainWindow( ) );
115+
dlg.exec();
116+
}
117+
118+
void QgsGeorefPlugin::about( )
119+
{
120+
QDialog dlg( mQGisIface->mainWindow( ) );
121+
dlg.setWindowFlags( dlg.windowFlags( ) | Qt::MSWindowsFixedSizeDialogHint );
122+
dlg.setWindowFlags( dlg.windowFlags( ) &~ Qt::WindowContextHelpButtonHint );
123+
QVBoxLayout *lines = new QVBoxLayout( &dlg );
124+
lines->addWidget( new QLabel( tr( "<b>Georeferencer GDAL</b>") ) );
125+
lines->addWidget( new QLabel( tr( " Based on original Georeferencer Plugin" ) ) );
126+
lines->addWidget( new QLabel( tr( "<b>Developers:</b>" ) ) );
127+
lines->addWidget( new QLabel( tr( " Lars Luthman (original Georeferencer)" ) ) );
128+
lines->addWidget( new QLabel( " Lynx (lynx21.12.12@gmail.ru)" ));
129+
lines->addWidget( new QLabel( " Maxim Dubinin (sim@gis-lab.info)" ));
130+
lines->addWidget( new QLabel( tr( "<b>Links:</b>" ) ) );
131+
QLabel *link = new QLabel( " <a href=\"http://gis-lab.info/qa/qgis-georef-new-eng.html\">http://gis-lab.info/qa/qgis-georef-new-eng.html</a>");
132+
link->setOpenExternalLinks( true );
133+
lines->addWidget( link );
134+
135+
dlg.exec( );
105136
}
106137

107138
// Slot called when the buffer menu item is triggered
108139
void QgsGeorefPlugin::run()
109140
{
110-
QgsGeorefPluginGui *myPluginGui = new QgsGeorefPluginGui( mQGisIface, QgsGeorefPluginGui::findMainWindow() );
111-
myPluginGui->show();
112-
myPluginGui->setFocus();
141+
// QgsGeorefPluginGui *myPluginGui = new QgsGeorefPluginGui( mQGisIface, QgsGeorefPluginGui::findMainWindow(), Qt::Window | Qt::WindowMinimizeButtonHint);
142+
// myPluginGui->show();
143+
// myPluginGui->setFocus();
144+
QgsPointDialog *myPlugin = new QgsPointDialog(mQGisIface, QgsPointDialog::findMainWindow(), Qt::Window | Qt::WindowMinimizeButtonHint);
145+
myPlugin->move(0, 0);
146+
myPlugin->show();
147+
myPlugin->setFocus();
113148
}
114149

115150
// Unload the plugin by cleaning up the GUI

‎src/plugins/georeferencer/plugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class QgsGeorefPlugin: public QObject, public QgisPlugin
8787
//! show the help document
8888
void help();
8989

90+
void about( );
9091
//////////////////////////////////////////////////////////////////////
9192
//
9293
// END OF MANDATORY PLUGIN METHODS

‎src/plugins/georeferencer/plugingui.cpp

Lines changed: 0 additions & 231 deletions
This file was deleted.

‎src/plugins/georeferencer/plugingui.h

Lines changed: 0 additions & 55 deletions
This file was deleted.

‎src/plugins/georeferencer/pluginguibase.ui

Lines changed: 0 additions & 112 deletions
This file was deleted.

‎src/plugins/georeferencer/qgsgeorefwarpoptionsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
QgsGeorefWarpOptionsDialog::QgsGeorefWarpOptionsDialog( QWidget* parent )
20-
: QgsGeorefWarpOptionsDialogBase()
20+
: QDialog(parent) //QgsGeorefWarpOptionsDialogBase()
2121
{
2222
setupUi( this );
2323
QStringList compressionMethods;

‎src/plugins/georeferencer/qgsimagewarper.cpp

Lines changed: 110 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,43 @@
2525

2626
#include "qgsimagewarper.h"
2727

28+
bool QgsImageWarper::openSrcDSAndGetWarpOpt(const QString &input, const QString &output,
29+
const ResamplingMethod &resampling, const GDALTransformerFunc &pfnTransform,
30+
GDALDatasetH &hSrcDS, GDALWarpOptions *&psWarpOptions)
31+
{
32+
// Open input file
33+
GDALAllRegister();
34+
hSrcDS = GDALOpen( QFile::encodeName( input ).constData(), GA_ReadOnly );
35+
if (hSrcDS == NULL) return false;
36+
37+
// Setup warp options.
38+
psWarpOptions = GDALCreateWarpOptions();
39+
psWarpOptions->hSrcDS = hSrcDS;
40+
psWarpOptions->nBandCount = GDALGetRasterCount( hSrcDS );
41+
psWarpOptions->panSrcBands =
42+
( int * ) CPLMalloc( sizeof( int ) * psWarpOptions->nBandCount );
43+
psWarpOptions->panDstBands =
44+
( int * ) CPLMalloc( sizeof( int ) * psWarpOptions->nBandCount );
45+
for ( int i = 0; i < psWarpOptions->nBandCount; ++i )
46+
{
47+
psWarpOptions->panSrcBands[i] = i + 1;
48+
psWarpOptions->panDstBands[i] = i + 1;
49+
}
50+
psWarpOptions->pfnProgress = GDALTermProgress;
51+
psWarpOptions->pfnTransformer = pfnTransform;
52+
psWarpOptions->eResampleAlg = GDALResampleAlg( resampling );
53+
54+
return true;
55+
}
56+
2857
void QgsImageWarper::warp( const QString& input, const QString& output,
2958
double& xOffset, double& yOffset,
3059
ResamplingMethod resampling, bool useZeroAsTrans, const QString& compression )
3160
{
32-
// Open input file
33-
GDALAllRegister();
34-
GDALDatasetH hSrcDS = GDALOpen( QFile::encodeName( input ).constData(), GA_ReadOnly );
35-
// Setup warp options.
36-
GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions();
37-
psWarpOptions->hSrcDS = hSrcDS;
38-
psWarpOptions->nBandCount = GDALGetRasterCount( hSrcDS );
39-
psWarpOptions->panSrcBands =
40-
( int * ) CPLMalloc( sizeof( int ) * psWarpOptions->nBandCount );
41-
psWarpOptions->panDstBands =
42-
( int * ) CPLMalloc( sizeof( int ) * psWarpOptions->nBandCount );
43-
for ( int i = 0; i < psWarpOptions->nBandCount; ++i )
44-
{
45-
psWarpOptions->panSrcBands[i] = i + 1;
46-
psWarpOptions->panDstBands[i] = i + 1;
47-
}
48-
psWarpOptions->pfnProgress = GDALTermProgress;
49-
psWarpOptions->pfnTransformer = &QgsImageWarper::transform;
50-
psWarpOptions->eResampleAlg = GDALResampleAlg( resampling );
61+
GDALDatasetH hSrcDS;
62+
GDALWarpOptions *psWarpOptions;
63+
openSrcDSAndGetWarpOpt(input, output, resampling, &QgsImageWarper::transform,
64+
hSrcDS, psWarpOptions);
5165

5266
// check the bounds for the warped raster
5367
// order: upper right, lower right, lower left (y points down)
@@ -142,3 +156,80 @@ int QgsImageWarper::transform( void *pTransformerArg, int bDstToSrc,
142156
}
143157
return TRUE;
144158
}
159+
160+
bool QgsImageWarper::warpgcp( const QString& input, const QString& output,
161+
const char *worldExt,
162+
std::vector<QgsPoint> mapCoords,
163+
std::vector<QgsPoint> pixelCoords,
164+
const int nReqOrder, ResamplingMethod resampling,
165+
bool useZeroAsTrans, const QString& compression, bool bUseTPS)
166+
{
167+
int n = mapCoords.size();
168+
if ((nReqOrder == 1 && n < 3) || (nReqOrder == 2 && n < 6) ||
169+
(nReqOrder == 3 && n < 10) ) return false;
170+
171+
CPLErr eErr;
172+
GDALDatasetH hSrcDS;
173+
GDALWarpOptions *psWarpOptions;
174+
openSrcDSAndGetWarpOpt(input, output, resampling,
175+
bUseTPS ? *GDALTPSTransform : *GDALGCPTransform,
176+
hSrcDS, psWarpOptions);
177+
178+
GDAL_GCP *pasGCPList = (GDAL_GCP *) malloc(n * sizeof(GDAL_GCP));
179+
180+
for (int i = 0; i < n; i++) {
181+
pasGCPList[i].pszId = (char *) malloc(20 * sizeof(char));
182+
sprintf(pasGCPList[i].pszId, "gcp%i", i);
183+
pasGCPList[i].pszInfo = NULL;
184+
pasGCPList[i].dfGCPPixel = pixelCoords[i].x();
185+
pasGCPList[i].dfGCPLine = - pixelCoords[i].y();
186+
pasGCPList[i].dfGCPX = mapCoords[i].x();
187+
pasGCPList[i].dfGCPY = mapCoords[i].y();
188+
pasGCPList[i].dfGCPZ = 0;
189+
}
190+
191+
GDALDriverH hDriver = GDALGetDriverByName( "GTiff" );
192+
GDALDatasetH hSrcCopyDS = GDALCreateCopy(hDriver, ".tmpcopy.tif", hSrcDS,
193+
TRUE, NULL, NULL, NULL);
194+
if (hSrcCopyDS == NULL) return false;
195+
196+
GDALSetGCPs(hSrcCopyDS, n, pasGCPList, "");
197+
198+
if(bUseTPS)
199+
psWarpOptions->pTransformerArg = GDALCreateTPSTransformer(n, pasGCPList, false);
200+
else
201+
psWarpOptions->pTransformerArg = GDALCreateGCPTransformer(n, pasGCPList, nReqOrder, false);
202+
203+
if (psWarpOptions->pTransformerArg == NULL) return false;
204+
205+
// create and warp the output file
206+
char **papszOptions = NULL;
207+
papszOptions = CSLSetNameValue( papszOptions, "INIT_DEST", "NO_DATA" );
208+
papszOptions = CSLSetNameValue( papszOptions, "COMPRESS", compression.toAscii() );
209+
eErr = GDALCreateAndReprojectImage(hSrcCopyDS,
210+
/*pszSrcWKT*/"", QFile::encodeName(output).constData(), /*pszDstWKT*/"",
211+
hDriver, papszOptions, GDALResampleAlg(resampling), 0.0, 0, NULL, NULL, psWarpOptions);
212+
if (eErr != CE_None) return false;
213+
214+
// write worldfile
215+
int nPixels, nLines;
216+
double adfGeoTransform[6];
217+
218+
eErr = GDALSuggestedWarpOutput(hSrcDS, GDALGCPTransform,
219+
psWarpOptions->pTransformerArg,
220+
adfGeoTransform, &nPixels, &nLines);
221+
if (eErr != CE_None) return false;
222+
223+
GDALWriteWorldFile(QFile::encodeName(output).constData(), worldExt, adfGeoTransform);
224+
225+
free(pasGCPList);
226+
if (bUseTPS)
227+
GDALDestroyTPSTransformer(psWarpOptions->pTransformerArg);
228+
else
229+
GDALDestroyGCPTransformer(psWarpOptions->pTransformerArg);
230+
GDALDestroyWarpOptions( psWarpOptions );
231+
GDALClose( hSrcDS );
232+
233+
return true;
234+
}
235+

‎src/plugins/georeferencer/qgsimagewarper.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <gdalwarper.h>
2020
#include <QString>
2121

22+
#include <vector>
23+
#include "qgspoint.h"
2224

2325
class QgsImageWarper
2426
{
@@ -28,15 +30,26 @@ class QgsImageWarper
2830
{
2931
NearestNeighbour = GRA_NearestNeighbour,
3032
Bilinear = GRA_Bilinear,
31-
Cubic = GRA_Cubic
33+
Cubic = GRA_Cubic,
3234
};
3335

3436

35-
QgsImageWarper( double angle ) : mAngle( angle ) { }
37+
QgsImageWarper() { };
38+
QgsImageWarper( double angle) : mAngle( angle ) { };
3639

3740
void warp( const QString& input, const QString& output,
3841
double& xOffset, double& yOffset,
39-
ResamplingMethod resampling = Bilinear, bool useZeroAsTrans = true, const QString& compression = "NONE" );
42+
ResamplingMethod resampling = Bilinear,
43+
bool useZeroAsTrans = true,
44+
const QString& compression = "NONE" );
45+
46+
bool warpgcp( const QString& input, const QString& output,
47+
const char *worldExt,
48+
std::vector<QgsPoint> mapCoords,
49+
std::vector<QgsPoint> pixelCoords,
50+
const int nReqOrder = 1, ResamplingMethod resampling = Bilinear,
51+
bool useZeroAsTrans = true, const QString& compression = "NONE",
52+
bool bUseTPS = false);
4053

4154
private:
4255

@@ -47,6 +60,9 @@ class QgsImageWarper
4760
double y0;
4861
};
4962

63+
bool openSrcDSAndGetWarpOpt(const QString &input, const QString &output,
64+
const ResamplingMethod &resampling, const GDALTransformerFunc &pfnTransform,
65+
GDALDatasetH &hSrcDS, GDALWarpOptions *&psWarpOptions);
5066

5167
static int transform( void *pTransformerArg, int bDstToSrc, int nPointCount,
5268
double *x, double *y, double *z, int *panSuccess );

‎src/plugins/georeferencer/qgsleastsquares.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void QgsLeastSquares::linear( std::vector<QgsPoint> mapCoords,
3030
int n = mapCoords.size();
3131
if ( n < 2 )
3232
{
33-
throw std::domain_error( QObject::tr( "Fit to a linear transform requires at least 2 points." ).toLocal8Bit().constData() );
33+
throw std::domain_error( QObject::tr( "Fit to a linear transform requires at "
34+
"least 2 points." ).toLocal8Bit().constData() );
3435
}
3536

3637
double sumPx( 0 ), sumPy( 0 ), sumPx2( 0 ), sumPy2( 0 ), sumPxMx( 0 ), sumPyMy( 0 ),
@@ -71,7 +72,8 @@ void QgsLeastSquares::helmert( std::vector<QgsPoint> mapCoords,
7172
int n = mapCoords.size();
7273
if ( n < 2 )
7374
{
74-
throw std::domain_error( QObject::tr( "Fit to a Helmert transform requires at least 2 points." ).toLocal8Bit().constData() );
75+
throw std::domain_error( QObject::tr( "Fit to a Helmert transform requires at "
76+
"least 2 points." ).toLocal8Bit().constData() );
7577
}
7678

7779
double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, G = 0, H = 0, I = 0, J = 0;
@@ -126,7 +128,8 @@ void QgsLeastSquares::affine( std::vector<QgsPoint> mapCoords,
126128
int n = mapCoords.size();
127129
if ( n < 4 )
128130
{
129-
throw std::domain_error( QObject::tr( "Fit to an affine transform requires at least 4 points." ).toLocal8Bit().constData() );
131+
throw std::domain_error( QObject::tr( "Fit to an affine transform requires at "
132+
"least 4 points." ).toLocal8Bit().constData() );
130133
}
131134

132135
double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0,

‎src/plugins/georeferencer/qgspointdialog.cpp

Lines changed: 388 additions & 125 deletions
Large diffs are not rendered by default.

‎src/plugins/georeferencer/qgspointdialog.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,36 @@ class QgsPointDialog : public QDialog, private Ui::QgsPointDialogBase
4343
void openImageFile( QString layerPath );
4444
void showCoordDialog( QgsPoint& pixelCoords );
4545
void deleteDataPoint( QgsPoint& pixelCoords );
46+
static QWidget* findMainWindow();
4647

4748
public slots:
4849

4950
void addPoint( const QgsPoint& pixelCoords, const QgsPoint& mapCoords );
51+
void on_pbnClose_clicked();
52+
void on_pbnSelectRaster_clicked();
5053
void on_pbnGenerateWorldFile_clicked();
5154
void on_pbnGenerateAndLoad_clicked();
5255
void on_pbnSelectWorldFile_clicked();
5356
void on_pbnSelectModifiedRaster_clicked();
57+
void on_pbnSaveGCPs_clicked();
58+
void on_pbnLoadGCPs_clicked();
5459
void on_cmbTransformType_currentIndexChanged( const QString& );
60+
void on_leSelectModifiedRaster_textChanged(const QString &);
5561
void zoomIn();
5662
void zoomOut();
5763
void zoomToLayer();
5864
void pan();
5965
void addPoint();
6066
void deletePoint();
61-
void enableRelevantControls( void );
67+
// void enableRelevantControls( void );
6268

6369
private:
6470

6571
void initialize();
66-
bool generateWorldFile();
72+
bool generateWorldFileAndWarp();
73+
bool helmertWarp();
74+
void loadGCPs(QString &);
75+
void saveGCPs( std::vector<QgsPoint>, std::vector<QgsPoint> );
6776
QString guessWorldFileName( const QString& raster );
6877

6978
void enableModifiedRasterControls( bool state );
@@ -86,6 +95,13 @@ class QgsPointDialog : public QDialog, private Ui::QgsPointDialogBase
8695
QgsMapTool* mToolAddPoint;
8796
QgsMapTool* mToolDeletePoint;
8897

98+
QString mProjBehaviour, mProjectCRS;
99+
int mProjectCrsId;
100+
/**dialog to enter reference point*/
101+
// QgsPointDialog* mPointDialog;
102+
/**Flag if plugin windows have been arranged with button*/
103+
// QSize origSize;
104+
// QPoint origPos;
89105

90106
// std::vector<QgsPoint> mPixelCoords, mMapCoords;
91107
// std::vector<QString> mAcetateIDs;

‎src/plugins/georeferencer/qgspointdialogbase.ui

Lines changed: 354 additions & 264 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.