QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmorientedminimumboundingbox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmorientedminimumboundingbox.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 QgsOrientedMinimumBoundingBoxAlgorithm::name() const
23 {
24  return QStringLiteral( "orientedminimumboundingbox" );
25 }
26 
27 QString QgsOrientedMinimumBoundingBoxAlgorithm::displayName() const
28 {
29  return QObject::tr( "Oriented minimum bounding box" );
30 }
31 
32 QStringList QgsOrientedMinimumBoundingBoxAlgorithm::tags() const
33 {
34  return QObject::tr( "bounding,boxes,envelope,rectangle,extent,oriented,angle" ).split( ',' );
35 }
36 
37 QString QgsOrientedMinimumBoundingBoxAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsOrientedMinimumBoundingBoxAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsOrientedMinimumBoundingBoxAlgorithm::outputName() const
48 {
49  return QObject::tr( "Bounding boxes" );
50 }
51 
52 QgsWkbTypes::Type QgsOrientedMinimumBoundingBoxAlgorithm::outputWkbType( QgsWkbTypes::Type ) const
53 {
54  return QgsWkbTypes::Polygon;
55 }
56 
57 QString QgsOrientedMinimumBoundingBoxAlgorithm::shortHelpString() const
58 {
59  return QObject::tr( "This algorithm calculates the minimum area rotated rectangle which covers each feature in an input layer." ) +
60  QStringLiteral( "\n\n" ) +
61  QObject::tr( "See the 'Minimum bounding geometry' algorithm for a oriented bounding box calculation which covers the whole layer or grouped subsets of features." );
62 }
63 
64 QgsOrientedMinimumBoundingBoxAlgorithm *QgsOrientedMinimumBoundingBoxAlgorithm::createInstance() const
65 {
66  return new QgsOrientedMinimumBoundingBoxAlgorithm();
67 }
68 
69 QgsFields QgsOrientedMinimumBoundingBoxAlgorithm::outputFields( const QgsFields &inputFields ) const
70 {
71  QgsFields fields = inputFields;
72  fields.append( QgsField( QStringLiteral( "width" ), QVariant::Double, QString(), 20, 6 ) );
73  fields.append( QgsField( QStringLiteral( "height" ), QVariant::Double, QString(), 20, 6 ) );
74  fields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double, QString(), 20, 6 ) );
75  fields.append( QgsField( QStringLiteral( "area" ), QVariant::Double, QString(), 20, 6 ) );
76  fields.append( QgsField( QStringLiteral( "perimeter" ), QVariant::Double, QString(), 20, 6 ) );
77  return fields;
78 }
79 
80 QgsFeatureList QgsOrientedMinimumBoundingBoxAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
81 {
82  QgsFeature f = feature;
83  if ( f.hasGeometry() )
84  {
85  double area = 0;
86  double angle = 0;
87  double width = 0;
88  double height = 0;
89  QgsGeometry outputGeometry = f.geometry().orientedMinimumBoundingBox( area, angle, width, height );
90  f.setGeometry( outputGeometry );
91  QgsAttributes attrs = f.attributes();
92  attrs << width
93  << height
94  << angle
95  << area
96  << 2 * width + 2 * height;
97  f.setAttributes( attrs );
98  }
99  else
100  {
101  QgsAttributes attrs = f.attributes();
102  attrs << QVariant()
103  << QVariant()
104  << QVariant()
105  << QVariant()
106  << QVariant();
107  f.setAttributes( attrs );
108  }
109  return QgsFeatureList() << f;
110 }
111 
113 
114 
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
QgsGeometry orientedMinimumBoundingBox(double &area, double &angle, double &width, double &height) const
Returns the oriented minimum bounding box for the geometry, which is the smallest (by area) rotated r...
A vector of attributes.
Definition: qgsattributes.h:58
Contains information about the context in which a processing algorithm is executed.
QgsAttributes attributes
Definition: qgsfeature.h:72