26using namespace Qt::StringLiterals;
30QString QgsSnapGeometriesAlgorithm::name()
const
32 return u
"snapgeometries"_s;
35QString QgsSnapGeometriesAlgorithm::displayName()
const
37 return QObject::tr(
"Snap geometries to layer" );
40QString QgsSnapGeometriesAlgorithm::shortHelpString()
const
43 "This algorithm snaps the geometries in a layer. Snapping can be done either to the geometries "
44 "from another layer, or to geometries within the same layer."
48 "A tolerance is specified in layer units to control how close vertices need "
49 "to be to the reference layer geometries before they are snapped."
53 "Snapping occurs to both nodes and edges. Depending on the snapping behavior, "
54 "either nodes or edges will be preferred."
58 "Vertices will be inserted or removed as required to make the geometries match "
59 "the reference geometries."
63QString QgsSnapGeometriesAlgorithm::shortDescription()
const
65 return QObject::tr(
"Snaps the geometries to the geometries within the same layer or from another layer." );
68QStringList QgsSnapGeometriesAlgorithm::tags()
const
70 return QObject::tr(
"geometry,snap,tolerance" ).split(
',' );
73QString QgsSnapGeometriesAlgorithm::group()
const
75 return QObject::tr(
"Vector geometry" );
78QString QgsSnapGeometriesAlgorithm::groupId()
const
80 return u
"vectorgeometry"_s;
90bool QgsSnapGeometriesAlgorithm::supportInPlaceEdit(
const QgsMapLayer *l )
const
92 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
99void QgsSnapGeometriesAlgorithm::initAlgorithm(
const QVariantMap & )
103 QObject::tr(
"Input layer" ),
107 u
"REFERENCE_LAYER"_s,
108 QObject::tr(
"Reference layer" ),
112 auto tolParam = std::make_unique<QgsProcessingParameterDistance>( u
"TOLERANCE"_s, QObject::tr(
"Tolerance" ), 10.0, u
"INPUT"_s,
false, 0.00000001 );
113 tolParam->setMetadata( { QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"decimals"_s, 8 } } ) } } ) } );
114 addParameter( tolParam.release() );
116 const QStringList options = QStringList()
117 << QObject::tr(
"Prefer aligning nodes, insert extra vertices where required" )
118 << QObject::tr(
"Prefer closest point, insert extra vertices where required" )
119 << QObject::tr(
"Prefer aligning nodes, don't insert new vertices" )
120 << QObject::tr(
"Prefer closest point, don't insert new vertices" )
121 << QObject::tr(
"Move end points only, prefer aligning nodes" )
122 << QObject::tr(
"Move end points only, prefer closest point" )
123 << QObject::tr(
"Snap end points to end points only" )
124 << QObject::tr(
"Snap to anchor nodes (single layer only)" );
125 addParameter(
new QgsProcessingParameterEnum( u
"BEHAVIOR"_s, QObject::tr(
"Behavior" ), options,
false, QVariantList() << 0 ) );
129QgsSnapGeometriesAlgorithm *QgsSnapGeometriesAlgorithm::createInstance()
const
131 return new QgsSnapGeometriesAlgorithm();
136 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
140 const std::unique_ptr<QgsProcessingFeatureSource> referenceSource( parameterAsSource( parameters, u
"REFERENCE_LAYER"_s, context ) );
141 if ( !referenceSource )
144 const double tolerance = parameterAsDouble( parameters, u
"TOLERANCE"_s, context );
148 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, source->fields(), source->wkbType(), source->sourceCrs() ) );
154 if ( parameters.value( u
"INPUT"_s ) != parameters.value( u
"REFERENCE_LAYER"_s ) )
156 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
158 throw QgsProcessingException( QObject::tr(
"This mode applies when the input and reference layer are the same." ) );
161 long long processed = 0;
171 outFeature.setGeometry( snapper.snapGeometry( f.
geometry(), tolerance, mode ) );
177 if ( !sink->addFeature( f ) )
184 else if ( mode == 7 )
188 feedback->
pushInfo( QObject::tr(
"Snapped %n geometries.",
nullptr, modified ) );
193 const double step = source->featureCount() > 0 ? 100.0 / ( source->featureCount() * 2 ) : 1;
194 long long processed = 0;
198 QList<QgsFeatureId> editedFeatureIds;
199 QMap<QgsFeatureId, QgsFeature> editedFeatures;
208 editedFeature.setGeometry( snapper.snapFeature( f ) );
210 editedFeatureIds << editedFeature.id();
211 editedFeatures.insert( editedFeature.id(), editedFeature );
217 std::reverse( editedFeatureIds.begin(), editedFeatureIds.end() );
218 for (
const QgsFeatureId &fid : std::as_const( editedFeatureIds ) )
223 QgsFeature editedFeature( editedFeatures.value( fid ) );
224 if ( editedFeature.hasGeometry() )
226 editedFeature.setGeometry( snapper.snapFeature( editedFeature ) );
227 editedFeatures.insert( editedFeature.id(), editedFeature );
230 std::reverse( editedFeatureIds.begin(), editedFeatureIds.end() );
235 for (
const QgsFeatureId &fid : std::as_const( editedFeatureIds ) )
237 QgsFeature outFeature( editedFeatures.value( fid ) );
238 if ( !sink->addFeature( outFeature ) )
249 outputs.insert( u
"OUTPUT"_s, dest );
@ VectorAnyGeometry
Any vector layer with geometry.
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
static int run(const QgsFeatureSource &source, QgsFeatureSink &sink, double thresh, QgsFeedback *feedback)
Run the algorithm on given source and output results to the sink, using threshold value in the source...
Allows a geometry to be snapped to the geometries within a different reference layer.
Allows a set of geometries to be snapped to each other.
Base class for all map layer types.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
Represents a vector layer which manages a vector based dataset.
bool isSpatial() const final
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features