Skip to content

Commit d96a3f4

Browse files
committedSep 13, 2017
Port simplify geometries to c++
1 parent 8e8f3ed commit d96a3f4

File tree

6 files changed

+96
-107
lines changed

6 files changed

+96
-107
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,6 @@ qgis:setzvalue: >
528528

529529
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.
530530

531-
qgis:simplifygeometries: >
532-
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.
533-
534-
The algorithm gives a choice of simplification methods, including distance based (the "Douglas-Peucker" algorithm), area based ("Visvalingam" algorithm) and snapping geometries to grid.
535-
536531
qgis:singlesidedbuffer: >
537532
This algorithm buffers lines by a specified distance on one side of the line only.
538533

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@
141141
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
142142
from .ShortestPathPointToLayer import ShortestPathPointToLayer
143143
from .ShortestPathPointToPoint import ShortestPathPointToPoint
144-
from .SimplifyGeometries import SimplifyGeometries
145144
from .SingleSidedBuffer import SingleSidedBuffer
146145
from .Slope import Slope
147146
from .SnapGeometries import SnapGeometriesToLayer
@@ -279,7 +278,6 @@ def getAlgs(self):
279278
ShortestPathLayerToPoint(),
280279
ShortestPathPointToLayer(),
281280
ShortestPathPointToPoint(),
282-
SimplifyGeometries(),
283281
SingleSidedBuffer(),
284282
Slope(),
285283
SnapGeometriesToLayer(),

‎python/plugins/processing/algs/qgis/SimplifyGeometries.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ tests:
12091209
name: expected/extract_nodes_lines.gml
12101210
type: vector
12111211

1212-
- algorithm: qgis:simplifygeometries
1212+
- algorithm: native:simplifygeometries
12131213
name: Simplify (lines)
12141214
params:
12151215
INPUT:
@@ -1221,7 +1221,7 @@ tests:
12211221
name: expected/simplify_lines.gml
12221222
type: vector
12231223

1224-
- algorithm: qgis:simplifygeometries
1224+
- algorithm: native:simplifygeometries
12251225
name: Simplify (multilines)
12261226
params:
12271227
INPUT:
@@ -1233,7 +1233,7 @@ tests:
12331233
name: expected/simplify_multilines.gml
12341234
type: vector
12351235

1236-
- algorithm: qgis:simplifygeometries
1236+
- algorithm: native:simplifygeometries
12371237
name: Simplify (visval)
12381238
params:
12391239
INPUT:
@@ -1249,7 +1249,7 @@ tests:
12491249
geometry:
12501250
precision: 7
12511251

1252-
- algorithm: qgis:simplifygeometries
1252+
- algorithm: native:simplifygeometries
12531253
name: Simplify (grid)
12541254
params:
12551255
INPUT:

‎src/core/processing/qgsnativealgorithms.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
8181
addAlgorithm( new QgsFixGeometriesAlgorithm() );
8282
addAlgorithm( new QgsMergeLinesAlgorithm() );
8383
addAlgorithm( new QgsSmoothAlgorithm() );
84+
addAlgorithm( new QgsSimplifyAlgorithm() );
8485
}
8586

8687
void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
@@ -1751,5 +1752,66 @@ QgsFeature QgsSmoothAlgorithm::processFeature( const QgsFeature &feature, QgsPro
17511752
return f;
17521753
}
17531754

