QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
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
20#include "qgsapplication.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsPoleOfInaccessibilityAlgorithm::name() const
29{
30 return u"poleofinaccessibility"_s;
31}
32
33QString QgsPoleOfInaccessibilityAlgorithm::displayName() const
34{
35 return QObject::tr( "Pole of inaccessibility" );
36}
37
38QStringList QgsPoleOfInaccessibilityAlgorithm::tags() const
39{
40 return QObject::tr( "furthest,point,distant,extreme,maximum,centroid,center,centre" ).split( ',' );
41}
42
43QString QgsPoleOfInaccessibilityAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsPoleOfInaccessibilityAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsPoleOfInaccessibilityAlgorithm::shortHelpString() const
54{
55 return QObject::tr(
56 "This algorithm calculates the pole of inaccessibility for a polygon layer, which is the most "
57 "distant internal point from the boundary of the surface. This algorithm uses the 'polylabel' "
58 "algorithm (Vladimir Agafonkin, 2016), which is an iterative approach guaranteed to find the "
59 "true pole of inaccessibility within a specified tolerance (in layer units). More precise "
60 "tolerances require more iterations and will take longer to calculate."
61 )
62 + u"\n\n"_s
63 + QObject::tr(
64 "The distance from the calculated pole to the polygon boundary will be stored as a new "
65 "attribute in the output layer."
66 );
67}
68
69QString QgsPoleOfInaccessibilityAlgorithm::shortDescription() const
70{
71 return QObject::tr(
72 "Creates a point layer with features representing the most "
73 "distant internal point from the boundary of the surface for a polygon layer."
74 );
75}
76
77QString QgsPoleOfInaccessibilityAlgorithm::svgIconPath() const
78{
79 return QgsApplication::iconPath( u"/algorithms/mAlgorithmCentroids.svg"_s );
80}
81
82QIcon QgsPoleOfInaccessibilityAlgorithm::icon() const
83{
84 return QgsApplication::getThemeIcon( u"/algorithms/mAlgorithmCentroids.svg"_s );
85}
86
87QString QgsPoleOfInaccessibilityAlgorithm::outputName() const
88{
89 return QObject::tr( "Point" );
90}
91
92QList<int> QgsPoleOfInaccessibilityAlgorithm::inputLayerTypes() const
93{
94 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
95}
96
97Qgis::ProcessingSourceType QgsPoleOfInaccessibilityAlgorithm::outputLayerType() const
98{
100}
101
102Qgis::WkbType QgsPoleOfInaccessibilityAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
103{
104 Q_UNUSED( inputWkbType );
105
107}
108
109QgsFields QgsPoleOfInaccessibilityAlgorithm::outputFields( const QgsFields &inputFields ) const
110{
111 QgsFields newFields;
112 newFields.append( QgsField( u"dist_pole"_s, QMetaType::Type::Double ) );
113
114 return QgsProcessingUtils::combineFields( inputFields, newFields );
115}
116
117QgsPoleOfInaccessibilityAlgorithm *QgsPoleOfInaccessibilityAlgorithm::createInstance() const
118{
119 return new QgsPoleOfInaccessibilityAlgorithm();
120}
121
122void QgsPoleOfInaccessibilityAlgorithm::initParameters( const QVariantMap & )
123{
124 auto toleranceParam = std::make_unique<QgsProcessingParameterDistance>( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), 1.0, u"INPUT"_s, 0.0 );
125 toleranceParam->setIsDynamic( true );
126 toleranceParam->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Tolerance"_s, QObject::tr( "Tolerance" ), QgsPropertyDefinition::Double ) );
127 toleranceParam->setDynamicLayerParameterName( u"INPUT"_s );
128 addParameter( toleranceParam.release() );
129}
130
131bool QgsPoleOfInaccessibilityAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
132{
133 mTolerance = parameterAsDouble( parameters, u"TOLERANCE"_s, context );
134 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"TOLERANCE"_s );
135 if ( mDynamicTolerance )
136 mToleranceProperty = parameters.value( u"TOLERANCE"_s ).value<QgsProperty>();
137
138 return true;
139}
140
141QgsFeatureList QgsPoleOfInaccessibilityAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
142{
143 QgsFeature outFeature = feature;
144 if ( outFeature.hasGeometry() )
145 {
146 double tolerance = mTolerance;
147 if ( mDynamicTolerance )
148 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
149
150 double distance;
151 const QgsGeometry outputGeom = outFeature.geometry().poleOfInaccessibility( tolerance, &distance );
152 if ( outputGeom.isNull() )
153 {
154 throw QgsProcessingException( QObject::tr( "Error calculating pole of inaccessibility" ) );
155 }
156 QgsAttributes attrs = outFeature.attributes();
157 attrs.append( distance );
158 outFeature.setAttributes( attrs );
159 outFeature.setGeometry( outputGeom );
160 }
161 else
162 {
163 QgsAttributes attrs = outFeature.attributes();
164 attrs.append( QVariant() );
165 outFeature.setAttributes( attrs );
166 }
167
168 return QgsFeatureList() << outFeature;
169}
170
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorPoint
Vector point layers.
Definition qgis.h:3648
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Point
Point.
Definition qgis.h:296
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:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
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:56
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:75
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:47
@ Double
Double value (including negative values).
Definition qgsproperty.h:56
A store for object properties.
QList< QgsFeature > QgsFeatureList