Graphics Reference
In-Depth Information
return tolerance
We calculate this by identifying two points on the map canvas that are ten pixels apart,
and converting both of these coordinates into layer coordinates. We then return the dis-
tance between these two points, which will be the tolerance in the layer coordinate sys-
tem.
We now get to the interesting part: moving and deleting vertices. Let's start with the meth-
od to move a vertex to a new location:
def moveVertexTo(self, pos):
geometry = self.feature.geometry()
layerPt = self.toLayerCoordinates(self.layer, pos)
geometry.moveVertex(layerPt.x(), layerPt.y(),
self.vertex)
self.layer.changeGeometry(self.feature.id(),
geometry)
self.onGeometryChanged()
As you can see, we convert the position into layer coordinates, tell the QgsGeometry
object to move the vertex to this location, and then tell the layer to save the updated geo-
metry. Finally, we use the onGeometryChanged callable object to tell the rest of the
application that the geometry has been changed.
Deleting a vertex is slightly more complicated, as we have to prevent the user from delet-
ing a vertex if there aren't enough vertices left to make a valid geometry—LineString
must have a minimum of two vertices, while a polygon must have at least three. Here is
the implementation of our deleteVertex() method:
def deleteVertex(self, feature, vertex):
geometry = feature.geometry()
if geometry.wkbType() == QGis.WKBLineString:
lineString = geometry.asPolyline()
if len(lineString) <= 2:
return
elif geometry.wkbType() == QGis.WKBPolygon:
polygon = geometry.asPolygon()
Search WWH ::




Custom Search