Skip to content
Permalink
Browse files

Port Remove Null Geometries algorithm to c++/new API

Enhancements:
- add a new optional output for null geometries, and make
the non-null geometry output optional. This allows the algorithm
to act as a router for features with null/not null geometries
inside of a model
  • Loading branch information
nyalldawson committed Jul 16, 2017
1 parent aec6a79 commit 5deb8fcb87b2b20c0afb9350d6ee673ff3c7151b
@@ -152,7 +152,6 @@
# from .Relief import Relief
# from .IdwInterpolation import IdwInterpolation
# from .TinInterpolation import TinInterpolation
# from .RemoveNullGeometry import RemoveNullGeometry
# from .ExtendLines import ExtendLines
# from .ExtractSpecificNodes import ExtractSpecificNodes
# from .GeometryByExpression import GeometryByExpression
@@ -221,7 +220,6 @@ def getAlgs(self):
# Slope(), Ruggedness(), Hillshade(),
# Relief(),
# IdwInterpolation(), TinInterpolation(),
# RemoveNullGeometry(),
# ExtendLines(), ExtractSpecificNodes(),
# GeometryByExpression(),
# PoleOfInaccessibility(),

This file was deleted.

@@ -1258,17 +1258,17 @@ tests:
# #TRIANULATION_FILE:
# # name: expected/triangulation.gml
# # type: vector
#
# - algorithm: qgis:removenullgeometries
# name: Remove null geometries
# params:
# INPUT_LAYER:
# name: polys.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/remove_null_polys.gml
# type: vector

- algorithm: native:removenullgeometries
name: Remove null geometries
params:
INPUT:
name: polys.gml
type: vector
results:
OUTPUT:
name: expected/remove_null_polys.gml
type: vector
#
- algorithm: native:extractbyexpression
name: Extract by Expression
@@ -67,6 +67,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsMultipartToSinglepartAlgorithm() );
addAlgorithm( new QgsSubdivideAlgorithm() );
addAlgorithm( new QgsTransformAlgorithm() );
addAlgorithm( new QgsRemoveNullGeometryAlgorithm() );
}

void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
@@ -1093,4 +1094,78 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
return outputs;
}


void QgsRemoveNullGeometryAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Non null geometries" ),
QgsProcessing::TypeVectorAny, QVariant(), true ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "NULL_OUTPUT" ), QObject::tr( "Null geometries" ),
QgsProcessing::TypeVectorAny, QVariant(), true ) );
}

QString QgsRemoveNullGeometryAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm removes any features which do not have a geometry from a vector layer. "
"All other features will be copied unchanged.\n\n"
"Optionally, the features with null geometries can be saved to a separate output." );
}

QgsRemoveNullGeometryAlgorithm *QgsRemoveNullGeometryAlgorithm::createInstance() const
{
return new QgsRemoveNullGeometryAlgorithm();
}

QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();

QString nonNullSinkId;
std::unique_ptr< QgsFeatureSink > nonNullSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, nonNullSinkId, source->fields(),
source->wkbType(), source->sourceCrs() ) );

QString nullSinkId;
std::unique_ptr< QgsFeatureSink > nullSink( parameterAsSink( parameters, QStringLiteral( "NULL_OUTPUT" ), context, nullSinkId, source->fields() ) );

long count = source->featureCount();
if ( count <= 0 )
return QVariantMap();

double step = 100.0 / count;
int current = 0;

QgsFeature f;
QgsFeatureIterator it = source->getFeatures();
while ( it.nextFeature( f ) )
{
if ( feedback->isCanceled() )
{
break;
}

if ( f.hasGeometry() && nonNullSink )
{
nonNullSink->addFeature( f, QgsFeatureSink::FastInsert );
}
else if ( !f.hasGeometry() && nullSink )
{
nullSink->addFeature( f, QgsFeatureSink::FastInsert );
}

feedback->setProgress( current * step );
current++;
}

QVariantMap outputs;
if ( nonNullSink )
outputs.insert( QStringLiteral( "OUTPUT" ), nonNullSinkId );
if ( nullSink )
outputs.insert( QStringLiteral( "NULL_OUTPUT" ), nullSinkId );
return outputs;
}


///@endcond
@@ -278,6 +278,31 @@ class QgsMultipartToSinglepartAlgorithm : public QgsProcessingAlgorithm

};


/**
* Remove null geometry algorithm.
*/
class QgsRemoveNullGeometryAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsRemoveNullGeometryAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override { return QStringLiteral( "removenullgeometries" ); }
QString displayName() const override { return QObject::tr( "Remove null geometries" ); }
virtual QStringList tags() const override { return QObject::tr( "remove,drop,delete,empty,geometry" ).split( ',' ); }
QString group() const override { return QObject::tr( "Vector selection tools" ); }
QString shortHelpString() const override;
QgsRemoveNullGeometryAlgorithm *createInstance() const override SIP_FACTORY;

protected:

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

};

///@endcond PRIVATE

#endif // QGSNATIVEALGORITHMS_H

0 comments on commit 5deb8fc

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