[Qgis-developer] Set Composer scale with pyqgis

roy roy royroge at outlook.com
Thu Jul 7 03:59:47 PDT 2016


Hi, I'm trying to print a map using pyqgis and an existing template:

# i already loaded a layer and zoomed to the layer:

qgis.utils.iface.zoomToActiveLayer()

# now i need to zoom to a "precise" scale
canvas = qgis.utils.iface.mapCanvas()
canvas.zoomScale(2500) # this works for the canvas not in the composer

# load the template
template_file = file(template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition = QgsComposition(canvas.mapSettings())
composition.loadFromTemplate(document)
map_item = composition.getComposerItemById('map')
map_item.setMapCanvas(canvas)

# at this point i need to set the extent to fit the layer
map_item.zoomToExtent(canvas.extent()) # this will not be in scale 2500 
of course

now i need the scale to be say 2500 but i cannot find how to
set the value

thanks for any help, Roy



More information about the Qgis-developer mailing list