7

I am trying to accomplish PyQGIS simple map rendering via the QGIS 2.0 Python console, but end up with only a blank image. I've followed instructions of this link. My objective is to export a simple image of the current extent of layers of interest to a png. Where can I be going wrong?

#identify crs
crs = QgsCoordinateReferenceSystem(3297, QgsCoordinateReferenceSystem.EpsgCrsId)

# 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 crs
render.setDestinationCrs(crs)

# set layer set
layers = qgis.utils.iface.legendInterface.layers()
lst = []
for layer in layers:
    lst.append(layer.())

render.setLayerSet(lst)

# set to CURRENT extent**map of layer's full extent is successfully rendered when QgsRectangle(render.fullExtent() is used)
rect = QgsRectangle(render.extent())
rect.scale(1.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")
4

Don't know if this is just a typo in your post, but if not, this could cause a blank image..

# set layer set
layers = qgis.utils.iface.legendInterface.layers()

This should actually be qgis.utils.iface.legendInterface().layers() (note that legendInterface must be the method call, done by adding braces() )

lst = []
for layer in layers:
    lst.append(layer.())

What you want to do, is to add each layers id by appending layer.id()
Hope that helps!

Edit:
Did you try to save a layer with scale-based visibility enabled? Cause the renderer scales the image to full-extend prior to saving - maybe your layer data is invisible at that zoom-level?!

Btw. this is the code that worked for me (or at least with the vector-, shape-, and raster layers I tested it with ;) )

PS. When using rect = QgsRectangle(renderer.extent()) instead of renderer.fullExtent() the image I get is blank, too.

    imageType = "png"
    pathToFile = "/Users/%u/Desktop/"

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

    renderer = QgsMapRenderer()
    # set layer set
    lst = []


    layers = iface.legendInterface().layers()
    for layer in layers:
        lst.append(layer.id())

    renderer.setLayerSet(lst)
    print "Set rendered"

    # set extent
    rect = QgsRectangle(renderer.fullExtent())
    rect.scale(1.1)
    renderer.setExtent(rect)

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

    print "Prepared to render"

    # do the rendering
    renderer.render(p)

    print "rendered"
    p.end()
    print "painter ended"
    # save image
    img.save(pathToFile + self.activeLayer.name() + "." + imageType ,imageType)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.