Skip to content
Permalink
Browse files

Port simplify geometries to c++

  • Loading branch information
nyalldawson committed Sep 13, 2017
1 parent 8e8f3ed commit d96a3f4dd483c8d17d6dd873bfd7ed30b9d3df60
@@ -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.

@@ -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
@@ -279,7 +278,6 @@ def getAlgs(self):
ShortestPathLayerToPoint(),
ShortestPathPointToLayer(),
ShortestPathPointToPoint(),
SimplifyGeometries(),
SingleSidedBuffer(),
Slope(),
SnapGeometriesToLayer(),

This file was deleted.

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

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

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

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

- algorithm: qgis:simplifygeometries
- algorithm: native:simplifygeometries
name: Simplify (grid)
params:
INPUT:
@@ -81,6 +81,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsFixGeometriesAlgorithm() );
addAlgorithm( new QgsMergeLinesAlgorithm() );
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
}

void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
@@ -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



@@ -24,6 +24,7 @@
#include "qgis.h"
#include "qgsprocessingalgorithm.h"
#include "qgsprocessingprovider.h"
#include "qgsmaptopixelgeometrysimplifier.h"

///@cond PRIVATE

@@ -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

0 comments on commit d96a3f4

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