QGIS API Documentation 3.99.0-Master (357b655ed83)
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( "This algorithm removes any features which do not have a geometry from a vector layer. "
65 "All other features will be copied unchanged.\n\n"
66 "Optionally, the features with null geometries can be saved to a separate output.\n\n"
67 "If 'Also remove empty geometries' is checked, the algorithm removes features whose geometries "
68 "have no coordinates, i.e., geometries that are empty. In that case, also the null "
69 "output will reflect this option, containing both null and empty geometries." );
70}
71
72QString QgsRemoveNullGeometryAlgorithm::shortDescription() const
73{
74 return QObject::tr( "Removes any features which do not have a geometry from a vector layer." );
75}
76
77QgsRemoveNullGeometryAlgorithm *QgsRemoveNullGeometryAlgorithm::createInstance() const
78{
79 return new QgsRemoveNullGeometryAlgorithm();
80}
81
82QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
83{
84 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
85 if ( !source )
86 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
87
88 const bool removeEmpty = parameterAsBoolean( parameters, u"REMOVE_EMPTY"_s, context );
89
90 QString nonNullSinkId;
91 std::unique_ptr<QgsFeatureSink> nonNullSink( parameterAsSink( parameters, u"OUTPUT"_s, context, nonNullSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
92
93 QString nullSinkId;
94 std::unique_ptr<QgsFeatureSink> nullSink( parameterAsSink( parameters, u"NULL_OUTPUT"_s, context, nullSinkId, source->fields() ) );
95
96 const long count = source->featureCount();
97
98 const double step = count > 0 ? 100.0 / count : 1;
99 int current = 0;
100
101 QgsFeature f;
103 while ( it.nextFeature( f ) )
104 {
105 if ( feedback->isCanceled() )
106 {
107 break;
108 }
109
110 if ( ( ( !removeEmpty && f.hasGeometry() ) || ( removeEmpty && !f.geometry().isEmpty() ) ) && nonNullSink )
111 {
112 if ( !nonNullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
113 throw QgsProcessingException( writeFeatureError( nonNullSink.get(), parameters, u"OUTPUT"_s ) );
114 }
115 else if ( ( ( !removeEmpty && !f.hasGeometry() ) || ( removeEmpty && f.geometry().isEmpty() ) ) && nullSink )
116 {
117 if ( !nullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
118 throw QgsProcessingException( writeFeatureError( nullSink.get(), parameters, u"NULL_OUTPUT"_s ) );
119 }
120
121 feedback->setProgress( current * step );
122 current++;
123 }
124
125 QVariantMap outputs;
126 if ( nonNullSink )
127 {
128 nonNullSink->finalize();
129 outputs.insert( u"OUTPUT"_s, nonNullSinkId );
130 }
131 if ( nullSink )
132 {
133 nullSink->finalize();
134 outputs.insert( u"NULL_OUTPUT"_s, nullSinkId );
135 }
136 return outputs;
137}
138
139
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3604
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
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:71
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:55
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.
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.