QGIS auto mapping

GIS

This week I learned a new thing, create a layout and automatically export to PNG files from various datasets using QGIS. It was a good timing, I need to produce a monthly bulletin on climate and vegetation update that required few maps to produce regularly for specific theme.

This is modified QGIS Automap from my office at the headquarter and adjusted for this bulletin. As the covered countries for the bulletin are spanning from South Asia to the Pacific, we divided the countries into 3 region/window analysis.

Below is the list

  • Region 1: TJK, KGZ, AFG and PAK

  • Region 2: IND, BTN, NPL, BGD, LKA, MMR, THA, KHM, LAO, VNM, PHL and DPRK

  • Region 3: IDN, TLS, PNG and SLB

To do a custom automatic mapping based on your case, you must prepare QGIS layout file, before continue the guideline below. This case, I have prepared *.qgz file before hand, list of products in csv format, symbology files and other supporting files.

Setting up environment

The code for the automap is written in Python 3. It is recommended to use either the Miniconda3 (minimal Anaconda) or Anaconda3 distribution. The below instructions will be Anaconda specific (although relevant to any Python virtual environment), and assume the use of a bash shell.

Requirements

  1. Download and install Anaconda Python on your machine for macOS, Linux, or Windows.

  2. Download and install QGIS Long Term Release 3.16.8 on your machine for macOS, Linux, or Windows.

  3. Download “automap_RBB” folder and it's content via this link: https://github.com/wfpidn/automap_RBB/archive/refs/heads/main.zip.

  4. Extract it into your working directory, you will have “automap_RBB” folder.

  5. Download geospatial data from this link: https://www.dropbox.com/s/ptfdp1p9ltrvwwa/_layers.zip?dl=0

  6. Extract above “zip” file and move the contents to “automap_RBB/templates/_layers”

Configure the python environment

A new Anaconda environment can be created using the conda environment management system that comes packaged with Anaconda. This step must only be done the first time. Once the environment has been created there is no need to do it again.

  1. Navigate to “automap_RBB” folder in your Terminal or Command-Prompt

  2. Create the environment from the “automapQGIS.yml” file.

    Type conda env create -f automapQGIS.yml then execute.

  3. Activate the environment

    Type conda activate automapQGIS then execute.

The environment now has been activated. The python scripts must be run inside this environment in order to work.

Producing maps

You need to get the geospatial data (in this case I got climate and vegetation data from the headquarter). Usually the data in IDRISI format (*.rst) are uploaded in this folder, link: https://drive.google.com/drive/u/0/folders/1kmpsja6_5BYhj7AtxUPou3e6BIOVZTJk.

Then you can easily convert it to GeoTIFF using GDAL. If you does not have GDAL in your machine, please install it via “conda”.

Get the data

  1. Install GDAL, type in your Terminal or Command-Prompt: conda install -c conda-forge gdal

  2. Download latest *.rst files from above link, let assume the result are inside Downloads folder (It can be in “~/Downloads” or “C:/Downloads”) and your file is “drive-download-20210811T141722Z-001.zip”.

  3. Unzip file “drive-download-20210811T141722Z-001.zip”

  4. Navigate to folder “drive-download-20210811T141722Z-001”, and create a new folder called “TIF”

  5. Convert *.rst to *.tif by typing below script in your Terminal or Command-Prompt

    for i in `find *.rst`; do gdal_translate -of GTiff -co COMPRESS=LZW -co PREDICTOR=1 $i ./TIF/$i.tif; done

  6. Move all the result (GeoTIFF files) inside TIF folder to “automap_RBB/products”. The files must respect the naming convention.

If your data already in GeoTIFF, you are good to go, no need to doing conversion as explain in above steps.

Running the script

In your Terminal or Command-Prompt,

  1. Navigate to the “automap_RBB” folder. Run the automap.py script by typing `python automap.py`

  2. By default all *.tif files in the products folder will be mapped as *.png maps in the maps folder.

  3. The maps will be stored in 3 different subfolders (Region1, Region2 and Region3) for each sub-region.

Options

When running the script, 4 additional flags can be added:

--select

# A window with the available product types will appear so that the user can choose which products to map.

# By default all of the products are mapped

--overwrite

# All the products will be mapped

# By default, only the products which are not present in the output folder will be mapped

--input

# Select input folder with the .tiff files

# Default is automap_RBB/products

--output

# Select output folder for the PNG maps

# Default is automap_RBB/maps

Example: python automap.py –-select --input` or `python automap.py --overwrite

The user can add a new product type.

  1. Update the “product_types.csv” file in “automap_RBB/csv”

    You should add the name of the product type (in capital letters), the title and the name of its color-ramp.

  2. Create the associated color ramp (*.qml file) and add it in “automap_RBB/templates/_layer_styles”

Example Maps

Region 1

wldviq2021j193.png

Region 2

Region 3

Previous
Previous

Upgrade the MBP MC724 Early 2011

Next
Next

Farewell WFP!