Skip to content

Commit

Permalink
Port simplify geometries to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 13, 2017
1 parent 8e8f3ed commit d96a3f4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 107 deletions.
5 changes: 0 additions & 5 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -528,11 +528,6 @@ qgis:setzvalue: >

If Z values already exist in the layer, they will be overwritten with the new value. If no Z values exist, the geometry will be upgraded to include Z values and the specified value used as the initial Z value for all geometries.

qgis:simplifygeometries: >
This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.

The algorithm gives a choice of simplification methods, including distance based (the "Douglas-Peucker" algorithm), area based ("Visvalingam" algorithm) and snapping geometries to grid.

qgis:singlesidedbuffer: >
This algorithm buffers lines by a specified distance on one side of the line only.

Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -141,7 +141,6 @@
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
from .ShortestPathPointToLayer import ShortestPathPointToLayer
from .ShortestPathPointToPoint import ShortestPathPointToPoint
from .SimplifyGeometries import SimplifyGeometries
from .SingleSidedBuffer import SingleSidedBuffer
from .Slope import Slope
from .SnapGeometries import SnapGeometriesToLayer
Expand Down Expand Up @@ -279,7 +278,6 @@ def getAlgs(self):
ShortestPathLayerToPoint(),
ShortestPathPointToLayer(),
ShortestPathPointToPoint(),
SimplifyGeometries(),
SingleSidedBuffer(),
Slope(),
SnapGeometriesToLayer(),
Expand Down
96 changes: 0 additions & 96 deletions python/plugins/processing/algs/qgis/SimplifyGeometries.py

This file was deleted.

Expand Up @@ -1209,7 +1209,7 @@ tests:
name: expected/extract_nodes_lines.gml
type: vector

- algorithm: qgis:simplifygeometries
- algorithm: native:simplifygeometries
name: Simplify (lines)
params:
INPUT:
Expand All @@ -1221,7 +1221,7 @@ tests:
name: expected/simplify_lines.gml
type: vector

- algorithm: qgis:simplifygeometries
- algorithm: native:simplifygeometries
name: Simplify (multilines)
params:
INPUT:
Expand All @@ -1233,7 +1233,7 @@ tests:
name: expected/simplify_multilines.gml
type: vector

- algorithm: qgis:simplifygeometries
- algorithm: native:simplifygeometries
name: Simplify (visval)
params:
INPUT:
Expand All @@ -1249,7 +1249,7 @@ tests:
geometry:
precision: 7

- algorithm: qgis:simplifygeometries
- algorithm: native:simplifygeometries
name: Simplify (grid)
params:
INPUT:
Expand Down
62 changes: 62 additions & 0 deletions src/core/processing/qgsnativealgorithms.cpp
Expand Up @@ -81,6 +81,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsFixGeometriesAlgorithm() );
addAlgorithm( new QgsMergeLinesAlgorithm() );
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
}

void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
Expand Down Expand Up @@ -1751,5 +1752,66 @@ QgsFeature QgsSmoothAlgorithm::processFeature( const QgsFeature &feature, QgsPro
return f;
}

QString QgsSimplifyAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer "
"with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.\n\n"
"The algorithm gives a choice of simplification methods, including distance based "
"(the \"Douglas-Peucker\" algorithm), area based (\"Visvalingam\" algorithm) and snapping geometries to a grid." );
}

QgsSimplifyAlgorithm *QgsSimplifyAlgorithm::createInstance() const
{
return new QgsSimplifyAlgorithm();
}

void QgsSimplifyAlgorithm::initParameters( const QVariantMap & )
{
QStringList methods;
methods << QObject::tr( "Distance (Douglas-Peucker)" )
<< QObject::tr( "Snap to grid" )
<< QObject::tr( "Area (Visvalingam)" );

addParameter( new QgsProcessingParameterEnum(
QStringLiteral( "METHOD" ),
QObject::tr( "Simplification method" ),
methods, false, 0 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "TOLERANCE" ),
QObject::tr( "Tolerance" ), QgsProcessingParameterNumber::Double,
1.0, false, 0, 10000000.0 ) );
}

bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
mMethod = static_cast< QgsMapToPixelSimplifier::SimplifyAlgorithm >( parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context ) );
if ( mMethod != QgsMapToPixelSimplifier::Distance )
mSimplifier.reset( new QgsMapToPixelSimplifier( QgsMapToPixelSimplifier::SimplifyGeometry, mTolerance, mMethod ) );

return true;
}

QgsFeature QgsSimplifyAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingFeedback * )
{
QgsFeature f = feature;
if ( f.hasGeometry() )
{
QgsGeometry inputGeometry = f.geometry();
QgsGeometry outputGeometry;
if ( mMethod == QgsMapToPixelSimplifier::Distance )
{
outputGeometry = inputGeometry.simplify( mTolerance );
}
else
{
outputGeometry = mSimplifier->simplify( inputGeometry );
}
f.setGeometry( outputGeometry );
}
return f;
}

///@endcond



30 changes: 30 additions & 0 deletions src/core/processing/qgsnativealgorithms.h
Expand Up @@ -24,6 +24,7 @@
#include "qgis.h"
#include "qgsprocessingalgorithm.h"
#include "qgsprocessingprovider.h"
#include "qgsmaptopixelgeometrysimplifier.h"

///@cond PRIVATE

Expand Down Expand Up @@ -632,6 +633,35 @@ class QgsSmoothAlgorithm : public QgsProcessingFeatureBasedAlgorithm
double mMaxAngle = 0;
};

/**
* Native simplify algorithm.
*/
class QgsSimplifyAlgorithm : public QgsProcessingFeatureBasedAlgorithm
{

public:

QgsSimplifyAlgorithm() = default;
QString name() const override { return QStringLiteral( "simplifygeometries" ); }
QString displayName() const override { return QObject::tr( "Simplify geometries" ); }
virtual QStringList tags() const override { return QObject::tr( "simplify,generalize,douglas,peucker,visvalingam" ).split( ',' ); }
QString group() const override { return QObject::tr( "Vector geometry" ); }
QString shortHelpString() const override;
QgsSimplifyAlgorithm *createInstance() const override SIP_FACTORY;
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;

protected:
QString outputName() const override { return QObject::tr( "Simplified" ); }
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;

private:

double mTolerance = 1.0;
QgsMapToPixelSimplifier::SimplifyAlgorithm mMethod = QgsMapToPixelSimplifier::Distance;
std::unique_ptr< QgsMapToPixelSimplifier > mSimplifier;

};
///@endcond PRIVATE

#endif // QGSNATIVEALGORITHMS_H
Expand Down

0 comments on commit d96a3f4

Please sign in to comment.