Skip to content

Commit

Permalink
[processing] Don't crash when hitting transform exceptions
Browse files Browse the repository at this point in the history
inside transform algorithm
  • Loading branch information
nyalldawson committed May 22, 2018
1 parent 681074b commit 33669ab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/analysis/processing/qgsalgorithmtransform.cpp
Expand Up @@ -79,7 +79,7 @@ bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, Qgs
return true;
}

QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
{
QgsFeature feature = f;
if ( !mCreatedTransform )
Expand All @@ -91,12 +91,21 @@ QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsPr
if ( feature.hasGeometry() )
{
QgsGeometry g = feature.geometry();
if ( g.transform( mTransform ) == 0 )
try
{
feature.setGeometry( g );
if ( g.transform( mTransform ) == 0 )
{
feature.setGeometry( g );
}
else
{
feature.clearGeometry();
}
}
else
catch ( QgsCsException & )
{
if ( feedback )
feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
feature.clearGeometry();
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/src/analysis/testqgsprocessingalgs.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgsprocessingmodelalgorithm.h"
#include "qgsnativealgorithms.h"
#include "qgsalgorithmimportphotos.h"
#include "qgsalgorithmtransform.h"

class TestQgsProcessingAlgs: public QObject
{
Expand All @@ -39,6 +40,7 @@ class TestQgsProcessingAlgs: public QObject
void loadLayerAlg();
void parseGeoTags();
void featureFilterAlg();
void transformAlg();

private:

Expand Down Expand Up @@ -404,6 +406,35 @@ void TestQgsProcessingAlgs::featureFilterAlg()
Q_ASSERT( outputParamDef2->flags() & QgsProcessingParameterDefinition::FlagHidden );
}

void TestQgsProcessingAlgs::transformAlg()
{
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:reprojectlayer" ) ) );
QVERIFY( alg );

std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
QgsProject p;
context->setProject( &p );

QgsProcessingFeedback feedback;

QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?crs=EPSG:4326field=col1:integer" ), QStringLiteral( "test" ), QStringLiteral( "memory" ) );
QVERIFY( layer->isValid() );
QgsFeature f;
// add a point with a bad geometry - this should result in a transform exception!
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -96215069, 41.673559 ) ) );
QVERIFY( layer->dataProvider()->addFeature( f ) );
p.addMapLayer( layer );

QVariantMap parameters;
parameters.insert( QStringLiteral( "INPUT" ), QStringLiteral( "test" ) );
parameters.insert( QStringLiteral( "OUTPUT" ), QStringLiteral( "memory:" ) );
parameters.insert( QStringLiteral( "TARGET_CRS" ), QStringLiteral( "EPSG:2163" ) );
bool ok = false;
QVariantMap results = alg->run( parameters, *context, &feedback, &ok );
Q_UNUSED( results );
QVERIFY( ok );
}


QGSTEST_MAIN( TestQgsProcessingAlgs )
#include "testqgsprocessingalgs.moc"

0 comments on commit 33669ab

Please sign in to comment.