QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmminimumenclosingcircle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmminimumenclosingcircle.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 QgsMinimumEnclosingCircleAlgorithm::name() const
23 {
24  return QStringLiteral( "minimumenclosingcircle" );
25 }
26 
27 QString QgsMinimumEnclosingCircleAlgorithm::displayName() const
28 {
29  return QObject::tr( "Minimum enclosing circles" );
30 }
31 
32 QStringList QgsMinimumEnclosingCircleAlgorithm::tags() const
33 {
34  return QObject::tr( "minimum,circle,ellipse,extent,bounds,bounding" ).split( ',' );
35 }
36 
37 QString QgsMinimumEnclosingCircleAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsMinimumEnclosingCircleAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsMinimumEnclosingCircleAlgorithm::outputName() const
48 {
49  return QObject::tr( "Minimum enclosing circles" );
50 }
51 
52 QgsWkbTypes::Type QgsMinimumEnclosingCircleAlgorithm::outputWkbType( QgsWkbTypes::Type ) const
53 {
54  return QgsWkbTypes::Polygon;
55 }
56 
57 void QgsMinimumEnclosingCircleAlgorithm::initParameters( const QVariantMap & )
58 {
59  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SEGMENTS" ), QObject::tr( "Number of segments in circles" ), QgsProcessingParameterNumber::Integer,
60  72, false, 8, 100000 ) );
61 }
62 
63 QString QgsMinimumEnclosingCircleAlgorithm::shortHelpString() const
64 {
65  return QObject::tr( "This algorithm calculates the minimum enclosing circle which covers each feature in an input layer." ) +
66  QStringLiteral( "\n\n" ) +
67  QObject::tr( "See the 'Minimum bounding geometry' algorithm for a minimal enclosing circle calculation which covers the whole layer or grouped subsets of features." );
68 }
69 
70 QgsMinimumEnclosingCircleAlgorithm *QgsMinimumEnclosingCircleAlgorithm::createInstance() const
71 {
72  return new QgsMinimumEnclosingCircleAlgorithm();
73 }
74 
75 QgsFields QgsMinimumEnclosingCircleAlgorithm::outputFields( const QgsFields &inputFields ) const
76 {
77  QgsFields fields = inputFields;
78  fields.append( QgsField( QStringLiteral( "radius" ), QVariant::Double, QString(), 20, 6 ) );
79  fields.append( QgsField( QStringLiteral( "area" ), QVariant::Double, QString(), 20, 6 ) );
80  return fields;
81 }
82 
83 bool QgsMinimumEnclosingCircleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
84 {
85  mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
86  return true;
87 }
88 
89 QgsFeatureList QgsMinimumEnclosingCircleAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
90 {
91  QgsFeature f = feature;
92  if ( f.hasGeometry() )
93  {
94  double radius = 0;
95  QgsPointXY center;
96  QgsGeometry outputGeometry = f.geometry().minimalEnclosingCircle( center, radius, mSegments );
97  f.setGeometry( outputGeometry );
98  QgsAttributes attrs = f.attributes();
99  attrs << radius
100  << M_PI *radius *radius;
101  f.setAttributes( attrs );
102  }
103  else
104  {
105  QgsAttributes attrs = f.attributes();
106  attrs << QVariant()
107  << QVariant();
108  f.setAttributes( attrs );
109  }
110  return QgsFeatureList() << f;
111 }
112 
114 
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A class to represent a 2D point.
Definition: qgspointxy.h:43
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
QgsGeometry minimalEnclosingCircle(QgsPointXY &center, double &radius, unsigned int segments=36) const
Returns the minimal enclosing circle for the geometry.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
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
A numeric parameter for processing algorithms.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
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