PyQGIS — Join Table — International visitors in Natural Protected Areas in Ecuador
By: Bryan R. Vallejo
Tags: Join Tables, Qgis, Pyqgis, Ecuador, GIS

PyQGIS has given a great solution for managing automation in GIS processes. The National System of Protected Areas (SNAP) of Ecuador acquired the information of the international visitors in spreadsheets but not necessarily as a map. So if maps are required is soft to create them with one python/PyQGIS script that can output proper layers including the number of visitors in the protected areas. The SNAP layer can be downloaded in the geo-portal of Environmental information of Ecuador here.

In this post/tutorial I will show you how to automate Join Table into a Shapefile in Qgis 3.10. The best of this script, is that interacts with the user ( in this case, you) with QInputDialog() so the input you have to use is only the URL of your data.

So, let’s take a look at the product of this script. As is mentioned, this script produces a NEW LAYER, that contains the additional information of the number of visitors added to the shapefile of natural protected areas. If you are not familiar with Joins, take a look here.

Image by the author, choropleth map with joined information of the number of international visitors

To begin, you have to open the python console in Qgis and open the editor.

Image by the author, Python console with editor window ‘Untitled-0’ at the right

Our first task will be to open the shapefile and to open the table into the Qgis. The only thing you will need for this tutorial is to copy and edit the URL of your data. The URL for my example goes as follow: C:\Desktop\Pyqgis\data

atural_areas.shp

First, copy and paste the code below into your python editor, and run. Then, you will see that a new dialog window is open.

#Dialog

qid1 = QInputDialog()

title = ‘Write the URL’

label = ‘Shapefile url: ‘

mode = QLineEdit.Normal

default = ‘<shp url here>’

snapUrl, ok = QInputDialog.getText(qid1, title, label,mode, default) #Adding Layer

snap_layer = iface.addVectorLayer(snapUrl,’Natural Areas’,’ogr’)

Image by the author, Input box to add the URL of your shapefile

Then you will see how the shapefile is added to the table of contents like a layer. You add the shapefile you want only be aware to write correctly the URL of your computer, including the extension of shp.

(e.g. \…\…\areas.shp)

Image by the author, shapefile on screen.

As you can see, you are interacting with the python console. Basically, it is asking you what to open, and you are replying with an URL. The next step is to do the same process but including the table to the display, in this case, the .csv, to make the join with Natural Areas.shp. Remember, that to process the join table correctly you need to have a similar attribute on both sides. In this case, the shapefile and the table both have a column with a code of the natural area.

Follow the code below to open the table.

#Dialog CSV june

qid2 = QInputDialog()

title = ‘url of table’

label = ‘csv url: ‘

mode = QLineEdit.Normal

default = ‘<csv url here>’

juncsvUrl, ok = QInputDialog.getText(qid2, title, label,mode, default) #Adding csv june urljun = r’file:\\’+juncsvUrl+’?delimiter={}’.format(“,”)

juncsv = QgsVectorLayer(urljun, “June 2019”, “delimitedtext”)

iface.addVectorLayer(juncsvUrl, “June visitors”, “ogr”)

Image by the author, input inbox to add the URL of your CSV table

Then, you will see that the table is added to the screen.