QGIS API Documentation 3.99.0-Master (09f76ad7019)
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( "This algorithm modifies the coordinates of geometries in a vector layer, so that all points "
59 "or vertices are snapped to the closest point of the grid.\n\n"
60 "If the snapped geometry cannot be calculated (or is totally collapsed) the feature's "
61 "geometry will be cleared.\n\n"
62 "Note that snapping to grid may generate an invalid geometry in some corner cases.\n\n"
63 "Snapping can be performed on the X, Y, Z or M axis. A grid spacing of 0 for any axis will "
64 "disable snapping for that axis." );
65}
66
67QString QgsSnapToGridAlgorithm::shortDescription() const
68{
69 return QObject::tr( "Modifies the coordinates of geometries in a vector layer, so that all points "
70 "or vertices are snapped to the closest point of a grid." );
71}
72
73QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance() const
74{
75 return new QgsSnapToGridAlgorithm();
76}
77
78void QgsSnapToGridAlgorithm::initParameters( const QVariantMap & )
79{
80 auto hSpacing = std::make_unique<QgsProcessingParameterDistance>( u"HSPACING"_s, QObject::tr( "X Grid Spacing" ), 1, u"INPUT"_s, false, 0 );
81 hSpacing->setIsDynamic( true );
82 hSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"HSPACING"_s, QObject::tr( "X Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
83 hSpacing->setDynamicLayerParameterName( u"INPUT"_s );
84 addParameter( hSpacing.release() );
85
86 auto vSpacing = std::make_unique<QgsProcessingParameterDistance>( u"VSPACING"_s, QObject::tr( "Y Grid Spacing" ), 1, u"INPUT"_s, false, 0 );
87 vSpacing->setIsDynamic( true );
88 vSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"VSPACING"_s, QObject::tr( "Y Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
89 vSpacing->setDynamicLayerParameterName( u"INPUT"_s );
90 addParameter( vSpacing.release() );
91
92 auto zSpacing = std::make_unique<QgsProcessingParameterNumber>( u"ZSPACING"_s, QObject::tr( "Z Grid Spacing" ), Qgis::ProcessingNumberParameterType::Double, 0, false, 0 );
93 zSpacing->setIsDynamic( true );
94 zSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ZSPACING"_s, QObject::tr( "Z Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
95 zSpacing->setDynamicLayerParameterName( u"INPUT"_s );
96 addParameter( zSpacing.release() );
97
98 auto mSpacing = std::make_unique<QgsProcessingParameterNumber>( u"MSPACING"_s, QObject::tr( "M Grid Spacing" ), Qgis::ProcessingNumberParameterType::Double, 0, false, 0 );
99 mSpacing->setIsDynamic( true );
100 mSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MSPACING"_s, QObject::tr( "M Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
101 mSpacing->setDynamicLayerParameterName( u"INPUT"_s );
102 addParameter( mSpacing.release() );
103}
104
105bool QgsSnapToGridAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
106{
107 mIntervalX = parameterAsDouble( parameters, u"HSPACING"_s, context );
108 mDynamicIntervalX = QgsProcessingParameters::isDynamic( parameters, u"HSPACING"_s );
109 if ( mDynamicIntervalX )
110 mIntervalXProperty = parameters.value( u"HSPACING"_s ).value<QgsProperty>();
111
112 mIntervalY = parameterAsDouble( parameters, u"VSPACING"_s, context );
113 mDynamicIntervalY = QgsProcessingParameters::isDynamic( parameters, u"VSPACING"_s );
114 if ( mDynamicIntervalY )
115 mIntervalYProperty = parameters.value( u"VSPACING"_s ).value<QgsProperty>();
116
117 mIntervalZ = parameterAsDouble( parameters, u"ZSPACING"_s, context );
118 mDynamicIntervalZ = QgsProcessingParameters::isDynamic( parameters, u"ZSPACING"_s );
119 if ( mDynamicIntervalZ )
120 mIntervalZProperty = parameters.value( u"ZSPACING"_s ).value<QgsProperty>();
121
122 mIntervalM = parameterAsDouble( parameters, u"MSPACING"_s, context );
123 mDynamicIntervalM = QgsProcessingParameters::isDynamic( parameters, u"MSPACING"_s );
124 if ( mDynamicIntervalM )
125 mIntervalMProperty = parameters.value( u"MSPACING"_s ).value<QgsProperty>();
126
127 return true;
128}
129
130QgsFeatureList QgsSnapToGridAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
131{
132 QgsFeature f = feature;
133 if ( f.hasGeometry() )
134 {
135 double intervalX = mIntervalX;
136 if ( mDynamicIntervalX )
137 intervalX = mIntervalXProperty.valueAsDouble( context.expressionContext(), intervalX );
138
139 double intervalY = mIntervalY;
140 if ( mDynamicIntervalY )
141 intervalY = mIntervalYProperty.valueAsDouble( context.expressionContext(), intervalY );
142
143 double intervalZ = mIntervalZ;
144 if ( mDynamicIntervalZ )
145 intervalZ = mIntervalZProperty.valueAsDouble( context.expressionContext(), intervalZ );
146
147 double intervalM = mIntervalM;
148 if ( mDynamicIntervalM )
149 intervalM = mIntervalMProperty.valueAsDouble( context.expressionContext(), intervalM );
150
151 const QgsGeometry outputGeometry = f.geometry().snappedToGrid( intervalX, intervalY, intervalZ, intervalM );
152 if ( outputGeometry.isNull() )
153 {
154 feedback->reportError( QObject::tr( "Error snapping geometry %1" ).arg( feature.id() ) );
155 }
156 f.setGeometry( outputGeometry );
157 }
158 return QgsFeatureList() << f;
159}
160
161Qgis::ProcessingFeatureSourceFlags QgsSnapToGridAlgorithm::sourceFlags() const
162{
164}
165
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
@ Double
Double/float values.
Definition qgis.h:3875
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:58
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