QGIS API Documentation  3.0.2-Girona (307d082)
qgsalgorithmsnaptogrid.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmsnaptogrid.cpp
3  --------------------------
4  begin : October 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsalgorithmsnaptogrid.h"
19 
21 
22 QString QgsSnapToGridAlgorithm::name() const
23 {
24  return QStringLiteral( "snappointstogrid" );
25 }
26 
27 QString QgsSnapToGridAlgorithm::displayName() const
28 {
29  return QObject::tr( "Snap points to grid" );
30 }
31 
32 QStringList QgsSnapToGridAlgorithm::tags() const
33 {
34  return QObject::tr( "snapped,grid,simplify,round,precision" ).split( ',' );
35 }
36 
37 QString QgsSnapToGridAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsSnapToGridAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsSnapToGridAlgorithm::outputName() const
48 {
49  return QObject::tr( "Snapped" );
50 }
51 
52 QString QgsSnapToGridAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm modifies the coordinates of geometries in a vector layer, so that all points "
55  "or vertices are snapped to the closest point of the grid.\n\n"
56  "If the snapped geometry cannot be calculated (or is totally collapsed) the feature's "
57  "geometry will be cleared.\n\n"
58  "Note that snapping to grid may generate an invalid geometry in some corner cases.\n\n"
59  "Snapping can be performed on the X, Y, Z or M axis. A grid spacing of 0 for any axis will "
60  "disable snapping for that axis." );
61 }
62 
63 QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance() const
64 {
65  return new QgsSnapToGridAlgorithm();
66 }
67 
68 void QgsSnapToGridAlgorithm::initParameters( const QVariantMap & )
69 {
70  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "HSPACING" ),
71  QObject::tr( "X Grid Spacing" ), QgsProcessingParameterNumber::Double,
72  1, false, 0 ) );
73  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "VSPACING" ),
74  QObject::tr( "Y Grid Spacing" ), QgsProcessingParameterNumber::Double,
75  1, false, 0 ) );
76  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "ZSPACING" ),
77  QObject::tr( "Z Grid Spacing" ), QgsProcessingParameterNumber::Double,
78  0, false, 0 ) );
79  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MSPACING" ),
80  QObject::tr( "M Grid Spacing" ), QgsProcessingParameterNumber::Double,
81  0, false, 0 ) );
82 }
83 
84 bool QgsSnapToGridAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
85 {
86  mIntervalX = parameterAsDouble( parameters, QStringLiteral( "HSPACING" ), context );
87  mIntervalY = parameterAsDouble( parameters, QStringLiteral( "VSPACING" ), context );
88  mIntervalZ = parameterAsDouble( parameters, QStringLiteral( "ZSPACING" ), context );
89  mIntervalM = parameterAsDouble( parameters, QStringLiteral( "MSPACING" ), context );
90  return true;
91 }
92 
93 QgsFeatureList QgsSnapToGridAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
94 {
95  QgsFeature f = feature;
96  if ( f.hasGeometry() )
97  {
98  QgsGeometry outputGeometry = f.geometry().snappedToGrid( mIntervalX, mIntervalY, mIntervalZ, mIntervalM );
99  if ( !outputGeometry )
100  {
101  feedback->reportError( QObject::tr( "Error snapping geometry %1" ).arg( feature.id() ) );
102  }
103  f.setGeometry( outputGeometry );
104  }
105  return QgsFeatureList() << f;
106 }
107 
109 
110 
QgsFeatureId id
Definition: qgsfeature.h:71
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A numeric parameter for processing algorithms.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
QgsGeometry snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0) const
Returns a new geometry with all points or vertices snapped to the closest point of the grid...
Contains information about the context in which a processing algorithm is executed.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.