Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[processing] Port Delete Holes algorithm to c++, allow dynamic
min area parameter
- Loading branch information
Showing
with
188 additions
and 80 deletions.
- +0 −75 python/plugins/processing/algs/qgis/DeleteHoles.py
- +0 −2 python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
- BIN python/plugins/processing/tests/testdata/custom/circular_strings.gpkg
- +3 −3 python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
- +1 −0 src/analysis/CMakeLists.txt
- +116 −0 src/analysis/processing/qgsalgorithmremoveholes.cpp
- +66 −0 src/analysis/processing/qgsalgorithmremoveholes.h
- +2 −0 src/analysis/processing/qgsnativealgorithms.cpp
Binary file not shown.
@@ -0,0 +1,116 @@ | ||
/*************************************************************************** | ||
qgsalgorithmremoveholes.cpp | ||
--------------------- | ||
begin : March 2018 | ||
copyright : (C) 2018 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsalgorithmremoveholes.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsRemoveHolesAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "deleteholes" ); | ||
} | ||
|
||
QString QgsRemoveHolesAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Delete holes" ); | ||
} | ||
|
||
QStringList QgsRemoveHolesAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "remove,delete,drop,holes,rings,fill" ).split( ',' ); | ||
} | ||
|
||
QString QgsRemoveHolesAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector geometry" ); | ||
} | ||
|
||
QString QgsRemoveHolesAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectorgeometry" ); | ||
} | ||
|
||
QString QgsRemoveHolesAlgorithm::outputName() const | ||
{ | ||
return QObject::tr( "Cleaned" ); | ||
} | ||
|
||
QList<int> QgsRemoveHolesAlgorithm::inputLayerTypes() const | ||
{ | ||
return QList<int>() << QgsProcessing::TypeVectorPolygon; | ||
} | ||
|
||
QgsProcessing::SourceType QgsRemoveHolesAlgorithm::outputLayerType() const | ||
{ | ||
return QgsProcessing::TypeVectorPolygon; | ||
} | ||
|
||
QString QgsRemoveHolesAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm takes a polygon layer and removes holes in polygons. It creates a new vector " | ||
"layer in which polygons with holes have been replaced by polygons with only their external ring. " | ||
"Attributes are not modified.\n\n" | ||
"An optional minimum area parameter allows removing only holes which are smaller than a specified " | ||
"area threshold. Leaving this parameter as 0.0 results in all holes being removed." ); | ||
} | ||
|
||
QgsRemoveHolesAlgorithm *QgsRemoveHolesAlgorithm::createInstance() const | ||
{ | ||
return new QgsRemoveHolesAlgorithm(); | ||
} | ||
|
||
void QgsRemoveHolesAlgorithm::initParameters( const QVariantMap & ) | ||
{ | ||
std::unique_ptr< QgsProcessingParameterNumber > minArea = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MIN_AREA" ), | ||
QObject::tr( "Remove holes with area less than" ), QgsProcessingParameterNumber::Double, | ||
0.0, false, 0 ); | ||
minArea->setIsDynamic( true ); | ||
minArea->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MIN_AREA" ), QObject::tr( "Remove holes with area less than" ), QgsPropertyDefinition::DoublePositive ) ); | ||
minArea->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); | ||
addParameter( minArea.release() ); | ||
} | ||
|
||
bool QgsRemoveHolesAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
mMinArea = parameterAsDouble( parameters, QStringLiteral( "MIN_AREA" ), context ); | ||
mDynamicMinArea = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN_AREA" ) ); | ||
if ( mDynamicMinArea ) | ||
mMinAreaProperty = parameters.value( QStringLiteral( "MIN_AREA" ) ).value< QgsProperty >(); | ||
|
||
return true; | ||
} | ||
|
||
QgsFeatureList QgsRemoveHolesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
QgsFeature f = feature; | ||
if ( f.hasGeometry() ) | ||
{ | ||
QgsGeometry geometry = f.geometry(); | ||
|
||
double minArea = mMinArea; | ||
if ( mDynamicMinArea ) | ||
minArea = mMinAreaProperty.valueAsDouble( context.expressionContext(), minArea ); | ||
|
||
f.setGeometry( geometry.removeInteriorRings( minArea > 0 ? minArea : -1 ) ); | ||
} | ||
return QgsFeatureList() << f; | ||
} | ||
|
||
|
||
///@endcond | ||
|
||
|
@@ -0,0 +1,66 @@ | ||
/*************************************************************************** | ||
qgsalgorithmremoveholes.h | ||
--------------------- | ||
begin : March 2018 | ||
copyright : (C) 2018 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMREMOVEHOLES_H | ||
#define QGSALGORITHMREMOVEHOLES_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native remove holes algorithm. | ||
*/ | ||
class QgsRemoveHolesAlgorithm : public QgsProcessingFeatureBasedAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsRemoveHolesAlgorithm() = default; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
QString shortHelpString() const override; | ||
QgsRemoveHolesAlgorithm *createInstance() const override SIP_FACTORY; | ||
void initParameters( const QVariantMap &configuration = QVariantMap() ) override; | ||
|
||
protected: | ||
QString outputName() const override; | ||
QList<int> inputLayerTypes() const override; | ||
QgsProcessing::SourceType outputLayerType() const override; | ||
bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
|
||
private: | ||
|
||
double mMinArea = 0.0; | ||
bool mDynamicMinArea = false; | ||
QgsProperty mMinAreaProperty; | ||
|
||
}; | ||
|
||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMREMOVEHOLES_H | ||
|
||
|