DEV Community

Raymond Lay for MIERUNE

Posted on

Get started with PyQGIS 03 - Manipulate vector layers with QGIS python console

On the previous chapter, we get started with vector layer classes called QgsVectorLayer.

It could be set as follow:

file_path = "your/path/data/target_file.shp"
layer_name = "my fantastic layer"

# declare a new layer and specify file_path and layer name
layer = QgsVectorLayer(file_path, layer_name, "ogr")
# import declared layer 
QgsProject.instance().addMapLayer(layer)
Enter fullscreen mode Exit fullscreen mode

and layer name can be retrieved as follow :

# each layer is a QgsVectorLayer object
for layer in QgsProject.instance().mapLayers().values():
     print(layer.name())
Enter fullscreen mode Exit fullscreen mode

Regarding documentation, QgsVectorLayer class is full of functions. Let check some basic functions with QGIS python console!

1. Open script editor of Python Console

In Python console we can launch commands one by one, but we need several commands to manage layers.
To run those at once, we need to open the script editor.

1.1. Open Python console in QGIS

As for the first chapter :
Open QGIS, menu -> Plugins -> Python Console
Python console
Or use shortcut Ctrl+Alt+P (Windows) command+option+P (mac)

Python console open usually at the bottom of QGIS frame

Python console on QGIS

1.2. Open Script Editor

Click on the Show Editor button to open script editor as below:

Editor

2. Vector Layer related commands

Import the vector layer you want, here is Japan Prefectures

Japan prefectures layer

2.1. Retrieve target layer by name

As last chapter, we will retrieve layer by name (here japan_pref ), by

layer = QgsProject.instance().mapLayersByName("japan_pref")[0]

# check
print(layer)
Enter fullscreen mode Exit fullscreen mode

Result :

<QgsVectorLayer: 'japan_pref' (ogr)>
Enter fullscreen mode Exit fullscreen mode

2.2. Retrieve layer extent

Simply add

layer_extent = layer.extent()
print("extent: ", layer_extent)
Enter fullscreen mode Exit fullscreen mode

The result should be a QgsRectangle object.

extent: <QgsRectangle: 122.93372516199046629 24.04561582899132333, 148.89439264601270452 45.55724341400912891>
Enter fullscreen mode Exit fullscreen mode

Coordinates looks to be in geographic system, let's check.

2.3. Retrieve layer extent

Simply write

layer_crs = layer.sourceCrs()
print("CRS: ", layer_crs)
Enter fullscreen mode Exit fullscreen mode

The result should be the WGS 84 (EPSG:4326) reference system.

CRS: <QgsCoordinateReferenceSystem: EPSG:4326>
Enter fullscreen mode Exit fullscreen mode

layer crs and extent

2.4. Retrieve the number of features of the target layer

featureCount() method can do it.
you can combine with name() method to write a clean message

msg = f"{layer.name()} layer has {layer.featureCount()} features."
print(msg)
Enter fullscreen mode Exit fullscreen mode

And here is the number of Japan prefectures:

japan_pref layer has 47 features.
Enter fullscreen mode Exit fullscreen mode

2.5. Retrieves vector layer fields

You can try

layer_fields = layer.fields()
print (layer_fields)
Enter fullscreen mode Exit fullscreen mode

Result is an unreadable code, BUT!
class name QgsFields makes us to find a way to solve it.

<qgis._core.QgsFields object at 0x0000024950231160>
Enter fullscreen mode Exit fullscreen mode

Let's check documentation.
You can check by searching QgsFields with your favourite search engine.
Regarding, Qgs Fields Documentation names method may be fine.

QgsFields

Let's try!

layer_fields = layer.fields()
print (layer_fields.names())
Enter fullscreen mode Exit fullscreen mode

I got it !
layer fields

3. Check output PyQgis class and its methods, a good pratice to make you grow up in PyQGIS

In this chapter, QgsVectorLayer basic methods has been used, and regarding documentation, many other usages can be done.

The last method to retrieve field is a combination of QgsVectorLayer and QgsFields.
However, we saw that when we hit to an unknown class, we can still moving on by checking documentation of this class, and find the good method easily.

This is a good way to understand easily a huge library such as PyQGIS.

Top comments (0)