QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
23 QString QgsPoleOfInaccessibilityAlgorithm::name() const
24 {
25  return QStringLiteral( "poleofinaccessibility" );
26 }
27 
28 QString QgsPoleOfInaccessibilityAlgorithm::displayName() const
29 {
30  return QObject::tr( "Pole of inaccessibility" );
31 }
32 
33 QStringList QgsPoleOfInaccessibilityAlgorithm::tags() const
34 {
35  return QObject::tr( "furthest,point,distant,extreme,maximum,centroid,center,centre" ).split( ',' );
36 }
37 
38 QString QgsPoleOfInaccessibilityAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsPoleOfInaccessibilityAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString 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 
60 QString QgsPoleOfInaccessibilityAlgorithm::svgIconPath() const
61 {
62  return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmCentroids.svg" ) );
63 }
64 
65 QIcon QgsPoleOfInaccessibilityAlgorithm::icon() const
66 {
67  return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmCentroids.svg" ) );
68 }
69 
70 QString QgsPoleOfInaccessibilityAlgorithm::outputName() const
71 {
72  return QObject::tr( "Point" );
73 }
74 
75 QList<int> QgsPoleOfInaccessibilityAlgorithm::inputLayerTypes() const
76 {
77  return QList<int>() << QgsProcessing::TypeVectorPolygon;
78 }
79 
80 QgsProcessing::SourceType QgsPoleOfInaccessibilityAlgorithm::outputLayerType() const
81 {
83 }
84 
85 QgsWkbTypes::Type QgsPoleOfInaccessibilityAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
86 {
87  Q_UNUSED( inputWkbType );
88 
89  return QgsWkbTypes::Point;
90 }
91 
92 QgsFields QgsPoleOfInaccessibilityAlgorithm::outputFields( const QgsFields &inputFields ) const
93 {
94  QgsFields outputFields = inputFields;
95  outputFields.append( QgsField( QStringLiteral( "dist_pole" ), QVariant::Double ) );
96 
97  return outputFields;
98 }
99 
100 QgsPoleOfInaccessibilityAlgorithm *QgsPoleOfInaccessibilityAlgorithm::createInstance() const
101 {
102  return new QgsPoleOfInaccessibilityAlgorithm();
103 }
104 
105 void QgsPoleOfInaccessibilityAlgorithm::initParameters( const QVariantMap & )
106 {
107  auto toleranceParam = qgis::make_unique < QgsProcessingParameterDistance >( QStringLiteral( "TOLERANCE" ), QObject::tr( "Tolerance" ), 1.0, QStringLiteral( "INPUT" ), 0.0 );
108  toleranceParam->setIsDynamic( true );
109  toleranceParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Tolerance" ), QObject::tr( "Tolerance" ), QgsPropertyDefinition::Double ) );
110  toleranceParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
111  addParameter( toleranceParam.release() );
112 }
113 
114 bool QgsPoleOfInaccessibilityAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
115 {
116  mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
117  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "TOLERANCE" ) );
118  if ( mDynamicTolerance )
119  mToleranceProperty = parameters.value( QStringLiteral( "TOLERANCE" ) ).value< QgsProperty >();
120 
121  return true;
122 }
123 
124 QgsFeatureList QgsPoleOfInaccessibilityAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
125 {
126  QgsFeature outFeature = feature;
127  if ( outFeature.hasGeometry() )
128  {
129  double tolerance = mTolerance;
130  if ( mDynamicTolerance )
131  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
132 
133  double distance;
134  QgsGeometry outputGeom = outFeature.geometry().poleOfInaccessibility( tolerance, &distance );
135  if ( outputGeom.isNull() )
136  {
137  throw QgsProcessingException( QObject::tr( "Error calculating pole of inaccessibility" ) );
138  }
139  QgsAttributes attrs = outFeature.attributes();
140  attrs.append( distance );
141  outFeature.setAttributes( attrs );
142  outFeature.setGeometry( outputGeom );
143  }
144  else
145  {
146  QgsAttributes attrs = outFeature.attributes();
147  attrs.append( QVariant() );
148  outFeature.setAttributes( attrs );
149  }
150 
151  return QgsFeatureList() << outFeature;
152 }
153 
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:605
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
QgsProcessingParameters::isDynamic
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...
Definition: qgsprocessingparameters.cpp:111
qgsalgorithmpoleofinaccessibility.h
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:50
QgsFields
Definition: qgsfields.h:44
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsPropertyDefinition::Double
@ Double
Double value (including negative values)
Definition: qgsproperty.h:58
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsApplication::iconPath
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Definition: qgsapplication.cpp:594
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:48
qgsapplication.h
QgsGeometry::poleOfInaccessibility
QgsGeometry poleOfInaccessibility(double precision, double *distanceToBoundary=nullptr) const
Calculates the approximate pole of inaccessibility for a surface, which is the most distant internal ...
Definition: qgsgeometry.cpp:2192
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:125
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsGeometry
Definition: qgsgeometry.h:122
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
QgsAttributes
Definition: qgsattributes.h:57
QgsFeature
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsProcessingException
Definition: qgsexception.h:82
QgsField
Definition: qgsfield.h:49