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
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."
69QString QgsSnapToGridAlgorithm::shortDescription()
const
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."
77QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance()
const
79 return new QgsSnapToGridAlgorithm();
82void QgsSnapToGridAlgorithm::initParameters(
const QVariantMap & )
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 );
87 hSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
88 addParameter( hSpacing.release() );
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 );
93 vSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
94 addParameter( vSpacing.release() );
97 zSpacing->setIsDynamic(
true );
99 zSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
100 addParameter( zSpacing.release() );
103 mSpacing->setIsDynamic(
true );
105 mSpacing->setDynamicLayerParameterName( u
"INPUT"_s );
106 addParameter( mSpacing.release() );
111 mIntervalX = parameterAsDouble( parameters, u
"HSPACING"_s, context );
113 if ( mDynamicIntervalX )
114 mIntervalXProperty = parameters.value( u
"HSPACING"_s ).value<
QgsProperty>();
116 mIntervalY = parameterAsDouble( parameters, u
"VSPACING"_s, context );
118 if ( mDynamicIntervalY )
119 mIntervalYProperty = parameters.value( u
"VSPACING"_s ).value<
QgsProperty>();
121 mIntervalZ = parameterAsDouble( parameters, u
"ZSPACING"_s, context );
123 if ( mDynamicIntervalZ )
124 mIntervalZProperty = parameters.value( u
"ZSPACING"_s ).value<
QgsProperty>();
126 mIntervalM = parameterAsDouble( parameters, u
"MSPACING"_s, context );
128 if ( mDynamicIntervalM )
129 mIntervalMProperty = parameters.value( u
"MSPACING"_s ).value<
QgsProperty>();
139 double intervalX = mIntervalX;
140 if ( mDynamicIntervalX )
141 intervalX = mIntervalXProperty.valueAsDouble( context.
expressionContext(), intervalX );
143 double intervalY = mIntervalY;
144 if ( mDynamicIntervalY )
147 double intervalZ = mIntervalZ;
148 if ( mDynamicIntervalZ )
151 double intervalM = mIntervalM;
152 if ( mDynamicIntervalM )
156 if ( outputGeometry.
isNull() )
158 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