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;


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

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

  • Thank you, this is very helpful. The information in OGR API Tutorial and the class documentation is not correct. One more question. How would I create a new dataset from a driver instead of opening an existing one? Something like this . . . OGRSFDriverRegistrar *registrar = OGRSFDriverRegistrar::GetRegistrar(); driver = registrar->GetDriverByName(DriverName); hDS = driver->CreateDataSource(); ? – khafen Aug 27 '14 at 22:47
  • It's not something that I've done but your code looks logical. Try it in debug and see. If it works then answer your own question - I'd vote for it! – Michael Stimson Aug 27 '14 at 22:49
  • Send a mail to gdal-dev list about wrong information in OGR API Tutorial so they can fix it. It may be because of the recent GDAL/OGR driver unification. – user30184 Aug 28 '14 at 4:47
  • I don't think it's wrong @user30184, the tutorial that khafen was following was the GDAL tutorial; it was where I started when I learned how to use the GDAL objects, that and the FW_Tools source code (thank you Frank!). – Michael Stimson Aug 28 '14 at 5:03
  • Probably you are right but it might be good if you as experienced programmer had a look at the API tutorial and think if it reflects the changes introduced by trac.osgeo.org/gdal/wiki/rfc46_gdal_ogr_unification. – user30184 Aug 28 '14 at 8:04

This code works for creating an OGRDataSource. Special thanks to Michael Miles-Stimson for getting me on the right track.

const char* path = "C:/Test/test.shp";
OGRDataSource *hDS;
OGRSFDriver *driver;
OGRSFDriverRegistrar *registrar =  OGRSFDriverRegistrar::GetRegistrar();
driver = registrar->GetDriverByName("ESRI Shapefile");
hDS = driver->CreateDataSource(path,NULL);
  • Thanks, I'll use that later when I need to create a shapefile without opening one first. – Michael Stimson Aug 27 '14 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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