Add clone method

This commit is contained in:
Nyall Dawson 2021-02-07 11:46:25 +10:00
parent ff195b373d
commit 30245eef4d
5 changed files with 71 additions and 5 deletions

View File

@ -42,6 +42,14 @@ based on a transformation method and a list of GCPs.
virtual QgsGcpTransformerInterface *clone() const = 0 /Factory/;
%Docstring
Clones the transformer, returning a new copy of the transformer with the same
parameters as this one.
Caller takes ownership of the returned object.
%End
virtual bool updateParametersFromGcps( const QVector<QgsPointXY> &mapCoordinates, const QVector<QgsPointXY> &layerCoordinates ) = 0;
%Docstring
Fits transformation parameters using the specified Ground Control Points (GCPs) lists of map coordinates and layer coordinates.

View File

@ -97,6 +97,13 @@ bool QgsLinearGeorefTransform::getOriginScale( QgsPointXY &origin, double &scale
return true;
}
QgsGcpTransformerInterface *QgsLinearGeorefTransform::clone() const
{
std::unique_ptr< QgsLinearGeorefTransform > res = qgis::make_unique< QgsLinearGeorefTransform >();
res->mParameters = mParameters;
return res.release();
}
bool QgsLinearGeorefTransform::updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &layerCoords )
{
if ( mapCoords.size() < minimumGcpCount() )
@ -206,6 +213,13 @@ bool QgsHelmertGeorefTransform::getOriginScaleRotation( QgsPointXY &origin, doub
return true;
}
QgsGcpTransformerInterface *QgsHelmertGeorefTransform::clone() const
{
std::unique_ptr< QgsHelmertGeorefTransform > res = qgis::make_unique< QgsHelmertGeorefTransform >();
res->mHelmertParameters = mHelmertParameters;
return res.release();
}
int QgsHelmertGeorefTransform::helmert_transform( void *pTransformerArg, int bDstToSrc, int nPointCount,
double *x, double *y, double *z, int *panSuccess )
{
@ -267,8 +281,6 @@ QgsGDALGeorefTransform::QgsGDALGeorefTransform( bool useTPS, unsigned int polyno
: mPolynomialOrder( std::min( 3u, polynomialOrder ) )
, mIsTPSTransform( useTPS )
{
mGDALTransformer = nullptr;
mGDALTransformerArgs = nullptr;
}
QgsGDALGeorefTransform::~QgsGDALGeorefTransform()
@ -276,8 +288,18 @@ QgsGDALGeorefTransform::~QgsGDALGeorefTransform()
destroyGdalArgs();
}
QgsGcpTransformerInterface *QgsGDALGeorefTransform::clone() const
{
std::unique_ptr< QgsGDALGeorefTransform > res = qgis::make_unique< QgsGDALGeorefTransform >( mIsTPSTransform, mPolynomialOrder );
res->updateParametersFromGcps( mMapCoords, mLayerCoords );
return res.release();
}
bool QgsGDALGeorefTransform::updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &layerCoords )
{
mMapCoords = mapCoords;
mLayerCoords = layerCoords;
assert( mapCoords.size() == layerCoords.size() );
if ( mapCoords.size() != layerCoords.size() )
return false;
@ -372,6 +394,13 @@ QgsProjectiveGeorefTransform::QgsProjectiveGeorefTransform()
: mParameters()
{}
QgsGcpTransformerInterface *QgsProjectiveGeorefTransform::clone() const
{
std::unique_ptr< QgsProjectiveGeorefTransform > res = qgis::make_unique< QgsProjectiveGeorefTransform >();
res->mParameters = mParameters;
return res.release();
}
bool QgsProjectiveGeorefTransform::updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &layerCoords )
{
if ( mapCoords.size() < minimumGcpCount() )

View File

@ -53,12 +53,20 @@ class ANALYSIS_EXPORT QgsGcpTransformerInterface SIP_ABSTRACT
virtual ~QgsGcpTransformerInterface() = default;
//! QgsGcpTransformerInterface cannot be copied
//! QgsGcpTransformerInterface cannot be copied - use clone() instead.
QgsGcpTransformerInterface( const QgsGcpTransformerInterface &other ) = delete;
//! QgsGcpTransformerInterface cannot be copied
//! QgsGcpTransformerInterface cannot be copied - use clone() instead.
QgsGcpTransformerInterface &operator=( const QgsGcpTransformerInterface &other ) = delete;
/**
* Clones the transformer, returning a new copy of the transformer with the same
* parameters as this one.
*
* Caller takes ownership of the returned object.
*/
virtual QgsGcpTransformerInterface *clone() const = 0 SIP_FACTORY;
/**
* Fits transformation parameters using the specified Ground Control Points (GCPs) lists of map coordinates and layer coordinates.
*
@ -134,6 +142,7 @@ class ANALYSIS_EXPORT QgsLinearGeorefTransform : public QgsGcpTransformerInterfa
*/
bool getOriginScale( QgsPointXY &origin, double &scaleX, double &scaleY ) const;
QgsGcpTransformerInterface *clone() const override;
bool updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &layerCoords ) override;
int minimumGcpCount() const override;
GDALTransformerFunc GDALTransformer() const override;
@ -168,6 +177,7 @@ class ANALYSIS_EXPORT QgsHelmertGeorefTransform : public QgsGcpTransformerInterf
*/
bool getOriginScaleRotation( QgsPointXY &origin, double &scale, double &rotation ) const;
QgsGcpTransformerInterface *clone() const override;
bool updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &layerCoords ) override;
int minimumGcpCount() const override;
GDALTransformerFunc GDALTransformer() const override;
@ -201,6 +211,7 @@ class ANALYSIS_EXPORT QgsGDALGeorefTransform : public QgsGcpTransformerInterface
QgsGDALGeorefTransform( bool useTPS, unsigned int polynomialOrder );
~QgsGDALGeorefTransform() override;
QgsGcpTransformerInterface *clone() const override;
bool updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &layerCoords ) override;
int minimumGcpCount() const override;
GDALTransformerFunc GDALTransformer() const override;
@ -210,10 +221,13 @@ class ANALYSIS_EXPORT QgsGDALGeorefTransform : public QgsGcpTransformerInterface
private:
void destroyGdalArgs();
QVector<QgsPointXY> mMapCoords;
QVector<QgsPointXY> mLayerCoords;
const int mPolynomialOrder;
const bool mIsTPSTransform;
GDALTransformerFunc mGDALTransformer;
GDALTransformerFunc mGDALTransformer = nullptr;
void *mGDALTransformerArgs = nullptr;
};
@ -232,6 +246,7 @@ class ANALYSIS_EXPORT QgsProjectiveGeorefTransform : public QgsGcpTransformerInt
public:
QgsProjectiveGeorefTransform();
QgsGcpTransformerInterface *clone() const override;
bool updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &layerCoords ) override;
int minimumGcpCount() const override;
GDALTransformerFunc GDALTransformer() const override;

