[Qgis-developer] How to render a transparent raster layer on top of one or more vector layers with PyQgis?

Pavel Ernesto Simo Rodriguez pavelsimo106 at hotmail.com
Fri Aug 16 20:38:48 PDT 2013


I been trying to render a map with a 3 vector layers and one raster 
layer. The idea is that the raster layer should be on top of the other 3
 vector layers. The problem is that even when I add the transparency 
parameters to the Raster Layer I can't visualize the vector layers. In 
other words, the Raster Layer completely covers the area.

#!/usr/bin/python
# -*- coding: utf-8 -*-
from qgis.core import *
from qgis.gui import *
from qgis.utils import *

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtCore import QFileInfo, QSettings


QgsApplication.setPrefixPath("/usr", True)

QgsApplication.initQgis()

# raster layer
filename = "points.png"
file_info = QFileInfo(filename)
base_name = file_info.baseName()

l_raster = QgsRasterLayer(filename, base_name)
l_raster.setCrs( QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) )
l_raster.setTransparency(20) 

l_a = QgsVectorLayer("a.MIF", "Layer A", "ogr")
l_b = QgsVectorLayer("b.MIF", "Layer B", "ogr")
l_c  = QgsVectorLayer("c.MIF", "Layer C", "ogr")

if not l_raster.isValid():
  print "Raster Layer failed to load!"  

if not l_a.isValid():
  print "A Layer failed to load!"

if not l_b.isValid():
  print "B Layer failed to load!"

if not l_c.isValid():
  print "C Layer failed to load!"  

QgsMapLayerRegistry.instance().addMapLayer(l_raster)
QgsMapLayerRegistry.instance().addMapLayer(l_a)
QgsMapLayerRegistry.instance().addMapLayer(l_b)
QgsMapLayerRegistry.instance().addMapLayer(l_c)

# create image
img = QImage(QSize(800,600), QImage.Format_ARGB32_Premultiplied)

# set image's background color
color = QColor(255,255,255)
img.fill(color.rgb())

# create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)

render = QgsMapRenderer()

# set layer set
# add ID of every layer
lst = [l_raster.getLayerID(), l_a.getLayerID(),  l_b.getLayerID(), l_c.getLayerID()]  
render.setLayerSet(lst)

# set extent
rect = QgsRectangle(render.fullExtent())
rect.scale(1)
render.setExtent(rect)

# set output size
render.setOutputSize(img.size(), img.logicalDpiX())

# do the rendering
render.render(p)

p.end()

# save image
img.save("render.png","png")

QgsApplication.exitQgis()



 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130817/757dd2bf/attachment.html>


More information about the Qgis-developer mailing list