QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
23QString QgsMinimumEnclosingCircleAlgorithm::name() const
24{
25 return QStringLiteral( "minimumenclosingcircle" );
26}
27
28QString QgsMinimumEnclosingCircleAlgorithm::displayName() const
29{
30 return QObject::tr( "Minimum enclosing circles" );
31}
32
33QStringList QgsMinimumEnclosingCircleAlgorithm::tags() const
34{
35 return QObject::tr( "minimum,circle,ellipse,extent,bounds,bounding" ).split( ',' );
36}
37
38QString QgsMinimumEnclosingCircleAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsMinimumEnclosingCircleAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsMinimumEnclosingCircleAlgorithm::outputName() const
49{
50 return QObject::tr( "Minimum enclosing circles" );
51}
52
53Qgis::WkbType QgsMinimumEnclosingCircleAlgorithm::outputWkbType( Qgis::WkbType ) const
54{
56}
57
58void QgsMinimumEnclosingCircleAlgorithm::initParameters( const QVariantMap & )
59{
60 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SEGMENTS" ), QObject::tr( "Number of segments in circles" ), Qgis::ProcessingNumberParameterType::Integer, 72, false, 8, 100000 ) );
61}
62
63QString QgsMinimumEnclosingCircleAlgorithm::shortHelpString() const
64{
65 return QObject::tr( "This algorithm calculates the minimum enclosing circle which covers each feature in an input layer." ) + QStringLiteral( "\n\n" ) + QObject::tr( "See the 'Minimum bounding geometry' algorithm for a minimal enclosing circle calculation which covers the whole layer or grouped subsets of features." );
66}
67
68QgsMinimumEnclosingCircleAlgorithm *QgsMinimumEnclosingCircleAlgorithm::createInstance() const
69{
70 return new QgsMinimumEnclosingCircleAlgorithm();
71}
72
73bool QgsMinimumEnclosingCircleAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
74{
75 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
76 if ( !layer )
77 return false;
78
80 return false;
81 // (no Z no M)
82 return !( QgsWkbTypes::hasM( layer->wkbType() ) || QgsWkbTypes::hasZ( layer->wkbType() ) );
83}
84
85QgsFields QgsMinimumEnclosingCircleAlgorithm::outputFields( const QgsFields &inputFields ) const
86{
87 QgsFields fields = inputFields;
88 fields.append( QgsField( QStringLiteral( "radius" ), QMetaType::Type::Double, QString(), 20, 6 ) );
89 fields.append( QgsField( QStringLiteral( "area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
90 return fields;
91}
92
93bool QgsMinimumEnclosingCircleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
94{
95 mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
96 return true;
97}
98
99QgsFeatureList QgsMinimumEnclosingCircleAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
100{
101 QgsFeature f = feature;
102 if ( f.hasGeometry() )
103 {
104 double radius = 0;
105 QgsPointXY center;
106 const QgsGeometry outputGeometry = f.geometry().minimalEnclosingCircle( center, radius, mSegments );
107 f.setGeometry( outputGeometry );
108 QgsAttributes attrs = f.attributes();
109 attrs << radius
110 << M_PI * radius * radius;
111 f.setAttributes( attrs );
112 }
113 else
114 {
115 QgsAttributes attrs = f.attributes();
116 attrs << QVariant()
117 << QVariant();
118 f.setAttributes( attrs );
119 }
120 return QgsFeatureList() << f;
121}
122
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ Polygon
Polygon.
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:53
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:70
A geometry is the spatial representation of a feature.
QgsGeometry minimalEnclosingCircle(QgsPointXY &center, double &radius, unsigned int segments=36) const
Returns the minimal enclosing circle for the geometry.
Base class for all map layer types.
Definition qgsmaplayer.h:76
A class to represent a 2D point.
Definition qgspointxy.h:60
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.
A numeric parameter for processing algorithms.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
QList< QgsFeature > QgsFeatureList