17#include "moc_qgsgcptransformer.cpp"
53 return QObject::tr(
"Linear" );
55 return QObject::tr(
"Helmert" );
57 return QObject::tr(
"Polynomial 1" );
59 return QObject::tr(
"Polynomial 2" );
61 return QObject::tr(
"Polynomial 3" );
63 return QObject::tr(
"Thin Plate Spline (TPS)" );
65 return QObject::tr(
"Projective" );
67 return QObject::tr(
"Not set" );
96 std::unique_ptr< QgsGcpTransformerInterface > transformer(
create(
method ) );
100 if ( !transformer->updateParametersFromGcps( sourceCoordinates, destinationCoordinates ) )
103 return transformer.release();
113 origin = mParameters.origin;
114 scaleX = mParameters.scaleX;
115 scaleY = mParameters.scaleY;
121 std::unique_ptr< QgsLinearGeorefTransform > res = std::make_unique< QgsLinearGeorefTransform >();
122 res->mParameters = mParameters;
123 return res.release();
131 mParameters.invertYAxis = invertYAxis;
132 QgsLeastSquares::linear( sourceCoordinates, destinationCoordinates, mParameters.origin, mParameters.scaleX, mParameters.scaleY );
143 return QgsLinearGeorefTransform::linearTransform;
148 return (
void * )&mParameters;
156int QgsLinearGeorefTransform::linearTransform(
void *pTransformerArg,
int bDstToSrc,
int nPointCount,
157 double *x,
double *y,
double *z,
int *panSuccess )
160 LinearParameters *t =
static_cast<LinearParameters *
>( pTransformerArg );
166 for (
int i = 0; i < nPointCount; ++i )
168 x[i] = x[i] * t->scaleX + t->origin.x();
169 y[i] = ( t->invertYAxis ? -1 : 1 ) * y[i] * t->scaleY + t->origin.y();
170 panSuccess[i] =
true;
176 if ( std::fabs( t->scaleX ) < std::numeric_limits<double>::epsilon() ||
177 std::fabs( t->scaleY ) < std::numeric_limits<double>::epsilon() )
179 for (
int i = 0; i < nPointCount; ++i )
181 panSuccess[i] =
false;
185 for (
int i = 0; i < nPointCount; ++i )
187 x[i] = ( x[i] - t->origin.x() ) / t->scaleX;
188 y[i] = ( y[i] - t->origin.y() ) / ( ( t->invertYAxis ? -1 : 1 ) * t->scaleY );
189 panSuccess[i] =
true;
204 mHelmertParameters.invertYAxis = invertYAxis;
205 QgsLeastSquares::helmert( sourceCoordinates, destinationCoordinates, mHelmertParameters.origin, mHelmertParameters.scale, mHelmertParameters.angle );
216 return QgsHelmertGeorefTransform::helmertTransform;
221 return (
void * )&mHelmertParameters;
231 origin = mHelmertParameters.origin;
232 scale = mHelmertParameters.scale;
233 rotation = mHelmertParameters.angle;
239 std::unique_ptr< QgsHelmertGeorefTransform > res = std::make_unique< QgsHelmertGeorefTransform >();
240 res->mHelmertParameters = mHelmertParameters;
241 return res.release();
244int QgsHelmertGeorefTransform::helmertTransform(
void *pTransformerArg,
int bDstToSrc,
int nPointCount,
245 double *x,
double *y,
double *z,
int *panSuccess )
248 const HelmertParameters *t =
static_cast< const HelmertParameters *
>( pTransformerArg );
252 double a = std::cos( t->angle );
253 double b = std::sin( t->angle );
254 const double x0 = t->origin.x();
255 const double y0 = t->origin.y();
256 const double s = t->scale;
261 for (
int i = 0; i < nPointCount; ++i )
263 const double xT = x[i];
264 const double yT = y[i];
266 if ( t->invertYAxis )
272 x[i] = x0 + ( a * xT + b * yT );
273 y[i] = y0 + ( b * xT - a * yT );
277 x[i] = x0 + ( a * xT - b * yT );
278 y[i] = y0 + ( b * xT + a * yT );
280 panSuccess[i] =
true;
286 if ( std::fabs( s ) < std::numeric_limits<double>::epsilon() )
288 for (
int i = 0; i < nPointCount; ++i )
290 panSuccess[i] =
false;
296 for (
int i = 0; i < nPointCount; ++i )
298 const double xT = x[i] - x0;
299 const double yT = y[i] - y0;
300 if ( t->invertYAxis )
304 x[i] = a * xT + b * yT;
305 y[i] = b * xT - a * yT;
309 x[i] = a * xT + b * yT;
310 y[i] = -b * xT + a * yT;
312 panSuccess[i] =
true;
323 : mPolynomialOrder( std::min( 3u, polynomialOrder ) )
324 , mIsTPSTransform( useTPS )
335 std::unique_ptr< QgsGDALGeorefTransform > res = std::make_unique< QgsGDALGeorefTransform >( mIsTPSTransform, mPolynomialOrder );
336 res->updateParametersFromGcps( mSourceCoords, mDestCoordinates, mInvertYAxis );
337 return res.release();
342 mSourceCoords = sourceCoordinates;
343 mDestCoordinates = destinationCoordinates;
344 mInvertYAxis = invertYAxis;
346 assert( sourceCoordinates.size() == destinationCoordinates.size() );
347 if ( sourceCoordinates.size() != destinationCoordinates.size() )
349 const int n = sourceCoordinates.size();
351 GDAL_GCP *GCPList =
new GDAL_GCP[n];
352 for (
int i = 0; i < n; i++ )
354 GCPList[i].pszId =
new char[20];
355 snprintf( GCPList[i].pszId, 19,
"gcp%i", i );
356 GCPList[i].pszInfo =
nullptr;
357 GCPList[i].dfGCPPixel = sourceCoordinates[i].x();
358 GCPList[i].dfGCPLine = ( mInvertYAxis ? -1 : 1 ) * sourceCoordinates[i].y();
359 GCPList[i].dfGCPX = destinationCoordinates[i].x();
360 GCPList[i].dfGCPY = destinationCoordinates[i].y();
361 GCPList[i].dfGCPZ = 0;
365 if ( mIsTPSTransform )
366 mGDALTransformerArgs = GDALCreateTPSTransformer( n, GCPList,
false );
368 mGDALTransformerArgs = GDALCreateGCPTransformer( n, GCPList, mPolynomialOrder,
false );
370 for (
int i = 0; i < n; i++ )
372 delete [] GCPList[i].pszId;
376 return nullptr != mGDALTransformerArgs;
381 if ( mIsTPSTransform )
384 return ( ( mPolynomialOrder + 2 ) * ( mPolynomialOrder + 1 ) ) / 2;
390 if ( !mGDALTransformerArgs )
393 if ( mIsTPSTransform )
394 return GDALTPSTransform;
396 return GDALGCPTransform;
401 return mGDALTransformerArgs;
406 if ( mIsTPSTransform )
409 switch ( mPolynomialOrder )
421void QgsGDALGeorefTransform::destroyGdalArgs()
423 if ( mGDALTransformerArgs )
425 if ( mIsTPSTransform )
426 GDALDestroyTPSTransformer( mGDALTransformerArgs );
428 GDALDestroyGCPTransformer( mGDALTransformerArgs );
442 std::unique_ptr< QgsProjectiveGeorefTransform > res = std::make_unique< QgsProjectiveGeorefTransform >();
443 res->mParameters = mParameters;
444 return res.release();
455 QVector<QgsPointXY> flippedPixelCoords;
456 flippedPixelCoords.reserve( sourceCoordinates.size() );
457 for (
const QgsPointXY &coord : sourceCoordinates )
459 flippedPixelCoords <<
QgsPointXY( coord.x(), -coord.y() );
470 double *H = mParameters.H;
473 adjoint[0] = H[4] * H[8] - H[5] * H[7];
474 adjoint[1] = -H[1] * H[8] + H[2] * H[7];
475 adjoint[2] = H[1] * H[5] - H[2] * H[4];
477 adjoint[3] = -H[3] * H[8] + H[5] * H[6];
478 adjoint[4] = H[0] * H[8] - H[2] * H[6];
479 adjoint[5] = -H[0] * H[5] + H[2] * H[3];
481 adjoint[6] = H[3] * H[7] - H[4] * H[6];
482 adjoint[7] = -H[0] * H[7] + H[1] * H[6];
483 adjoint[8] = H[0] * H[4] - H[1] * H[3];
485 const double det = H[0] * adjoint[0] + H[3] * adjoint[1] + H[6] * adjoint[2];
487 if ( std::fabs( det ) < 1024.0 * std::numeric_limits<double>::epsilon() )
489 mParameters.hasInverse =
false;
493 mParameters.hasInverse =
true;
494 const double oo_det = 1.0 / det;
495 for (
int i = 0; i < 9; i++ )
497 mParameters.Hinv[i] = adjoint[i] * oo_det;
510 return QgsProjectiveGeorefTransform::projectiveTransform;
515 return (
void * )&mParameters;
523int QgsProjectiveGeorefTransform::projectiveTransform(
void *pTransformerArg,
int bDstToSrc,
int nPointCount,
524 double *x,
double *y,
double *z,
int *panSuccess )
527 ProjectiveParameters *t =
static_cast<ProjectiveParameters *
>( pTransformerArg );
538 if ( !t->hasInverse )
540 for (
int i = 0; i < nPointCount; ++i )
542 panSuccess[i] =
false;
550 for (
int i = 0; i < nPointCount; ++i )
552 const double Z = x[i] * H[6] + y[i] * H[7] + H[8];
554 if ( std::fabs( Z ) < 1024.0 * std::numeric_limits<double>::epsilon() )
556 panSuccess[i] =
false;
559 const double X = ( x[i] * H[0] + y[i] * H[1] + H[2] ) / Z;
560 const double Y = ( x[i] * H[3] + y[i] * H[4] + H[5] ) / Z;
565 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...
A class to represent a 2D point.