Skip to content

Commit

Permalink
Add public transform method
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 21, 2021
1 parent 30245ee commit ef78bf9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
Expand Up @@ -65,6 +65,15 @@ Returns the minimum number of Ground Control Points (GCPs) required for paramete
virtual TransformMethod method() const = 0;
%Docstring
Returns the transformation method.
%End

bool transform( double &x /In,Out/, double &y /In,Out/, bool inverseTransform = false ) const;
%Docstring
Transforms the point (``x``, ``y``) from source to destination coordinates.

If ``inverseTransform`` is set to ``True``, the point will be transformed from the destination to the source.

:return: ``True`` if transformation was successful.
%End

static QString methodToString( TransformMethod method );
Expand Down
18 changes: 18 additions & 0 deletions src/analysis/georeferencing/qgsgcptransformer.cpp
Expand Up @@ -26,6 +26,24 @@
#include <limits>


bool QgsGcpTransformerInterface::transform( double &x, double &y, bool inverseTransform ) const
{
GDALTransformerFunc t = GDALTransformer();
// Fail if no transformer function was returned
if ( !t )
return false;

double z = 0.0;
int success = 0;

// Call GDAL transform function
( *t )( GDALTransformerArgs(), inverseTransform ? 1 : 0, 1, &x, &y, &z, &success );
if ( !success )
return false;

return true;
}

QString QgsGcpTransformerInterface::methodToString( QgsGcpTransformerInterface::TransformMethod method )
{
switch ( method )
Expand Down
9 changes: 9 additions & 0 deletions src/analysis/georeferencing/qgsgcptransformer.h
Expand Up @@ -84,6 +84,15 @@ class ANALYSIS_EXPORT QgsGcpTransformerInterface SIP_ABSTRACT
*/
virtual TransformMethod method() const = 0;

/**
* Transforms the point (\a x, \a y) from source to destination coordinates.
*
* If \a inverseTransform is set to TRUE, the point will be transformed from the destination to the source.
*
* \returns TRUE if transformation was successful.
*/
bool transform( double &x SIP_INOUT, double &y SIP_INOUT, bool inverseTransform = false ) const;

/**
* Returns a translated string representing the specified transform \a method.
*/
Expand Down
11 changes: 1 addition & 10 deletions src/app/georeferencer/qgsgeoreftransform.cpp
Expand Up @@ -188,20 +188,11 @@ bool QgsGeorefTransform::getOriginScaleRotation( QgsPointXY &origin, double &sca

bool QgsGeorefTransform::gdal_transform( const QgsPointXY &src, QgsPointXY &dst, int dstToSrc ) const
{
GDALTransformerFunc t = GDALTransformer();
// Fail if no transformer function was returned
if ( !t )
return false;

// Copy the source coordinate for inplace transform
double x = src.x();
double y = src.y();
double z = 0.0;
int success = 0;

// Call GDAL transform function
( *t )( GDALTransformerArgs(), dstToSrc, 1, &x, &y, &z, &success );
if ( !success )
if ( !QgsGcpTransformerInterface::transform( x, y, dstToSrc == 1 ) )
return false;

dst.setX( x );
Expand Down

0 comments on commit ef78bf9

Please sign in to comment.