QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsvectorlayer.h"
20 
22 
23 QString QgsMinimumEnclosingCircleAlgorithm::name() const
24 {
25  return QStringLiteral( "minimumenclosingcircle" );
26 }
27 
28 QString QgsMinimumEnclosingCircleAlgorithm::displayName() const
29 {
30  return QObject::tr( "Minimum enclosing circles" );
31 }
32 
33 QStringList QgsMinimumEnclosingCircleAlgorithm::tags() const
34 {
35  return QObject::tr( "minimum,circle,ellipse,extent,bounds,bounding" ).split( ',' );
36 }
37 
38 QString QgsMinimumEnclosingCircleAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsMinimumEnclosingCircleAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsMinimumEnclosingCircleAlgorithm::outputName() const
49 {
50  return QObject::tr( "Minimum enclosing circles" );
51 }
52 
53 QgsWkbTypes::Type QgsMinimumEnclosingCircleAlgorithm::outputWkbType( QgsWkbTypes::Type ) const
54 {
55  return QgsWkbTypes::Type::Polygon;
56 }
57 
58 void QgsMinimumEnclosingCircleAlgorithm::initParameters( const QVariantMap & )
59 {
60  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SEGMENTS" ), QObject::tr( "Number of segments in circles" ), QgsProcessingParameterNumber::Integer,
61  72, false, 8, 100000 ) );
62 }
63 
64 QString QgsMinimumEnclosingCircleAlgorithm::shortHelpString() const
65 {
66  return QObject::tr( "This algorithm calculates the minimum enclosing circle which covers each feature in an input layer." ) +
67  QStringLiteral( "\n\n" ) +
68  QObject::tr( "See the 'Minimum bounding geometry' algorithm for a minimal enclosing circle calculation which covers the whole layer or grouped subsets of features." );
69 }
70 
71 QgsMinimumEnclosingCircleAlgorithm *QgsMinimumEnclosingCircleAlgorithm::createInstance() const
72 {
73  return new QgsMinimumEnclosingCircleAlgorithm();
74 }
75 
76 bool QgsMinimumEnclosingCircleAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
77 {
78  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
79  if ( !layer )
80  return false;
81 
83  return false;
84  // (no Z no M)
85  return !( QgsWkbTypes::hasM( layer->wkbType() ) || QgsWkbTypes::hasZ( layer->wkbType() ) );
86 }
87 
88 QgsFields QgsMinimumEnclosingCircleAlgorithm::outputFields( const QgsFields &inputFields ) const
89 {
90  QgsFields fields = inputFields;
91  fields.append( QgsField( QStringLiteral( "radius" ), QVariant::Double, QString(), 20, 6 ) );
92  fields.append( QgsField( QStringLiteral( "area" ), QVariant::Double, QString(), 20, 6 ) );
93  return fields;
94 }
95 
96 bool QgsMinimumEnclosingCircleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
97 {
98  mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
99  return true;
100 }
101 
102 QgsFeatureList QgsMinimumEnclosingCircleAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
103 {
104  QgsFeature f = feature;
105  if ( f.hasGeometry() )
106  {
107  double radius = 0;
108  QgsPointXY center;
109  const QgsGeometry outputGeometry = f.geometry().minimalEnclosingCircle( center, radius, mSegments );
110  f.setGeometry( outputGeometry );
111  QgsAttributes attrs = f.attributes();
112  attrs << radius
113  << M_PI *radius *radius;
114  f.setAttributes( attrs );
115  }
116  else
117  {
118  QgsAttributes attrs = f.attributes();
119  attrs << QVariant()
120  << QVariant();
121  f.setAttributes( attrs );
122  }
123  return QgsFeatureList() << f;
124 }
125 
127 
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:725
QgsProcessingParameterNumber
A numeric parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2179
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsGeometry::minimalEnclosingCircle
QgsGeometry minimalEnclosingCircle(QgsPointXY &center, double &radius, unsigned int segments=36) const
Returns the minimal enclosing circle for the geometry.
Definition: qgsgeometry.cpp:1153
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
Definition: qgsprocessingalgorithm.cpp:1139
QgsFields::append
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
qgsalgorithmminimumenclosingcircle.h
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsWkbTypes::hasM
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1130
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:2186
qgsvectorlayer.h
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsWkbTypes::hasZ
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1080
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50