[Qgis-developer] pyQgis: How to create a map composition in 1:20 scale

Luca Mandolesi mandoluca at gmail.com
Thu Jun 21 13:19:48 PDT 2012

Hi to all,
I'm trying to pass to the map composition a layer, setting the scale at 1:20.

with this method I'm creating a bbox around my geometry, then I
convert the measures (in meters) into 1:20 scale to obtain the
dimension of my page.
For example: for a bbox of 1x2 meters I want print a page 5 cm x 10 cm.

	def converter_1_20(self, n):
		n *= 100
		res = n / 20
		return res

	def test_bbox(self):
		self.layerUS.select( [] ) # recuperi tutte le geometrie senza attributi
		featPoly = QgsFeature() # crei una feature vuota per il poligono

		dizionario_id_contains = {}
		lista_quote = []

		self.layerUS.nextFeature( featPoly ) # cicli sulle feature
recuperate, featPoly conterra la feature poligonale attuale
		bbox = featPoly.geometry().boundingBox() # recupera i punti nel bbox
del poligono

		self.height = self.converter_1_20(float(bbox.height()))
		self.width = self.converter_1_20(float(bbox.width()))

Then I try to pass this measures to map composition but I obtain a
different scale. I try to use the metod mapRenderer.setScale(1.20) but
this do nothing.

How can I pass to the printer system to obtain a 1:20 scale map?

Thanks a lot and best regards.

	def print_map(self):
		mapRenderer = self.iface.mapCanvas().mapRenderer()
		c = QgsComposition(mapRenderer)

		#map - this item tells the libraries where to put the map itself.
Here we create a map and stretch it over the whole paper size:
		x, y = 0,-4
		w, h = c.paperWidth(), c.paperHeight()
		composerMap = QgsComposerMap(c, x, y, w, h)

		#scale bar
		item = QgsComposerScaleBar(c)
		item.setStyle('Numeric') # optionally modify the style

		width = self.height*10
		height = self.width*10
		dpi = 300


More information about the Qgis-developer mailing list