26QString QgsSnapGeometriesAlgorithm::name()
const
28 return QStringLiteral(
"snapgeometries" );
31QString QgsSnapGeometriesAlgorithm::displayName()
const
33 return QObject::tr(
"Snap geometries to layer" );
36QString QgsSnapGeometriesAlgorithm::shortHelpString()
const
38 return QObject::tr(
"This algorithm snaps the geometries in a layer. Snapping can be done either to the geometries "
39 "from another layer, or to geometries within the same layer." )
40 + QStringLiteral(
"\n\n" )
41 + QObject::tr(
"A tolerance is specified in layer units to control how close vertices need "
42 "to be to the reference layer geometries before they are snapped." )
43 + QStringLiteral(
"\n\n" )
44 + QObject::tr(
"Snapping occurs to both nodes and edges. Depending on the snapping behavior, "
45 "either nodes or edges will be preferred." )
46 + QStringLiteral(
"\n\n" )
47 + QObject::tr(
"Vertices will be inserted or removed as required to make the geometries match "
48 "the reference geometries." );
51QString QgsSnapGeometriesAlgorithm::shortDescription()
const
53 return QObject::tr(
"Snaps the geometries to the geometries within the same layer or from another layer." );
56QStringList QgsSnapGeometriesAlgorithm::tags()
const
58 return QObject::tr(
"geometry,snap,tolerance" ).split(
',' );
61QString QgsSnapGeometriesAlgorithm::group()
const
63 return QObject::tr(
"Vector geometry" );
66QString QgsSnapGeometriesAlgorithm::groupId()
const
68 return QStringLiteral(
"vectorgeometry" );
78bool QgsSnapGeometriesAlgorithm::supportInPlaceEdit(
const QgsMapLayer *l )
const
80 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
87void QgsSnapGeometriesAlgorithm::initAlgorithm(
const QVariantMap & )
92 auto tolParam = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral(
"TOLERANCE" ), QObject::tr(
"Tolerance" ), 10.0, QStringLiteral(
"INPUT" ),
false, 0.00000001 );
93 tolParam->setMetadata(
94 { QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"decimals" ), 8 } } ) } } )
97 addParameter( tolParam.release() );
99 const QStringList options = QStringList()
100 << QObject::tr(
"Prefer aligning nodes, insert extra vertices where required" )
101 << QObject::tr(
"Prefer closest point, insert extra vertices where required" )
102 << QObject::tr(
"Prefer aligning nodes, don't insert new vertices" )
103 << QObject::tr(
"Prefer closest point, don't insert new vertices" )
104 << QObject::tr(
"Move end points only, prefer aligning nodes" )
105 << QObject::tr(
"Move end points only, prefer closest point" )
106 << QObject::tr(
"Snap end points to end points only" )
107 << QObject::tr(
"Snap to anchor nodes (single layer only)" );
108 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"BEHAVIOR" ), QObject::tr(
"Behavior" ), options,
false, QVariantList() << 0 ) );
112QgsSnapGeometriesAlgorithm *QgsSnapGeometriesAlgorithm::createInstance()
const
114 return new QgsSnapGeometriesAlgorithm();
119 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
123 const std::unique_ptr<QgsProcessingFeatureSource> referenceSource( parameterAsSource( parameters, QStringLiteral(
"REFERENCE_LAYER" ), context ) );
124 if ( !referenceSource )
127 const double tolerance = parameterAsDouble( parameters, QStringLiteral(
"TOLERANCE" ), context );
131 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, source->fields(), source->wkbType(), source->sourceCrs() ) );
137 if ( parameters.value( QStringLiteral(
"INPUT" ) ) != parameters.value( QStringLiteral(
"REFERENCE_LAYER" ) ) )
139 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
141 throw QgsProcessingException( QObject::tr(
"This mode applies when the input and reference layer are the same." ) );
144 long long processed = 0;
154 outFeature.setGeometry( snapper.snapGeometry( f.
geometry(), tolerance, mode ) );
160 if ( !sink->addFeature( f ) )
167 else if ( mode == 7 )
171 feedback->
pushInfo( QObject::tr(
"Snapped %n geometries.",
nullptr, modified ) );
176 const double step = source->featureCount() > 0 ? 100.0 / ( source->featureCount() * 2 ) : 1;
177 long long processed = 0;
181 QList<QgsFeatureId> editedFeatureIds;
182 QMap<QgsFeatureId, QgsFeature> editedFeatures;
191 editedFeature.setGeometry( snapper.snapFeature( f ) );
193 editedFeatureIds << editedFeature.id();
194 editedFeatures.insert( editedFeature.id(), editedFeature );
200 std::reverse( editedFeatureIds.begin(), editedFeatureIds.end() );
201 for (
const QgsFeatureId &fid : std::as_const( editedFeatureIds ) )
206 QgsFeature editedFeature( editedFeatures.value( fid ) );
207 if ( editedFeature.hasGeometry() )
209 editedFeature.setGeometry( snapper.snapFeature( editedFeature ) );
210 editedFeatures.insert( editedFeature.id(), editedFeature );
213 std::reverse( editedFeatureIds.begin(), editedFeatureIds.end() );
218 for (
const QgsFeatureId &fid : std::as_const( editedFeatureIds ) )
220 QgsFeature outFeature( editedFeatures.value( fid ) );
221 if ( !sink->addFeature( outFeature ) )
232 outputs.insert( QStringLiteral(
"OUTPUT" ), 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