QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
87 if ( !source )
88 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
89
90 const bool removeEmpty = parameterAsBoolean( parameters, u"REMOVE_EMPTY"_s, context );
91
92 QString nonNullSinkId;
93 std::unique_ptr<QgsFeatureSink> nonNullSink( parameterAsSink( parameters, u"OUTPUT"_s, context, nonNullSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
94
95 QString nullSinkId;
96 std::unique_ptr<QgsFeatureSink> nullSink( parameterAsSink( parameters, u"NULL_OUTPUT"_s, context, nullSinkId, source->fields() ) );
97
98 const long count = source->featureCount();
99
100 const double step = count > 0 ? 100.0 / count : 1;
101 int current = 0;
102
103 QgsFeature f;
105 while ( it.nextFeature( f ) )
106 {
107 if ( feedback->isCanceled() )
108 {
109 break;
110 }
111
112 if ( ( ( !removeEmpty && f.hasGeometry() ) || ( removeEmpty && !f.geometry().isEmpty() ) ) && nonNullSink )
113 {
114 if ( !nonNullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
115 throw QgsProcessingException( writeFeatureError( nonNullSink.get(), parameters, u"OUTPUT"_s ) );
116 }
117 else if ( ( ( !removeEmpty && !f.hasGeometry() ) || ( removeEmpty && f.geometry().isEmpty() ) ) && nullSink )
118 {
119 if ( !nullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
120 throw QgsProcessingException( writeFeatureError( nullSink.get(), parameters, u"NULL_OUTPUT"_s ) );
121 }
122
123 feedback->setProgress( current * step );
124 current++;
125 }
126
127 QVariantMap outputs;
128 if ( nonNullSink )
129 {
130 nonNullSink->finalize();
131 outputs.insert( u"OUTPUT"_s, nonNullSinkId );
132 }
133 if ( nullSink )
134 {
135 nullSink->finalize();
136 outputs.insert( u"NULL_OUTPUT"_s, nullSinkId );
137 }
138 return outputs;
139}
140
141
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3653
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3647
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
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: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.
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.