Tuesday, October 25, 2016

Splitting vector and raster files in QGIS (Part 2 - Creating a proper QGIS plugin)

Part 1. Splitting vector and raster files in QGIS
Part 2. Creating a proper QGIS plugin

On my previous post I've created a python script which could be executed inside QGIS in order to split the various layers onto smaller, more manageable chunks.

I've been wanting to create a proper QGIS plugin to package that logic for some time but I've been postponing it as I was expecting it to be slightly tricky, mostly due to the fact that I've never done a QGIS plugin before and anticipated that it could quite challenging.

Truth be told, it was actually quite simple. Due to a mix of awesome online documentation and some starter tools this was mostly a straightforward process.

Let me start by showing the end-result and them I'll explain how it was built.

The plugin:

It's called "Geo Grid Cut" and it basically receives as input all the layers of a map. For example, assuming we have the following layers open on QGIS

After installing (I'll talk about that later) the plugin can be accessed on the "plugins" menu on option "Geo Grid Cut":

It provides various options but for now I'm just going to split a small custom area in Europe

For this particular example the end-result is 6 zip files, each corresponding to an area of 10x10 (degrees) that includes the corresponding features of all 4 layers inside that area.

Creating the plugin:

I was very pleasantly surprised that this process is so well documented here: http://www.qgistutorials.com/en/docs/building_a_python_plugin.html

Besides providing a detailed explanation it's also very "iterative" in the sense that in 5 min you have something up and running.

As that page includes most required info I'll just add some additional considerations:
  • After installing the Qt Creator and the Python Bindings for Qt my "make" command was not working. On MacOsX a restart was needed
  • (at least on MacOSX) Be careful with the casing on the plugin name and the corresponding folder on disk. If you name your plugin as "MyFirstPlugin" be sure to also name the repo as "MyFirstPlugin". For simplicity I would simply keep everything lowercase
  • Most existing plugins are open-source, which is particularly useful to see how things are done
  • By default you'll have a python "<plugin>_dialog_base.py" and a "<plugin>.py" file. Keep the UI logic on the corresponding "dialog" python file. Keeps thinks cleaner

Additional plugin parameters:

The plugin includes some additional configuration options:
  • Extent - Defines the bounding box that will be split into smaller chunks
    • Layers - Automatically sets the extent to cover all the layers on the map (default)
    • Canvas - Will use the current viewport to set the extent
    • Custom - Set the coordinates for the bounding box manually
  • Grid Size
    • Width - Width of each cell of the grid (in degrees)
    • Height - Height of each cell of the grid (in degrees)
    • Buffer - Defines the overlap (for each direction) when creating the grid cells. For example, if a buffer is set to anything > 0 an area around the edges will be shared by various cells. This is useful to prevent gaps between cells when processing the resulting output.
  • Output
    • Folder - Base folder where the output will be placed
    • Compress output - If checked will create a zip file for each cell. Otherwise an uncompressed folder will be created
    • Create boundaries description file - For each cell creates a description file that indicates the coordinates for that cell boundaries

Source-code and Installing

The source-code is available at: https://github.com/pmcxs/geogridcut

To install locally the easiest way is to:
  • Go to the QGIS folder and clone the plugin's repo with its default name
cd /Users/<username>/.qgis2/python/plugins
git clone https://github.com/pmcxs/geogridcut

  • Launch (or relaunch) QGIS and go to "Plugins > Manage and Install Plugins"
  • Under the "Installed" section you'll see "Geo Grid Cut" there. Press the checkbox near its name to activate it
  • The plugin should now appear on the plugins menu, ready to use :)

No comments:

Post a Comment