QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
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
22QString QgsBoundingBoxAlgorithm::name() const
23{
24 return QStringLiteral( "boundingboxes" );
25}
26
27QString QgsBoundingBoxAlgorithm::displayName() const
28{
29 return QObject::tr( "Bounding boxes" );
30}
31
32QStringList QgsBoundingBoxAlgorithm::tags() const
33{
34 return QObject::tr( "bounding,boxes,envelope,rectangle,extent" ).split( ',' );
35}
36
37QString QgsBoundingBoxAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsBoundingBoxAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsBoundingBoxAlgorithm::outputName() const
48{
49 return QObject::tr( "Bounds" );
50}
51
52QString QgsBoundingBoxAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm calculates the bounding box (envelope) for each feature in an input layer." ) + QStringLiteral( "\n\n" ) + QObject::tr( "See the 'Minimum bounding geometry' algorithm for a bounding box calculation which covers the whole layer or grouped subsets of features." );
55}
56
57QString QgsBoundingBoxAlgorithm::shortDescription() const
58{
59 return QObject::tr( "Calculates the bounding box (envelope) for each feature in an input layer." );
60}
61
62QgsBoundingBoxAlgorithm *QgsBoundingBoxAlgorithm::createInstance() const
63{
64 return new QgsBoundingBoxAlgorithm();
65}
66
67QgsFields QgsBoundingBoxAlgorithm::outputFields( const QgsFields &inputFields ) const
68{
69 QgsFields newFields;
70 newFields.append( QgsField( QStringLiteral( "width" ), QMetaType::Type::Double, QString(), 20, 6 ) );
71 newFields.append( QgsField( QStringLiteral( "height" ), QMetaType::Type::Double, QString(), 20, 6 ) );
72 newFields.append( QgsField( QStringLiteral( "area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
73 newFields.append( QgsField( QStringLiteral( "perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
74 return QgsProcessingUtils::combineFields( inputFields, newFields );
75}
76
77QgsFeatureList QgsBoundingBoxAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
78{
79 QgsFeature f = feature;
80 if ( f.hasGeometry() )
81 {
82 const QgsRectangle bounds = f.geometry().boundingBox();
83 const QgsGeometry outputGeometry = QgsGeometry::fromRect( bounds );
84 f.setGeometry( outputGeometry );
85 QgsAttributes attrs = f.attributes();
86 attrs << bounds.width()
87 << bounds.height()
88 << bounds.area()
89 << bounds.perimeter();
90 f.setAttributes( attrs );
91 }
92 else
93 {
94 QgsAttributes attrs = f.attributes();
95 attrs << QVariant()
96 << QVariant()
97 << QVariant()
98 << QVariant();
99 f.setAttributes( attrs );
100 }
101 return QgsFeatureList() << f;
102}
103
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
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:53
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:70
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