Graphics Reference
In-Depth Information
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
Note
As you can see, this method takes an optional
excludeFeature
parameter. This lets us
exclude a given feature from the search, which will be important later on.
Next up, we'll define the
findVertexAt()
method, which identifies the vertex close to
the given click location (if any). Here is the implementation of this method:
def findVertexAt(self, feature, pos):
mapPt,layerPt = self.transformCoordinates(pos)
tolerance = self.calcTolerance(pos)