3

I'm trying to check if a shapefile has the fast feature count. I have the following C++ code:

#include <iostream>
#include "ogrsf_frmts.h"

int main()
{
    GDALAllRegister();
    GDALDataset *poDS;
    poDS = (GDALDataset*)GDALOpenEx("C:/Users/root/Desktop/2016/GIS Programming/Plugin_new_new/Shapefiles_Karte/Ortsstrasse/temp/buildings - Kopie (2).shp", GDAL_OF_VECTOR, NULL, NULL, NULL);
    if (poDS == NULL)
    {
        printf("Open failed.\n");
        exit(1);
    }
    if (poDS->TestCapability(OLCFastFeatureCount) == true) {
        printf("true\n");
    } else {
        printf("false\n");
    }

    GDALClose(poDS);
}

The code tells me every time "false" even though I am 100% sure the input shapefile has the fast feature count ability.

So I assume that there is a problem in the function call. Isn't this the way you call the TestCapability function?

I am very new to C++ and have only used GDAL with Python so far ..

1
  • I answered below, but it was sloppy. The layers of a shape datasource support fast feature count (in some cases), but the datasource doesn't. Read more about the OGR data model if you are unclear on the distinction. Your answer below shows this more than mine, so I'll just drop mine
    – user10353
    Commented Dec 7, 2016 at 21:35

2 Answers 2

1

Ok, by looking at the source code of a python script doing the exact same thing I think I found the error. The working source code is this one:

#include <iostream>
#include "ogrsf_frmts.h"

int main()
{
    GDALAllRegister();
    GDALDataset *poDS;    
    poDS = (GDALDataset*)GDALOpenEx("C:/Users/root/Desktop/2016/GIS Programming/Plugin_new_new/Shapefiles_Karte/Landkreise/Landkreise.shp", GDAL_OF_VECTOR, NULL, NULL, NULL);
    if (poDS == NULL)
    {
        printf("Open failed.\n");
        exit(1);
    }

    bool fastFeatureCount = true;

    for (int i = 0; i < poDS->GetLayerCount(); i++){
       auto layer = poDS->GetLayer(i);
       if (layer->TestCapability(OLCFastFeatureCount) == false) fastFeatureCount = false;
    }

    printf("%d\n", fastFeatureCount);


    GDALClose(poDS);
}

Might be some sloppy coding style in there, but I'm new to c++ and don't know any better ..

0
    if (poDS->TestCapability(OLCFastFeatureCount) == true) {

This is dangerous: TestCapability returns an int, which is documented to be TRUE or FALSE. Comparing an int to the boolean value true will cause the boolean to be implicitly converted to 1, so if TRUE is defined as -1 or some other value, you'll get false from this comparison.

Instead you should compare to the constant TRUE:

    if (poDS->TestCapability(OLCFastFeatureCount) == TRUE) {

Or just check for nonzero (because any sane library defines FALSE as 0):

    if (poDS->TestCapability(OLCFastFeatureCount)) {

Your Answer

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

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