QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsBoundingBoxAlgorithm::name() const
27{
28 return u"boundingboxes"_s;
29}
30
31QString QgsBoundingBoxAlgorithm::displayName() const
32{
33 return QObject::tr( "Bounding boxes" );
34}
35
36QStringList QgsBoundingBoxAlgorithm::tags() const
37{
38 return QObject::tr( "bounding,boxes,envelope,rectangle,extent" ).split( ',' );
39}
40
41QString QgsBoundingBoxAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsBoundingBoxAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsBoundingBoxAlgorithm::outputName() const
52{
53 return QObject::tr( "Bounds" );
54}
55
56QString QgsBoundingBoxAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm calculates the bounding box (envelope) for each feature in an input layer." ) + u"\n\n"_s + QObject::tr( "See the 'Minimum bounding geometry' algorithm for a bounding box calculation which covers the whole layer or grouped subsets of features." );
59}
60
61QString QgsBoundingBoxAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Calculates the bounding box (envelope) for each feature in an input layer." );
64}
65
66QgsBoundingBoxAlgorithm *QgsBoundingBoxAlgorithm::createInstance() const
67{
68 return new QgsBoundingBoxAlgorithm();
69}
70
71QgsFields QgsBoundingBoxAlgorithm::outputFields( const QgsFields &inputFields ) const
72{
73 QgsFields newFields;
74 newFields.append( QgsField( u"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
75 newFields.append( QgsField( u"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
76 newFields.append( QgsField( u"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
77 newFields.append( QgsField( u"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
78 return QgsProcessingUtils::combineFields( inputFields, newFields );
79}
80
81QgsFeatureList QgsBoundingBoxAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
82{
83 QgsFeature f = feature;
84 if ( f.hasGeometry() )
85 {
86 const QgsRectangle bounds = f.geometry().boundingBox();
87 const QgsGeometry outputGeometry = QgsGeometry::fromRect( bounds );
88 f.setGeometry( outputGeometry );
89 QgsAttributes attrs = f.attributes();
90 attrs << bounds.width()
91 << bounds.height()
92 << bounds.area()
93 << bounds.perimeter();
94 f.setAttributes( attrs );
95 }
96 else
97 {
98 QgsAttributes attrs = f.attributes();
99 attrs << QVariant()
100 << QVariant()
101 << QVariant()
102 << QVariant();
103 f.setAttributes( attrs );
104 }
105 return QgsFeatureList() << f;
106}
107
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:76
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
A rectangle specified with double values.
double perimeter
QList< QgsFeature > QgsFeatureList