26#include "moc_qgsgcptransformer.cpp"
51 return QObject::tr(
"Linear" );
53 return QObject::tr(
"Helmert" );
55 return QObject::tr(
"Polynomial 1" );
57 return QObject::tr(
"Polynomial 2" );
59 return QObject::tr(
"Polynomial 3" );
61 return QObject::tr(
"Thin Plate Spline (TPS)" );
63 return QObject::tr(
"Projective" );
65 return QObject::tr(
"Not set" );
94 std::unique_ptr<QgsGcpTransformerInterface> transformer(
create(
method ) );
98 if ( !transformer->updateParametersFromGcps( sourceCoordinates, destinationCoordinates ) )
101 return transformer.release();
111 origin = mParameters.origin;
112 scaleX = mParameters.scaleX;
113 scaleY = mParameters.scaleY;
119 auto res = std::make_unique<QgsLinearGeorefTransform>();
120 res->mParameters = mParameters;
121 return res.release();
129 mParameters.invertYAxis = invertYAxis;
130 QgsLeastSquares::linear( sourceCoordinates, destinationCoordinates, mParameters.origin, mParameters.scaleX, mParameters.scaleY );
141 return QgsLinearGeorefTransform::linearTransform;
146 return (
void * ) &mParameters;
154int QgsLinearGeorefTransform::linearTransform(
void *pTransformerArg,
int bDstToSrc,
int nPointCount,
double *x,
double *y,
double *z,
int *panSuccess )
157 LinearParameters *t =
static_cast<LinearParameters *
>( pTransformerArg );
163 for (
int i = 0; i < nPointCount; ++i )
165 x[i] = x[i] * t->scaleX + t->origin.x();
166 y[i] = ( t->invertYAxis ? -1 : 1 ) * y[i] * t->scaleY + t->origin.y();
167 panSuccess[i] =
true;
173 if ( std::fabs( t->scaleX ) < std::numeric_limits<double>::epsilon() || std::fabs( t->scaleY ) < std::numeric_limits<double>::epsilon() )
175 for (
int i = 0; i < nPointCount; ++i )
177 panSuccess[i] =
false;
181 for (
int i = 0; i < nPointCount; ++i )
183 x[i] = ( x[i] - t->origin.x() ) / t->scaleX;
184 y[i] = ( y[i] - t->origin.y() ) / ( ( t->invertYAxis ? -1 : 1 ) * t->scaleY );
185 panSuccess[i] =
true;
200 mHelmertParameters.invertYAxis = invertYAxis;
201 QgsLeastSquares::helmert( sourceCoordinates, destinationCoordinates, mHelmertParameters.origin, mHelmertParameters.scale, mHelmertParameters.angle );
212 return QgsHelmertGeorefTransform::helmertTransform;
217 return (
void * ) &mHelmertParameters;
227 origin = mHelmertParameters.origin;
228 scale = mHelmertParameters.scale;
229 rotation = mHelmertParameters.angle;
235 auto res = std::make_unique<QgsHelmertGeorefTransform>();
236 res->mHelmertParameters = mHelmertParameters;
237 return res.release();
240int QgsHelmertGeorefTransform::helmertTransform(
void *pTransformerArg,
int bDstToSrc,
int nPointCount,
double *x,
double *y,
double *z,
int *panSuccess )
243 const HelmertParameters *t =
static_cast<const HelmertParameters *
>( pTransformerArg );
247 double a = std::cos( t->angle );
248 double b = std::sin( t->angle );
249 const double x0 = t->origin.x();
250 const double y0 = t->origin.y();
251 const double s = t->scale;
256 for (
int i = 0; i < nPointCount; ++i )
258 const double xT = x[i];
259 const double yT = y[i];
261 if ( t->invertYAxis )
267 x[i] = x0 + ( a * xT + b * yT );
268 y[i] = y0 + ( b * xT - a * yT );
272 x[i] = x0 + ( a * xT - b * yT );
273 y[i] = y0 + ( b * xT + a * yT );
275 panSuccess[i] =
true;
281 if ( std::fabs( s ) < std::numeric_limits<double>::epsilon() )
283 for (
int i = 0; i < nPointCount; ++i )
285 panSuccess[i] =
false;
291 for (
int i = 0; i < nPointCount; ++i )
293 const double xT = x[i] - x0;
294 const double yT = y[i] - y0;
295 if ( t->invertYAxis )
299 x[i] = a * xT + b * yT;
300 y[i] = b * xT - a * yT;
304 x[i] = a * xT + b * yT;
305 y[i] = -b * xT + a * yT;
307 panSuccess[i] =
true;
318 : mPolynomialOrder( std::min( 3u, polynomialOrder ) )
319 , mIsTPSTransform( useTPS )
330 auto res = std::make_unique<QgsGDALGeorefTransform>( mIsTPSTransform, mPolynomialOrder );
331 res->updateParametersFromGcps( mSourceCoords, mDestCoordinates, mInvertYAxis );
332 return res.release();
337 mSourceCoords = sourceCoordinates;
338 mDestCoordinates = destinationCoordinates;
339 mInvertYAxis = invertYAxis;
341 assert( sourceCoordinates.size() == destinationCoordinates.size() );
342 if ( sourceCoordinates.size() != destinationCoordinates.size() )
344 const int n = sourceCoordinates.size();
346 GDAL_GCP *GCPList =
new GDAL_GCP[n];
347 for (
int i = 0; i < n; i++ )
349 GCPList[i].pszId =
new char[20];
350 snprintf( GCPList[i].pszId, 19,
"gcp%i", i );
351 GCPList[i].pszInfo =
nullptr;
352 GCPList[i].dfGCPPixel = sourceCoordinates[i].x();
353 GCPList[i].dfGCPLine = ( mInvertYAxis ? -1 : 1 ) * sourceCoordinates[i].y();
354 GCPList[i].dfGCPX = destinationCoordinates[i].x();
355 GCPList[i].dfGCPY = destinationCoordinates[i].y();
356 GCPList[i].dfGCPZ = 0;
360 if ( mIsTPSTransform )
361 mGDALTransformerArgs = GDALCreateTPSTransformer( n, GCPList,
false );
363 mGDALTransformerArgs = GDALCreateGCPTransformer( n, GCPList, mPolynomialOrder,
false );
365 for (
int i = 0; i < n; i++ )
367 delete[] GCPList[i].pszId;
371 return nullptr != mGDALTransformerArgs;
376 if ( mIsTPSTransform )
379 return ( ( mPolynomialOrder + 2 ) * ( mPolynomialOrder + 1 ) ) / 2;
385 if ( !mGDALTransformerArgs )
388 if ( mIsTPSTransform )
389 return GDALTPSTransform;
391 return GDALGCPTransform;
396 return mGDALTransformerArgs;
401 if ( mIsTPSTransform )
404 switch ( mPolynomialOrder )
416void QgsGDALGeorefTransform::destroyGdalArgs()
418 if ( mGDALTransformerArgs )
420 if ( mIsTPSTransform )
421 GDALDestroyTPSTransformer( mGDALTransformerArgs );
423 GDALDestroyGCPTransformer( mGDALTransformerArgs );
437 auto res = std::make_unique<QgsProjectiveGeorefTransform>();
438 res->mParameters = mParameters;
439 return res.release();
450 QVector<QgsPointXY> flippedPixelCoords;
451 flippedPixelCoords.reserve( sourceCoordinates.size() );
452 for (
const QgsPointXY &coord : sourceCoordinates )
454 flippedPixelCoords <<
QgsPointXY( coord.x(), -coord.y() );
465 double *H = mParameters.H;
468 adjoint[0] = H[4] * H[8] - H[5] * H[7];
469 adjoint[1] = -H[1] * H[8] + H[2] * H[7];
470 adjoint[2] = H[1] * H[5] - H[2] * H[4];
472 adjoint[3] = -H[3] * H[8] + H[5] * H[6];
473 adjoint[4] = H[0] * H[8] - H[2] * H[6];
474 adjoint[5] = -H[0] * H[5] + H[2] * H[3];
476 adjoint[6] = H[3] * H[7] - H[4] * H[6];
477 adjoint[7] = -H[0] * H[7] + H[1] * H[6];
478 adjoint[8] = H[0] * H[4] - H[1] * H[3];
480 const double det = H[0] * adjoint[0] + H[3] * adjoint[1] + H[6] * adjoint[2];
482 if ( std::fabs( det ) < 1024.0 * std::numeric_limits<double>::epsilon() )
484 mParameters.hasInverse =
false;
488 mParameters.hasInverse =
true;
489 const double oo_det = 1.0 / det;
490 for (
int i = 0; i < 9; i++ )
492 mParameters.Hinv[i] = adjoint[i] * oo_det;
505 return QgsProjectiveGeorefTransform::projectiveTransform;
510 return (
void * ) &mParameters;
518int QgsProjectiveGeorefTransform::projectiveTransform(
void *pTransformerArg,
int bDstToSrc,
int nPointCount,
double *x,
double *y,
double *z,
int *panSuccess )
521 ProjectiveParameters *t =
static_cast<ProjectiveParameters *
>( pTransformerArg );
532 if ( !t->hasInverse )
534 for (
int i = 0; i < nPointCount; ++i )
536 panSuccess[i] =
false;
544 for (
int i = 0; i < nPointCount; ++i )
546 const double Z = x[i] * H[6] + y[i] * H[7] + H[8];
548 if ( std::fabs( Z ) < 1024.0 * std::numeric_limits<double>::epsilon() )
550 panSuccess[i] =
false;
553 const double X = ( x[i] * H[0] + y[i] * H[1] + H[2] ) / Z;
554 const double Y = ( x[i] * H[3] + y[i] * H[4] + H[5] ) / Z;
559 panSuccess[i] =
true;
static void helmert(const QVector< QgsPointXY > &sourceCoordinates, const QVector< QgsPointXY > &destinationCoordinates, QgsPointXY &origin, double &pixelSize, double &rotation)
Transforms the point at origin in-place, using a helmert transformation calculated from the list of s...
static void projective(const QVector< QgsPointXY > &sourceCoordinates, const QVector< QgsPointXY > &destinationCoordinates, double H[9])
Calculates projective parameters from the list of source and destination Ground Control Points (GCPs)...
static void linear(const QVector< QgsPointXY > &sourceCoordinates, const QVector< QgsPointXY > &destinationCoordinates, QgsPointXY &origin, double &pixelXSize, double &pixelYSize)
Transforms the point at origin in-place, using a linear transformation calculated from the list of so...