QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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." )
59 + u"\n\n"_s
60 + QObject::tr( "See the 'Minimum bounding geometry' algorithm for a bounding box calculation which covers the whole layer or grouped subsets of features." );
61}
62
63QString QgsBoundingBoxAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Calculates the bounding box (envelope) for each feature in an input layer." );
66}
67
68QgsBoundingBoxAlgorithm *QgsBoundingBoxAlgorithm::createInstance() const
69{
70 return new QgsBoundingBoxAlgorithm();
71}
72
73QgsFields QgsBoundingBoxAlgorithm::outputFields( const QgsFields &inputFields ) const
74{
75 QgsFields newFields;
76 newFields.append( QgsField( u"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
77 newFields.append( QgsField( u"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
78 newFields.append( QgsField( u"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
79 newFields.append( QgsField( u"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
80 return QgsProcessingUtils::combineFields( inputFields, newFields );
81}
82
83QgsFeatureList QgsBoundingBoxAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
84{
85 QgsFeature f = feature;
86 if ( f.hasGeometry() )
87 {
88 const QgsRectangle bounds = f.geometry().boundingBox();
89 const QgsGeometry outputGeometry = QgsGeometry::fromRect( bounds );
90 f.setGeometry( outputGeometry );
91 QgsAttributes attrs = f.attributes();
92 attrs << bounds.width() << bounds.height() << bounds.area() << bounds.perimeter();
93 f.setAttributes( attrs );
94 }
95 else
96 {
97 QgsAttributes attrs = f.attributes();
98 attrs << QVariant() << QVariant() << QVariant() << 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: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:75
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