1755+
QString QgsSimplifyAlgorithm::shortHelpString() const
1756+
{
1757+
return QObject::tr( "This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer "
1758+
"with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.\n\n"
1759+
"The algorithm gives a choice of simplification methods, including distance based "
1760+
"(the \"Douglas-Peucker\" algorithm), area based (\"Visvalingam\" algorithm) and snapping geometries to a grid." );
1761+
}
1762+
1763+
QgsSimplifyAlgorithm *QgsSimplifyAlgorithm::createInstance() const
1764+
{
1765+
return new QgsSimplifyAlgorithm();
1766+
}
1767+
1768+
void QgsSimplifyAlgorithm::initParameters( const QVariantMap & )
1769+
{
1770+
QStringList methods;
1771+
methods << QObject::tr( "Distance (Douglas-Peucker)" )
1772+
<< QObject::tr( "Snap to grid" )
1773+
<< QObject::tr( "Area (Visvalingam)" );
1774+
1775+
addParameter( new QgsProcessingParameterEnum(
1776+
QStringLiteral( "METHOD" ),
1777+
QObject::tr( "Simplification method" ),
1778+
methods, false, 0 ) );
1779+
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "TOLERANCE" ),
1780+
QObject::tr( "Tolerance" ), QgsProcessingParameterNumber::Double,
1781+
1.0, false, 0, 10000000.0 ) );
1782+
}
1783+
1784+
bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
1785+
{
1786+
mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
1787+
mMethod = static_cast< QgsMapToPixelSimplifier::SimplifyAlgorithm >( parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context ) );
1788+
if ( mMethod != QgsMapToPixelSimplifier::Distance )
1789+
mSimplifier.reset( new QgsMapToPixelSimplifier( QgsMapToPixelSimplifier::SimplifyGeometry, mTolerance, mMethod ) );
1790+
1791+
return true;
1792+
}
1793+
1794+
QgsFeature QgsSimplifyAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingFeedback * )
1795+
{
1796+
QgsFeature f = feature;
1797+
if ( f.hasGeometry() )
1798+
{
1799+
QgsGeometry inputGeometry = f.geometry();
1800+
QgsGeometry outputGeometry;
1801+
if ( mMethod == QgsMapToPixelSimplifier::Distance )
1802+
{
1803+
outputGeometry = inputGeometry.simplify( mTolerance );
1804+
}
1805+
else
1806+
{
1807+
outputGeometry = mSimplifier->simplify( inputGeometry );
1808+
}
1809+
f.setGeometry( outputGeometry );
1810+
}
1811+
return f;
1812+
}
1813+
17541814
///@endcond
17551815

1816+
1817+

‎src/core/processing/qgsnativealgorithms.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgis.h"
2525
#include "qgsprocessingalgorithm.h"
2626
#include "qgsprocessingprovider.h"
27+
#include "qgsmaptopixelgeometrysimplifier.h"
2728

2829
///@cond PRIVATE
2930

@@ -632,6 +633,35 @@ class QgsSmoothAlgorithm : public QgsProcessingFeatureBasedAlgorithm
632633
double mMaxAngle = 0;
633634
};
634635

636+
/**
637+
* Native simplify algorithm.
638+
*/
639+
class QgsSimplifyAlgorithm : public QgsProcessingFeatureBasedAlgorithm
640+
{
641+
642+
public:
643+
644+
QgsSimplifyAlgorithm() = default;
645+
QString name() const override { return QStringLiteral( "simplifygeometries" ); }
646+
QString displayName() const override { return QObject::tr( "Simplify geometries" ); }
647+
virtual QStringList tags() const override { return QObject::tr( "simplify,generalize,douglas,peucker,visvalingam" ).split( ',' ); }
648+
QString group() const override { return QObject::tr( "Vector geometry" ); }
649+
QString shortHelpString() const override;
650+
QgsSimplifyAlgorithm *createInstance() const override SIP_FACTORY;
651+
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
652+
653+
protected:
654+
QString outputName() const override { return QObject::tr( "Simplified" ); }
655+
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
656+
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
657+
658+
private:
659+
660+
double mTolerance = 1.0;
661+
QgsMapToPixelSimplifier::SimplifyAlgorithm mMethod = QgsMapToPixelSimplifier::Distance;
662+
std::unique_ptr< QgsMapToPixelSimplifier > mSimplifier;
663+
664+
};
635665
///@endcond PRIVATE
636666

637667
#endif // QGSNATIVEALGORITHMS_H

0 commit comments

Comments
 (0)
Please sign in to comment.