QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
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
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsSnapToGridAlgorithm::name() const
27{
28 return u"snappointstogrid"_s;
29}
30
31QString QgsSnapToGridAlgorithm::displayName() const
32{
33 return QObject::tr( "Snap points to grid" );
34}
35
36QStringList QgsSnapToGridAlgorithm::tags() const
37{
38 return QObject::tr( "snapped,grid,simplify,round,precision" ).split( ',' );
39}
40
41QString QgsSnapToGridAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsSnapToGridAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsSnapToGridAlgorithm::outputName() const
52{
53 return QObject::tr( "Snapped" );
54}
55
56QString QgsSnapToGridAlgorithm::shortHelpString() const
57{
58 return QObject::tr(
59 "This algorithm modifies the coordinates of geometries in a vector layer, so that all points "
60 "or vertices are snapped to the closest point of the grid.\n\n"
61 "If the snapped geometry cannot be calculated (or is totally collapsed) the feature's "
62 "geometry will be cleared.\n\n"
63 "Note that snapping to grid may generate an invalid geometry in some corner cases.\n\n"
64 "Snapping can be performed on the X, Y, Z or M axis. A grid spacing of 0 for any axis will "
65 "disable snapping for that axis."
66 );
67}
68
69QString QgsSnapToGridAlgorithm::shortDescription() const
70{
71 return QObject::tr(
72 "Modifies the coordinates of geometries in a vector layer, so that all points "
73 "or vertices are snapped to the closest point of a grid."
74 );
75}
76
77QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance() const
78{
79 return new QgsSnapToGridAlgorithm();
80}
81
82void QgsSnapToGridAlgorithm::initParameters( const QVariantMap & )
83{
84 auto hSpacing = std::make_unique<QgsProcessingParameterDistance>( u"HSPACING"_s, QObject::tr( "X Grid Spacing" ), 1, u"INPUT"_s, false, 0 );
85 hSpacing->setIsDynamic( true );
86 hSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"HSPACING"_s, QObject::tr( "X Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
87 hSpacing->setDynamicLayerParameterName( u"INPUT"_s );
88 addParameter( hSpacing.release() );
89
90 auto vSpacing = std::make_unique<QgsProcessingParameterDistance>( u"VSPACING"_s, QObject::tr( "Y Grid Spacing" ), 1, u"INPUT"_s, false, 0 );
91 vSpacing->setIsDynamic( true );
92 vSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"VSPACING"_s, QObject::tr( "Y Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
93 vSpacing->setDynamicLayerParameterName( u"INPUT"_s );
94 addParameter( vSpacing.release() );
95
96 auto zSpacing = std::make_unique<QgsProcessingParameterNumber>( u"ZSPACING"_s, QObject::tr( "Z Grid Spacing" ), Qgis::ProcessingNumberParameterType::Double, 0, false, 0 );
97 zSpacing->setIsDynamic( true );
98 zSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ZSPACING"_s, QObject::tr( "Z Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
99 zSpacing->setDynamicLayerParameterName( u"INPUT"_s );
100 addParameter( zSpacing.release() );
101
102 auto mSpacing = std::make_unique<QgsProcessingParameterNumber>( u"MSPACING"_s, QObject::tr( "M Grid Spacing" ), Qgis::ProcessingNumberParameterType::Double, 0, false, 0 );
103 mSpacing->setIsDynamic( true );
104 mSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MSPACING"_s, QObject::tr( "M Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
105 mSpacing->setDynamicLayerParameterName( u"INPUT"_s );
106 addParameter( mSpacing.release() );
107}
108
109bool QgsSnapToGridAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
110{
111 mIntervalX = parameterAsDouble( parameters, u"HSPACING"_s, context );
112 mDynamicIntervalX = QgsProcessingParameters::isDynamic( parameters, u"HSPACING"_s );
113 if ( mDynamicIntervalX )
114 mIntervalXProperty = parameters.value( u"HSPACING"_s ).value<QgsProperty>();
115
116 mIntervalY = parameterAsDouble( parameters, u"VSPACING"_s, context );
117 mDynamicIntervalY = QgsProcessingParameters::isDynamic( parameters, u"VSPACING"_s );
118 if ( mDynamicIntervalY )
119 mIntervalYProperty = parameters.value( u"VSPACING"_s ).value<QgsProperty>();
120
121 mIntervalZ = parameterAsDouble( parameters, u"ZSPACING"_s, context );
122 mDynamicIntervalZ = QgsProcessingParameters::isDynamic( parameters, u"ZSPACING"_s );
123 if ( mDynamicIntervalZ )
124 mIntervalZProperty = parameters.value( u"ZSPACING"_s ).value<QgsProperty>();
125
126 mIntervalM = parameterAsDouble( parameters, u"MSPACING"_s, context );
127 mDynamicIntervalM = QgsProcessingParameters::isDynamic( parameters, u"MSPACING"_s );
128 if ( mDynamicIntervalM )
129 mIntervalMProperty = parameters.value( u"MSPACING"_s ).value<QgsProperty>();
130
131 return true;
132}
133
134QgsFeatureList QgsSnapToGridAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
135{
136 QgsFeature f = feature;
137 if ( f.hasGeometry() )
138 {
139 double intervalX = mIntervalX;
140 if ( mDynamicIntervalX )
141 intervalX = mIntervalXProperty.valueAsDouble( context.expressionContext(), intervalX );
142
143 double intervalY = mIntervalY;
144 if ( mDynamicIntervalY )
145 intervalY = mIntervalYProperty.valueAsDouble( context.expressionContext(), intervalY );
146
147 double intervalZ = mIntervalZ;
148 if ( mDynamicIntervalZ )
149 intervalZ = mIntervalZProperty.valueAsDouble( context.expressionContext(), intervalZ );
150
151 double intervalM = mIntervalM;
152 if ( mDynamicIntervalM )
153 intervalM = mIntervalMProperty.valueAsDouble( context.expressionContext(), intervalM );
154
155 const QgsGeometry outputGeometry = f.geometry().snappedToGrid( intervalX, intervalY, intervalZ, intervalM );
156 if ( outputGeometry.isNull() )
157 {
158 feedback->reportError( QObject::tr( "Error snapping geometry %1" ).arg( feature.id() ) );
159 }
160 f.setGeometry( outputGeometry );
161 }
162 return QgsFeatureList() << f;
163}
164
165Qgis::ProcessingFeatureSourceFlags QgsSnapToGridAlgorithm::sourceFlags() const
166{
168}
169
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
@ Double
Double/float values.
Definition qgis.h:3921
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
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.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:47
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:57
A store for object properties.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
QList< QgsFeature > QgsFeatureList