Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering QGIS

You're reading from   Mastering QGIS Go beyond the basics and unleash the full power of QGIS with practical, step-by-step examples

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781784398682
Length 420 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. A Refreshing Look at QGIS FREE CHAPTER 2. Creating Spatial Databases 3. Styling Raster and Vector Data 4. Preparing Vector Data for Processing 5. Preparing Raster Data for Processing 6. Advanced Data Creation and Editing 7. The Processing Toolbox 8. Automating Workflows with the Graphical Modeler 9. Creating QGIS Plugins with PyQGIS and Problem Solving 10. PyQGIS Scripting 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 that shows the ID of each feature:

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

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

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

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 only 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 ) ):
  ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image