QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmsnaptogrid.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsnaptogrid.cpp
3 --------------------------
4 begin : October 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
21
22QString QgsSnapToGridAlgorithm::name() const
23{
24 return QStringLiteral( "snappointstogrid" );
25}
26
27QString QgsSnapToGridAlgorithm::displayName() const
28{
29 return QObject::tr( "Snap points to grid" );
30}
31
32QStringList QgsSnapToGridAlgorithm::tags() const
33{
34 return QObject::tr( "snapped,grid,simplify,round,precision" ).split( ',' );
35}
36
37QString QgsSnapToGridAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsSnapToGridAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsSnapToGridAlgorithm::outputName() const
48{
49 return QObject::tr( "Snapped" );
50}
51
52QString QgsSnapToGridAlgorithm::shortHelpString() const
53{
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." );
61}
62
63QgsSnapToGridAlgorithm *QgsSnapToGridAlgorithm::createInstance() const
64{
65 return new QgsSnapToGridAlgorithm();
66}
67
68void QgsSnapToGridAlgorithm::initParameters( const QVariantMap & )
69{
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 );
72 hSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "HSPACING" ), QObject::tr( "X Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
73 hSpacing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
74 addParameter( hSpacing.release() );
75
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 );
78 vSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "VSPACING" ), QObject::tr( "Y Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
79 vSpacing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
80 addParameter( vSpacing.release() );
81
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 );
84 zSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ZSPACING" ), QObject::tr( "Z Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
85 zSpacing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
86 addParameter( zSpacing.release() );
87
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 );
90 mSpacing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MSPACING" ), QObject::tr( "M Grid Spacing" ), QgsPropertyDefinition::DoublePositive ) );
91 mSpacing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
92 addParameter( mSpacing.release() );
93}
94
95bool QgsSnapToGridAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
96{
97 mIntervalX = parameterAsDouble( parameters, QStringLiteral( "HSPACING" ), context );
98 mDynamicIntervalX = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "HSPACING" ) );
99 if ( mDynamicIntervalX )
100 mIntervalXProperty = parameters.value( QStringLiteral( "HSPACING" ) ).value<QgsProperty>();
101
102 mIntervalY = parameterAsDouble( parameters, QStringLiteral( "VSPACING" ), context );
103 mDynamicIntervalY = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "VSPACING" ) );
104 if ( mDynamicIntervalY )
105 mIntervalYProperty = parameters.value( QStringLiteral( "VSPACING" ) ).value<QgsProperty>();
106
107 mIntervalZ = parameterAsDouble( parameters, QStringLiteral( "ZSPACING" ), context );
108 mDynamicIntervalZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ZSPACING" ) );
109 if ( mDynamicIntervalZ )
110 mIntervalZProperty = parameters.value( QStringLiteral( "ZSPACING" ) ).value<QgsProperty>();
111
112 mIntervalM = parameterAsDouble( parameters, QStringLiteral( "MSPACING" ), context );
113 mDynamicIntervalM = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MSPACING" ) );
114 if ( mDynamicIntervalM )
115 mIntervalMProperty = parameters.value( QStringLiteral( "MSPACING" ) ).value<QgsProperty>();
116
117 return true;
118}
119
120QgsFeatureList QgsSnapToGridAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
121{
122 QgsFeature f = feature;
123 if ( f.hasGeometry() )
124 {
125 double intervalX = mIntervalX;
126 if ( mDynamicIntervalX )
127 intervalX = mIntervalXProperty.valueAsDouble( context.expressionContext(), intervalX );
128
129 double intervalY = mIntervalY;
130 if ( mDynamicIntervalY )
131 intervalY = mIntervalYProperty.valueAsDouble( context.expressionContext(), intervalY );
132
133 double intervalZ = mIntervalZ;
134 if ( mDynamicIntervalZ )
135 intervalZ = mIntervalZProperty.valueAsDouble( context.expressionContext(), intervalZ );
136
137 double intervalM = mIntervalM;
138 if ( mDynamicIntervalM )
139 intervalM = mIntervalMProperty.valueAsDouble( context.expressionContext(), intervalM );
140
141 const QgsGeometry outputGeometry = f.geometry().snappedToGrid( intervalX, intervalY, intervalZ, intervalM );
142 if ( outputGeometry.isNull() )
143 {
144 feedback->reportError( QObject::tr( "Error snapping geometry %1" ).arg( feature.id() ) );
145 }
146 f.setGeometry( outputGeometry );
147 }
148 return QgsFeatureList() << f;
149}
150
151Qgis::ProcessingFeatureSourceFlags QgsSnapToGridAlgorithm::sourceFlags() const
152{
154}
155
@ 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.
Definition qgis.h:3507
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
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 &parameters, 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.
Definition qgsproperty.h:45
@ DoublePositive
Positive double value (including 0)
Definition qgsproperty.h:56
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