QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmboundingbox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmboundingbox.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 QgsBoundingBoxAlgorithm::name() const
23 {
24  return QStringLiteral( "boundingboxes" );
25 }
26 
27 QString QgsBoundingBoxAlgorithm::displayName() const
28 {
29  return QObject::tr( "Bounding boxes" );
30 }
31 
32 QStringList QgsBoundingBoxAlgorithm::tags() const
33 {
34  return QObject::tr( "bounding,boxes,envelope,rectangle,extent" ).split( ',' );
35 }
36 
37 QString QgsBoundingBoxAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsBoundingBoxAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsBoundingBoxAlgorithm::outputName() const
48 {
49  return QObject::tr( "Bounds" );
50 }
51 
52 QString QgsBoundingBoxAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm calculates the bounding box (envelope) for each feature in an input layer." ) +
55  QStringLiteral( "\n\n" ) +
56  QObject::tr( "See the 'Minimum bounding geometry' algorithm for a bounding box calculation which covers the whole layer or grouped subsets of features." );
57 }
58 
59 QgsBoundingBoxAlgorithm *QgsBoundingBoxAlgorithm::createInstance() const
60 {
61  return new QgsBoundingBoxAlgorithm();
62 }
63 
64 QgsFields QgsBoundingBoxAlgorithm::outputFields( const QgsFields &inputFields ) const
65 {
66  QgsFields fields = inputFields;
67  fields.append( QgsField( QStringLiteral( "width" ), QVariant::Double, QString(), 20, 6 ) );
68  fields.append( QgsField( QStringLiteral( "height" ), QVariant::Double, QString(), 20, 6 ) );
69  fields.append( QgsField( QStringLiteral( "area" ), QVariant::Double, QString(), 20, 6 ) );
70  fields.append( QgsField( QStringLiteral( "perimeter" ), QVariant::Double, QString(), 20, 6 ) );
71  return fields;
72 }
73 
74 QgsFeatureList QgsBoundingBoxAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
75 {
76  QgsFeature f = feature;
77  if ( f.hasGeometry() )
78  {
79  const QgsRectangle bounds = f.geometry().boundingBox();
80  const QgsGeometry outputGeometry = QgsGeometry::fromRect( bounds );
81  f.setGeometry( outputGeometry );
82  QgsAttributes attrs = f.attributes();
83  attrs << bounds.width()
84  << bounds.height()
85  << bounds.area()
86  << bounds.perimeter();
87  f.setAttributes( attrs );
88  }
89  else
90  {
91  QgsAttributes attrs = f.attributes();
92  attrs << QVariant()
93  << QVariant()
94  << QVariant()
95  << QVariant();
96  f.setAttributes( attrs );
97  }
98  return QgsFeatureList() << f;
99 }
100 
QgsRectangle::perimeter
double perimeter() const SIP_HOLDGIL
Returns the perimeter of the rectangle.
Definition: qgsrectangle.h:246
QgsRectangle::height
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:230
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsRectangle::area
double area() const SIP_HOLDGIL
Returns the area of the rectangle.
Definition: qgsrectangle.h:239
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsRectangle::width
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
QgsGeometry::fromRect
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
Definition: qgsgeometry.cpp:241
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:1080
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgsalgorithmboundingbox.h
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50