Skip to content

Commit

Permalink
Add support for GDAL 2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastic committed Nov 6, 2015
1 parent ea998f4 commit 53d43e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions PolygonRepair.cpp
Expand Up @@ -165,22 +165,44 @@ double PolygonRepair::computeRobustness(OGRGeometry *geometry) {

bool PolygonRepair::saveToShp(OGRGeometry* geometry, const char *fileName) {
const char *driverName = "ESRI Shapefile";
#if GDAL_VERSION_MAJOR < 2
OGRRegisterAll();
OGRSFDriver *driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName);
#else
GDALAllRegister();
GDALDriver *driver = GetGDALDriverManager()->GetDriverByName(driverName);
#endif
if (driver == NULL) {
std::cout << "\tError: OGR Shapefile driver not found." << std::endl;
return false;
}

#if GDAL_VERSION_MAJOR < 2
OGRDataSource *dataSource = driver->Open(fileName, false);
#else
GDALDataset *dataSource = (GDALDataset*) GDALOpenEx(fileName, GDAL_OF_READONLY, NULL, NULL, NULL);
#endif
if (dataSource != NULL) {
std::cout << "\tOverwriting file..." << std::endl;
#if GDAL_VERSION_MAJOR < 2
if (driver->DeleteDataSource(dataSource->GetName())!= OGRERR_NONE) {
#else
if (driver->Delete(fileName)!= CE_None) {
#endif
std::cout << "\tError: Couldn't erase file with same name." << std::endl;
return false;
#if GDAL_VERSION_MAJOR < 2
} OGRDataSource::DestroyDataSource(dataSource);
#else
} GDALClose(dataSource);
#endif
}
std::cout << "\tCreating " << fileName << std::endl;
#if GDAL_VERSION_MAJOR < 2
dataSource = driver->CreateDataSource(fileName, NULL);
#else
dataSource = driver->Create(fileName,0,0,0,GDT_Unknown,NULL);
#endif
if (dataSource == NULL) {
std::cout << "\tError: Could not create file." << std::endl;
return false;
Expand All @@ -197,7 +219,11 @@ bool PolygonRepair::saveToShp(OGRGeometry* geometry, const char *fileName) {
std::cout << "\tError: Could not create feature." << std::endl;
}
OGRFeature::DestroyFeature(feature);
#if GDAL_VERSION_MAJOR < 2
OGRDataSource::DestroyDataSource(dataSource);
#else
GDALClose(dataSource);
#endif
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions prepair.cpp
Expand Up @@ -132,8 +132,13 @@ int main (int argc, const char * argv[]) {

//-- reading from a ogr dataset (most supported: shp, geojson, gml, etc)
else if (strcmp(argv[argNum], "--ogr") == 0) {
#if GDAL_VERSION_MAJOR < 2
OGRRegisterAll();
OGRDataSource *dataSource = OGRSFDriverRegistrar::Open(argv[argNum+1], false);
#else
GDALAllRegister();
GDALDataset *dataSource = (GDALDataset*) GDALOpenEx(argv[argNum+1], GDAL_OF_READONLY, NULL, NULL, NULL);
#endif
++argNum;
if (dataSource == NULL) {
std::cerr << "Error: Could not open file." << std::endl;
Expand Down

0 comments on commit 53d43e9

Please sign in to comment.