@@ -67,93 +67,88 @@ QIcon QgsPoleOfInaccessibilityAlgorithm::icon() const
67
67
return QgsApplication::getThemeIcon ( QStringLiteral ( " /algorithms/mAlgorithmCentroids.svg" ) );
68
68
}
69
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
+ {
82
+ return QgsProcessing::TypeVectorPoint;
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
+
70
100
QgsPoleOfInaccessibilityAlgorithm *QgsPoleOfInaccessibilityAlgorithm::createInstance () const
71
101
{
72
102
return new QgsPoleOfInaccessibilityAlgorithm ();
73
103
}
74
104
75
- void QgsPoleOfInaccessibilityAlgorithm::initAlgorithm ( const QVariantMap & )
105
+ void QgsPoleOfInaccessibilityAlgorithm::initParameters ( const QVariantMap & )
76
106
{
77
- addParameter ( new QgsProcessingParameterFeatureSource ( QStringLiteral ( " INPUT" ), QObject::tr ( " Input layer" ), QList< int >() << QgsProcessing::TypeVectorPolygon ) );
78
-
79
107
auto toleranceParam = qgis::make_unique < QgsProcessingParameterDistance >( QStringLiteral ( " TOLERANCE" ), QObject::tr ( " Tolerance" ), 1.0 , QStringLiteral ( " INPUT" ), 0.0 );
80
108
toleranceParam->setIsDynamic ( true );
81
109
toleranceParam->setDynamicPropertyDefinition ( QgsPropertyDefinition ( QStringLiteral ( " Tolerance" ), QObject::tr ( " Tolerance" ), QgsPropertyDefinition::Double ) );
82
110
toleranceParam->setDynamicLayerParameterName ( QStringLiteral ( " INPUT" ) );
83
111
addParameter ( toleranceParam.release () );
84
-
85
- addParameter ( new QgsProcessingParameterFeatureSink ( QStringLiteral ( " OUTPUT" ), QObject::tr ( " Point" ), QgsProcessing::TypeVectorPoint ) );
86
112
}
87
113
88
- QVariantMap QgsPoleOfInaccessibilityAlgorithm::processAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
114
+ bool QgsPoleOfInaccessibilityAlgorithm::prepareAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
89
115
{
90
- std::unique_ptr< QgsProcessingFeatureSource > source ( parameterAsSource ( parameters, QStringLiteral ( " INPUT" ), context ) );
91
- if ( !source )
92
- throw QgsProcessingException ( invalidSourceError ( parameters, QStringLiteral ( " INPUT" ) ) );
93
-
94
- double toleranceValue = parameterAsDouble ( parameters, QStringLiteral ( " TOLERANCE" ), context );
95
- bool dynamicTolerance = QgsProcessingParameters::isDynamic ( parameters, QStringLiteral ( " TOLERANCE" ) );
96
- QgsExpressionContext expressionContext = createExpressionContext ( parameters, context, source.get () );
97
- QgsProperty toleranceProperty;
98
- if ( dynamicTolerance )
99
- toleranceProperty = parameters.value ( QStringLiteral ( " TOLERANCE" ) ).value < QgsProperty >();
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 >();
100
120
101
- QgsFields fields = source->fields ();
102
- fields.append ( QgsField ( QStringLiteral ( " dist_pole" ), QVariant::Double ) );
103
-
104
- QString dest;
105
- std::unique_ptr< QgsFeatureSink > sink ( parameterAsSink ( parameters, QStringLiteral ( " OUTPUT" ), context, dest, fields, QgsWkbTypes::Point, source->sourceCrs () ) );
106
- if ( !sink )
107
- throw QgsProcessingException ( invalidSinkError ( parameters, QStringLiteral ( " OUTPUT" ) ) );
108
-
109
- double step = source->featureCount () > 0 ? 100.0 / source->featureCount () : 1 ;
121
+ return true ;
122
+ }
110
123
111
- int i = 0 ;
112
- QgsFeature f;
113
- QgsFeatureIterator it = source-> getFeatures () ;
114
- while ( it. nextFeature ( f ) )
124
+ QgsFeatureList QgsPoleOfInaccessibilityAlgorithm::processFeature ( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
125
+ {
126
+ QgsFeature outFeature = feature ;
127
+ if ( outFeature. hasGeometry ( ) )
115
128
{
116
- if ( feedback->isCanceled () )
117
- {
118
- break ;
119
- }
129
+ double tolerance = mTolerance ;
130
+ if ( mDynamicTolerance )
131
+ tolerance = mToleranceProperty .valueAsDouble ( context.expressionContext (), tolerance );
120
132
121
- QgsFeature outFeature = f;
122
- if ( f.hasGeometry () )
123
- {
124
- double tolerance = toleranceValue;
125
- if ( dynamicTolerance )
126
- {
127
- expressionContext.setFeature ( f );
128
- tolerance = toleranceProperty.valueAsDouble ( expressionContext, toleranceValue );
129
- }
130
-
131
- double distance;
132
- QgsGeometry outputGeom = f.geometry ().poleOfInaccessibility ( tolerance, &distance );
133
- if ( outputGeom.isNull () )
134
- {
135
- throw QgsProcessingException ( QObject::tr ( " Error calculating pole of inaccessibility" ) );
136
- }
137
- QgsAttributes attrs = f.attributes ();
138
- attrs.append ( distance );
139
- outFeature.setAttributes ( attrs );
140
- outFeature.setGeometry ( outputGeom );
141
- }
142
- else
133
+ double distance;
134
+ QgsGeometry outputGeom = outFeature.geometry ().poleOfInaccessibility ( tolerance, &distance );
135
+ if ( outputGeom.isNull () )
143
136
{
144
- QgsAttributes attrs = f.attributes ();
145
- attrs.append ( QVariant () );
146
- outFeature.setAttributes ( attrs );
137
+ throw QgsProcessingException ( QObject::tr ( " Error calculating pole of inaccessibility" ) );
147
138
}
148
-
149
- sink->addFeature ( outFeature, QgsFeatureSink::FastInsert );
150
- feedback->setProgress ( i * step );
151
- i++;
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 );
152
149
}
153
150
154
- QVariantMap outputs;
155
- outputs.insert ( QStringLiteral ( " OUTPUT" ), dest );
156
- return outputs;
151
+ return QgsFeatureList () << outFeature;
157
152
}
158
153
159
154
// /@endcond
0 commit comments