QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsalgorithmremovenullgeometry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmremovenullgeometry.cpp
3  ---------------------
4  begin : April 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 
21 
22 QString QgsRemoveNullGeometryAlgorithm::name() const
23 {
24  return QStringLiteral( "removenullgeometries" );
25 }
26 
27 QString QgsRemoveNullGeometryAlgorithm::displayName() const
28 {
29  return QObject::tr( "Remove null geometries" );
30 }
31 
32 QStringList QgsRemoveNullGeometryAlgorithm::tags() const
33 {
34  return QObject::tr( "remove,drop,delete,empty,geometry" ).split( ',' );
35 }
36 
37 QString QgsRemoveNullGeometryAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsRemoveNullGeometryAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 void QgsRemoveNullGeometryAlgorithm::initAlgorithm( const QVariantMap & )
48 {
49  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
50  addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "REMOVE_EMPTY" ), QObject::tr( "Also remove empty geometries" ), false ) );
51 
52  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Non null geometries" ),
53  QgsProcessing::TypeVectorAnyGeometry, QVariant(), true ) );
54  QgsProcessingParameterFeatureSink *nullOutput = new QgsProcessingParameterFeatureSink( QStringLiteral( "NULL_OUTPUT" ), QObject::tr( "Null geometries" ),
55  QgsProcessing::TypeVector, QVariant(), true );
56  nullOutput->setCreateByDefault( false );
57  addParameter( nullOutput );
58 }
59 
60 QString QgsRemoveNullGeometryAlgorithm::shortHelpString() const
61 {
62  return QObject::tr( "This algorithm removes any features which do not have a geometry from a vector layer. "
63  "All other features will be copied unchanged.\n\n"
64  "Optionally, the features with null geometries can be saved to a separate output.\n\n"
65  "If 'Also remove empty geometries' is checked, the algorithm removes features whose geometries "
66  "have no coordinates, i.e., geometries that are empty. In that case, also the null "
67  "output will reflect this option, containing both null and empty geometries." );
68 }
69 
70 QgsRemoveNullGeometryAlgorithm *QgsRemoveNullGeometryAlgorithm::createInstance() const
71 {
72  return new QgsRemoveNullGeometryAlgorithm();
73 }
74 
75 QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76 {
77  std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
78  if ( !source )
79  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
80 
81  const bool removeEmpty = parameterAsBoolean( parameters, QStringLiteral( "REMOVE_EMPTY" ), context );
82 
83  QString nonNullSinkId;
84  std::unique_ptr< QgsFeatureSink > nonNullSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, nonNullSinkId, source->fields(),
85  source->wkbType(), source->sourceCrs() ) );
86 
87  QString nullSinkId;
88  std::unique_ptr< QgsFeatureSink > nullSink( parameterAsSink( parameters, QStringLiteral( "NULL_OUTPUT" ), context, nullSinkId, source->fields() ) );
89 
90  long count = source->featureCount();
91 
92  double step = count > 0 ? 100.0 / count : 1;
93  int current = 0;
94 
95  QgsFeature f;
97  while ( it.nextFeature( f ) )
98  {
99  if ( feedback->isCanceled() )
100  {
101  break;
102  }
103 
104  if ( ( ( !removeEmpty && f.hasGeometry() ) || ( removeEmpty && !f.geometry().isEmpty() ) ) && nonNullSink )
105  {
106  nonNullSink->addFeature( f, QgsFeatureSink::FastInsert );
107  }
108  else if ( ( ( !removeEmpty && !f.hasGeometry() ) || ( removeEmpty && f.geometry().isEmpty() ) ) && nullSink )
109  {
110  nullSink->addFeature( f, QgsFeatureSink::FastInsert );
111  }
112 
113  feedback->setProgress( current * step );
114  current++;
115  }
116 
117  QVariantMap outputs;
118  if ( nonNullSink )
119  outputs.insert( QStringLiteral( "OUTPUT" ), nonNullSinkId );
120  if ( nullSink )
121  outputs.insert( QStringLiteral( "NULL_OUTPUT" ), nullSinkId );
122  return outputs;
123 }
124 
125 
127 
128 
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:205
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Contains information about the context in which a processing algorithm is executed.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
A boolean parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:48