QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #include "qgsvectorlayer.h"
20 
22 
23 QString QgsOrientedMinimumBoundingBoxAlgorithm::name() const
24 {
25  return QStringLiteral( "orientedminimumboundingbox" );
26 }
27 
28 QString QgsOrientedMinimumBoundingBoxAlgorithm::displayName() const
29 {
30  return QObject::tr( "Oriented minimum bounding box" );
31 }
32 
33 QStringList QgsOrientedMinimumBoundingBoxAlgorithm::tags() const
34 {
35  return QObject::tr( "bounding,boxes,envelope,rectangle,extent,oriented,angle" ).split( ',' );
36 }
37 
38 QString QgsOrientedMinimumBoundingBoxAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsOrientedMinimumBoundingBoxAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsOrientedMinimumBoundingBoxAlgorithm::outputName() const
49 {
50  return QObject::tr( "Bounding boxes" );
51 }
52 
53 QgsWkbTypes::Type QgsOrientedMinimumBoundingBoxAlgorithm::outputWkbType( QgsWkbTypes::Type ) const
54 {
55  return QgsWkbTypes::Polygon;
56 }
57 
58 QString QgsOrientedMinimumBoundingBoxAlgorithm::shortHelpString() const
59 {
60  return QObject::tr( "This algorithm calculates the minimum area rotated rectangle which covers each feature in an input layer." ) +
61  QStringLiteral( "\n\n" ) +
62  QObject::tr( "See the 'Minimum bounding geometry' algorithm for a oriented bounding box calculation which covers the whole layer or grouped subsets of features." );
63 }
64 
65 QgsOrientedMinimumBoundingBoxAlgorithm *QgsOrientedMinimumBoundingBoxAlgorithm::createInstance() const
66 {
67  return new QgsOrientedMinimumBoundingBoxAlgorithm();
68 }
69 
70 bool QgsOrientedMinimumBoundingBoxAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
71 {
72  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
73  if ( !layer )
74  return false;
75 
77  return false;
78  // Polygons only
79  return layer->wkbType() == QgsWkbTypes::Type::Polygon || layer->wkbType() == QgsWkbTypes::Type::MultiPolygon;
80 }
81 
82 QgsFields QgsOrientedMinimumBoundingBoxAlgorithm::outputFields( const QgsFields &inputFields ) const
83 {
84  QgsFields fields = inputFields;
85  fields.append( QgsField( QStringLiteral( "width" ), QVariant::Double, QString(), 20, 6 ) );
86  fields.append( QgsField( QStringLiteral( "height" ), QVariant::Double, QString(), 20, 6 ) );
87  fields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double, QString(), 20, 6 ) );
88  fields.append( QgsField( QStringLiteral( "area" ), QVariant::Double, QString(), 20, 6 ) );
89  fields.append( QgsField( QStringLiteral( "perimeter" ), QVariant::Double, QString(), 20, 6 ) );
90  return fields;
91 }
92 
93 QgsFeatureList QgsOrientedMinimumBoundingBoxAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
94 {
95  QgsFeature f = feature;
96  if ( f.hasGeometry() )
97  {
98  double area = 0;
99  double angle = 0;
100  double width = 0;
101  double height = 0;
102  QgsGeometry outputGeometry = f.geometry().orientedMinimumBoundingBox( area, angle, width, height );
103  f.setGeometry( outputGeometry );
104  QgsAttributes attrs = f.attributes();
105  attrs << width
106  << height
107  << angle
108  << area
109  << 2 * width + 2 * height;
110  f.setAttributes( attrs );
111  }
112  else
113  {
114  QgsAttributes attrs = f.attributes();
115  attrs << QVariant()
116  << QVariant()
117  << QVariant()
118  << QVariant()
119  << QVariant();
120  f.setAttributes( attrs );
121  }
122  return QgsFeatureList() << f;
123 }
124 
126 
127 
Base class for all map layer types.
Definition: qgsmaplayer.h:78
Base class for providing feedback from a processing algorithm.
QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
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:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
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:68
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
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
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...
QgsGeometry geometry
Definition: qgsfeature.h:67
A vector of attributes.
Definition: qgsattributes.h:57
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
QgsAttributes attributes
Definition: qgsfeature.h:65