Skip to content

Commit

Permalink
Move factory method to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 21, 2021
1 parent 6628d5d commit 125c33b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
Expand Up @@ -64,6 +64,13 @@ Returns the transformation method.
Returns a translated string representing the specified transform ``method``.
%End

static QgsGcpTransformerInterface *create( TransformMethod method ) /Factory/;
%Docstring
Creates a new QgsGcpTransformerInterface subclass representing the specified transform ``method``.

Caller takes ownership of the returned object.
%End


private:
QgsGcpTransformerInterface( const QgsGcpTransformerInterface &other );
Expand Down
23 changes: 23 additions & 0 deletions src/analysis/georeferencing/qgsgcptransformer.cpp
Expand Up @@ -49,6 +49,29 @@ QString QgsGcpTransformerInterface::methodToString( QgsGcpTransformerInterface::
}
}

QgsGcpTransformerInterface *QgsGcpTransformerInterface::create( QgsGcpTransformerInterface::TransformMethod method )
{
switch ( method )
{
case TransformMethod::Linear:
return new QgsLinearGeorefTransform;
case TransformMethod::Helmert:
return new QgsHelmertGeorefTransform;
case TransformMethod::PolynomialOrder1:
return new QgsGDALGeorefTransform( false, 1 );
case TransformMethod::PolynomialOrder2:
return new QgsGDALGeorefTransform( false, 2 );
case TransformMethod::PolynomialOrder3:
return new QgsGDALGeorefTransform( false, 3 );
case TransformMethod::ThinPlateSpline:
return new QgsGDALGeorefTransform( true, 0 );
case TransformMethod::Projective:
return new QgsProjectiveGeorefTransform;
default:
return nullptr;
}
}


//
// QgsLinearGeorefTransform
Expand Down
7 changes: 7 additions & 0 deletions src/analysis/georeferencing/qgsgcptransformer.h
Expand Up @@ -81,6 +81,13 @@ class ANALYSIS_EXPORT QgsGcpTransformerInterface SIP_ABSTRACT
*/
static QString methodToString( TransformMethod method );

/**
* Creates a new QgsGcpTransformerInterface subclass representing the specified transform \a method.
*
* Caller takes ownership of the returned object.
*/
static QgsGcpTransformerInterface *create( TransformMethod method ) SIP_FACTORY;

#ifndef SIP_RUN