View File

@ -70,8 +70,18 @@ bool QgsGeorefTransform::parametersInitialized() const
return mParametersInitialized;
}
QgsGcpTransformerInterface *QgsGeorefTransform::clone() const
{
std::unique_ptr< QgsGeorefTransform > res( new QgsGeorefTransform( *this ) );
res->updateParametersFromGcps( mMapCoords, mLayerCoords );
return res.release();
}
bool QgsGeorefTransform::updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &pixelCoords )
{
mMapCoords = mapCoords;
mLayerCoords = pixelCoords;
if ( !mGeorefTransformImplementation )
{
return false;

View File

@ -69,6 +69,7 @@ class QgsGeorefTransform : public QgsGcpTransformerInterface
//! \returns whether the parameters of this transform have been initialized by \ref updateParametersFromGCPs
bool parametersInitialized() const;
QgsGcpTransformerInterface *clone() const override;
bool updateParametersFromGcps( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &pixelCoords ) override;
int minimumGcpCount() const override;
TransformMethod method() const override;
@ -111,6 +112,9 @@ class QgsGeorefTransform : public QgsGcpTransformerInterface
// convenience wrapper around GDALTransformerFunc
bool gdal_transform( const QgsPointXY &src, QgsPointXY &dst, int dstToSrc ) const;
QVector<QgsPointXY> mMapCoords;
QVector<QgsPointXY> mLayerCoords;
std::unique_ptr< QgsGcpTransformerInterface > mGeorefTransformImplementation;
TransformMethod mTransformParametrisation = TransformMethod::InvalidTransform;