Monday 3 July 2017

PyQGIS script works in QGIS console but produces no error or output as standalone


I'm trying map rendering with stand alone script, so I have small script which works fine in python console provided QGIS, however when I run same code in stand-alone script it executes without error, but output is not produced.


Here is my standalone script:



from qgis.core import *
from qgis.gui import *
from qgis.utils import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *

QgsApplication.setPrefixPathddd("C:\Program Files (x86)\QGIS Valmiera", True)

QgsApplication.initQgis()


mapinstance = QgsMapLayerRegistry.instance()
mapinstance.removeAllMapLayers()

layer = QgsVectorLayer("F:\scripts\map\wb.shp", "test", "ogr")

if layer.isValid():
print "shplayer succesfully loaded - adding to mapinstance"
mapinstance.addMapLayer(layer)
else:

print "shplayer failed to load!"

print mapinstance.mapLayers()

# create image
img = QImage(QSize(800,600), QImage.Format_ARGB32_Premultiplied)
# set image 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
lst = [ layer.id() ] # add ID of every layer
render.setLayerSet(lst)
# set extent
rect = QgsRectangle(render.fullExtent())
#rect.scale(1.39800703)

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('F:\scripts\out.png',"png")


QgsApplication.exitQgis()


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...