In this series of articles we are going to discuss how to access the vector type of data with Python and QGis. Firstly we will discuss how to load various vector data formats (shapefile, postgis and spatiality), then how to access the geometric and attribute properties, and finally, some examples of spatial processing for this type of data. Among the large number of possible formats for vector data, we will discuss the main two: shapefile and postgis.
For the examples we will use QGis Script Runner plugin, but you can type lines of code in the Python QGis console.
Loadashapefile with Python
We will use a shapefile file available in Github for this example. You can download it here:
We suppose next that you unzip this file in a directory c: / data.
Start QGis and open Script Runner (this assumes you have already installed and activated it).
Click on the tool New Script
and name your script load_vector.py
In tab Source you will have the contents of the new created file.
Open the context menu of the script by right clicking on the script name and select Edit script in external editor
You can copy- paste this text, but ATTENTION!! : you will have to correct the indentation of the lines for the script to work. The display HTML ignores spaces at the beginning of the line, but Python … DOES NOT!
Your script must appear in ScriptRunner as follows
If you have a message like this one:
it is that there is a syntax error in your script and that ScriptRunner cannot interpret your code source.
If you run the script from ScriptRunner you will have the following:
your shapefile loaded in QGis.
Let’s now see in detail what we have done in this script:
layer=QgsVectorLayer(“/ data /NYC_MUSEUMS_GEO.shp»,«MuseumsinNew YorkCity»,«ogr“)
This code line creates the layer . QgsVectorLayer requires three parameters:
a file path for the data to be loaded: ” / data /NYC_MUSEUMS_GEO.shp «
the name we want to give this layer in the QGis window : ” Museumsof theCityof New York ”
and the data provider (data provider) to use: here “ogr “ of the GDAL library . OGR determines the data format from the file extension and adapts the driver to use in function. Here is the list of formats managed by ogr :
If for some reason QgsVectorLayer failed in the creation of the layer (error in the file name, data corrupted, etc …) it does not return an error message. Your script will continue to run and will crash further on. The error message that you see has a good chance of having nothing to do with the real problem. Therefore, it is quite convenient to test the validity of each layer created and to display the error immediately.
Finally, in the last line, we add the vector layer to the QgsMapLayerRegistry, which makes it available and visible on the map.
The registry keeps track of all layers of the project. The reason why QGIS works this way is to allow you to load multiple layers, assign them a style, filter them, as well as several other operations, before displaying being displayed in the map.
How to code thefile chosento load
In our code, the file to load is written in hard, which is rarely the case in the reality of the processes . We will see how to code the file search with the window “Open a file”
In ScriptRunner create a new script dialog_file.py .
Enter the following code
Your script must look like this:
When you run it, the file search window will be displayed. When you click on “Open”, the script displays the full name of your file .
The line from PyQt4.QtGui import * is essential to be able to call the dialogue.
QFileDialog uses three parameters:
the object QFileDialog : qfd
the title of the dialogue window : title
the starting path of the window: in this example C: / ( but you can indicate anything regardless of the directory )
The result (f) contains a string with the path and file name selected. If the user does not click Open but Cancel, the chain is empty.
Now we can modify the first script (loading a shape) to use the dialog instead of the hard encoding:
At the execution of the script, the dialog window opens , you choose the file to load, and then the script is loaded into QGis.
In the next article we will see how to do the same but from a Postgis table or a database Spatiality database.
Si cet article vous a intéressé et que vous pensez qu'il pourrait bénéficier à d'autres personnes, n'hésitez pas à le partager sur vos réseaux sociaux en utilisant les boutons ci-dessous. Votre partage est apprécié !