QGIS API Documentation 4.3.0-Master (45633be667c)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsRemoveNullGeometryAlgorithm::name() const
27{
28 return u"removenullgeometries"_s;
29}
30
31QString QgsRemoveNullGeometryAlgorithm::displayName() const
32{
33 return QObject::tr( "Remove null geometries" );
34}
35
36QStringList QgsRemoveNullGeometryAlgorithm::tags() const
37{
38 return QObject::tr( "remove,drop,delete,empty,geometry" ).split( ',' );
39}
40
41QString QgsRemoveNullGeometryAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsRemoveNullGeometryAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51void QgsRemoveNullGeometryAlgorithm::initAlgorithm( const QVariantMap & )
52{
53 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
54 addParameter( new QgsProcessingParameterBoolean( u"REMOVE_EMPTY"_s, QObject::tr( "Also remove empty geometries" ), false ) );
55
56 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Non null geometries" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true ) );
57 QgsProcessingParameterFeatureSink *nullOutput = new QgsProcessingParameterFeatureSink( u"NULL_OUTPUT"_s, QObject::tr( "Null geometries" ), Qgis::ProcessingSourceType::Vector, QVariant(), true );
58 nullOutput->setCreateByDefault( false );
59 addParameter( nullOutput );
60}
61
62QString QgsRemoveNullGeometryAlgorithm::shortHelpString() const
63{
64 return QObject::tr(
65 "This algorithm removes any features which do not have a geometry from a vector layer. "
66 "All other features will be copied unchanged.\n\n"
67 "Optionally, the features with null geometries can be saved to a separate output.\n\n"
68 "If 'Also remove empty geometries' is checked, the algorithm removes features whose geometries "
69 "have no coordinates, i.e., geometries that are empty. In that case, also the null "
70 "output will reflect this option, containing both null and empty geometries."
71 );
72}
73
74QString QgsRemoveNullGeometryAlgorithm::shortDescription() const
75{
76 return QObject::tr( "Removes any features which do not have a geometry from a vector layer." );
77}
78
79QgsRemoveNullGeometryAlgorithm *QgsRemoveNullGeometryAlgorithm::createInstance() const
80{
81 return new QgsRemoveNullGeometryAlgorithm();
82}
83
84QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
85{
86 QGS_MARK_ALGORITHM_SOURCE
87
88 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
89 if ( !source )
90 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
91
92 const bool removeEmpty = parameterAsBoolean( parameters, u"REMOVE_EMPTY"_s, context );
93
94 QString nonNullSinkId;
95 std::unique_ptr<QgsFeatureSink> nonNullSink( parameterAsSink( parameters, u"OUTPUT"_s, context, nonNullSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
96
97 QString nullSinkId;
98 std::unique_ptr<QgsFeatureSink> nullSink( parameterAsSink( parameters, u"NULL_OUTPUT"_s, context, nullSinkId, source->fields() ) );
99
100 const long count = source->featureCount();
101
102 const double step = count > 0 ? 100.0 / count : 1;
103 int current = 0;
104
105 QgsFeature f;
107 while ( it.nextFeature( f ) )
108 {
109 if ( feedback->isCanceled() )
110 {
111 break;
112 }
113
114 if ( ( ( !removeEmpty && f.hasGeometry() ) || ( removeEmpty && !f.geometry().isEmpty() ) ) && nonNullSink )
115 {
116 if ( !nonNullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
117 throw QgsProcessingException( writeFeatureError( nonNullSink.get(), parameters, u"OUTPUT"_s ) );
118 else
119 feedback->featureAddedToSink( u"OUTPUT"_s );
120 }
121 else if ( ( ( !removeEmpty && !f.hasGeometry() ) || ( removeEmpty && f.geometry().isEmpty() ) ) && nullSink )
122 {
123 if ( !nullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
124 throw QgsProcessingException( writeFeatureError( nullSink.get(), parameters, u"NULL_OUTPUT"_s ) );
125 feedback->featureAddedToSink( u"NULL_OUTPUT"_s );
126 }
127
128 feedback->setProgress( current * step );
129 current++;
130 }
131
132 QVariantMap outputs;
133 if ( nonNullSink )
134 {
135 nonNullSink->finalize();
136 feedback->featureSinkFinalized( u"OUTPUT"_s );
137
138 outputs.insert( u"OUTPUT"_s, nonNullSinkId );
139 }
140 if ( nullSink )
141 {
142 nullSink->finalize();
143 feedback->featureSinkFinalized( u"NULL_OUTPUT"_s );
144 outputs.insert( u"NULL_OUTPUT"_s, nullSinkId );
145 }
146 return outputs;
147}
148
149
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3757
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3751
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3954
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
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:60
QgsGeometry geometry
Definition qgsfeature.h:66
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
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.
Base class for providing feedback from a processing algorithm.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.