[Qgis-user] getting QgsMapCanvasSnapper to work
Stefanie Tellex
stefie10 at media.mit.edu
Tue Nov 18 09:47:11 PST 2008
Here's what I do for snapping in python:
def canvasReleaseEvent(self, e):
print "mouse click", e
mapcoords =
self.canvas.getCoordinateTransform().toMapCoordinates(e.x(), e.y())
print "map position", mapcoords
snapdist = 2
r = qgis_utils.snap(self.layer, mapcoords,
QgsSnapper.SnapToVertexAndSegment,
snapdist=0.3)
if r != None:
fid = r.snappedAtGeometry
self.emit(SIGNAL("selectedFeature(int)"), (fid))
def snap(layer, point, snaptype, snapdist=1):
print "\nsnapping"
x, reslist = layer.snapWithContext(point, snapdist, snaptype)
print "x", x
if x != 0:
return None
return getBestSnapResult(reslist)
def getBestSnapResult(snapresult):
print "result", snapresult
keys = snapresult.keys()
keys.sort()
for k in keys:
l = snapresult[k]
for r in l:
return r
It works, but not great. It doesn't always get a snap result, even when
I think it really should. I think part of the problem is that I'm using
a fixed snapdist no matter what the zoom level is... but I haven't
looked at it closely.
Möri Cedric pisze:
> Hi all!
>
> I'm trying to snap a vertex with my python plugin. (Many thanks to Martin for the quick work with the bindings!)
>
> My code looks like this:
>
> def canvasReleaseEvent(self,event):
> x = event.pos().x()
> y = event.pos().y()
>
> startingPoint = QPoint(x,y)
> result = []
> excludePoints = []
>
> snapper = QgsMapCanvasSnapper(self.canvas)
>
> snapper.snapToCurrentLayer (startingPoint,result,QgsSnapper.SnapToVertex, 10000, excludePoints)
> print "result: ",result
>
> The problem is, whatever I do, the result keeps empty. I tried to play with tolerance and scale and I'm quite sure it has to find a vertex but without any success (the layer unit is meter).
>
> So I thought about using QgsSnapper directly. But I'm not able to set it up properly. Doing the following snippet always leads to a attribut error on the snapLayer struct.
>
> snapLayer = QgsSnapper.SnapLayer
> snapLayer.mLayer = self.iface.mapCanvas().currentLayer()
> snapLayer.mTolerance = 1000
> snapLayer.mSnapTo = QgsSnapper.SnapToVertex
>
>
> So, if anyone has a hint why the QgsMapCanvasSnapper doesn't want to give me a result or how I set up the struct correctly, it would be wonderful to share it with me :-)
>
> Best regards and kudos to all the helping people here on the list!
> Cédric
>
>
> mit freundlichen Grüssen
>
> Cédric Möri
> GIS-Informatiker
>
> --
> Kanton Solothurn
> Bau- und Justizdepartement
> Amt für Geoinformation
> Rötistrasse 4
> 4501 Solothurn
>
> Telefon: +41 (0)32 627 24 75
> Telefax: +41 (0)32 627 22 14
> mailto:cedric.moeri at bd.so.ch
> http://www.agi.so.ch
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-user
x
More information about the Qgis-user
mailing list