Skip to content
Permalink
Browse files

[processing] Don't crash when hitting transform exceptions

inside transform algorithm
  • Loading branch information
nyalldawson committed May 22, 2018
1 parent 681074b commit 33669ab7232ef4fde9a27c6ed495bf86d10ddd4e
Showing with 44 additions and 4 deletions.
  1. +13 −4 src/analysis/processing/qgsalgorithmtransform.cpp
  2. +31 −0 tests/src/analysis/testqgsprocessingalgs.cpp
@@ -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 )
@@ -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();
}
}
@@ -24,6 +24,7 @@
#include "qgsprocessingmodelalgorithm.h"
#include "qgsnativealgorithms.h"
#include "qgsalgorithmimportphotos.h"
#include "qgsalgorithmtransform.h"

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

private:

@@ -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.
You can’t perform that action at this time.