10

I'm using this code to get features from QgsVectorLayer

 QgsVectorDataProvider* provider=theVectorLayer->dataProvider();
 if(!provider)
 {
 return;
 }
 theVectorLayer->select(provider->attributeIndexes(),theVectorLayer->extent(),true,false);
 theVectorLayer->selectedFeatureCount();//here returns 0 but there is one polygon
 while(theVectorLayer->nextFeature(currentFeature))
 {
 ....//here Iget attributes and current geometry (one polygon)
 }

How do I get feature count?

Method featureCount() returns -1.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 13, 2012 at 14:59
4
  • I suggest you check its source, -1 sounds like it encountered an error and then intentionally returned an invalid result. Commented Jul 13, 2012 at 15:11
  • Why method selectedFeatureCount returns 0? Commented Jul 13, 2012 at 15:20
  • 2
    The obvious answer would be that the previous select call didn't select anything. Check your parameters. Commented Jul 13, 2012 at 15:23
  • In while I get one feature with polygon. Commented Jul 13, 2012 at 15:24

1 Answer 1

15

When featureCount() returns -1 the only safe bet to get the feature count is to iterate over all feature and count them.

selectedFeatureCount() is the number of features that are selected on the layer (e.g. with setSelectedFeatures() or void select(QgsFeatureId featureId, bool emitSignal=true)), but has nothing to do with the select()/nextFeature() combo.

unicoletti
7,38724 silver badges34 bronze badges
answered Jul 13, 2012 at 20:55
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.