Class: QgsGeometrySnapper

Allows a geometry to be snapped to the geometries within a different reference layer.

Vertices in the geometries will be modified to match the reference layer features within a specified snap tolerance.

Class Hierarchy

Inheritance diagram of qgis.analysis.QgsGeometrySnapper

Base classes

QObject

Methods

snapFeatures

Snaps a set of features to the reference layer and returns the result.

snapGeometry

Snaps a geometry to the reference layer and returns the result.

Signals

featureSnapped

Emitted each time a feature has been processed when calling snapFeatures()

Attributes

EndPointPreferClosest

EndPointPreferNodes

EndPointToEndPoint

PreferClosest

PreferClosestNoExtraVertices

PreferNodes

PreferNodesNoExtraVertices

class qgis.analysis.QgsGeometrySnapper[source]

Bases: QObject

__init__(referenceSource: QgsFeatureSource | None)

Constructor for QgsGeometrySnapper. A reference feature source which contains geometries to snap to must be set. It is assumed that all geometries snapped using this object will have the same CRS as the reference source (ie, no reprojection is performed).

Parameters:

referenceSource (Optional[QgsFeatureSource])

EndPointPreferClosest = 5
EndPointPreferNodes = 4
EndPointToEndPoint = 6
PreferClosest = 1
PreferClosestNoExtraVertices = 3
PreferNodes = 0
PreferNodesNoExtraVertices = 2
class SnapMode

Bases: int

signal featureSnapped[source]

Emitted each time a feature has been processed when calling snapFeatures()

snapFeatures(self, features: Iterable[QgsFeature], snapTolerance: float, mode: QgsGeometrySnapper.SnapMode = QgsGeometrySnapper.PreferNodes) List[QgsFeature][source]

Snaps a set of features to the reference layer and returns the result. This operation is multithreaded for performance. The featureSnapped() signal will be emitted each time a feature is processed. The snap tolerance is specified in the layer units for the reference layer.

Parameters:
  • features (Iterable[QgsFeature])

  • snapTolerance (float)

  • mode (QgsGeometrySnapper.SnapMode = QgsGeometrySnapper.PreferNodes)

Return type:

List[QgsFeature]

snapGeometry(self, geometry: QgsGeometry, snapTolerance: float, mode: QgsGeometrySnapper.SnapMode = QgsGeometrySnapper.PreferNodes) QgsGeometry[source]

Snaps a geometry to the reference layer and returns the result. The geometry must be in the same CRS as the reference layer, and must have the same type as the reference layer geometry. The snap tolerance is specified in the layer units for the reference layer.

Parameters:
  • geometry (QgsGeometry)

  • snapTolerance (float)

  • mode (QgsGeometrySnapper.SnapMode = QgsGeometrySnapper.PreferNodes)

Return type:

QgsGeometry

snapGeometry(geometry: QgsGeometry, snapTolerance: float, referenceGeometries: Iterable[QgsGeometry], mode: QgsGeometrySnapper.SnapMode = QgsGeometrySnapper.PreferNodes) QgsGeometry[source]

Snaps a single geometry against a list of reference geometries.

Parameters:
  • geometry (QgsGeometry)

  • snapTolerance (float)

  • referenceGeometries (Iterable[QgsGeometry])

  • mode (QgsGeometrySnapper.SnapMode = QgsGeometrySnapper.PreferNodes)

Return type:

QgsGeometry