Skip to content

Commit 19596a8

Browse files
committedDec 12, 2019
[processing] make pole of inaccessibility feature-based
1 parent 8bc571c commit 19596a8

File tree

2 files changed

+76
-69
lines changed

2 files changed

+76
-69
lines changed
 

‎src/analysis/processing/qgsalgorithmpoleofinaccessibility.cpp

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -67,93 +67,88 @@ QIcon QgsPoleOfInaccessibilityAlgorithm::icon() const
6767
return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmCentroids.svg" ) );
6868
}
6969

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+
70100
QgsPoleOfInaccessibilityAlgorithm *QgsPoleOfInaccessibilityAlgorithm::createInstance() const
71101
{
72102
return new QgsPoleOfInaccessibilityAlgorithm();
73103
}
74104

75-
void QgsPoleOfInaccessibilityAlgorithm::initAlgorithm( const QVariantMap & )
105+
void QgsPoleOfInaccessibilityAlgorithm::initParameters( const QVariantMap & )
76106
{
77-
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList< int >() << QgsProcessing::TypeVectorPolygon ) );
78-
79107
auto toleranceParam = qgis::make_unique < QgsProcessingParameterDistance >( QStringLiteral( "TOLERANCE" ), QObject::tr( "Tolerance" ), 1.0, QStringLiteral( "INPUT" ), 0.0 );
80108
toleranceParam->setIsDynamic( true );
81109
toleranceParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Tolerance" ), QObject::tr( "Tolerance" ), QgsPropertyDefinition::Double ) );
82110
toleranceParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
83111
addParameter( toleranceParam.release() );
84-
85-
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Point" ), QgsProcessing::TypeVectorPoint ) );
86112
}
87113

88-
QVariantMap QgsPoleOfInaccessibilityAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
114+
bool QgsPoleOfInaccessibilityAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
89115
{
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 >();
100120

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+
}
110123

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() )
115128
{
116-
if ( feedback->isCanceled() )
117-
{
118-
break;
119-
}
129+
double tolerance = mTolerance;
130+
if ( mDynamicTolerance )
131+
tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
120132

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() )
143136
{
144-
QgsAttributes attrs = f.attributes();
145-
attrs.append( QVariant() );
146-
outFeature.setAttributes( attrs );
137+
throw QgsProcessingException( QObject::tr( "Error calculating pole of inaccessibility" ) );
147138
}
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 );
152149
}
153150

154-
QVariantMap outputs;
155-
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
156-
return outputs;
151+
return QgsFeatureList() << outFeature;
157152
}
158153

159154
///@endcond

‎src/analysis/processing/qgsalgorithmpoleofinaccessibility.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* Native pole of inaccessibility algorithm.
3030
*/
31-
class QgsPoleOfInaccessibilityAlgorithm : public QgsProcessingAlgorithm
31+
class QgsPoleOfInaccessibilityAlgorithm : public QgsProcessingFeatureBasedAlgorithm
3232
{
3333

3434
public:
@@ -42,13 +42,25 @@ class QgsPoleOfInaccessibilityAlgorithm : public QgsProcessingAlgorithm
4242
QString shortHelpString() const override;
4343
QString svgIconPath() const override;
4444
QIcon icon() const override;
45-
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
45+
QList<int> inputLayerTypes() const override;
46+
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
4647
QgsPoleOfInaccessibilityAlgorithm *createInstance() const override SIP_FACTORY;
4748

4849
protected:
4950

50-
QVariantMap processAlgorithm( const QVariantMap &parameters,
51-
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
51+
QString outputName() const override;
52+
QgsFields outputFields( const QgsFields &inputFields ) const override;
53+
QgsProcessing::SourceType outputLayerType() const override;
54+
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
55+
56+
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
57+
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
58+
59+
private:
60+
double mTolerance = 0.0;
61+
bool mDynamicTolerance = false;
62+
QgsProperty mToleranceProperty;
63+
5264
};
5365

5466
///@endcond PRIVATE

0 commit comments

Comments
 (0)
Please sign in to comment.