QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
22 QString QgsExtractByExtentAlgorithm::name() const
23 {
24  return QStringLiteral( "extractbyextent" );
25 }
26 
27 QString QgsExtractByExtentAlgorithm::displayName() const
28 {
29  return QObject::tr( "Extract/clip by extent" );
30 }
31 
32 QStringList QgsExtractByExtentAlgorithm::tags() const
33 {
34  return QObject::tr( "clip,extract,intersect,intersection,mask,extent" ).split( ',' );
35 }
36 
37 QString QgsExtractByExtentAlgorithm::group() const
38 {
39  return QObject::tr( "Vector overlay" );
40 }
41 
42 QString QgsExtractByExtentAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectoroverlay" );
45 }
46 void 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 
54 QString 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 
62 QgsExtractByExtentAlgorithm *QgsExtractByExtentAlgorithm::createInstance() const
63 {
64  return new QgsExtractByExtentAlgorithm();
65 }
66 
67 QVariantMap QgsExtractByExtentAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
68 {
69  std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
70  if ( !featureSource )
71  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
72 
73  QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, featureSource->sourceCrs() );
74  bool clip = parameterAsBoolean( parameters, QStringLiteral( "CLIP" ), context );
75 
76  // if clipping, we force multi output
77  QgsWkbTypes::Type outType = clip ? QgsWkbTypes::multiType( featureSource->wkbType() ) : featureSource->wkbType();
78 
79  QString dest;
80  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, featureSource->fields(), outType, featureSource->sourceCrs() ) );
81 
82  if ( !sink )
83  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
84 
85  QgsGeometry clipGeom = parameterAsExtentGeometry( parameters, QStringLiteral( "EXTENT" ), context, featureSource->sourceCrs() );
86 
87  double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
88  QgsFeatureIterator inputIt = featureSource->getFeatures( QgsFeatureRequest().setFilterRect( extent ).setFlags( QgsFeatureRequest::ExactIntersect ) );
89  QgsFeature f;
90  int i = -1;
91  while ( inputIt.nextFeature( f ) )
92  {
93  i++;
94  if ( feedback->isCanceled() )
95  {
96  break;
97  }
98 
99  if ( clip )
100  {
101  QgsGeometry g = f.geometry().intersection( clipGeom );
102  g.convertToMultiType();
103  f.setGeometry( g );
104  }
105 
106  sink->addFeature( f, QgsFeatureSink::FastInsert );
107  feedback->setProgress( i * step );
108  }
109 
110  QVariantMap outputs;
111  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
112  return outputs;
113 }
114 
116 
QgsWkbTypes::multiType
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:301
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:75
QgsFeatureRequest::ExactIntersect
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
Definition: qgsfeaturerequest.h:109
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingParameterFeatureSource
Definition: qgsprocessingparameters.h:2612
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsRectangle
Definition: qgsrectangle.h:41
QgsGeometry::intersection
QgsGeometry intersection(const QgsGeometry &geometry) const
Returns a geometry representing the points shared by this geometry and other.
Definition: qgsgeometry.cpp:2395
QgsProcessingParameterFeatureSink
Definition: qgsprocessingparameters.h:2773
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
qgsalgorithmextractbyextent.h
QgsProcessingParameterExtent
Definition: qgsprocessingparameters.h:1502
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
QgsProcessingParameterBoolean
Definition: qgsprocessingparameters.h:1439
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
QgsGeometry
Definition: qgsgeometry.h:122
QgsGeometry::convertToMultiType
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
Definition: qgsgeometry.cpp:1468
QgsFeature
Definition: qgsfeature.h:55
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsProcessingException
Definition: qgsexception.h:82
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70