3

So, basically I've created a QgsRubberBand which copies feature from layer. And I need to be able visually move this rubberband on mouse event 'move'. Actually same visualisation like during self.iface.actionMoveFeature()

class mMapTool(QgsMapTool):

def __init__(self, mainLayer, canvas):

    self.mlayer = mainLayer
    self.canvas = canvas
    self.active = False
    QgsMapTool.__init__(self, self.canvas)
    self.rubberBand = QgsRubberBand(self.canvas, QGis.Polygon)
    mFillColor = QColor(0, 0, 0, 88);
    self.rubberBand.setColor(mFillColor)
    self.rubberBand.setWidth(2)
    self.reset()


def reset(self):

    self.startPoint = self.endPoint = None
    self.isEmittingPoint = False
    self.rubberBand.reset(QGis.Polygon)

def canvasPressEvent(self, e):


    global mdict
    mdict = {}

    if e.button() == 1:
        self.isEmittingPoint = True
        for feature in self.mlayer.selectedFeatures():
            centroid_tuple = (feature.geometry().centroid().asPoint().x(),
                              feature.geometry().centroid().asPoint().y())
            mdict[feature.id()] = [centroid_tuple]
            geo = feature.geometry()
            break

        self.rubberBand.addGeometry(geo, None)

def get_canvas_point(self, position):

    x = position.x()
    y = position.y()
    trans = self.canvas.getCoordinateTransform()
    return trans.toMapCoordinates(x, y)

def canvasMoveEvent(self, e):
    if not self.isEmittingPoint:
        return
    pos = e.pos()
    point = self.get_canvas_point(pos)
    self.rubberBand.movePoint(point)
    self.rubberBand.show()

But my current code doesn't work. Tried different options. Searched for solution everywhere but it seems like I'm only one with this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.