From 1133fbed1fc33d2bfbf95c140c99065c4803d68c Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sun, 12 Aug 2018 12:28:27 +0200 Subject: [PATCH 1/4] Make it compatible with GDAL 2. --- polysplit.cpp | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/polysplit.cpp b/polysplit.cpp index c047e9e..8928acb 100644 --- a/polysplit.cpp +++ b/polysplit.cpp @@ -17,6 +17,12 @@ #define OUTPUTTYPE wkbPolygon #define IDFIELD "id" +#if GDAL_VERSION_MAJOR >= 2 + using gdal_dataset_type = GDALDataset; +#else + using gdal_dataset_type = OGRDataSource; +#endif + typedef std::vector OGRPolyList; typedef int feature_id_t; @@ -101,19 +107,26 @@ void split_polygons(OGRPolyList *pieces, OGRGeometry* geometry, int max_vertices if (polygonIsPwned) delete polygon; } -OGRDataSource *create_destination(const char* drivername, const char* filename, +gdal_dataset_type *create_destination(const char* drivername, const char* filename, const char *layername, const char *id_field_name) { /* Find the requested OGR output driver. */ - OGRSFDriver* driver; - driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(drivername); +#if GDAL_VERSION_MAJOR >= 2 + GDALDriver* driver = GetGDALDriverManager()->GetDriverByName(drivername); +#else + OGRSFDriver* driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(drivername); +#endif if( driver == NULL ) { std::cerr << drivername << " driver not available.\n"; return NULL; } /* Create the output data source. */ +#if GDAL_VERSION_MAJOR >= 2 + GDALDataset* ds = driver->Create( filename, 0, 0, 0, GDT_Unknown, 0); +#else OGRDataSource* ds = driver->CreateDataSource( filename, NULL ); +#endif if( ds == NULL ) { std::cerr << "Creation of output file " << filename << " failed.\n"; return NULL; @@ -188,11 +201,18 @@ int main(int argc, char** argv) { dest_name = argv[1]; /* Register the OGR datasource drivers. */ +#if GDAL_VERSION_MAJOR >= 2 + GDALAllRegister(); +#else OGRRegisterAll(); +#endif /* Open the input data source */ - OGRDataSource* source; - source = OGRSFDriverRegistrar::Open( source_name, FALSE ); +#if GDAL_VERSION_MAJOR >= 2 + GDALDataset* source = static_cast(GDALOpen( source_name, GA_ReadOnly )); +#else + OGRDataSource* source = OGRSFDriverRegistrar::Open( source_name, FALSE ); +#endif if( source == NULL ) { std::cerr << "Opening " << source_name << " failed." << std::endl; exit( 1 ); @@ -224,7 +244,7 @@ int main(int argc, char** argv) { } /* Create the output data source. */ - OGRDataSource* dest = create_destination(driver_name, dest_name, + gdal_dataset_type* dest = create_destination(driver_name, dest_name, dest_layer_name, id_field_name); if( dest == NULL ) exit( 1 ); @@ -267,8 +287,13 @@ int main(int argc, char** argv) { } /* Close the input and output data sources. */ +#if GDAL_VERSION_MAJOR >= 2 + GDALClose(source); + GDALClose(dest); +#else OGRDataSource::DestroyDataSource( source ); OGRDataSource::DestroyDataSource( dest ); +#endif std::cerr << features_read << " features read, " << features_written << " written.\n"; From dcbd8f57cb36a584631f95b45524e049a12ffc50 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sun, 12 Aug 2018 12:28:47 +0200 Subject: [PATCH 2/4] Compile with more warnings. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e7cf1f4..c973a70 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS := -O3 -Wall $(shell gdal-config --cflags) +CFLAGS := -O3 -Wall -Wextra $(shell gdal-config --cflags) # CFLAGS += -ggdb LIBS := $(shell gdal-config --libs) From 1acfc93d08c5218c4d257e73719a3098af8ba4b2 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sun, 12 Aug 2018 12:29:49 +0200 Subject: [PATCH 3/4] Remove whitespace at line ends. --- polysplit.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/polysplit.cpp b/polysplit.cpp index 8928acb..7bfedb0 100644 --- a/polysplit.cpp +++ b/polysplit.cpp @@ -4,7 +4,7 @@ * written by Schuyler Erle * copyright (c) 2012 Geoloqi, Inc. * published under the 3-clause BSD license -- see README.txt for details. - * + * */ #include @@ -32,7 +32,7 @@ void split_polygons(OGRPolyList *pieces, OGRGeometry* geometry, int max_vertices /* split_polygons recursively splits the (multi)polygon into smaller * polygons until each polygon has at most max_vertices, and pushes each * one onto the pieces vector. - * + * * Multipolygons are automatically divided into their constituent polygons. * Empty polygons and other geometry types are ignored. Invalid polygons * get cleaned up to the best of our ability, but this does trigger @@ -47,21 +47,21 @@ void split_polygons(OGRPolyList *pieces, OGRGeometry* geometry, int max_vertices std::cerr << "WARNING: NULL geometry passed to split_polygons!\n"; return; } - + if (geometry->IsEmpty()) return; - + if (geometry->getGeometryType() == wkbMultiPolygon) { OGRMultiPolygon *multi = (OGRMultiPolygon*) geometry; for (int i = 0; i < multi->getNumGeometries(); i++) { split_polygons(pieces, multi->getGeometryRef(i), max_vertices); } return; - } - + } + if (geometry->getGeometryType() != wkbPolygon) return; - + OGRPolygon* polygon = (OGRPolygon*) geometry; if (polygon->getExteriorRing()->getNumPoints() <= max_vertices) { pieces->push_back((OGRPolygon*) polygon->clone()); @@ -102,7 +102,7 @@ void split_polygons(OGRPolyList *pieces, OGRGeometry* geometry, int max_vertices OGRGeometry* piece = mask.Intersection(polygon); split_polygons(pieces, piece, max_vertices); delete piece; - } + } if (polygonIsPwned) delete polygon; } @@ -241,8 +241,8 @@ int main(int argc, char** argv) { std::cerr << "ID field " << id_field_name << " isn't integer type!\n"; exit( 1 ); } - } - + } + /* Create the output data source. */ gdal_dataset_type* dest = create_destination(driver_name, dest_name, dest_layer_name, id_field_name); @@ -268,7 +268,7 @@ int main(int argc, char** argv) { feature_id_t id = (id_field >= 0 ? feature->GetFieldAsInteger(id_field) : feature->GetFID()); OGRGeometry *geometry = feature->GetGeometryRef(); - + /* Recursively split the geometry, and write a new feature for each * polygon that comes out. */ split_polygons(&pieces, geometry, max_vertices); @@ -295,6 +295,6 @@ int main(int argc, char** argv) { OGRDataSource::DestroyDataSource( dest ); #endif - std::cerr << features_read << " features read, " + std::cerr << features_read << " features read, " << features_written << " written.\n"; } From db7f639217f8b7cd81938a1003f84540c3afcc42 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sun, 12 Aug 2018 14:33:34 +0200 Subject: [PATCH 4/4] Use correct Open function for GDAL2 code. --- polysplit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polysplit.cpp b/polysplit.cpp index 7bfedb0..b9e4cc8 100644 --- a/polysplit.cpp +++ b/polysplit.cpp @@ -209,7 +209,7 @@ int main(int argc, char** argv) { /* Open the input data source */ #if GDAL_VERSION_MAJOR >= 2 - GDALDataset* source = static_cast(GDALOpen( source_name, GA_ReadOnly )); + GDALDataset* source = static_cast(GDALOpenEx( source_name, GDAL_OF_VECTOR | GDAL_OF_READONLY | GDAL_OF_VERBOSE_ERROR, nullptr, nullptr, nullptr )); #else OGRDataSource* source = OGRSFDriverRegistrar::Open( source_name, FALSE ); #endif