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