3

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.