An important part of customizing a map is to add your logo or other graphics to the composition. In this recipe, we'll add a simple logo to the map.
You will need to download the following zipped shapefile and extract it to your qgis_data
directory, to a subdirectory named ms
:
https://geospatialpython.googlecode.com/svn/Mississippi.zip
You will also need a logo image, which you can download from https://geospatialpython.googlecode.com/svn/trunk/logo.png.
Place the image in your qgis_data/rasters
directory.
If you haven't already done so in the previous recipe, download the MapComposer
library from https://geospatialpython.googlecode.com/svn/MapComposer.py, to simplify the creation of the map composition.
Place the file in the .qgis2/python
directory within your home directory.
In this recipe, we will create the map composition, add the logo image, and save the map as an image. To do this, we need to perform the following steps:
from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgis.gui import * import MapComposer
lyr = QgsVectorLayer("/qgis_data/ms/mississippi.shp", "Mississippi", "ogr") reg = QgsMapLayerRegistry.instance() reg.addMapLayer(lyr) mr = iface.mapCanvas().mapRenderer() qc = MapComposer.MapComposer(qmlr=reg, qmr=mr)
qc.logo = QgsComposerPicture(qc.c)
qc.logo.setPictureFile("/qgis_data/rasters/logo.png")
qc.logo.setSceneRect(QRectF(0,0,50,70))
lx = qc.x + 50 ly = qc.y – 120
mcw = qc.composerMap.rect().width() mch = qc.composerMap.rect().height() lx = qc.x ly = qc.y - 20
qc.output("/qgis_data/map.jpg", "jpg")