My colleague Topi crafted a beautiful map for the #30DayMapChallenge showing the ecoregions of the world in a globe-like map with round halo. I was amazed when he told me that he had done it using nothing but QGIS. This made me wonder what if this could be scripted and even make it as a plugin? I decided to practice my PyQGIS skills and try to develop my first QGIS 3 Plugin.
Experimenting with Python console
Before creating a draft of the plugin I started experimenting with integrated QGIS Python Console and its editor to see whether I could recreate the process programmatically. Changing the project projection to Azimuthal Orthographic “world from space” projection was fairly easy using instructions provided Alasdair Rae especially since the clipping method described here is no longer needed since QGIS 3.2. To populate the map I just used 1:110 Million scale countries provided by Natural Earth. The most difficult part in the process actually was creating the halo with shadow effects. First I tried creating the halo layer using Natural Earth’s 1 degree graticule line layer converted into a polygon layer. This resulted in some rendering artefacts and slow rendering.
Second attempt was to use a point located in the origin of interest and to use Geometry Generator style to buffer it with the radius of the earth. Here the key finding was to change the coordinate system of the layer to match the previously set word from space projection of the QGIS project to avoid some unwanted behaviour.
This was a lightweight solution but unfortunately the number of segments in the circle was not satisfactory and for some reason the halo disappeared when zoomed in.
Third attempt was to use a point located in the origin of the projection buffered with radius of the earth to compose a polygon. This seems like a best solution for this time. The shadow effects were achieved using Inner Shadow and Drop Shadow Draw effects (which was quite difficult to get it working with PyQGIS at first).
Plugin Builder and onwards
After having the initial logic ready in the form of a snippet I decided to build an actual plugin for it. As recommended I started with a Plugin Builder and made the first release fairly quickly. As anticipated, the most time consuming task was the user interface design and implementation. After initial release the open source community showed its power and people started to report bugs and feature enchantment ideas for the plugin. Fairly quickly a simple project grew to have additional features such as geocoding, visualization customization, Sentinel-2 cloudless data source and ability to add globe to the layout.
Go ahead and test the plugin on your QGIS! Globe Builder can be downloaded from the QGIS Plugins Repository or from Github. And of course, if you find any bugs or have improvement ideas, do not hesitate to report them!
Lessons learned during the development
Here are a few tips that I found helpful during the development of the plugin:
- Experiment everything in the Python console of QGIS with the help of internal editor first and only then apply the logic to the plugin code
- Having a proper Makefile helps automating the repeating tasks in the development
- Using relative imports inside plugin code makes things lot easier
- Use either GNU GPL-2.0 or GPL-3.0 license in your project