If you missed Part 1 and Part 2 read them before you continue reading...
In this part I will cover the following topics:-
1~ Introduction to PyQGIS
2~ Basic GIS Operations using pyqgis
Let's get started...
1~ Introduction to PyQGIS
PyQGIS is the official python module for the QGIS software. It allows you to perform spatial operations and automate spatial processes in the QGIS software.
The official python documentation is available on this website url (the C++ documentation is available here. Some notable resources for learning to use the module includes:-
i) PyQGIS developer cookbook - part of QGIS Documentation
ii) PyQGIS Programmer's Guide by Gary Sherman
iii) PyQGIS 101 by Anita Graser
The resources above are good enough to get you started.
2~ Basic GIS Operations using PyQGIS
Lets take a look at some common operations we usually do in QGIS software and see how we can do the same in PyQGIS.
i) Loading and rendering shapefile in PyQGIS
# Path to the shapefile...
vector_file = r"C:\Users\Yusuf_08039508010\Desktop\Working_Files\GIS Data\NGR\Nig Adm\NGA_adm2.shp"
# Create vector layer object...
layer_1 = QgsVectorLayer(vector_file, 'DISPLAYNAME', 'ogr')
# Add shapefile to map canvas...
QgsProject.instance().addMapLayer(layer_1)
# Create project instance object
project = QgsProject.instance()
# Get project file path name
project.fileName()
# Get project file base name
project.baseName()
# Set project file to new title
project.setTitle('New Title Name...')
# Get lenght/number of layers in a project layer panel
len(project.mapLayers())
# Find more attribute and methods for QgsProject
dir(QgsProject.instance())
ii) Accessing Shapefile Attributes Table
# get attribute columns names
for f in layer_1.fields():
print(f.name())