PyQGIS

Tips and tricks, and lots of code scraps for using with Python and Quantum GIS.

Using the Graphical Modeler in QGIS 3.0

While I've written a handful of plugins in the past for QGIS, I've always wanted to learn about the graphical modeler which is a lot easier than writing all the Python by hand, especially creating the dialogs and setting up the methods yourself. 

Today I’ve been studying up a lot on Python and Qt

Today I’ve been studying up a lot on Python and Qt. πŸ‡Ά

Qt is a library used to build graphical interfaces, while Python is a very popular and widely used scripting language. I have coded a bit in PyQt for various QGIS plugins in over the years, but I am hardly an expert on either Python or Qt so I am trying to understand the ins and outs more. While I doubt I have much of a need for GUI programs — I prefer things that are small, simple command line things — the Unix way — it’s good to have a better understanding of Qt for the QGIS plugins I use for my various projects.

My Favorite Features of QGIS 3.0…To Date β€” Bird’s Eye View GIS

My Favorite Features of QGIS 3.0…To Date β€” Bird’s Eye View GIS

I wanted to upgrade to QGIS 3.0 but I am going to hold off as I am running Ubuntu 16.04 LTS and they are not going to backport libraries to this soon to be obsolete version of software. Eventually I'll upgrade to Ubuntu 18.04 LTS when that comes out and also install QGIS 3.01 or whatever the bug fix version is at that point. I'm going to have to port over my pyqgis scripts that I use to make maps which is another consideration before I can get up and going with this next version of the software but I'm excited about some of the new features like easy label adjustments and more coloring options. QGIS is rapidly becoming a very professional grade of software. I should think seriously about donating to their cause.

Code Scrap for Exporting a Map to a Raster PDF

I don’t like the vector Adobe Acrobat PDF that Quantum GIS exports by default if you don’t specifically check “Export as Raster” in the Composer View. It’s true that Vector PDF is a higher quality, but the files are much larger and slower loading. A 300 DPI raster PDF file will suffice for most printing needs.

I struggled for a number of days to figure out the best way to export a Raster PDF from Quantum GIS with PyQGIS. I ended up giving up, preferring to just export a high-resolution JPEG and convert the JPEG to a Adobe PDF using ImageMagick’s convert command. It gave me much smaller PDF files due to higher compression then what you get through the PDF export built into QGIS’s Composers with the “Export as Raster” checked.

When you use printPageAsRaster(0), all settings related to resolution are preserved in the exported file (such as 300 DPI format and 8×10 page size), so ImageMagick automatically converts it over.

You don’t have to include all the libraries that are listed below, I was lazy, and I needed all of them for my map automation plugin, so I left them in. You should remove the ones you don’t use. Don’t forget to change the composer name from Horizontal to the composer you use, and adjust the output paths accordingly.

It may not be elegant, but it works well. This is my solution, it may not be yours.

from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication,QFileInfo,QSizeF
from PyQt4.QtGui import QAction, QIcon, QPrinter,QPainter
from os import sys 

composerId = 0
composers = iface.activeComposers()
for item in composers:
	if item.composerWindow().windowTitle() == 'Horizontal':
		break
	composerId += 1

c = composers[composerId].composition()

image = c.printPageAsRaster(0)
image.save("/tmp/"+QgsProject.instance().title()+".jpg", "jpg")
os.system('convert "/tmp/'+QgsProject.instance().title()+'.jpg" "/tmp/'+QgsProject.instance().title()+'.pdf"')

Evening Down in the Ravine

A follow up to yesterday's post about Vector export of QGIS Composers using PyQGIS. This does the export as a Raster PDF, for much smaller files but with the same or better quality.