QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
21
22QString QgsRemoveNullGeometryAlgorithm::name() const
23{
24 return QStringLiteral( "removenullgeometries" );
25}
26
27QString QgsRemoveNullGeometryAlgorithm::displayName() const
28{
29 return QObject::tr( "Remove null geometries" );
30}
31
32QStringList QgsRemoveNullGeometryAlgorithm::tags() const
33{
34 return QObject::tr( "remove,drop,delete,empty,geometry" ).split( ',' );
35}
36
37QString QgsRemoveNullGeometryAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsRemoveNullGeometryAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47void 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" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true ) );
53 QgsProcessingParameterFeatureSink *nullOutput = new QgsProcessingParameterFeatureSink( QStringLiteral( "NULL_OUTPUT" ), QObject::tr( "Null geometries" ), Qgis::ProcessingSourceType::Vector, QVariant(), true );
54 nullOutput->setCreateByDefault( false );
55 addParameter( nullOutput );
56}
57
58QString QgsRemoveNullGeometryAlgorithm::shortHelpString() const
59{
60 return QObject::tr( "This algorithm removes any features which do not have a geometry from a vector layer. "
61 "All other features will be copied unchanged.\n\n"
62 "Optionally, the features with null geometries can be saved to a separate output.\n\n"
63 "If 'Also remove empty geometries' is checked, the algorithm removes features whose geometries "
64 "have no coordinates, i.e., geometries that are empty. In that case, also the null "
65 "output will reflect this option, containing both null and empty geometries." );
66}
67
68QString QgsRemoveNullGeometryAlgorithm::shortDescription() const
69{
70 return QObject::tr( "Removes any features which do not have a geometry from a vector layer." );
71}
72
73QgsRemoveNullGeometryAlgorithm *QgsRemoveNullGeometryAlgorithm::createInstance() const
74{
75 return new QgsRemoveNullGeometryAlgorithm();
76}
77
78QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
79{
80 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
81 if ( !source )
82 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
83
84 const bool removeEmpty = parameterAsBoolean( parameters, QStringLiteral( "REMOVE_EMPTY" ), context );
85
86 QString nonNullSinkId;
87 std::unique_ptr<QgsFeatureSink> nonNullSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, nonNullSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
88
89 QString nullSinkId;
90 std::unique_ptr<QgsFeatureSink> nullSink( parameterAsSink( parameters, QStringLiteral( "NULL_OUTPUT" ), context, nullSinkId, source->fields() ) );
91
92 const long count = source->featureCount();
93
94 const double step = count > 0 ? 100.0 / count : 1;
95 int current = 0;
96
97 QgsFeature f;
99 while ( it.nextFeature( f ) )
100 {
101 if ( feedback->isCanceled() )
102 {
103 break;
104 }
105
106 if ( ( ( !removeEmpty && f.hasGeometry() ) || ( removeEmpty && !f.geometry().isEmpty() ) ) && nonNullSink )
107 {
108 if ( !nonNullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
109 throw QgsProcessingException( writeFeatureError( nonNullSink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
110 }
111 else if ( ( ( !removeEmpty && !f.hasGeometry() ) || ( removeEmpty && f.geometry().isEmpty() ) ) && nullSink )
112 {
113 if ( !nullSink->addFeature( f, QgsFeatureSink::FastInsert ) )
114 throw QgsProcessingException( writeFeatureError( nullSink.get(), parameters, QStringLiteral( "NULL_OUTPUT" ) ) );
115 }
116
117 feedback->setProgress( current * step );
118 current++;
119 }
120
121 QVariantMap outputs;
122 if ( nonNullSink )
123 {
124 nonNullSink->finalize();
125 outputs.insert( QStringLiteral( "OUTPUT" ), nonNullSinkId );
126 }
127 if ( nullSink )
128 {
129 nullSink->finalize();
130 outputs.insert( QStringLiteral( "NULL_OUTPUT" ), nullSinkId );
131 }
132 return outputs;
133}
134
135
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3533
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
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:58
QgsGeometry geometry
Definition qgsfeature.h:69
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:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
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.