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