2

I show you my code, but basically, I have been following the steps in the official bibliography about Map Composition The code works, but I don't get the items I have been adding to the map (scalebar, labels, and legend). Sorry, some words are in Spanish

QGIS 2.14 (Essen)

I'm using Text Editor of QGIS

from qgis.core import *
from qgis.utils import iface
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *

lyr = QgsVectorLayer("\\ROUTE\\MUNICIPIOS.shp", "Municipios", "ogr")
reg = QgsMapLayerRegistry.instance()
reg.addMapLayer(lyr)
##etiquetamos
#etiquetas = QgsPalLayerSettings()
#etiquetas.readFromLayer(municipios)
#etiquetas.enabled = True
#etiquetas.fieldName = 'Municipios'
#etiquetas.placement= QgsPalLayerSettings.Horizontal
#etiquetas.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'8','')
#etiquetas.writeToLayer(municipios)
##fin de etiquetas

#EMPEZAMOS CON LA IMAGEN

i = QImage(QSize(800,800), QImage.Format_ARGB32_Premultiplied)
c = QColor('#cda56d')
i.fill(c.rgb())
p = QPainter()
p.begin(i)

lyrs = reg.mapLayers().keys()
#AQUI EL MAP RENDERER 
el_renderer = iface.mapCanvas().mapRenderer()
#el_renderer = QgsMapRenderer()
el_renderer.setLayerSet(lyrs)
rect = QgsRectangle(el_renderer.fullExtent())
rect.scale(1.2)
el_renderer.setExtent(rect)
el_renderer.setOutputSize(i.size(), i.logicalDpiX())
el_renderer.render(p)
p.end()

# Aqui tengo mi COMPOSICION DE MAPA ----> . QgsComposition
composition = QgsComposition(el_renderer) 

composition.setPlotStyle(QgsComposition.Print)
#composition.setPaperSize(215.9, 279.4)
# Mi QgsComposerMap
w, h = composition.paperWidth() * .50, composition.paperHeight() * .50
x = (composition.paperWidth() - w) / 2
y = ((composition.paperHeight() - h)) / 2
componer_mapa = QgsComposerMap(composition,x,y,w,h)
composition.addItem(componer_mapa)

#Aqui termina el ComposerMap

#Aqui esta mi etiquetado. QgsComposerLabel
etiqueta = QgsComposerLabel(composition)
#QgsComposerLabel::adjustSizeToText
etiqueta.adjustSizeToText()
#QgsComposerLabel::setText
etiqueta.setText('Municipios')
#QgsComposerLabel::marginX()
etiqueta.marginX()
#QgsComposerLabel::marginY()
etiqueta.marginY()
#QgsComposerLabel::displayText()
#etiqueta.displayText()
#QgsComposerLabel::displayName()
#etiqueta.displayName()
composition.addItem(etiqueta) #DOESN'T WORK

legend = QgsComposerLegend(composition)
legend.model().setLayerSet(el_renderer.layerSet())
composition.addItem(legend)

scBar = QgsComposerScaleBar(composition)
scBar.setStyle('Numeric') 
scBar.setComposerMap(componer_mapa)
scBar.applyDefaultSize()
composition.addItem(scBar) #DOESN'T WORK

i.save("RUTA\\mapa_con_todo.jpg","jpg")

closed as unclear what you're asking by Midavalo, Andre Silva, Chad Cooper, Mapperz Apr 28 '16 at 13:52

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • This post implies that there might be a problem with the size of the item you're trying to add. Have you tried to call adjustSizeToText() after setText()? – ArMoraer Apr 27 '16 at 15:52
  • It is not working, but should I use a new method to adjust all the items? – user64823 Apr 27 '16 at 15:55
  • Do I have to use iface.mapCanvas().refresh() method ? – user64823 Apr 28 '16 at 7:31
  • 1
    Sorry, I'm not really familiar with map composition in PyQgis. I was just sharing a post showing a similar issue. Hope you will find a solution to your problem, though. – ArMoraer Apr 28 '16 at 7:45
  • well, I already have the solution, but the great problem now is that I can't see my shapefile features jeje – user64823 Apr 28 '16 at 9:38

Browse other questions tagged or ask your own question.