QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmpoleofinaccessibility.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmpoleofinaccessibility.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy 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 "qgsapplication.h"
20
22
23QString QgsPoleOfInaccessibilityAlgorithm::name() const
24{
25 return QStringLiteral( "poleofinaccessibility" );
26}
27
28QString QgsPoleOfInaccessibilityAlgorithm::displayName() const
29{
30 return QObject::tr( "Pole of inaccessibility" );
31}
32
33QStringList QgsPoleOfInaccessibilityAlgorithm::tags() const
34{
35 return QObject::tr( "furthest,point,distant,extreme,maximum,centroid,center,centre" ).split( ',' );
36}
37
38QString QgsPoleOfInaccessibilityAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsPoleOfInaccessibilityAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsPoleOfInaccessibilityAlgorithm::shortHelpString() const
49{
50 return QObject::tr( "This algorithm calculates the pole of inaccessibility for a polygon layer, which is the most "
51 "distant internal point from the boundary of the surface. This algorithm uses the 'polylabel' "
52 "algorithm (Vladimir Agafonkin, 2016), which is an iterative approach guaranteed to find the "
53 "true pole of inaccessibility within a specified tolerance (in layer units). More precise "
54 "tolerances require more iterations and will take longer to calculate." )
55 + QStringLiteral( "\n\n" )
56 + QObject::tr( "The distance from the calculated pole to the polygon boundary will be stored as a new "
57 "attribute in the output layer." );
58}
59
60QString QgsPoleOfInaccessibilityAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Creates a point layer with features representing the most "
63 "distant internal point from the boundary of the surface for a polygon layer." );
64}
65
66QString QgsPoleOfInaccessibilityAlgorithm::svgIconPath() const
67{
68 return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmCentroids.svg" ) );
69}
70
71QIcon QgsPoleOfInaccessibilityAlgorithm::icon() const
72{
73 return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmCentroids.svg" ) );
74}
75
76QString QgsPoleOfInaccessibilityAlgorithm::outputName() const
77{
78 return QObject::tr( "Point" );
79}
80
81QList<int> QgsPoleOfInaccessibilityAlgorithm::inputLayerTypes() const
82{
83 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
84}
85
86Qgis::ProcessingSourceType QgsPoleOfInaccessibilityAlgorithm::outputLayerType() const
87{
89}
90
91Qgis::WkbType QgsPoleOfInaccessibilityAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
92{
93 Q_UNUSED( inputWkbType );
94
96}
97
98QgsFields QgsPoleOfInaccessibilityAlgorithm::outputFields( const QgsFields &inputFields ) const
99{
100 QgsFields newFields;
101 newFields.append( QgsField( QStringLiteral( "dist_pole" ), QMetaType::Type::Double ) );
102
103 return QgsProcessingUtils::combineFields( inputFields, newFields );
104}
105
106QgsPoleOfInaccessibilityAlgorithm *QgsPoleOfInaccessibilityAlgorithm::createInstance() const
107{
108 return new QgsPoleOfInaccessibilityAlgorithm();
109}
110
111void QgsPoleOfInaccessibilityAlgorithm::initParameters( const QVariantMap & )
112{
113 auto toleranceParam = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "TOLERANCE" ), QObject::tr( "Tolerance" ), 1.0, QStringLiteral( "INPUT" ), 0.0 );
114 toleranceParam->setIsDynamic( true );
115 toleranceParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Tolerance" ), QObject::tr( "Tolerance" ), QgsPropertyDefinition::Double ) );
116 toleranceParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
117 addParameter( toleranceParam.release() );
118}
119
120bool QgsPoleOfInaccessibilityAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
121{
122 mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
123 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "TOLERANCE" ) );
124 if ( mDynamicTolerance )
125 mToleranceProperty = parameters.value( QStringLiteral( "TOLERANCE" ) ).value<QgsProperty>();
126
127 return true;
128}
129
130QgsFeatureList QgsPoleOfInaccessibilityAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
131{
132 QgsFeature outFeature = feature;
133 if ( outFeature.hasGeometry() )
134 {
135 double tolerance = mTolerance;
136 if ( mDynamicTolerance )
137 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
138
139 double distance;
140 const QgsGeometry outputGeom = outFeature.geometry().poleOfInaccessibility( tolerance, &distance );
141 if ( outputGeom.isNull() )
142 {
143 throw QgsProcessingException( QObject::tr( "Error calculating pole of inaccessibility" ) );
144 }
145 QgsAttributes attrs = outFeature.attributes();
146 attrs.append( distance );
147 outFeature.setAttributes( attrs );
148 outFeature.setGeometry( outputGeom );
149 }
150 else
151 {
152 QgsAttributes attrs = outFeature.attributes();
153 attrs.append( QVariant() );
154 outFeature.setAttributes( attrs );
155 }
156
157 return QgsFeatureList() << outFeature;
158}
159
ProcessingSourceType
Processing data source types.
Definition qgis.h:3399
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
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 poleOfInaccessibility(double precision, double *distanceToBoundary=nullptr) const
Calculates the approximate pole of inaccessibility for a surface, which is the most distant internal ...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
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).
Definition for a property.
Definition qgsproperty.h:45
@ Double
Double value (including negative values)
Definition qgsproperty.h:55
A store for object properties.
QList< QgsFeature > QgsFeatureList