Skip to content
Permalink
Browse files

[processing] Throw exceptions when sources/sinks cannot be loaded

Instead of silently indicating algorithm success

(cherry-picked from 0156656)
  • Loading branch information
nyalldawson committed Apr 10, 2018
1 parent 7d18d52 commit 76a6886693f8bfde60a6fa756951be8d27ebf871
@@ -81,12 +81,12 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters,
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(), QgsWkbTypes::Polygon, source->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );

// fixed parameters
bool dissolve = parameterAsBool( parameters, QStringLiteral( "DISSOLVE" ), context );
@@ -71,17 +71,17 @@ QVariantMap QgsClipAlgorithm::processAlgorithm( const QVariantMap &parameters, Q
{
std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !featureSource )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

std::unique_ptr< QgsFeatureSource > maskSource( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
if ( !maskSource )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for OVERLAY" ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, featureSource->fields(), QgsWkbTypes::multiType( featureSource->wkbType() ), featureSource->sourceCrs() ) );

if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

// first build up a list of clip geometries
QVector< QgsGeometry > clipGeoms;
@@ -28,13 +28,13 @@ QVariantMap QgsCollectorAlgorithm::processCollection( const QVariantMap &paramet
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(), QgsWkbTypes::multiType( source->wkbType() ), source->sourceCrs() ) );

if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QStringList fields = parameterAsFields( parameters, QStringLiteral( "FIELD" ), context );

@@ -53,7 +53,7 @@ QVariantMap QgsExtentToLayerAlgorithm::processAlgorithm( const QVariantMap &para
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Polygon, crs ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QgsFeature f;
f.setAttributes( QgsAttributes() << 1 );
@@ -85,7 +85,7 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString fieldName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
Operation op = static_cast< Operation >( parameterAsEnum( parameters, QStringLiteral( "OPERATOR" ), context ) );
@@ -95,7 +95,7 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
std::unique_ptr< QgsFeatureSink > matchingSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, matchingSinkId, source->fields(),
source->wkbType(), source->sourceCrs() ) );
if ( !matchingSink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QString nonMatchingSinkId;
std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral( "FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(),
@@ -72,15 +72,15 @@ QVariantMap QgsExtractByExpressionAlgorithm::processAlgorithm( const QVariantMap
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString expressionString = parameterAsExpression( parameters, QStringLiteral( "EXPRESSION" ), context );

QString matchingSinkId;
std::unique_ptr< QgsFeatureSink > matchingSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, matchingSinkId, source->fields(),
source->wkbType(), source->sourceCrs() ) );
if ( !matchingSink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QString nonMatchingSinkId;
std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral( "FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(),
@@ -68,7 +68,7 @@ QVariantMap QgsExtractByExtentAlgorithm::processAlgorithm( const QVariantMap &pa
{
std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !featureSource )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, featureSource->sourceCrs() );
bool clip = parameterAsBool( parameters, QStringLiteral( "CLIP" ), context );
@@ -80,7 +80,7 @@ QVariantMap QgsExtractByExtentAlgorithm::processAlgorithm( const QVariantMap &pa
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, featureSource->fields(), outType, featureSource->sourceCrs() ) );

if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QgsGeometry clipGeom = parameterAsExtentGeometry( parameters, QStringLiteral( "EXTENT" ), context, featureSource->sourceCrs() );

@@ -349,7 +349,7 @@ QVariantMap QgsExtractByLocationAlgorithm::processAlgorithm( const QVariantMap &
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, input->fields(), input->wkbType(), input->sourceCrs() ) );

if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

auto addToSink = [&]( const QgsFeature & feature )
{
@@ -70,7 +70,7 @@ QVariantMap QgsExtractVerticesAlgorithm::processAlgorithm( const QVariantMap &pa
{
std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !featureSource )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QgsWkbTypes::Type outputWkbType = QgsWkbTypes::Point;
if ( QgsWkbTypes::hasM( featureSource->wkbType() ) )
@@ -96,7 +96,7 @@ QVariantMap QgsExtractVerticesAlgorithm::processAlgorithm( const QVariantMap &pa
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, outputWkbType, featureSource->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
QgsFeatureIterator fi = featureSource->getFeatures( QgsFeatureRequest() );
@@ -82,7 +82,7 @@ QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &pa
std::unique_ptr< QgsFeatureSource > input( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
std::unique_ptr< QgsFeatureSource > input2( parameterAsSource( parameters, QStringLiteral( "INPUT_2" ), context ) );
if ( !input || !input2 )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layers" ) );

QString field1Name = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
QString field2Name = parameterAsString( parameters, QStringLiteral( "FIELD_2" ), context );
@@ -91,7 +91,7 @@ QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &pa
int joinField1Index = input->fields().lookupField( field1Name );
int joinField2Index = input2->fields().lookupField( field2Name );
if ( joinField1Index < 0 || joinField2Index < 0 )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Invalid join fields" ) );

QgsFields outFields2;
QgsAttributeList fields2Indices;
@@ -125,7 +125,7 @@ QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &pa
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outFields,
input->wkbType(), input->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );


// cache attributes of input2
@@ -90,7 +90,7 @@ QVariantMap QgsJoinWithLinesAlgorithm::processAlgorithm( const QVariantMap &para
std::unique_ptr< QgsFeatureSource > hubSource( parameterAsSource( parameters, QStringLiteral( "HUBS" ), context ) );
std::unique_ptr< QgsFeatureSource > spokeSource( parameterAsSource( parameters, QStringLiteral( "SPOKES" ), context ) );
if ( !hubSource || !spokeSource )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layers" ) );

QString fieldHubName = parameterAsString( parameters, QStringLiteral( "HUB_FIELD" ), context );
int fieldHubIndex = hubSource->fields().lookupField( fieldHubName );
@@ -176,7 +176,7 @@ QVariantMap QgsJoinWithLinesAlgorithm::processAlgorithm( const QVariantMap &para
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields,
outType, hubSource->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

auto getPointFromFeature = [hasZ, hasM]( const QgsFeature & feature )->QgsPoint
{
@@ -80,11 +80,11 @@ QVariantMap QgsLineIntersectionAlgorithm::processAlgorithm( const QVariantMap &p
{
std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !sourceA )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "INTERSECT" ), context ) );
if ( !sourceB )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INTERSECT" ) );

const QStringList fieldsA = parameterAsFields( parameters, QStringLiteral( "INPUT_FIELDS" ), context );
const QStringList fieldsB = parameterAsFields( parameters, QStringLiteral( "INTERSECT_FIELDS" ), context );
@@ -142,7 +142,7 @@ QVariantMap QgsLineIntersectionAlgorithm::processAlgorithm( const QVariantMap &p
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outFields, QgsWkbTypes::Point, sourceA->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QgsSpatialIndex spatialIndex( sourceB->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ).setDestinationCrs( sourceA->sourceCrs(), context.transformContext() ) ), feedback );
QgsFeature outFeature;
@@ -75,7 +75,7 @@ QVariantMap QgsMeanCoordinatesAlgorithm::processAlgorithm( const QVariantMap &pa
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString weightFieldName = parameterAsString( parameters, QStringLiteral( "WEIGHT" ), context );
QString uniqueFieldName = parameterAsString( parameters, QStringLiteral( "UID" ), context );
@@ -110,7 +110,7 @@ QVariantMap QgsMeanCoordinatesAlgorithm::processAlgorithm( const QVariantMap &pa
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields,
QgsWkbTypes::Point, source->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( attributes ) );

@@ -71,7 +71,7 @@ QVariantMap QgsOrderByExpressionAlgorithm::processAlgorithm( const QVariantMap &
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString expressionString = parameterAsExpression( parameters, QStringLiteral( "EXPRESSION" ), context );

@@ -81,7 +81,7 @@ QVariantMap QgsOrderByExpressionAlgorithm::processAlgorithm( const QVariantMap &
QString sinkId;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, sinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

long count = source->featureCount();
double step = count > 0 ? 100.0 / count : 1;
@@ -72,7 +72,7 @@ QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString nonNullSinkId;
std::unique_ptr< QgsFeatureSink > nonNullSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, nonNullSinkId, source->fields(),
@@ -73,7 +73,7 @@ QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parame
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;


int count = selectLayer->selectedFeatureCount();
@@ -69,19 +69,19 @@ QVariantMap QgsSplitWithLinesAlgorithm::processAlgorithm( const QVariantMap &par
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

std::unique_ptr< QgsFeatureSource > linesSource( parameterAsSource( parameters, QStringLiteral( "LINES" ), context ) );
if ( !linesSource )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for LINES" ) );

bool sameLayer = parameters.value( QStringLiteral( "INPUT" ) ) == parameters.value( QStringLiteral( "LINES" ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(),
QgsWkbTypes::multiType( source->wkbType() ), source->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QgsSpatialIndex spatialIndex;
QMap< QgsFeatureId, QgsGeometry > splitGeoms;
@@ -91,7 +91,7 @@ QVariantMap QgsTransectAlgorithm::processAlgorithm( const QVariantMap &parameter

std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QgsFields fields = source->fields();

@@ -112,7 +112,7 @@ QVariantMap QgsTransectAlgorithm::processAlgorithm( const QVariantMap &parameter
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields,
outputWkb, source->sourceCrs() ) );
if ( !sink )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not create destination layer for OUTPUT" ) );;

QgsFeatureIterator features = source->getFeatures( );

@@ -79,7 +79,7 @@ QVariantMap QgsAddUniqueValueIndexAlgorithm::processAlgorithm( const QVariantMap
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();
throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );

QString newFieldName = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
QgsFields fields = source->fields();

0 comments on commit 76a6886

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