QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:75
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingDestinationParameter::setCreateByDefault
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
Definition: qgsprocessingparameters.cpp:5603
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsProcessingParameterFeatureSource
Definition: qgsprocessingparameters.h:2612
QgsProcessingParameterFeatureSink
Definition: qgsprocessingparameters.h:2773
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
qgsalgorithmremovenullgeometry.h
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessing::TypeVectorAnyGeometry
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:47
QgsGeometry::isEmpty
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Definition: qgsgeometry.cpp:367
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
QgsProcessingParameterBoolean
Definition: qgsprocessingparameters.h:1439
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsFeature
Definition: qgsfeature.h:55
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsProcessingException
Definition: qgsexception.h:82
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70