Skip to content

Commit fe622dd

Browse files
committedDec 20, 2019
[API][processing] New parameter type for coordinate operations
Allows selection of the proj coordinate operation to use when reprojecting between two CRSes
1 parent c04d4fa commit fe622dd

File tree

6 files changed

+535
-0
lines changed

6 files changed

+535
-0
lines changed
 

‎python/core/auto_generated/processing/qgsprocessingparameters.sip.in

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ their acceptable ranges, defaults, etc.
191191
sipType = sipType_QgsProcessingParameterLayoutItem;
192192
else if ( sipCpp->type() == QgsProcessingParameterColor::typeName() )
193193
sipType = sipType_QgsProcessingParameterColor;
194+
else if ( sipCpp->type() == QgsProcessingParameterCoordinateOperation::typeName() )
195+
sipType = sipType_QgsProcessingParameterCoordinateOperation;
194196
else
195197
sipType = nullptr;
196198
%End
@@ -3210,6 +3212,128 @@ Creates a new parameter using the definition from a script code.
32103212
};
32113213

32123214

3215+
class QgsProcessingParameterCoordinateOperation : QgsProcessingParameterDefinition
3216+
{
3217+
%Docstring
3218+
A coordinate operation parameter for processing algorithms, for selection between available
3219+
coordinate operations to use when projecting between a source and destination coordinate reference system.
3220+
3221+
.. versionadded:: 3.12
3222+
%End
3223+
3224+
%TypeHeaderCode
3225+
#include "qgsprocessingparameters.h"
3226+
%End
3227+
public:
3228+
3229+
QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
3230+
const QString &sourceCrsParameterName = QString(), const QString &destinationCrsParameterName = QString(),
3231+
const QVariant &staticSourceCrs = QVariant(), const QVariant &staticDestinationCrs = QVariant(),
3232+
bool optional = false );
3233+
%Docstring
3234+
Constructor for QgsProcessingParameterCoordinateOperation.
3235+
%End
3236+
3237+
static QString typeName();
3238+
%Docstring
3239+
Returns the type name for the parameter class.
3240+
%End
3241+
virtual QgsProcessingParameterDefinition *clone() const /Factory/;
3242+
3243+
virtual QString type() const;
3244+
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
3245+
3246+
virtual QString asScriptCode() const;
3247+
3248+
virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;
3249+
3250+
3251+
virtual QVariantMap toVariantMap() const;
3252+
3253+
virtual bool fromVariantMap( const QVariantMap &map );
3254+
3255+
3256+
static QgsProcessingParameterCoordinateOperation *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
3257+
%Docstring
3258+
Creates a new parameter using the definition from a script code.
3259+
%End
3260+
3261+
QString sourceCrsParameterName() const;
3262+
%Docstring
3263+
Returns the name of the source CRS parameter, or an empty string if this is not set.
3264+
3265+
.. seealso:: :py:func:`setSourceCrsParameterName`
3266+
3267+
.. seealso:: :py:func:`destinationCrsParameterName`
3268+
%End
3269+
3270+
void setSourceCrsParameterName( const QString &name );
3271+
%Docstring
3272+
Sets the ``name`` of the source CRS parameter. Use an empty string if this is not required.
3273+
3274+
.. seealso:: :py:func:`sourceCrsParameterName`
3275+
3276+
.. seealso:: :py:func:`setDestinationCrsParameterName`
3277+
%End
3278+
3279+
QString destinationCrsParameterName() const;
3280+
%Docstring
3281+
Returns the name of the destination CRS parameter, or an empty string if this is not set.
3282+
3283+
.. seealso:: :py:func:`setDestinationCrsParameterName`
3284+
3285+
.. seealso:: :py:func:`sourceCrsParameterName`
3286+
%End
3287+
3288+
void setDestinationCrsParameterName( const QString &name );
3289+
%Docstring
3290+
Sets the ``name`` of the destination CRS parameter. Use an empty string if this is not required.
3291+
3292+
.. seealso:: :py:func:`destinationCrsParameterName`
3293+
3294+
.. seealso:: :py:func:`setSourceCrsParameterName`
3295+
%End
3296+
3297+
QVariant sourceCrs() const;
3298+
%Docstring
3299+
Returns the static source CRS, or an invalid value if this is not set.
3300+
3301+
.. seealso:: :py:func:`setSourceCrs`
3302+
3303+
.. seealso:: :py:func:`destinationCrs`
3304+
%End
3305+
3306+
void setSourceCrs( const QVariant &crs );
3307+
%Docstring
3308+
Sets the static source ``crs``.
3309+
3310+
.. seealso:: :py:func:`sourceCrs`
3311+
3312+
.. seealso:: :py:func:`setDestinationCrs`
3313+
%End
3314+
3315+
QVariant destinationCrs() const;
3316+
%Docstring
3317+
Returns the static destination CRS, or an invalid value if this is not set.
3318+
3319+
.. seealso:: :py:func:`setDestinationCrs`
3320+
3321+
.. seealso:: :py:func:`sourceCrs`
3322+
%End
3323+
3324+
void setDestinationCrs( const QVariant &crs );
3325+
%Docstring
3326+
Sets the static destination ``crs``.
3327+
3328+
.. seealso:: :py:func:`destinationCrs`
3329+
3330+
.. seealso:: :py:func:`setSourceCrs`
3331+
%End
3332+
3333+
};
3334+
3335+
3336+
32133337

