QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
20#include "qgsvectorlayer.h"
21
23
24QString QgsOrientedMinimumBoundingBoxAlgorithm::name() const
25{
26 return QStringLiteral( "orientedminimumboundingbox" );
27}
28
29QString QgsOrientedMinimumBoundingBoxAlgorithm::displayName() const
30{
31 return QObject::tr( "Oriented minimum bounding box" );
32}
33
34QStringList QgsOrientedMinimumBoundingBoxAlgorithm::tags() const
35{
36 return QObject::tr( "bounding,boxes,envelope,rectangle,extent,oriented,angle" ).split( ',' );
37}
38
39QString QgsOrientedMinimumBoundingBoxAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsOrientedMinimumBoundingBoxAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsOrientedMinimumBoundingBoxAlgorithm::outputName() const
50{
51 return QObject::tr( "Bounding boxes" );
52}
53
54Qgis::WkbType QgsOrientedMinimumBoundingBoxAlgorithm::outputWkbType( Qgis::WkbType ) const
55{
57}
58
59QString QgsOrientedMinimumBoundingBoxAlgorithm::shortHelpString() const
60{
61 return QObject::tr( "This algorithm calculates the minimum area rotated rectangle which covers each feature in an input layer." ) + QStringLiteral( "\n\n" ) + QObject::tr( "For singlepart point features, the output corresponds to the bounding box of the geometry." ) + QStringLiteral( "\n\n" ) + QObject::tr( "See the 'Minimum bounding geometry' algorithm for an oriented bounding box calculation which covers the whole layer or grouped subsets of features." );
62}
63
64QString QgsOrientedMinimumBoundingBoxAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Calculates the minimum area rotated rectangle which covers each feature in an input layer." );
67}
68
69QgsOrientedMinimumBoundingBoxAlgorithm *QgsOrientedMinimumBoundingBoxAlgorithm::createInstance() const
70{
71 return new QgsOrientedMinimumBoundingBoxAlgorithm();
72}
73
74bool QgsOrientedMinimumBoundingBoxAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
75{
76 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
77 if ( !layer )
78 return false;
79
81 return false;
82 // Polygons only
83 return layer->wkbType() == Qgis::WkbType::Polygon || layer->wkbType() == Qgis::WkbType::MultiPolygon;
84}
85
86QgsFields QgsOrientedMinimumBoundingBoxAlgorithm::outputFields( const QgsFields &inputFields ) const
87{
88 QgsFields newFields;
89 newFields.append( QgsField( QStringLiteral( "width" ), QMetaType::Type::Double, QString(), 20, 6 ) );
90 newFields.append( QgsField( QStringLiteral( "height" ), QMetaType::Type::Double, QString(), 20, 6 ) );
91 newFields.append( QgsField( QStringLiteral( "angle" ), QMetaType::Type::Double, QString(), 20, 6 ) );
92 newFields.append( QgsField( QStringLiteral( "area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
93 newFields.append( QgsField( QStringLiteral( "perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
94 return QgsProcessingUtils::combineFields( inputFields, newFields );
95}
96
97QgsFeatureList QgsOrientedMinimumBoundingBoxAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
98{
99 QgsFeature f = feature;
100 if ( f.hasGeometry() )
101 {
102 double area = 0;
103 double angle = 0;
104 double width = 0;
105 double height = 0;
106 const QgsGeometry outputGeometry = f.geometry().orientedMinimumBoundingBox( area, angle, width, height );
107 f.setGeometry( outputGeometry );
108 QgsAttributes attrs = f.attributes();
109 attrs << width
110 << height
111 << angle
112 << area
113 << 2 * width + 2 * height;
114 f.setAttributes( attrs );
115 }
116 else
117 {
118 QgsAttributes attrs = f.attributes();
119 attrs << QVariant()
120 << QVariant()
121 << QVariant()
122 << QVariant()
123 << QVariant();
124 f.setAttributes( attrs );
125 }
126 return QgsFeatureList() << f;
127}
128
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
@ Polygon
Polygon.
Definition qgis.h:281
@ MultiPolygon
MultiPolygon.
Definition qgis.h:285
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:54
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:73
A geometry is the spatial representation of a feature.
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...
Base class for all map layer types.
Definition qgsmaplayer.h:80
Contains information about the context in which a processing algorithm is executed.
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
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).
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
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).
QList< QgsFeature > QgsFeatureList