The world’s leading publication for data science, AI, and ML professionals.

PyQGIS – Join Table – International visitors in Natural Protected Areas in Ecuador

PyQGIS has given a great solution for managing huge amount of data. It is manageable if you write code with a sample and then use it for…

How to create a choropleth map using join table in PyQGIS 3.10


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
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
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:DesktopPyqgisdatanatural_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
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.
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
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.

Image by the author, CSV table on the screen.
Image by the author, CSV table on the screen.

At the moment, we have the data we need for the Join Table. Now we run the code below to process the join. You will see that a new message box is opened asking for an URL, you have to write the URL of the folder where you want to save the new layer. Do not forget to include the extension. (e.g. …..join.shp).

If you are using your own data. Be aware the in the line FIELD you must include the name of the column of your shapefile, and in FIELD 2 you must include the name of the column of your CSV table.

#Dialog JOIN 
qid4 = QInputDialog()
title = 'Output layer of Join'
label = 'url + name.shp: '
mode = QLineEdit.Normal
default = '<new layer url here>'
out, ok = QInputDialog.getText(qid4, title, label,mode, default)
#Join Table June
processing.run("native:joinattributestable", 
{'INPUT':snapUrl,
'FIELD':'csnap',
'INPUT_2':juncsvUrl,
'FIELD_2':'codt_snap',
'FIELDS_TO_COPY':[],
'METHOD':1,
'DISCARD_NONMATCHING':True,
'PREFIX':'',
'OUTPUT': out,})
jun_visitors = iface.addVectorLayer(out,'Visitors in June 2019','ogr')
Image by the author, message box for the URL of your new layer.
Image by the author, message box for the URL of your new layer.

Finally, you see that the new layer is added. The one that contains the number of visitors.

Image by the author, new layer on the screen containing the data of the table.
Image by the author, new layer on the screen containing the data of the table.

This is all you should know about joining tables to shapefiles. You can easily rewrite the code and make it work with your own data. This tutorial is part of a short class I gave about PyQGIS. If you have inquiries in bigger projects ping me on my profile.


Related Articles