Skip to content
Permalink
Browse files

[processing] Port Delete Holes algorithm to c++, allow dynamic

min area parameter
  • Loading branch information
nyalldawson committed Mar 11, 2018
1 parent d88da33 commit 8df954d6b812c29711374585f3ad4d7e6db9e7a0

This file was deleted.

@@ -55,7 +55,6 @@
from .Delaunay import Delaunay
from .DeleteColumn import DeleteColumn
from .DeleteDuplicateGeometries import DeleteDuplicateGeometries
from .DeleteHoles import DeleteHoles
from .DensifyGeometries import DensifyGeometries
from .DensifyGeometriesInterval import DensifyGeometriesInterval
from .Difference import Difference
@@ -176,7 +175,6 @@ def getAlgs(self):
Delaunay(),
DeleteColumn(),
DeleteDuplicateGeometries(),
DeleteHoles(),
DensifyGeometries(),
DensifyGeometriesInterval(),
Difference(),
Binary file not shown.
@@ -215,7 +215,7 @@ tests:
type: vector

- name: Delete Holes
algorithm: qgis:deleteholes
algorithm: native:deleteholes
params:
- name: polys.gml
type: vector
@@ -2321,7 +2321,7 @@ tests:
geometry:
precision: 7

- algorithm: qgis:deleteholes
- algorithm: native:deleteholes
name: Delete holes (no min)
params:
INPUT:
@@ -2333,7 +2333,7 @@ tests:
name: expected/removed_holes.gml
type: vector

- algorithm: qgis:deleteholes
- algorithm: native:deleteholes
name: Delete holes (with min)
params:
INPUT:
@@ -56,6 +56,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmpromotetomultipart.cpp
processing/qgsalgorithmrasterlayeruniquevalues.cpp
processing/qgsalgorithmremoveduplicatevertices.cpp
processing/qgsalgorithmremoveholes.cpp
processing/qgsalgorithmremovenullgeometry.cpp
processing/qgsalgorithmrenamelayer.cpp
processing/qgsalgorithmrotate.cpp
@@ -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 &parameters, 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 &parameters, 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


@@ -53,6 +53,7 @@
#include "qgsalgorithmpromotetomultipart.h"
#include "qgsalgorithmrasterlayeruniquevalues.h"
#include "qgsalgorithmremoveduplicatevertices.h"
#include "qgsalgorithmremoveholes.h"
#include "qgsalgorithmremovenullgeometry.h"
#include "qgsalgorithmrenamelayer.h"
#include "qgsalgorithmrotate.h"
@@ -147,6 +148,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsPromoteToMultipartAlgorithm() );
addAlgorithm( new QgsRasterLayerUniqueValuesReportAlgorithm() );
addAlgorithm( new QgsAlgorithmRemoveDuplicateVertices() );
addAlgorithm( new QgsRemoveHolesAlgorithm() );
addAlgorithm( new QgsRemoveNullGeometryAlgorithm() );
addAlgorithm( new QgsRenameLayerAlgorithm() );
addAlgorithm( new QgsRotateFeaturesAlgorithm() );

0 comments on commit 8df954d

Please sign in to comment.
You can’t perform that action at this time.