[GIS] Error getting ESRI Shapefile driver OGR in C++

cgdalogr

I am unable to create an OGR Layer using the C++ API. I have followed the documentation at this link exactly http://www.gdal.org/ogr_apitut.html
I get an error trying to get the shapefile driver. Any suggestions?
const char *driverName = "ESRI Shapefile";
GDALDriver *pDriver;

GDALAllRegister();

pDriver = GetGDALDriverManager()->GetDriverByName(driverName);
if (pDriver == NULL)
{
    qDebug()<<"Error loading driver"; // this error happens
}   

Best Answer

You're trying to use an OGR (vector) driver with GDAL (raster) tools. Here's a few lines of my working code that may help:

char* BasePath = new char[FullPathMax];  // this does have a value before it's used
OGRRegisterAll();
OGRDataSource *hDS = NULL;
OGRSFDriver   *Driver = NULL;
hDS = OGRSFDriverRegistrar::Open(BasePath,FALSE,&Driver);

As you can see I don't go making the driver first, it's populated when I open the dataset using the OGRSFDriverRegistrar, that way it could be shapefile or any other OGR format. Calling the GDALAllRegister is fine for GDAL (raster) objects but you need to invoke the OGR equivalent before using OGR objects.