QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsExtractByExtentAlgorithm::name() const
27{
28 return u"extractbyextent"_s;
29}
30
31QString QgsExtractByExtentAlgorithm::displayName() const
32{
33 return QObject::tr( "Extract/clip by extent" );
34}
35
36QStringList QgsExtractByExtentAlgorithm::tags() const
37{
38 return QObject::tr( "clip,extract,intersect,intersection,mask,extent" ).split( ',' );
39}
40
41QString QgsExtractByExtentAlgorithm::group() const
42{
43 return QObject::tr( "Vector overlay" );
44}
45
46QString QgsExtractByExtentAlgorithm::groupId() const
47{
48 return u"vectoroverlay"_s;
49}
50void QgsExtractByExtentAlgorithm::initAlgorithm( const QVariantMap & )
51{
52 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
53 addParameter( new QgsProcessingParameterExtent( u"EXTENT"_s, QObject::tr( "Extent" ) ) );
54 addParameter( new QgsProcessingParameterBoolean( u"CLIP"_s, QObject::tr( "Clip features to extent" ), false ) );
55 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Extracted" ) ) );
56}
57
58QString QgsExtractByExtentAlgorithm::shortHelpString() const
59{
60 return QObject::tr( "This algorithm creates a new vector layer that only contains features which fall within a specified extent. "
61 "Any features which intersect the extent will be included.\n\n"
62 "Optionally, feature geometries can also be clipped to the extent. If this option is selected, then the output "
63 "geometries will automatically be converted to multi geometries to ensure uniform output geometry types." );
64}
65
66QString QgsExtractByExtentAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Creates a vector layer that only contains features which intersect a specified extent." );
69}
70
71QgsExtractByExtentAlgorithm *QgsExtractByExtentAlgorithm::createInstance() const
72{
73 return new QgsExtractByExtentAlgorithm();
74}
75
76QVariantMap QgsExtractByExtentAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
77{
78 std::unique_ptr<QgsFeatureSource> featureSource( parameterAsSource( parameters, u"INPUT"_s, context ) );
79 if ( !featureSource )
80 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
81
82 if ( featureSource->hasSpatialIndex() == Qgis::SpatialIndexPresence::NotPresent )
83 feedback->pushWarning( QObject::tr( "No spatial index exists for input layer, performance will be severely degraded" ) );
84
85 const QgsRectangle extent = parameterAsExtent( parameters, u"EXTENT"_s, context, featureSource->sourceCrs() );
86 const bool clip = parameterAsBoolean( parameters, u"CLIP"_s, context );
87
88 // if clipping, we force multi output
89 const Qgis::WkbType outType = clip ? QgsWkbTypes::promoteNonPointTypesToMulti( featureSource->wkbType() ) : featureSource->wkbType();
90
91 QString dest;
92 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, featureSource->fields(), outType, featureSource->sourceCrs() ) );
93
94 if ( !sink )
95 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
96
97 const QgsGeometry clipGeom = parameterAsExtentGeometry( parameters, u"EXTENT"_s, context, featureSource->sourceCrs() );
98
99 const double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
100 QgsFeatureIterator inputIt = featureSource->getFeatures( QgsFeatureRequest().setFilterRect( extent ).setFlags( Qgis::FeatureRequestFlag::ExactIntersect ) );
101 QgsFeature f;
102 int i = -1;
103 while ( inputIt.nextFeature( f ) )
104 {
105 i++;
106 if ( feedback->isCanceled() )
107 {
108 break;
109 }
110
111 if ( clip )
112 {
113 QgsGeometry g = f.geometry().intersection( clipGeom );
114
115 if ( g.type() != Qgis::GeometryType::Point )
116 {
117 // some data providers are picky about the geometries we pass to them: we can't add single-part geometries
118 // when we promised multi-part geometries, so ensure we have the right type
120 }
121
122 f.setGeometry( g );
123 }
124
125 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
126 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
127 feedback->setProgress( i * step );
128 }
129
130 sink->finalize();
131
132 QVariantMap outputs;
133 outputs.insert( u"OUTPUT"_s, dest );
134 return outputs;
135}
136
@ NotPresent
No spatial index exists for the source.
Definition qgis.h:579
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
Definition qgis.h:2256
@ Point
Points.
Definition qgis.h:366
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
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
void setGeometry(const QgsGeometry &geometry)
Set the feature's 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
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...