Skip to content

Commit

Permalink
Optionally, remove empty geoms in RemoveNullGeometries algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
gacarrillor authored and nyalldawson committed Mar 15, 2020
1 parent e791d29 commit 766b1e4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 0 additions & 3 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -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.

Expand Down
12 changes: 9 additions & 3 deletions src/analysis/processing/qgsalgorithmremovenullgeometry.cpp
Expand Up @@ -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 ) );
Expand All @@ -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
Expand All @@ -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() ) );
Expand All @@ -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 );
}
Expand Down

0 comments on commit 766b1e4

Please sign in to comment.