Skip to content
Permalink
Browse files

[processing] Allow CRS parameters to be set to a direct QgsCoordinate…

…ReferenceSystem object
  • Loading branch information
nyalldawson committed Sep 6, 2018
1 parent f83b3ec commit 88b324db26fe83a3d68decc0418b46c5041164b9
@@ -687,7 +687,12 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsCrs( const QgsP

QVariant val = value;

if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
if ( val.canConvert<QgsCoordinateReferenceSystem>() )
{
// input is a QgsCoordinateReferenceSystem - done!
return val.value< QgsCoordinateReferenceSystem >();
}
else if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
{
// input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
@@ -1717,7 +1722,11 @@ bool QgsProcessingParameterCrs::checkValueIsAcceptable( const QVariant &input, Q
if ( !input.isValid() )
return mFlags & FlagOptional;

if ( input.canConvert<QgsProcessingFeatureSourceDefinition>() )
if ( input.canConvert<QgsCoordinateReferenceSystem>() )
{
return true;
}
else if ( input.canConvert<QgsProcessingFeatureSourceDefinition>() )
{
return true;
}
@@ -1746,6 +1755,14 @@ QString QgsProcessingParameterCrs::valueAsPythonString( const QVariant &value, Q
if ( !value.isValid() )
return QStringLiteral( "None" );

if ( value.canConvert<QgsCoordinateReferenceSystem>() )
{
if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
return QStringLiteral( "QgsCoordinateReferenceSystem()" );
else
return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
}

if ( value.canConvert<QgsProperty>() )
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );

@@ -257,6 +257,7 @@ class CORE_EXPORT QgsProcessingParameterTypeCrs : public QgsProcessingParameterT
<< QObject::tr( "str: layer ID. CRS of layer is used." )
<< QObject::tr( "str: layer name. CRS of layer is used." )
<< QObject::tr( "str: layer source. CRS of layer is used." )
<< QObject::tr( "QgsCoordinateReferenceSystem" )
<< QObject::tr( "QgsMapLayer: CRS of layer is used" )
<< QObject::tr( "QgsProcessingFeatureSourceDefinition: CRS of source is used" )
<< QStringLiteral( "QgsProperty" );
@@ -2125,6 +2125,8 @@ void TestQgsProcessing::parameterCrs()
QVERIFY( def->checkValueIsAcceptable( "EPSG:12003" ) );
QVERIFY( def->checkValueIsAcceptable( "EPSG:3111" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsCoordinateReferenceSystem() ) );
QVERIFY( def->checkValueIsAcceptable( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( r1->id() ) ) );
@@ -2139,6 +2141,12 @@ void TestQgsProcessing::parameterCrs()
params.insert( "non_optional", QVariant::fromValue( v1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsCrs( def.get(), params, context ).authid(), QString( "EPSG:3111" ) );

// using QgsCoordinateReferenceSystem
params.insert( "non_optional", QgsCoordinateReferenceSystem( "EPSG:28356" ) );
QCOMPARE( QgsProcessingParameters::parameterAsCrs( def.get(), params, context ).authid(), QString( "EPSG:28356" ) );
params.insert( "non_optional", QgsCoordinateReferenceSystem() );
QVERIFY( !QgsProcessingParameters::parameterAsCrs( def.get(), params, context ).isValid() );

// special ProjectCrs string
params.insert( "non_optional", QStringLiteral( "ProjectCrs" ) );
QCOMPARE( QgsProcessingParameters::parameterAsCrs( def.get(), params, context ).authid(), QString( "EPSG:28353" ) );
@@ -2172,6 +2180,8 @@ void TestQgsProcessing::parameterCrs()
QCOMPARE( QgsProcessingParameters::parameterAsCrs( def.get(), params, context ).authid(), QString( "EPSG:3111" ) );

QCOMPARE( def->valueAsPythonString( QVariant(), context ), QStringLiteral( "None" ) );
QCOMPARE( def->valueAsPythonString( QgsCoordinateReferenceSystem( "EPSG:3111" ), context ), QStringLiteral( "QgsCoordinateReferenceSystem('EPSG:3111')" ) );
QCOMPARE( def->valueAsPythonString( QgsCoordinateReferenceSystem(), context ), QStringLiteral( "QgsCoordinateReferenceSystem()" ) );
QCOMPARE( def->valueAsPythonString( "EPSG:12003", context ), QStringLiteral( "'EPSG:12003'" ) );
QCOMPARE( def->valueAsPythonString( "ProjectCrs", context ), QStringLiteral( "'ProjectCrs'" ) );
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );

0 comments on commit 88b324d

Please sign in to comment.
You can’t perform that action at this time.