Vertex snapping
To implement vertex snapping, we're going to add some new methods to MapToolMixin
. We'll start with the findFeatureAt()
method. This method finds a feature close to the click location. Here is the implementation of this method:
def findFeatureAt(self, pos, excludeFeature=None): mapPt,layerPt = self.transformCoordinates(pos) tolerance = self.calcTolerance(pos) searchRect = QgsRectangle(layerPt.x() - tolerance, layerPt.y() - tolerance, layerPt.x() + tolerance, layerPt.y() + tolerance) request = QgsFeatureRequest() request.setFilterRect(searchRect) request.setFlags(QgsFeatureRequest.ExactIntersect) for feature in self.layer.getFeatures(request): if excludeFeature != None: if feature.id() == excludeFeature.id(): continue return feature return None