22using namespace Qt::StringLiterals;
26QString QgsSnapToGridAlgorithm::name()
const
28 return u
"snappointstogrid"_s;
31QString QgsSnapToGridAlgorithm::displayName()
const
33 return QObject::tr(
"Snap points to grid" );
36QStringList QgsSnapToGridAlgorithm::tags()
const
38 return QObject::tr(
"snapped,grid,simplify,round,precision" ).split(
',' );
41QString QgsSnapToGridAlgorithm::group()
const
43 return QObject::tr(
"Vector geometry" );
46QString QgsSnapToGridAlgorithm::groupId()
const
48 return u
"vectorgeometry"_s;
51QString QgsSnapToGridAlgorithm::outputName()
const
53 return QObject::tr(
"Snapped" );
56QString QgsSnapToGridAlgorithm::shortHelpString()
const
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." );
67QString QgsSnapToGridAlgorithm::shortDescription()
const
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." );
73QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance()
const
75 return new QgsSnapToGridAlgorithm();
78void QgsSnapToGridAlgorithm::initParameters(
const QVariantMap & )
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 );
83 hSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
84 addParameter( hSpacing.release() );
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 );
89 vSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
90 addParameter( vSpacing.release() );
93 zSpacing->setIsDynamic(
true );
95 zSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
96 addParameter( zSpacing.release() );
99 mSpacing->setIsDynamic(
true );
101 mSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
102 addParameter( mSpacing.release() );
107 mIntervalX = parameterAsDouble( parameters, u
"HSPACING"_s, context );
109 if ( mDynamicIntervalX )
110 mIntervalXProperty = parameters.value( u
"HSPACING"_s ).value<
QgsProperty>();
112 mIntervalY = parameterAsDouble( parameters, u
"VSPACING"_s, context );
114 if ( mDynamicIntervalY )
115 mIntervalYProperty = parameters.value( u
"VSPACING"_s ).value<
QgsProperty>();
117 mIntervalZ = parameterAsDouble( parameters, u
"ZSPACING"_s, context );
119 if ( mDynamicIntervalZ )
120 mIntervalZProperty = parameters.value( u
"ZSPACING"_s ).value<
QgsProperty>();
122 mIntervalM = parameterAsDouble( parameters, u
"MSPACING"_s, context );
124 if ( mDynamicIntervalM )
125 mIntervalMProperty = parameters.value( u
"MSPACING"_s ).value<
QgsProperty>();
135 double intervalX = mIntervalX;
136 if ( mDynamicIntervalX )
137 intervalX = mIntervalXProperty.valueAsDouble( context.
expressionContext(), intervalX );
139 double intervalY = mIntervalY;
140 if ( mDynamicIntervalY )
143 double intervalZ = mIntervalZ;
144 if ( mDynamicIntervalZ )
147 double intervalM = mIntervalM;
148 if ( mDynamicIntervalM )
152 if ( outputGeometry.
isNull() )
154 feedback->
reportError( QObject::tr(
"Error snapping geometry %1" ).arg( feature.
id() ) );
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
@ Double
Double/float values.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
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 ¶meters, 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.
@ DoublePositive
Positive double value (including 0).
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