Skip to content

Commit

Permalink
[processing] Also warn on non-matching point and extent CRS
Browse files Browse the repository at this point in the history
if algorithm cannot handle automatic reprojection
  • Loading branch information
nyalldawson committed May 1, 2018
1 parent c32611b commit 386495b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -176,7 +176,7 @@ bool QgsProcessingAlgorithm::validateInputCrs( const QVariantMap &parameters, Qg
QgsCoordinateReferenceSystem crs;
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->type() == QStringLiteral( "layer" ) || def->type() == QStringLiteral( "raster" ) )
if ( def->type() == QgsProcessingParameterMapLayer::typeName() || def->type() == QgsProcessingParameterRasterLayer::typeName() )
{
QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( def, parameters, context );
if ( layer )
Expand All @@ -192,7 +192,7 @@ bool QgsProcessingAlgorithm::validateInputCrs( const QVariantMap &parameters, Qg
}
}
}
else if ( def->type() == QStringLiteral( "source" ) )
else if ( def->type() == QgsProcessingParameterFeatureSource::typeName() )
{
std::unique_ptr< QgsFeatureSource > source( QgsProcessingParameters::parameterAsSource( def, parameters, context ) );
if ( source )
Expand All @@ -208,7 +208,7 @@ bool QgsProcessingAlgorithm::validateInputCrs( const QVariantMap &parameters, Qg
}
}
}
else if ( def->type() == QStringLiteral( "multilayer" ) )
else if ( def->type() == QgsProcessingParameterMultipleLayers::typeName() )
{
QList< QgsMapLayer *> layers = QgsProcessingParameters::parameterAsLayerList( def, parameters, context );
Q_FOREACH ( QgsMapLayer *layer, layers )
Expand All @@ -227,6 +227,32 @@ bool QgsProcessingAlgorithm::validateInputCrs( const QVariantMap &parameters, Qg
}
}
}
else if ( def->type() == QgsProcessingParameterExtent::typeName() )
{
QgsCoordinateReferenceSystem extentCrs = QgsProcessingParameters::parameterAsExtentCrs( def, parameters, context );
if ( foundCrs && extentCrs.isValid() && crs != extentCrs )
{
return false;
}
else if ( !foundCrs && extentCrs.isValid() )
{
foundCrs = true;
crs = extentCrs;
}
}
else if ( def->type() == QgsProcessingParameterPoint::typeName() )
{
QgsCoordinateReferenceSystem pointCrs = QgsProcessingParameters::parameterAsPointCrs( def, parameters, context );
if ( foundCrs && pointCrs.isValid() && crs != pointCrs )
{
return false;
}
else if ( !foundCrs && pointCrs.isValid() )
{
foundCrs = true;
crs = pointCrs;
}
}
}
return true;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -256,6 +256,24 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
addParameter( new QgsProcessingParameterMultipleLayers( "p5" ) );
parameters.insert( "p5", QVariantList() << layer4326->id() << r1->id() );
QVERIFY( !validateInputCrs( parameters, context ) );

// extent
parameters.clear();
parameters.insert( "p1", QVariant::fromValue( layer3111 ) );
addParameter( new QgsProcessingParameterExtent( "extent" ) );
parameters.insert( "extent", QgsReferencedRectangle( QgsRectangle( 1, 2, 3, 4 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );
QVERIFY( !validateInputCrs( parameters, context ) );
parameters.insert( "extent", QgsReferencedRectangle( QgsRectangle( 1, 2, 3, 4 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ) );
QVERIFY( validateInputCrs( parameters, context ) );

// point
parameters.clear();
parameters.insert( "p1", QVariant::fromValue( layer3111 ) );
addParameter( new QgsProcessingParameterPoint( "point" ) );
parameters.insert( "point", QgsReferencedPointXY( QgsPointXY( 1, 2 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );
QVERIFY( !validateInputCrs( parameters, context ) );
parameters.insert( "point", QgsReferencedPointXY( QgsPointXY( 1, 2 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ) );
QVERIFY( validateInputCrs( parameters, context ) );
}

void runAsPythonCommandChecks()
Expand Down

0 comments on commit 386495b

Please sign in to comment.