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." );
 
   63QString QgsSnapToGridAlgorithm::shortDescription()
 const 
   65  return QObject::tr( 
"Modifies the coordinates of geometries in a vector layer, so that all points " 
   66                      "or vertices are snapped to the closest point of a grid." );
 
   69QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance()
 const 
   71  return new QgsSnapToGridAlgorithm();
 
   74void QgsSnapToGridAlgorithm::initParameters( 
const QVariantMap & )
 
   76  auto hSpacing = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( 
"HSPACING" ), QObject::tr( 
"X Grid Spacing" ), 1, QStringLiteral( 
"INPUT" ), 
false, 0 );
 
   77  hSpacing->setIsDynamic( 
true );
 
   79  hSpacing->setDynamicLayerParameterName( QStringLiteral( 
"INPUT" ) );
 
   80  addParameter( hSpacing.release() );
 
   82  auto vSpacing = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( 
"VSPACING" ), QObject::tr( 
"Y Grid Spacing" ), 1, QStringLiteral( 
"INPUT" ), 
false, 0 );
 
   83  vSpacing->setIsDynamic( 
true );
 
   85  vSpacing->setDynamicLayerParameterName( QStringLiteral( 
"INPUT" ) );
 
   86  addParameter( vSpacing.release() );
 
   89  zSpacing->setIsDynamic( 
true );
 
   91  zSpacing->setDynamicLayerParameterName( QStringLiteral( 
"INPUT" ) );
 
   92  addParameter( zSpacing.release() );
 
   95  mSpacing->setIsDynamic( 
true );
 
   97  mSpacing->setDynamicLayerParameterName( QStringLiteral( 
"INPUT" ) );
 
   98  addParameter( mSpacing.release() );
 
  103  mIntervalX = parameterAsDouble( parameters, QStringLiteral( 
"HSPACING" ), context );
 
  105  if ( mDynamicIntervalX )
 
  106    mIntervalXProperty = parameters.value( QStringLiteral( 
"HSPACING" ) ).value<
QgsProperty>();
 
  108  mIntervalY = parameterAsDouble( parameters, QStringLiteral( 
"VSPACING" ), context );
 
  110  if ( mDynamicIntervalY )
 
  111    mIntervalYProperty = parameters.value( QStringLiteral( 
"VSPACING" ) ).value<
QgsProperty>();
 
  113  mIntervalZ = parameterAsDouble( parameters, QStringLiteral( 
"ZSPACING" ), context );
 
  115  if ( mDynamicIntervalZ )
 
  116    mIntervalZProperty = parameters.value( QStringLiteral( 
"ZSPACING" ) ).value<
QgsProperty>();
 
  118  mIntervalM = parameterAsDouble( parameters, QStringLiteral( 
"MSPACING" ), context );
 
  120  if ( mDynamicIntervalM )
 
  121    mIntervalMProperty = parameters.value( QStringLiteral( 
"MSPACING" ) ).value<
QgsProperty>();
 
  131    double intervalX = mIntervalX;
 
  132    if ( mDynamicIntervalX )
 
  133      intervalX = mIntervalXProperty.valueAsDouble( context.
expressionContext(), intervalX );
 
  135    double intervalY = mIntervalY;
 
  136    if ( mDynamicIntervalY )
 
  139    double intervalZ = mIntervalZ;
 
  140    if ( mDynamicIntervalZ )
 
  143    double intervalM = mIntervalM;
 
  144    if ( mDynamicIntervalM )
 
  148    if ( outputGeometry.
isNull() )
 
  150      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