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'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.

share|improve this question
    
I suggest you check its source, -1 sounds like it encountered an error and then intentionally returned an invalid result. –  lynxlynxlynx Jul 13 '12 at 15:11
    
Why method selectedFeatureCount returns 0? –  bossman Jul 13 '12 at 15:20
1  
The obvious answer would be that the previous select call didn't select anything. Check your parameters. –  lynxlynxlynx Jul 13 '12 at 15:23
    
In while I get one feature with polygon. –  bossman Jul 13 '12 at 15:24
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks I count features in loob –  bossman Jul 14 '12 at 11:02
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.