Cartogram Plugin for QGIS318 February 2017
Seriously, now. QGIS 3.0 is the upcoming next major version of QGIS, and it means a significant change to the ecosystem the open source GIS has built up over the last decade. That is good and healthy – APIs tend to become inconsistent and cluttered when they grow, dependencies see new major versions (and API/ABI changes), and a careful but thorough spring cleaning has not done nearly as much damage as eight year-old me claimed when I had to sort out my toys. Python 3 and Qt 5 have been around for quite a while now, after all.
But in certain ways it remains a challenge, especially for the mostly voluntary developers of the –as of today– 719 plugins in the project’s plugin repository (plus the numerous un(der)published ones). While the changes between Python 2 and Python 3 are a) not so extensive, b) have been known for years and c) (could) have been defanged by writing code suitable for both versions alike, PyQt5 brings more severe changes under its hood. The most challenging changes, though, clearly come from QGIS’ pyqgis API changing. No wonder, it has been growing tremendously since its last major release 2.0, back in 2013. Nyall Dawson wrote a good heads-up of the changes and how fundamental some of them are going to be.
Curiosity is big in me: one way that manifests is that I always (always!) have to try to build the newest versions of any software I am using. You’ll never find an outdated version of, say, GDAL on any of my computers. No, sir! No can do. So recently I thought it might be time for having a QGIS which uses Qt5 and Python3 – maybe I could even get rid of all that Qt4 clutter on my installation and switch to Python 3 for good! (spoiler alarm: nah, still takes its while)
And then that: There’s a total of 17 plugins available for QGIS 3. Seventeen! Out of seven hundred! Of course zero of my favourites.
For austromorph I had heavily tweaked the cartogram plugin, originally authored by Carson Farmer, later enhanced by Morten Wulff, and had added rudimentary parallel processing and a simple batch processing capability. As heavily it was tweaked, as ugly the code looked afterwards. I would not have published that piece at any price (even tough other users might have liked my additions). I have had plans of rewriting the whole plugin for a while, as I saw some room for improvements in performance. Since at austromorph we use the plugin to compute animation frames for our animated morphing maps, any speed-up would be a great enhancement.
I am proud to present to you The All New cartogram3 plugin for QGIS 3. Coming to your home straight from Vienna, Austria, it is a complete rewrite which draws in parts on the previous cartogram plugins, but brings major improvements in performance and adds some nifty new features.
Let me walk you through it:
- First, you install it from QGIS’ extension manager (Plugins→Manage and Install Plugins)
That was easy!
- Then you open up any polygon layer you have laying (höhö!) around. Check that it has one variable which is an absolute count of something, like for instance humans. I use this gorgeous dataset of People on and in between Mountains, or as others tend to call it Population Numbers of Austria.
- Run the plugin. Select which layer you want to compute a cartogram of, and choose one or more attributes for batch processing. Then, define stop conditions: since the algorithm is iterative, the results get gradually better with every repetition. The more iterations, the better; but also: the more iterations, the longer the computation time. We recommend an absolute minimum of 10 iterations. If the requested quality is met earlier, i.e. the average areal error of the cartogram is smaller than the set value, the computation stops earlier.
- Now, your computer needs to have power™ and/or stamina® – the computation takes from split seconds to –quite literally– ages, depending on the computer and the dataset. The algorithm iterates over every point × every polygon, and thus scales extremely poorly. Consider simplifying your input dataset beforehand.
But worry not: you can alway hit cancel and start over.
- And finally, you should have a shiny result:
Be sure to try it out as soon as you get hold of QGIS>=3! There’s a sample dataset included (optimised to compute muy rapido)