Skip to content
Permalink
Browse files

Optionally, remove empty geoms in RemoveNullGeometries algorithm

  • Loading branch information
gacarrillor authored and nyalldawson committed Mar 14, 2020
1 parent e791d29 commit 766b1e43b45635a76f82cafedb4102d14f07db6e
@@ -336,9 +336,6 @@ qgis:regularpoints: >
qgis:relief: >
This algorithm creates a shaded relief layer from digital elevation data.

qgis:removenullgeometries: >
This algorithm removes any features which do not have a geometry from a vector layer. All other features will be copied unchanged.

qgis:reprojectlayer: >
This algorithm reprojects a vector layer. It creates a new layer with the same features as the input one, but with geometries reprojected to a new CRS.

@@ -47,6 +47,7 @@ QString QgsRemoveNullGeometryAlgorithm::groupId() const
void QgsRemoveNullGeometryAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "REMOVE_EMPTY" ), QObject::tr( "Also remove empty geometries" ), false ) );

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Non null geometries" ),
QgsProcessing::TypeVectorAnyGeometry, QVariant(), true ) );
@@ -60,7 +61,10 @@ 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." );
"Optionally, the features with null geometries can be saved to a separate output.\n\n"
"If 'Also remove empty geometries' is checked, the algorithm removes features whose geometries"
"have no coordinates, i.e., geometries that are empty. In that case, also the null "
"output will reflect this option, containing both null and empty geometries." );
}

QgsRemoveNullGeometryAlgorithm *QgsRemoveNullGeometryAlgorithm::createInstance() const
@@ -74,6 +78,8 @@ QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap
if ( !source )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );

const bool remove_empty = parameterAsBoolean( parameters, QStringLiteral( "REMOVE_EMPTY" ), context );

QString nonNullSinkId;
std::unique_ptr< QgsFeatureSink > nonNullSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, nonNullSinkId, source->fields(),
source->wkbType(), source->sourceCrs() ) );
@@ -95,11 +101,11 @@ QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap
break;
}

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

0 comments on commit 766b1e4

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