32143338

32153339
/************************************************************************

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
17471747
def.reset( new QgsProcessingParameterLayoutItem( name ) );
17481748
else if ( type == QgsProcessingParameterColor::typeName() )
17491749
def.reset( new QgsProcessingParameterColor( name ) );
1750+
else if ( type == QgsProcessingParameterCoordinateOperation::typeName() )
1751+
def.reset( new QgsProcessingParameterCoordinateOperation( name ) );
17501752
else
17511753
{
17521754
QgsProcessingParameterType *paramType = QgsApplication::instance()->processingRegistry()->parameterType( type );
@@ -1841,6 +1843,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
18411843
return QgsProcessingParameterLayoutItem::fromScriptCode( name, description, isOptional, definition );
18421844
else if ( type == QStringLiteral( "color" ) )
18431845
return QgsProcessingParameterColor::fromScriptCode( name, description, isOptional, definition );
1846+
else if ( type == QStringLiteral( "coordinateoperation" ) )
1847+
return QgsProcessingParameterCoordinateOperation::fromScriptCode( name, description, isOptional, definition );
18441848

18451849
return nullptr;
18461850
}
@@ -5923,3 +5927,121 @@ QgsProcessingParameterColor *QgsProcessingParameterColor::fromScriptCode( const
59235927

59245928
return new QgsProcessingParameterColor( name, description, defaultValue, allowOpacity, isOptional );
59255929
}
5930+
5931+
//
5932+
// QgsProcessingParameterCoordinateOperation
5933+
//
5934+
QgsProcessingParameterCoordinateOperation::QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description, const QVariant &defaultValue, const QString &sourceCrsParameterName, const QString &destinationCrsParameterName, const QVariant &staticSourceCrs, const QVariant &staticDestinationCrs, bool optional )
5935+
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5936+
, mSourceParameterName( sourceCrsParameterName )
5937+
, mDestParameterName( destinationCrsParameterName )
5938+
, mSourceCrs( staticSourceCrs )
5939+
, mDestCrs( staticDestinationCrs )
5940+
{
5941+
5942+
}
5943+
5944+
QgsProcessingParameterDefinition *QgsProcessingParameterCoordinateOperation::clone() const
5945+
{
5946+
return new QgsProcessingParameterCoordinateOperation( * this );
5947+
}
5948+
5949+
QString QgsProcessingParameterCoordinateOperation::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
5950+
{
5951+
if ( !value.isValid() || value.isNull() )
5952+
return QStringLiteral( "None" );
5953+
5954+
if ( value.canConvert<QgsCoordinateReferenceSystem>() )
5955+
{
5956+
if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
5957+
return QStringLiteral( "QgsCoordinateReferenceSystem()" );
5958+
else
5959+
return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
5960+
}
5961+
5962+
if ( value.canConvert<QgsProperty>() )
5963+
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5964+
5965+
QVariantMap p;
5966+
p.insert( name(), value );
5967+
QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
5968+
if ( layer )
5969+
return QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) );
5970+
5971+
QString s = value.toString();
5972+
return QgsProcessingUtils::stringToPythonLiteral( s );
5973+
}
5974+
5975+
QString QgsProcessingParameterCoordinateOperation::asScriptCode() const
5976+
{
5977+
QString code = QStringLiteral( "##%1=" ).arg( mName );
5978+
if ( mFlags & FlagOptional )
5979+
code += QStringLiteral( "optional " );
5980+
code += QStringLiteral( "coordinateoperation " );
5981+
5982+
code += mDefault.toString();
5983+
return code.trimmed();
5984+
}
5985+
5986+
QString QgsProcessingParameterCoordinateOperation::asPythonString( QgsProcessing::PythonOutputType outputType ) const
5987+
{
5988+
switch ( outputType )
5989+
{
5990+
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
5991+
{
5992+
QgsProcessingContext c;
5993+
QString code = QStringLiteral( "QgsProcessingParameterCoordinateOperation('%1', '%2'" ).arg( name(), description() );
5994+
if ( mFlags & FlagOptional )
5995+
code += QStringLiteral( ", optional=True" );
5996+
if ( !mSourceParameterName.isEmpty() )
5997+
code += QStringLiteral( ", sourceCrsParameterName=%1" ).arg( valueAsPythonString( mSourceParameterName, c ) );
5998+
if ( !mDestParameterName.isEmpty() )
5999+
code += QStringLiteral( ", destinationCrsParameterName=%1" ).arg( valueAsPythonString( mDestParameterName, c ) );
6000+
6001+
if ( mSourceCrs.isValid() )
6002+
code += QStringLiteral( ", staticSourceCrs=%1" ).arg( valueAsPythonString( mSourceCrs, c ) );
6003+
if ( mDestCrs.isValid() )
6004+
code += QStringLiteral( ", staticDestinationCrs=%1" ).arg( valueAsPythonString( mDestCrs, c ) );
6005+
6006+
code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6007+
return code;
6008+
}
6009+
}
6010+
return QString();
6011+
}
6012+
6013+
QVariantMap QgsProcessingParameterCoordinateOperation::toVariantMap() const
6014+
{
6015+
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
6016+
map.insert( QStringLiteral( "source_crs_parameter_name" ), mSourceParameterName );
6017+
map.insert( QStringLiteral( "dest_crs_parameter_name" ), mDestParameterName );
6018+
map.insert( QStringLiteral( "static_source_crs" ), mSourceCrs );
6019+
map.insert( QStringLiteral( "static_dest_crs" ), mDestCrs );
6020+
return map;
6021+
}
6022+
6023+
bool QgsProcessingParameterCoordinateOperation::fromVariantMap( const QVariantMap &map )
6024+
{
6025+
QgsProcessingParameterDefinition::fromVariantMap( map );
6026+
mSourceParameterName = map.value( QStringLiteral( "source_crs_parameter_name" ) ).toString();
6027+
mDestParameterName = map.value( QStringLiteral( "dest_crs_parameter_name" ) ).toString();
6028+
mSourceCrs = map.value( QStringLiteral( "static_source_crs" ) );
6029+
mDestCrs = map.value( QStringLiteral( "static_dest_crs" ) );
6030+
return true;
6031+
}
6032+
6033+
QgsProcessingParameterCoordinateOperation *QgsProcessingParameterCoordinateOperation::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6034+
{
6035+
QString def = definition;
6036+
6037+
if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
6038+
def = def.mid( 1 );
6039+
if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
6040+
def.chop( 1 );
6041+
6042+
QVariant defaultValue = def;
6043+
if ( def == QStringLiteral( "None" ) )
6044+
defaultValue = QVariant();
6045+
6046+
return new QgsProcessingParameterCoordinateOperation( name, description, defaultValue, QString(), QString(), QVariant(), QVariant(), isOptional );
6047+
}

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
268268
sipType = sipType_QgsProcessingParameterLayoutItem;
269269
else if ( sipCpp->type() == QgsProcessingParameterColor::typeName() )
270270
sipType = sipType_QgsProcessingParameterColor;
271+
else if ( sipCpp->type() == QgsProcessingParameterCoordinateOperation::typeName() )
272+
sipType = sipType_QgsProcessingParameterCoordinateOperation;
271273
else
272274
sipType = nullptr;
273275
SIP_END
@@ -3001,6 +3003,110 @@ class CORE_EXPORT QgsProcessingParameterColor : public QgsProcessingParameterDef
30013003

30023004
};
30033005

3006+
3007+
/**
3008+
* \class QgsProcessingParameterCoordinateOperation
3009+
* \ingroup core
3010+
* A coordinate operation parameter for processing algorithms, for selection between available
3011+
* coordinate operations to use when projecting between a source and destination coordinate reference system.
3012+
* \since QGIS 3.12
3013+
*/
3014+
class CORE_EXPORT QgsProcessingParameterCoordinateOperation : public QgsProcessingParameterDefinition
3015+
{
3016+
public:
3017+
3018+
/**
3019+
* Constructor for QgsProcessingParameterCoordinateOperation.
3020+
*/
3021+
QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
3022+
const QString &sourceCrsParameterName = QString(), const QString &destinationCrsParameterName = QString(),
3023+
const QVariant &staticSourceCrs = QVariant(), const QVariant &staticDestinationCrs = QVariant(),
3024+
bool optional = false );
3025+
3026+
/**
3027+
* Returns the type name for the parameter class.
3028+
*/
3029+
static QString typeName() { return QStringLiteral( "coordinateoperation" ); }
3030+
QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
3031+
QString type() const override { return typeName(); }
3032+
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
3033+
QString asScriptCode() const override;
3034+
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
3035+
3036+
QVariantMap toVariantMap() const override;
3037+
bool fromVariantMap( const QVariantMap &map ) override;
3038+
3039+
/**
3040+
* Creates a new parameter using the definition from a script code.
3041+
*/
3042+
static QgsProcessingParameterCoordinateOperation *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;
3043+
3044+
/**
3045+
* Returns the name of the source CRS parameter, or an empty string if this is not set.
3046+
* \see setSourceCrsParameterName()
3047+
* \see destinationCrsParameterName()
3048+
*/
3049+
QString sourceCrsParameterName() const { return mSourceParameterName; }
3050+
3051+
/**
3052+
* Sets the \a name of the source CRS parameter. Use an empty string if this is not required.
3053+
* \see sourceCrsParameterName()
3054+
* \see setDestinationCrsParameterName()
3055+
*/
3056+
void setSourceCrsParameterName( const QString &name ) { mSourceParameterName = name; }
3057+
3058+
/**
3059+
* Returns the name of the destination CRS parameter, or an empty string if this is not set.
3060+
* \see setDestinationCrsParameterName()
3061+
* \see sourceCrsParameterName()
3062+
*/
3063+
QString destinationCrsParameterName() const { return mDestParameterName; }
3064+
3065+
/**
3066+
* Sets the \a name of the destination CRS parameter. Use an empty string if this is not required.
3067+
* \see destinationCrsParameterName()
3068+
* \see setSourceCrsParameterName()
3069+
*/
3070+
void setDestinationCrsParameterName( const QString &name ) { mDestParameterName = name; }
3071+
3072+
/**
3073+
* Returns the static source CRS, or an invalid value if this is not set.
3074+
* \see setSourceCrs()
3075+
* \see destinationCrs()
3076+
*/
3077+
QVariant sourceCrs() const { return mSourceCrs; }
3078+
3079+
/**
3080+
* Sets the static source \a crs.
3081+
* \see sourceCrs()
3082+
* \see setDestinationCrs()
3083+
*/
3084+
void setSourceCrs( const QVariant &crs ) { mSourceCrs = crs; }
3085+
3086+
/**
3087+
* Returns the static destination CRS, or an invalid value if this is not set.
3088+
* \see setDestinationCrs()
3089+
* \see sourceCrs()
3090+
*/
3091+
QVariant destinationCrs() const { return mDestCrs; }
3092+
3093+
/**
3094+
* Sets the static destination \a crs.
3095+
* \see destinationCrs()
3096+
* \see setSourceCrs()
3097+
*/
3098+
void setDestinationCrs( const QVariant &crs ) { mDestCrs = crs; }
3099+
3100+
private:
3101+
3102+
QString mSourceParameterName;
3103+
QString mDestParameterName;
3104+
QVariant mSourceCrs;
3105+
QVariant mDestCrs;
3106+
3107+
};
3108+
3109+
30043110
// clazy:excludeall=qstring-allocations
30053111

30063112
#endif // QGSPROCESSINGPARAMETERS_H

0 commit comments

Comments
 (0)
Please sign in to comment.