Python Methods for QGis: how to examine vector data

Once a layer is loaded, usually we want to access the data included in the layer, whether the geometries or the attributes. Now, we will discuss how to code in Python the access to these two types of data.

How to access the geometry of a vector layer

We will resume the loading of a shapefile script previously created (load_vector.py)

 

# 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)
layer = QgsVectorLayer (f, ”   Museums in New York City   »,«   ogr   “)
if not layer.isValid ():
print «   loading error of% s layer   »% Layer.name ()
QgsMapLayerRegistry.instance (). addMapLayers ([layer])

Replace the last line

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

by 

features = layer.getFeatures ()
feat = features.next ()
g = feat.geometry ()
print g.asPoint ()

We avoid loading the layer in QGis and we display in the Python console the x and y coordinates of the first point of the layer.

The script must look like this:

The procedure is the following :

features = layer.getFeatures ()

Create a pointer named  features on the layer layer

feat = features.next ()

Moving the cursor on the next entity, here this is the first entity of the table and charge this entity in the object feat .

g = feat.geometry ()

Load the geometry of the current cursor entity in subject g.

Once executed , we get the result in the Python console:

NOTES:

We could access by others means to geometry of an entity but the use of pointers in Python allows working with very important data loads without having to load the entire table into the memory.

Here we have loaded a shapefile, but regardless of the data source, once the layer is created the Python code does not change.

How to access attributes of a vector layer

To access the non-geometric attributes of the entities, we will modify the previous script

Replace the last two lines

g = feat.geometry ()
print g.asPoint ()

by

print feat.attributes ()

The script must look like this:

And its execution gives the following result in the Python console:

The result is a Python list with the set of attributes of the first entity of the table.

This information is usually related to the names of the fields in the table. For a list of field names, replace in the previous script the following line

print feat.attributes ()

by

print [c.name () for c in feat.fields (). toList ()]

The script must look like this:

And its execution gives the following result in the Python console

Leave a Reply

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