Graphics Reference
In-Depth Information
The Set Start Point and Set End Point
actions
The Set Start Point and Set End Point toolbar actions allow the user to set the start and
end points in order to calculate the shortest path between these two points. To implement
these actions, we're going to need a new map tool that lets the user click on a track vertex
to select the starting or ending points.
Note
By positioning the start point and the end point on vertices, we guarantee that the points lie
on a track's LineString. We could theoretically be more sophisticated and snap the starting
and ending points to anywhere along a track segment, but that's more work, and we're try-
ing to keep the implementation simple.
Go back to the mapTools.py module and add the following class definition to this file:
class SelectVertexTool(QgsMapTool, MapToolMixin):
def __init__(self, canvas, trackLayer, onVertexSelected):
QgsMapTool.__init__(self, canvas)
self.onVertexSelected = onVertexSelected
self.setLayer(trackLayer)
self.setCursor(Qt.CrossCursor)
def canvasReleaseEvent(self, event):
feature = self.findFeatureAt(event.pos())
if feature != None:
vertex = self.findVertexAt(feature, event.pos())
if vertex != None:
self.onVertexSelected(feature, vertex)
This map tool uses the mixin's methods to identify which feature and vertex the user
clicked on, and then calls the onVertexSelected() callback to allow the application
to respond to the selection.
Let's use this map tool to implement the Set Start Point and Set End Point actions. Back
in the forestTrails.py module, add the following to the end of the
setupMapTools() method:
Search WWH ::




Custom Search