Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I do

from qgis.core import *
layer = QgsVectorLayer(r"D:\file.shp", "boundingBoxes", "ogr")
layer.featureCount()

outcome is 428

layer.select(layer.pendingAllAttributesList())
layer.selectedFeatureCount()

outcome is 0

Why the results are not equal?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted
+50
  • layer.featureCount() - Total Number of features in the layer.
  • layer.selectedFeatureCount() - Total number of features that are actually selected on the map canvas for the particular layer
  • layer.select(layer.pendingAllAttributesList()) - This does not in fact make any visible map selection. What this does is to create an iterator to loop over all the features in the layer.

For making a real selection in the map, you will need to iterate and store the feature ids to a list and then pass it to setSelectedFeatures(const QgsFeatureIds & ids) method. Like below..

layer.select(layer.pendingAllAttributesList())
ids = []
for feature in layer:
    ids.append(feature.id())
layer.setSelectedFeatures(ids)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.