Skip to content
Permalink
Browse files

Move factory method to base class

  • Loading branch information
nyalldawson committed Feb 21, 2021
1 parent 6628d5d commit 125c33bf5c669d07be8afe463704e0eff5744aa8
@@ -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 );
@@ -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
@@ -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

/**
@@ -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
{
@@ -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;
}
@@ -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
@@ -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 );
}

@@ -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;
@@ -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.