Python Methods for QGis: access to vector data (shapefile)

 

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.

Load a shapefile with Python

We will use a shapefile file available in Github for this example. You can download it here:

nycmuseums

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

 

# Customize this starter script by adding code
# to the run_script function. See the Help for
# complete information on how to create a script
# and use Script Runner.

”    ”   »Your description of the script goes here«    ”   “

# Some commonly used imports

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *

def run_script ( iface ):
layer = QgsVectorLayer (”   / data / NYC_MUSEUMS_GEO.shp   »,«   Museums in New York City   »,«   ogr   “)
if not layer.isValid ():
print «   loading error of% s layer   »% Layer.name ()
QgsMapLayerRegistry.instance (). addMapLayers ([layer])

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 »,« Museums in New York City »,« 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 : ”   Museums of the City of 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 :

List of OGR vector formats

 

Liste des formats vecteur OGR

 

Nom du Format Code Création Géoréférencement Compilé par défaut
fichiers Aeronav FAA AeronavFAA Non OUI OUI
AmigoCloud API AmigoCloud OUI OUI Non, nécessite la librairie libcurl
ESRI ArcObjects ArcObjects Non OUI Non, nécessite la librairie ESRI ArcObjects
Arc/Info Binary Coverage AVCBin Non OUI OUI
Arc/Info .E00 (ASCII) Coverage AVCE00 Non OUI OUI
Arc/Info Generate ARCGEN Non Non OUI
Atlas BNA BNA OUI Non OUI
AutoCAD DWG DWG Non Non Non, nécessite la librairie Open Design Alliance Teigha
AutoCAD DXF DXF OUI Non OUI
Carto Carto OUI OUI Non, nécessite la librairie libcurl
Cloudant / CouchDB Cloudant OUI OUI Non, nécessite la librairie libcurl
CouchDB / GeoCouch CouchDB OUI OUI Non, nécessite la librairie libcurl
Comma Separated Value (.csv) CSV OUI Non OUI
OGC CSW (Catalog Service for the Web) CSW Non OUI Non, nécessite la librairie libcurl
Czech Cadastral Exchange Data Format VFK Non OUI Non, nécessite la librairie libsqlite3
DB2 Spatial DB2ODBC OUI OUI Non, nécessite la librairie ODBC
DODS/OPeNDAP DODS Non OUI Non, nécessite la librairie libdap
EDIGEO EDIGEO Non OUI OUI
ElasticSearch ElasticSearch OUI OUI Non, nécessite la librairie libcurl
ESRI FileGDB FileGDB OUI OUI Non, nécessite la librairie FileGDB API
ESRI Personal GeoDatabase PGeo Non OUI Non, nécessite la librairie ODBC
ESRI ArcSDE SDE Non OUI Non, nécessite la librairie ESRI SDE
ESRI Shapefile / DBF ESRI Shapefile OUI OUI OUI
FMEObjects Gateway FMEObjects Gateway Non OUI Non, nécessite la librairie FME
GeoJSON GeoJSON OUI OUI OUI
Géoconcept Export Geoconcept OUI OUI OUI
Geomedia .mdb Geomedia Non Non Non, nécessite la librairie ODBC
GeoPackage GPKG OUI OUI Non, nécessite la librairie libsqlite3
GeoRSS GeoRSS OUI OUI OUI (la lecture nécessite la librairie libexpat)
Tables Google Fusion GFT OUI OUI Non, nécessite la librairie libcurl
GML GML OUI OUI OUI (la lecture nécessite la librairie Xerces or libexpat)
GMT GMT OUI OUI OUI
GPSBabel GPSBabel OUI OUI OUI (nécessite la librairie GPSBabel et GPX driver)
GPX GPX OUI OUI OUI (la lecture nécessite la librairie libexpat)
GRASS Vector Format GRASS Non OUI Non, nécessite la librairie libgrass
GPSTrackMaker (.gtm, .gtz) GPSTrackMaker OUI OUI OUI
Hydrographic Transfer Format HTF Non OUI OUI
Idrisi Vector (.VCT) Idrisi Non OUI OUI
Informix DataBlade IDB OUI OUI Non, nécessite la librairie Informix DataBlade
INTERLIS “Interlis 1” et “Interlis 2” OUI OUI Non, nécessite la librairie Xerces
INGRES INGRES OUI Non Non, nécessite la librairie INGRESS
JML OpenJUMP .jml OUI Non OUI (la lecture nécessite la librairie libexpat)
KML KML OUI OUI OUI (la lecture nécessite la librairie libexpat)
LIBKML LIBKML OUI OUI Non, nécessite la librairie libkml
Fichiers Mapinfo MapInfo File OUI OUI OUI
Microstation DGN DGN OUI Non OUI
Access MDB (PGeo et Geomedia) MDB Non OUI Non, nécessite la librairie JDK/JRE
Memory Memory OUI OUI OUI
MongoDB MongoDB OUI OUI Non, nécessite la librairie Mongo C++ client
MySQL MySQL Non OUI Non, nécessite la librairie MySQL
NAS – ALKIS NAS Non OUI Non, nécessite la librairie Xerces
NetCDF netCDF OUI OUI Non, nécessite la librairie libnetcdf
Oracle Spatial OCI OUI OUI Non, nécessite la librairie OCI
ODBC ODBC Non OUI Non, nécessite la librairie ODBC
MS SQL Spatial MSSQLSpatial OUI OUI Non, nécessite la librairie ODBC
Open Document Spreadsheet ODS OUI Non Non, nécessite la librairie libexpat
OGDI Vectors (VPF, VMAP, DCW) OGDI Non OUI Non, nécessite la librairie OGDI
OpenAir OpenAir Non OUI OUI
ESRI FileGDB OpenFileGDB Non OUI OUI
OpenStreetMap XML et PBF OSM Non OUI Non, nécessite la librairie libsqlite3 (et libexpat pour OSM XML)
PCI Geomatics Database File PCIDSK OUI OUI OUI, utilisant en interne PCIDSK SDK (de GDAL 1.7.0)
Geospatial PDF PDF OUI OUI OUI (la lecture nécessite libpoppler ou libpodofo)
PDS PDS Non OUI OUI
Planet Labs Scenes API PLScenes Non OUI Non, nécessite la librairie libcurl
PostgreSQL SQL dump PGDump OUI OUI OUI
PostgreSQL/PostGIS PostgreSQL/PostGIS OUI OUI Non, nécessite la librairie PostgreSQL client (libpq)
EPIInfo .REC REC Non Non OUI
S-57 (ENC) S57 Non OUI OUI
SDTS SDTS Non OUI OUI
SEG-P1 / UKOOA P1/90 SEGUKOOA Non OUI OUI
SEG-Y SEGY Non Non OUI
format Selafin/Seraphin Selafin OUI Partial (seulement codes EPSG) OUI
Norwegian SOSI Standard SOSI Non OUI Non, nécessite la librairie FYBA
SQLite/SpatiaLite SQLite OUI OUI Non, nécessite la librairie libsqlite3 or libspatialite
SUA SUA Non OUI OUI
SVG SVG Non OUI Non, nécessite la librairie libexpat
Storage et eXchange Format SXF Non OUI OUI
UK .NTF UK. NTF Non OUI OUI
U.S. Census TIGER/Line TIGER Non OUI OUI
VRT – Virtual Datasource VRT Non OUI OUI
OGC WFS (Web Feature Service) WFS OUI OUI Non, nécessite la librairie libcurl
MS Excel format XLS Non Non Non, nécessite la librairie libfreexl
MS Office Open XML spreadsheet XLSX OUI Non Non, nécessite la librairie libexpat
X-Plane/Flightgear aeronautical data XPLANE Non OUI OUI
VDV-451/VDV-452/IDF VDV OUI OUI OUI
Walk Walk Non OUI Non, nécessite la librairie ODBC
WAsP .map format WAsP OUI OUI OUI

 

 

 

We have introduced two code lines:

if not layer.isValid ():
print « loading error of% s layer »% Layer.name ()

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.

QgsMapLayerRegistry.instance ( ). addMapLayers ([layer])

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 the file chosen to 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

 

# Customize this starter script by adding code
# to the run_script function. See the Help for
# complete information on how to create a script
# and use Script Runner.

”    ”   »Your description of the script goes here«    ”   “

# Some commonly used imports

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *

def run_script ( iface ):
qfd = QFileDialog ()
title = ‘ Open a file ‘
path = ”   C: /   ”
f = QFileDialog.getOpenFileName ( qfd , title, path)
print f

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.

Leave a Reply

Your email address will not be published. Required fields are marked *