QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmextractbyextent.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextractbyextent.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 QgsExtractByExtentAlgorithm::name() const
23{
24 return QStringLiteral( "extractbyextent" );
25}
26
27QString QgsExtractByExtentAlgorithm::displayName() const
28{
29 return QObject::tr( "Extract/clip by extent" );
30}
31
32QStringList QgsExtractByExtentAlgorithm::tags() const
33{
34 return QObject::tr( "clip,extract,intersect,intersection,mask,extent" ).split( ',' );
35}
36
37QString QgsExtractByExtentAlgorithm::group() const
38{
39 return QObject::tr( "Vector overlay" );
40}
41
42QString QgsExtractByExtentAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectoroverlay" );
45}
46void QgsExtractByExtentAlgorithm::initAlgorithm( const QVariantMap & )
47{
48 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
49 addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ) ) );
50 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "CLIP" ), QObject::tr( "Clip features to extent" ), false ) );
51 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extracted" ) ) );
52}
53
54QString QgsExtractByExtentAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm creates a new vector layer that only contains features which fall within a specified extent. "
57 "Any features which intersect the extent will be included.\n\n"
58 "Optionally, feature geometries can also be clipped to the extent. If this option is selected, then the output "
59 "geometries will automatically be converted to multi geometries to ensure uniform output geometry types." );
60}
61
62QString QgsExtractByExtentAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Creates a vector layer that only contains features which intersect a specified extent." );
65}
66
67QgsExtractByExtentAlgorithm *QgsExtractByExtentAlgorithm::createInstance() const
68{
69 return new QgsExtractByExtentAlgorithm();
70}
71
72QVariantMap QgsExtractByExtentAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
73{
74 std::unique_ptr<QgsFeatureSource> featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
75 if ( !featureSource )
76 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
77
78 if ( featureSource->hasSpatialIndex() == Qgis::SpatialIndexPresence::NotPresent )
79 feedback->pushWarning( QObject::tr( "No spatial index exists for input layer, performance will be severely degraded" ) );
80
81 const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, featureSource->sourceCrs() );
82 const bool clip = parameterAsBoolean( parameters, QStringLiteral( "CLIP" ), context );
83
84 // if clipping, we force multi output
85 const Qgis::WkbType outType = clip ? QgsWkbTypes::promoteNonPointTypesToMulti( featureSource->wkbType() ) : featureSource->wkbType();
86
87 QString dest;
88 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, featureSource->fields(), outType, featureSource->sourceCrs() ) );
89
90 if ( !sink )
91 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
92
93 const QgsGeometry clipGeom = parameterAsExtentGeometry( parameters, QStringLiteral( "EXTENT" ), context, featureSource->sourceCrs() );
94
95 const double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
96 QgsFeatureIterator inputIt = featureSource->getFeatures( QgsFeatureRequest().setFilterRect( extent ).setFlags( Qgis::FeatureRequestFlag::ExactIntersect ) );
97 QgsFeature f;
98 int i = -1;
99 while ( inputIt.nextFeature( f ) )
100 {
101 i++;
102 if ( feedback->isCanceled() )
103 {
104 break;
105 }
106
107 if ( clip )
108 {
109 QgsGeometry g = f.geometry().intersection( clipGeom );
110
111 if ( g.type() != Qgis::GeometryType::Point )
112 {
113 // some data providers are picky about the geometries we pass to them: we can't add single-part geometries
114 // when we promised multi-part geometries, so ensure we have the right type
116 }
117
118 f.setGeometry( g );
119 }
120
121 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
122 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
123 feedback->setProgress( i * step );
124 }
125
126 sink->finalize();
127
128 QVariantMap outputs;
129 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
130 return outputs;
131}
132
@ NotPresent
No spatial index exists for the source.
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
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
void setGeometry(const QgsGeometry &geometry)
Set the feature's 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
A geometry is the spatial representation of a feature.
Qgis::GeometryType type
QgsGeometry intersection(const QgsGeometry &geometry, const QgsGeometryParameters &parameters=QgsGeometryParameters()) const
Returns a geometry representing the points shared by this geometry and other.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
A boolean parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A rectangle specified with double values.
static Qgis::WkbType promoteNonPointTypesToMulti(Qgis::WkbType type)
Promotes a WKB geometry type to its multi-type equivalent, with the exception of point geometry types...