Book Image

Mastering Geospatial Development with QGIS 3.x - Third Edition

By : Shammunul Islam, Simon Miles, Kurt Menke, GISP, Richard Smith Jr., GISP, Luigi Pirelli, John Van Hoesen, GISP
Book Image

Mastering Geospatial Development with QGIS 3.x - Third Edition

By: Shammunul Islam, Simon Miles, Kurt Menke, GISP, Richard Smith Jr., GISP, Luigi Pirelli, John Van Hoesen, GISP

Overview of this book

QGIS is an open source solution to GIS and widely used by GIS professionals all over the world. It is the leading alternative to proprietary GIS software. Although QGIS is described as intuitive, it is also, by default, complex. Knowing which tools to use and how to apply them is essential to producing valuable deliverables on time. Starting with a refresher on the QGIS basics and getting you acquainted with the latest QGIS 3.6 updates, this book will take you all the way through to teaching you how to create a spatial database and a GeoPackage. Next, you will learn how to style raster and vector data by choosing and managing different colors. The book will then focus on processing raster and vector data. You will be then taught advanced applications, such as creating and editing vector data. Along with that, you will also learn about the newly updated Processing Toolbox, which will help you develop the advanced data visualizations. The book will then explain to you the graphic modeler, how to create QGIS plugins with PyQGIS, and how to integrate Python analysis scripts with QGIS. By the end of the book, you will understand how to work with all aspects of QGIS and will be ready to use it for any type of GIS work.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
About Packt
Contributors
Preface
Index

Iterating over features


Now, it's time to discover how to get all the features or a subset of them. The main way to iterate over all features or records of myVector is by using the following code, which shows the ID of each feature:

for feature in myVector.getFeatures():
    feature.id()

This will print a list of all 653 record IDs, as shown here:

0
1
...[cut]...
652

It's not always necessary to parse all records to get a subset of them. In this case, we have to set the QgsFeatureRequest class parameters to instruct getFeatures and then retrieve only a subset of records; in some cases, we must also retrieve a subset of columns.

The following code will get a subset of features and columns:

rect = QgsRectangle(1223070.695, 2293653.357, 9046974.211,
 4184988.662)
myVector.setSubsetString('"AREA_MI" > 1000')
request = QgsFeatureRequest()
request.setSubsetOfAttributes([0, 2])
request.setFilterRect(rect)
for index, feature in enumerate(myVector.getFeatures(request)):
    print("The record %d has...