How can I get the coordinates of generated labels on Canvas? Or is there a way to make static labels for each feature? I want the labels to not move when the canvas changes scale or moves.

My situation: my plugin loads the pipelines (LineString) into memory layer. Label for the pipelines must be parallel and above the line. After first rendering of the canvas I need to remember the position of these labels. The ideal variant would be to get the x, y coordinates and angle of rotation of each labels and put them into additional fields of layer.

I'm interested in only stage of extraction of coordinates.


1 Answer 1


For me in a simple:

mc = self.iface.mapCanvas()
lr = mc.labelingResults()
extent = mc.extent()


for lrl in lr.labelsWithinRect(extent):
    if lrl.layerID == labellayer.id():
        angle = 90 - QgsPoint(lrl.cornerPoints[0]).azimuth(QgsPoint(lrl.cornerPoints[1]))
        fieldIdx = labellayerpr.fields().indexFromName('xpos')
        fieldIdy = labellayerpr.fields().indexFromName('ypos')
        fieldIdr = labellayerpr.fields().indexFromName('rpos')
        xlabel = lrl.cornerPoints[0][0]
        ylabel = lrl.cornerPoints[0][1]
        rlabel = angle
        labellayer.changeAttributeValue(lrl.featureId, fieldIdx, xlabel)
        labellayer.changeAttributeValue(lrl.featureId, fieldIdy, ylabel)
        labellayer.changeAttributeValue(lrl.featureId, fieldIdr, rlabel)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.