[Quick Coding Tip] How to count the features from a vector layer in QGIS using Python

Posts with the [Quick Coding Tip] tag are thought for giving a quick solution to simple PyQGIS tasks.


If you already know How to load a layer in QGIS using Python, you may run the following code:

feats_count = layer.featureCount()

for getting the total number of features that are stored in the current vector layer.

Instead, if you want to obtain the count of the selected features only (see here for learning how to perform this task), you may run the following code:

feats_count = layer.selectedFeatureCount()

Regardless of the method used, the total number of features will be stored in the feats_count variable, which could be easily returned by a simple  print:

print feats_count
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s