I'm trying to set label on polygon and line layer using PyQGIS. Setting label on polygon layer succeed, but fails on line layer.
Here my script :
#Set polygon layer
palyr = QgsPalLayerSettings()
palyr.readFromLayer(layer_base)
palyr.enabled = True # this works
palyr.fieldName = 'no_urut' # this works
palyr.fontSizeInMapUnits = False
palyr.textFont.setPointSize(6) # results in 4 - seems to be integer only
palyr.textColor = QColor(0, 0, 0) # this works
palyr.writeToLayer(layer_base)
#set label line layer
palyr3 = QgsPalLayerSettings()
palyr3.readFromLayer(layer_jalan)
palyr3.enabled = True # this works
palyr3.fieldName = 'nama_jalan' # this works
palyr3.fontSizeInMapUnits = False
palyr3.textFont.setPointSize(11) # results in 4 - seems to be integer only
palyr3.textColor = QColor(0, 0, 255) # this works
palyr3.placementFlags = QgsPalLayerSettings.AboveLine
palyr3.writeToLayer(layer_jalan)
Any ideas ?, btw when I load it and check on the layer properties, the label has been set according to the properties in the script above and, when I push the apply button on this properties window, the label appears on the canvas.
Answer
There is probably a mistake in the syntax for QgsPalLayerSettings().
You may try to write this:
palyr3.placement = QgsPalLayerSettings.AboveLine
instead of:
palyr3.placementFlags = QgsPalLayerSettings.AboveLine
Furthermore, you should add this line at the end of the code:
iface.legendInterface().refreshLayerSymbology(layer_base)
With these edits, you should use this code (it worked for me):
palyr3 = QgsPalLayerSettings()
palyr3.readFromLayer(layer_jalan)
palyr3.enabled = True # this works
palyr3.fieldName = 'nama_jalan' # this works
palyr3.fontSizeInMapUnits = False
palyr3.textFont.setPointSize(11) # results in 4 - seems to be integer only
palyr3.textColor = QColor(0, 0, 255) # this works
palyr3.placement = QgsPalLayerSettings.AboveLine
palyr3.writeToLayer(layer_jalan)
iface.legendInterface().refreshLayerSymbology(layer_jalan)
No comments:
Post a Comment