GDAL C++ test layer capabilities

by Leo.1906   Last Updated July 10, 2018 02:22 AM

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

Tags : gdal c++


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

Leo.1906
Leo.1906
December 07, 2016 21:02 PM

Related Questions


When should I use gdal_merge?

Updated February 22, 2018 16:22 PM

Mosaic Imagery using GDAL Edited

Updated November 29, 2017 20:22 PM


Can I change the pixel sizes in a world file?

Updated July 03, 2018 02:22 AM