Creating Dashboard Styled Map Layout with QGIS

with a help from Pandas, Matplotlib and PyQGIS to automate it

Gunawan Wardhana
3 min readJan 9, 2021

Creating data driven map is not that hard, QGIS can easily help you to create it. QGIS has a lot of great features to create good maps from various data sources. You can add charts, images, text as an additional element along with map in layout to enrich your map so it has a dashboard elements.

Challenge

If you or your organization publishes printed maps with the same template on a regular basis and you have to update the content (layers, symbols, charts, text, etc ) manually it can turn into a really boring tasks.

a dashboard styled map created automatically with pandas, matplotlib and pyqgis
a dashboard styled map created with PyQGIS, Pandas and Matplotlib automatically

Solution

QGIS support python scripting to extend its core functionality. It gives us a lot of advantages. Python is easy to use, having supportive community, it has hundreds of different libraries that really useful in saving time to do specific tasks like image processing, data visualization, machine learning, etc.

In our case, we re going to use python libraries to process tabular data and to create chart that are Pandas and Matplotlib and of couse PyQGIS itself to access core functionality in QGIS. Python is required to automate the whole process from read data sources, filtering and manipulating data, create charts, add them to the layout and finally export to PDF file.

I assuming you already familiar with QGIS and having prior knowledge in very basic python programming to follow this tutorial.

Let’s get started!

Data

For this tutorial, I will only need two layers : a dynamic data as theme layer and a basemap layer.

Tools

You need QGIS of course, and some of python libraries : Pandas and Matplotlib to be installed in your computer. We are going to run these python libraries inside QGIS environment. Unfortunately, QGIS doesn’t provide these libs natively so you must install them manually. You can read this post if you need guidance to install python libraries in QGIS environment.

First Step : Create Layout Template

This layout template is much like container that arrange where the information — like charts, maps, label values — will be showed. Its layout composition will help us later on, to define correct position when write python script to plot the information on layout.

QGIS Layout Template

Second Step : Prepare Workspace Directory

Create directories to organize input and output files for this tutorial.

D:\PLAYGROUND\MAP-DASHBOARD
| create_map.py
| LICENSE
| README.md
| utils.py
|
+---data
| Carto-Voyager.xml
| earthquakes_2019.csv
| earthquakes_2019.csvt
| style_gempa.qml
|
+---img
| pyqgis.svg
| report_template_A4.qpt
| usgs-logo.jpg
|
\---report

Third Step: Write The Script

To make it more easy to digest, I break the code into three parts :

  1. Read and load data to canvas

This part is about importing required libraries, define inputs & outputs data, read data and load to canvas and apply predefined style to layer.

import pandas as pd
import datetime as dt
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import matplotlib as mpl
import time

define inputs & outputs

path = “D:\playground\map-dashboard”
file_basemap = os.path.join(path,”data\Carto-Voyager.xml”)
file_csv = os.path.join(path,”data\earthquakes_2019.csv”)
style_layer_gempa = os.path.join(path,”data\style_gempa.qml”)
qpt_path = os.path.join(path,”img/report_template_A4.qpt”)
output_pdf = os.path.join(path,”report/report_A4.pdf”)
#CRS definition
wgs84 = QgsCoordinateReferenceSystem(4326)

load data to QGIS canvas

#Add layer to canvas
layer_basemap = iface.addRasterLayer(file_basemap, "", "gdal")
layer_gempa = iface.addVectorLayer(file_csv, "csv", "ogr")
layer_gempa.setCrs(wgs84, True)
#apply style to layer gempa
layer_gempa.loadNamedStyle(style_layer_gempa)
layer_gempa.triggerRepaint()

2. Data processing and chart generation

We need to convert the csv files to pandas dataframe. Pandas is really powerful, we will get more advantages of pandas features to process the raw data table. It also good in handling large data.

The chart is going to be generated based on data in pandas dataframe.

3. Load template and plot to layout

Fourth Step : Run the python

Open QGIS and open Python Console Panel or use short cut key (Ctrl + Alt + P). Click Open Script button to load python script you have just created and then Run it.

If you’re interested, you can download the code in my github repo https://github.com/gwisnu/map-dashboard

--

--

Responses (1)