22 QString QgsSnapToGridAlgorithm::name()
const
24 return QStringLiteral(
"snappointstogrid" );
27 QString QgsSnapToGridAlgorithm::displayName()
const
29 return QObject::tr(
"Snap points to grid" );
32 QStringList QgsSnapToGridAlgorithm::tags()
const
34 return QObject::tr(
"snapped,grid,simplify,round,precision" ).split(
',' );
37 QString QgsSnapToGridAlgorithm::group()
const
39 return QObject::tr(
"Vector geometry" );
42 QString QgsSnapToGridAlgorithm::groupId()
const
44 return QStringLiteral(
"vectorgeometry" );
47 QString QgsSnapToGridAlgorithm::outputName()
const
49 return QObject::tr(
"Snapped" );
52 QString 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." );
63 QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance()
const
65 return new QgsSnapToGridAlgorithm();
68 void QgsSnapToGridAlgorithm::initParameters(
const QVariantMap & )
70 std::unique_ptr< QgsProcessingParameterDistance> hSpacing = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral(
"HSPACING" ),
71 QObject::tr(
"X Grid Spacing" ), 1, QStringLiteral(
"INPUT" ),
false, 0 );
72 hSpacing->setIsDynamic(
true );
74 hSpacing->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
75 addParameter( hSpacing.release() );
77 std::unique_ptr< QgsProcessingParameterDistance> vSpacing = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral(
"VSPACING" ),
78 QObject::tr(
"Y Grid Spacing" ), 1, QStringLiteral(
"INPUT" ),
false, 0 );
79 vSpacing->setIsDynamic(
true );
81 vSpacing->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
82 addParameter( vSpacing.release() );
84 std::unique_ptr< QgsProcessingParameterNumber > zSpacing = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"ZSPACING" ),
87 zSpacing->setIsDynamic(
true );
89 zSpacing->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
90 addParameter( zSpacing.release() );
92 std::unique_ptr< QgsProcessingParameterNumber > mSpacing = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"MSPACING" ),
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() ) );