22QString QgsSnapToGridAlgorithm::name()
const
24 return QStringLiteral(
"snappointstogrid" );
27QString QgsSnapToGridAlgorithm::displayName()
const
29 return QObject::tr(
"Snap points to grid" );
32QStringList QgsSnapToGridAlgorithm::tags()
const
34 return QObject::tr(
"snapped,grid,simplify,round,precision" ).split(
',' );
37QString QgsSnapToGridAlgorithm::group()
const
39 return QObject::tr(
"Vector geometry" );
42QString QgsSnapToGridAlgorithm::groupId()
const
44 return QStringLiteral(
"vectorgeometry" );
47QString QgsSnapToGridAlgorithm::outputName()
const
49 return QObject::tr(
"Snapped" );
52QString QgsSnapToGridAlgorithm::shortHelpString()
const
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." );
63QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance()
const
65 return new QgsSnapToGridAlgorithm();
68void QgsSnapToGridAlgorithm::initParameters(
const QVariantMap & )
70 std::unique_ptr<QgsProcessingParameterDistance> hSpacing = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral(
"HSPACING" ), QObject::tr(
"X Grid Spacing" ), 1, QStringLiteral(
"INPUT" ),
false, 0 );
71 hSpacing->setIsDynamic(
true );
73 hSpacing->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
74 addParameter( hSpacing.release() );
76 std::unique_ptr<QgsProcessingParameterDistance> vSpacing = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral(
"VSPACING" ), QObject::tr(
"Y Grid Spacing" ), 1, QStringLiteral(
"INPUT" ),
false, 0 );
77 vSpacing->setIsDynamic(
true );
79 vSpacing->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
80 addParameter( vSpacing.release() );
82 std::unique_ptr<QgsProcessingParameterNumber> zSpacing = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"ZSPACING" ), QObject::tr(
"Z Grid Spacing" ),
Qgis::ProcessingNumberParameterType::Double, 0,
false, 0 );
83 zSpacing->setIsDynamic(
true );
85 zSpacing->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
86 addParameter( zSpacing.release() );
88 std::unique_ptr<QgsProcessingParameterNumber> mSpacing = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"MSPACING" ), QObject::tr(
"M Grid Spacing" ),
Qgis::ProcessingNumberParameterType::Double, 0,
false, 0 );
89 mSpacing->setIsDynamic(
true );
91 mSpacing->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
92 addParameter( mSpacing.release() );
97 mIntervalX = parameterAsDouble( parameters, QStringLiteral(
"HSPACING" ), context );
99 if ( mDynamicIntervalX )
100 mIntervalXProperty = parameters.value( QStringLiteral(
"HSPACING" ) ).value<
QgsProperty>();
102 mIntervalY = parameterAsDouble( parameters, QStringLiteral(
"VSPACING" ), context );
104 if ( mDynamicIntervalY )
105 mIntervalYProperty = parameters.value( QStringLiteral(
"VSPACING" ) ).value<
QgsProperty>();
107 mIntervalZ = parameterAsDouble( parameters, QStringLiteral(
"ZSPACING" ), context );
109 if ( mDynamicIntervalZ )
110 mIntervalZProperty = parameters.value( QStringLiteral(
"ZSPACING" ) ).value<
QgsProperty>();
112 mIntervalM = parameterAsDouble( parameters, QStringLiteral(
"MSPACING" ), context );
114 if ( mDynamicIntervalM )
115 mIntervalMProperty = parameters.value( QStringLiteral(
"MSPACING" ) ).value<
QgsProperty>();
125 double intervalX = mIntervalX;
126 if ( mDynamicIntervalX )
127 intervalX = mIntervalXProperty.valueAsDouble( context.
expressionContext(), intervalX );
129 double intervalY = mIntervalY;
130 if ( mDynamicIntervalY )
133 double intervalZ = mIntervalZ;
134 if ( mDynamicIntervalZ )
137 double intervalM = mIntervalM;
138 if ( mDynamicIntervalM )
142 if ( outputGeometry.
isNull() )
144 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