Python QGIS Tutorial: Adding CSV Data

Comma Separated Value (CSV) data usually contains coordinates information that can be displayed on a map. We are frequently dealing with CSV data when working in GIS. Therefore it's very important we can work with this data type.

In QGIS we can display CSV data using Add Delimited Text Layer tool. If you haven't tried it, it can be found in Add Layer menu. But we will not discuss about it, because this is a series tutorial of QGIS Python programming. So we will discuss how to add a CSV data using python code in QGIS.

Checking CSV Data

Before adding a CSV data with python code in QGIS. I suggest to check the CSV data firstly.  The reason is, a CSV data may be structured in different way for any CSV data. It could be found the value is separated with comma(,), semicolon (;), tab, etc. So knowing how it is formatted will be a guidance for us how to handle the data. To check the data can be done with opening it in a text application like Notepad, Notepad++, etc. Any text application can be used, just choose your favorite one. 

In this tutorial I will use a CSV data from USGS Earthquake Catalog that contains earthquake location around the world. I opened the data with Notepad++ and it looks like figure 1.

Screenshot of earthquake csv data
Figure 1. Screenshot of earthquake CSV data
From the screenshot in figure 1, can be seen that the values are separated with comma(,) and there is no parentheses. So we will use this format in coding later.

Adding CSV Data

Now, let's adding the CSV data into QGIS with python. To add a CSV data we're using QGIS interface or QGIS project instance. But firstly we have to make the CSV file as a vector layer using QgsVectorLayer class with the following expression:
csv_layer=QgsVectorLayer(uri:string,layer name: string, library:string)

In the expression above can be seen that the first argument is we have to define an Uniform Resources Identifier (URI) for the CSV file. An URI could be a file address on a server or over internet. If the file is at local machine the URI can be written as file:///path. The second argument is layer name, give it what ever you like. The third one is library, for CSV we are using delimitedtext

Next we have to define some parameters in the URI such as:
  • encoding : data encoding (optional)
  • xField : Column name for longitude value
  • yField : Column name for latitude value
  • crs : Coordinate system in EPSG number
The complete URI for the earthquake CSV data will be like the expression below:

uri = "file:///F:/eq-data.csv?encoding=%s&delimiter=%s&xField=%s&yField=%s&crs=%s" % ("UTF-8",",", "longitude", "latitude","epsg:4326")

So making sure the URI is correct is the key to add a CSV data. After defining the URI, then we make a vector layer and add it to QGIS. The complete code can be seen as below.

uri = "file:///F:/eq-data.csv?encoding=%s&delimiter=%s&xField=%s&yField=%s&crs=%s" % ("UTF-8",",", "longitude", "latitude","epsg:4326")

#Make a vector layer

#Check if layer is valid
if not eq_layer.isValid():
    print ("Layer not loaded")

#Add CSV data    

Figure 2 shows the result when the code was executed.
Earthquake csv data added with python
Figure 2. Earthquake csv data added with python

That's all the tutorial how to add vector layer with python in QGIS. Please see other tutorials which discuss other topics in this QGIS python programming tutorial series.

Related Posts

Disqus Comments