Graphics Reference
In-Depth Information
Here is the implementation of our snapToNearestVertex() method:
def snapToNearestVertex(self, pos, trackLayer,
excludeFeature=None):
mapPt,layerPt = self.transformCoordinates(pos)
feature = self.findFeatureAt(pos, excludeFeature)
if feature == None: return layerPt
vertex = self.findVertexAt(feature, pos)
if vertex == None: return layerPt
return feature.geometry().vertexAt(vertex)
As you can see, we use our findFeatureAt() method to search for features that are
close to the given click point. If we find a feature, we then call
self.findVertexAt() to find the vertex close to where the user clicked. Finally, if
we find a vertex, we return the coordinates of that vertex. Otherwise, we return the origin-
al click position converted to layer coordinates.
With these extensions to our mixin class, we can easily add snapping to our AddTrack
tool. All we have to do is replace our addVertex() method with the following:
def addVertex(self, canvasPoint):
snapPt = self.snapToNearestVertex(canvasPoint,
self.layer)
mapPt = self.toMapCoordinates(self.layer, snapPt)
self.rubberBand.addPoint(mapPt)
self.capturedPoints.append(snapPt)
self.tempRubberBand.reset(QGis.Line)
self.tempRubberBand.addPoint(mapPt)
Now that we have vertex snapping enabled, it'll be easy to ensure that our tracks are con-
nected. Note that we'll also use vertex snapping when we edit a track and when the user
selects the start and end points for the Shortest Available Path calculation. This is why
we've added these methods to our mixin rather than to the AddTrack tool.
Search WWH ::




Custom Search