Skip to content

Commit 33669ab

Browse files
committedMay 22, 2018
[processing] Don't crash when hitting transform exceptions
inside transform algorithm
1 parent 681074b commit 33669ab

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed
 

‎src/analysis/processing/qgsalgorithmtransform.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, Qgs
7979
return true;
8080
}
8181

82-
QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
82+
QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
8383
{
8484
QgsFeature feature = f;
8585
if ( !mCreatedTransform )
@@ -91,12 +91,21 @@ QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsPr
9191
if ( feature.hasGeometry() )
9292
{
9393
QgsGeometry g = feature.geometry();
94-
if ( g.transform( mTransform ) == 0 )
94+
try
9595
{
96-
feature.setGeometry( g );
96+
if ( g.transform( mTransform ) == 0 )
97+
{
98+
feature.setGeometry( g );
99+
}
100+
else
101+
{
102+
feature.clearGeometry();
103+
}
97104
}
98-
else
105+
catch ( QgsCsException & )
99106
{
107+
if ( feedback )
108+
feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
100109
feature.clearGeometry();
101110
}
102111
}

‎tests/src/analysis/testqgsprocessingalgs.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgsprocessingmodelalgorithm.h"
2525
#include "qgsnativealgorithms.h"
2626
#include "qgsalgorithmimportphotos.h"
27+
#include "qgsalgorithmtransform.h"
2728

2829
class TestQgsProcessingAlgs: public QObject
2930
{
@@ -39,6 +40,7 @@ class TestQgsProcessingAlgs: public QObject
3940
void loadLayerAlg();
4041
void parseGeoTags();
4142
void featureFilterAlg();
43+
void transformAlg();
4244

4345
private:
4446

@@ -404,6 +406,35 @@ void TestQgsProcessingAlgs::featureFilterAlg()
404406
Q_ASSERT( outputParamDef2->flags() & QgsProcessingParameterDefinition::FlagHidden );
405407
}
406408

409+
void TestQgsProcessingAlgs::transformAlg()
410+
{
411+
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:reprojectlayer" ) ) );
412+
QVERIFY( alg );
413+
414+
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
415+
QgsProject p;
416+
context->setProject( &p );
417+
418+
QgsProcessingFeedback feedback;
419+
420+
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?crs=EPSG:4326field=col1:integer" ), QStringLiteral( "test" ), QStringLiteral( "memory" ) );
421+
QVERIFY( layer->isValid() );
422+
QgsFeature f;
423+
// add a point with a bad geometry - this should result in a transform exception!
424+
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -96215069, 41.673559 ) ) );
425+
QVERIFY( layer->dataProvider()->addFeature( f ) );
426+
p.addMapLayer( layer );
427+
428+
QVariantMap parameters;
429+
parameters.insert( QStringLiteral( "INPUT" ), QStringLiteral( "test" ) );
430+
parameters.insert( QStringLiteral( "OUTPUT" ), QStringLiteral( "memory:" ) );
431+
parameters.insert( QStringLiteral( "TARGET_CRS" ), QStringLiteral( "EPSG:2163" ) );
432+
bool ok = false;
433+
QVariantMap results = alg->run( parameters, *context, &feedback, &ok );
434+
Q_UNUSED( results );
435+
QVERIFY( ok );
436+
}
437+
407438

408439
QGSTEST_MAIN( TestQgsProcessingAlgs )
409440
#include "testqgsprocessingalgs.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.