/**
Expand Down
48 changes: 6 additions & 42 deletions src/app/georeferencer/qgsgeoreftransform.cpp
Expand Up @@ -26,29 +26,17 @@

QgsGeorefTransform::QgsGeorefTransform( const QgsGeorefTransform &other )
{
mTransformParametrisation = TransformMethod::InvalidTransform;
mGeorefTransformImplementation = nullptr;
selectTransformParametrisation( other.mTransformParametrisation );
}

QgsGeorefTransform::QgsGeorefTransform( TransformMethod parametrisation )
{
mTransformParametrisation = TransformMethod::InvalidTransform;
mGeorefTransformImplementation = nullptr;
selectTransformParametrisation( parametrisation );
}

QgsGeorefTransform::QgsGeorefTransform()
{
mTransformParametrisation = TransformMethod::InvalidTransform;
mGeorefTransformImplementation = nullptr;
mParametersInitialized = false;
}
QgsGeorefTransform::QgsGeorefTransform() = default;

QgsGeorefTransform::~QgsGeorefTransform()
{
delete mGeorefTransformImplementation;
}
QgsGeorefTransform::~QgsGeorefTransform() = default;

QgsGeorefTransform::TransformMethod QgsGeorefTransform::transformParametrisation() const
{
Expand All @@ -59,8 +47,7 @@ void QgsGeorefTransform::selectTransformParametrisation( TransformMethod paramet
{
if ( parametrisation != mTransformParametrisation )
{
delete mGeorefTransformImplementation;
mGeorefTransformImplementation = QgsGeorefTransform::createImplementation( parametrisation );
mGeorefTransformImplementation.reset( QgsGcpTransformerInterface::create( parametrisation ) );
mParametersInitialized = false;
mTransformParametrisation = parametrisation;
}
Expand Down Expand Up @@ -130,29 +117,6 @@ void *QgsGeorefTransform::GDALTransformerArgs() const
return mGeorefTransformImplementation ? mGeorefTransformImplementation->GDALTransformerArgs() : nullptr;
}

QgsGcpTransformerInterface *QgsGeorefTransform::createImplementation( TransformMethod parametrisation )
{
switch ( parametrisation )
{
case TransformMethod::Linear:
return new QgsLinearGeorefTransform;
case TransformMethod::Helmert:
return new QgsHelmertGeorefTransform;
case TransformMethod::PolynomialOrder1:
return new QgsGDALGeorefTransform( false, 1 );
case TransformMethod::PolynomialOrder2:
return new QgsGDALGeorefTransform( false, 2 );
case TransformMethod::PolynomialOrder3:
return new QgsGDALGeorefTransform( false, 3 );
case TransformMethod::ThinPlateSpline:
return new QgsGDALGeorefTransform( true, 0 );
case TransformMethod::Projective:
return new QgsProjectiveGeorefTransform;
default:
return nullptr;
}
}

bool QgsGeorefTransform::transformRasterToWorld( const QgsPointXY &raster, QgsPointXY &world )
{
// flip y coordinate due to different CS orientation
Expand Down Expand Up @@ -183,7 +147,7 @@ bool QgsGeorefTransform::getLinearOriginScale( QgsPointXY &origin, double &scale
{
return false;
}
QgsLinearGeorefTransform *transform = dynamic_cast<QgsLinearGeorefTransform *>( mGeorefTransformImplementation );
QgsLinearGeorefTransform *transform = dynamic_cast<QgsLinearGeorefTransform *>( mGeorefTransformImplementation.get() );
return transform && transform->getOriginScale( origin, scaleX, scaleY );
}

Expand All @@ -193,13 +157,13 @@ bool QgsGeorefTransform::getOriginScaleRotation( QgsPointXY &origin, double &sca
if ( mTransformParametrisation == TransformMethod::Linear )
{
rotation = 0.0;
QgsLinearGeorefTransform *transform = dynamic_cast<QgsLinearGeorefTransform *>( mGeorefTransformImplementation );
QgsLinearGeorefTransform *transform = dynamic_cast<QgsLinearGeorefTransform *>( mGeorefTransformImplementation.get() );
return transform && transform->getOriginScale( origin, scaleX, scaleY );
}
else if ( mTransformParametrisation == TransformMethod::Helmert )
{
double scale;
QgsHelmertGeorefTransform *transform = dynamic_cast<QgsHelmertGeorefTransform *>( mGeorefTransformImplementation );
QgsHelmertGeorefTransform *transform = dynamic_cast<QgsHelmertGeorefTransform *>( mGeorefTransformImplementation.get() );
if ( !transform || ! transform->getOriginScaleRotation( origin, scale, rotation ) )
{
return false;
Expand Down
12 changes: 5 additions & 7 deletions src/app/georeferencer/qgsgeoreftransform.h
Expand Up @@ -108,16 +108,14 @@ class QgsGeorefTransform : public QgsGcpTransformerInterface
QgsGeorefTransform( const QgsGeorefTransform &other );
QgsGeorefTransform &operator= ( const QgsGeorefTransform & ) = delete;

//! Factory function which creates an implementation for the given parametrisation.
static QgsGcpTransformerInterface *createImplementation( TransformMethod parametrisation );

// convenience wrapper around GDALTransformerFunc
bool gdal_transform( const QgsPointXY &src, QgsPointXY &dst, int dstToSrc ) const;

QgsGcpTransformerInterface *mGeorefTransformImplementation = nullptr;
TransformMethod mTransformParametrisation;
bool mParametersInitialized;
QgsRasterChangeCoords mRasterChangeCoords;
std::unique_ptr< QgsGcpTransformerInterface > mGeorefTransformImplementation;

TransformMethod mTransformParametrisation = TransformMethod::InvalidTransform;
bool mParametersInitialized = false;
QgsRasterChangeCoords mRasterChangeCoords;
};

#endif //QGSGEOREFTRANSFORM_H

0 comments on commit 125c33b

Please sign in to comment.