1

Some days ago I upgraded from QGIS 2.18 to 3.6.0 - not knowing that many Python scripts won't run without modifications.

I could convert all of them but not this one:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui 
import processing, sys, time, psycopg2
import pprint

...
creatin layers skipped (got this running)
...

################### Create Graph #####################

mapRenderer = iface.mapCanvas().mapRenderer()

layers = [missingLayer.id(),clipLayer.id()]
mapRenderer.setLayerSet(layers)

c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)

c.setPaperSize(148,90)
c.setPrintResolution(300)

# add map
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c, x,y,w,h)
c.addItem(composerMap)

#output to image
dpi = c.printResolution()
dpmm = dpi / 25.4
width = int(dpmm * c.paperWidth())
height = int(dpmm * c.paperHeight())

BackgroundColor = QColor(0,0,255)

# create output image and initialize it
image = QImage(QSize(width, height), QImage.Format_ARGB32)
image.setDotsPerMeterX(dpmm * 1000)
image.setDotsPerMeterY(dpmm * 1000)
image.fill(BackgroundColor)

header = QgsComposerLabel(c)
header.setItemPosition(40,1)
header.setBackgroundEnabled(0)
newFont = QFont("Comic Sans MS", 18)
header.setFont(newFont)
header.setText("Missing Countries Map")
header.adjustSizeToText()
c.addItem(header)

header2 = QgsComposerLabel(c)
header2.setItemPosition(50,11)
header2.setBackgroundEnabled(0)
newFont2 = QFont("Comic Sans MS", 10)
header2.setFont(newFont2)
header2.setText("TS="+ts)
header2.adjustSizeToText()
c.addItem(header2)

footer = QgsComposerLabel(c)
footer.setItemPosition(124,87.5)
footer.setBackgroundEnabled(0)
newFont3 = QFont("Times Roman", 4)
footer.setFont(newFont3)
footer.setText(time.ctime())
footer.adjustSizeToText()
c.addItem(footer)

legend = QgsComposerLegend(c)
legend.model().setLayerSet(mapRenderer.layerSet())
legend.setItemPosition(5,65)
legend.setFrameEnabled(1)
newFont = QFont("Comic Sans MS", 6)
legend.setStyleFont(QgsComposerLegendStyle.Title, newFont)
legend.setStyleFont(QgsComposerLegendStyle.Subgroup, newFont)
legend.setStyleFont(QgsComposerLegendStyle.SymbolLabel, newFont)
legend.setStyleMargin(QgsComposerLegendStyle.Title,2)
legend.setTitle("Legend")
legend.setSymbolHeight(2)
legend.adjustBoxSize()
c.addItem(legend)

# render the composition
imagePainter = QPainter(image)
imagePainter.setRenderHint(QPainter.Antialiasing)
sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())
targetArea = QRectF(0, 0, width, height)
c.render(imagePainter, targetArea, sourceArea)
imagePainter.end()
imageFile = "missing_boundaries"+ts+".png"
print "saving to "+imageFile
image.save(imageFile)

Yes, I do know the QqsComposer has gone but I can't find any documentation or example for QGIS3 plotting. Except the API description of course.

I don't need the whole script converted - only a hint how to open/create a png the new way.

1

Assuming you have your layout done, you can export it as PNG :

layoutManager = QgsProject.instance().layoutManager()
layout = layoutManager.layoutByName("your_layout_name")

Example of accessing items :

I have called the legend legend as item id, so :

legend_layout = layout.itemById('legend')
legend_layout.setTitle('layout_title_label')
legend_layout.refresh()

# EXPORTING

pdf_path = os.path.join( QgsProject.instance().homePath(), "{}.pdf".format(filename))  #
png_path = os.path.join( QgsProject.instance().homePath(), "{}.png".format(filename))

# you need to create an exported layout object
exporter_layout = QgsLayoutExporter(layout)

exporter_layout.exportToPdf(pdf_path, QgsLayoutExporter.PdfExportSettings())
exporter_layout.exportToImage(png_path, QgsLayoutExporter.ImageExportSettings())

Hope it helps ;)

For code to export, see PyQGIS developer cookbook, Output using print layout

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.