QGIS API Documentation  3.0.2-Girona (307d082)
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 
51  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Non null geometries" ),
52  QgsProcessing::TypeVectorAnyGeometry, QVariant(), true ) );
53  QgsProcessingParameterFeatureSink *nullOutput = new QgsProcessingParameterFeatureSink( QStringLiteral( "NULL_OUTPUT" ), QObject::tr( "Null geometries" ),
54  QgsProcessing::TypeVector, QVariant(), true );
55  nullOutput->setCreateByDefault( false );
56  addParameter( nullOutput );
57 }
58 
59 QString QgsRemoveNullGeometryAlgorithm::shortHelpString() const
60 {
61  return QObject::tr( "This algorithm removes any features which do not have a geometry from a vector layer. "
62  "All other features will be copied unchanged.\n\n"
63  "Optionally, the features with null geometries can be saved to a separate output." );
64 }
65 
66 QgsRemoveNullGeometryAlgorithm *QgsRemoveNullGeometryAlgorithm::createInstance() const
67 {
68  return new QgsRemoveNullGeometryAlgorithm();
69 }
70 
71 QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
72 {
73  std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
74  if ( !source )
75  throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) );
76 
77  QString nonNullSinkId;
78  std::unique_ptr< QgsFeatureSink > nonNullSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, nonNullSinkId, source->fields(),
79  source->wkbType(), source->sourceCrs() ) );
80 
81  QString nullSinkId;
82  std::unique_ptr< QgsFeatureSink > nullSink( parameterAsSink( parameters, QStringLiteral( "NULL_OUTPUT" ), context, nullSinkId, source->fields() ) );
83 
84  long count = source->featureCount();
85 
86  double step = count > 0 ? 100.0 / count : 1;
87  int current = 0;
88 
89  QgsFeature f;
91  while ( it.nextFeature( f ) )
92  {
93  if ( feedback->isCanceled() )
94  {
95  break;
96  }
97 
98  if ( f.hasGeometry() && nonNullSink )
99  {
100  nonNullSink->addFeature( f, QgsFeatureSink::FastInsert );
101  }
102  else if ( !f.hasGeometry() && nullSink )
103  {
104  nullSink->addFeature( f, QgsFeatureSink::FastInsert );
105  }
106 
107  feedback->setProgress( current * step );
108  current++;
109  }
110 
111  QVariantMap outputs;
112  if ( nonNullSink )
113  outputs.insert( QStringLiteral( "OUTPUT" ), nonNullSinkId );
114  if ( nullSink )
115  outputs.insert( QStringLiteral( "NULL_OUTPUT" ), nullSinkId );
116  return outputs;
117 }
118 
119 
121 
122 
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
A feature sink output for processing algorithms.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
An input feature source (such as vector layers) parameter for processing algorithms.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
bool nextFeature(QgsFeature &f)
Contains information about the context in which a processing algorithm is executed.
Any vector layer with geometry.
Definition: qgsprocessing.h:48