From 6de5f28411a5bed2d7155357cf686730c9373a24 Mon Sep 17 00:00:00 2001 From: Thomas Weidner Date: Thu, 9 Jul 2015 15:05:43 +0200 Subject: [PATCH] Add option to prevent overwriting existing files --- tools/ctb-tile.cpp | 53 +++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/tools/ctb-tile.cpp b/tools/ctb-tile.cpp index 2c5eb1e..6f6d951 100644 --- a/tools/ctb-tile.cpp +++ b/tools/ctb-tile.cpp @@ -72,7 +72,8 @@ class TerrainBuild : public Command { tileSize(0), startZoom(-1), endZoom(-1), - verbosity(1) + verbosity(1), + resume(false) {} void @@ -136,6 +137,11 @@ class TerrainBuild : public Command { ++(static_cast(Command::self(command))->verbosity); } + static void + setResume(command_t* command) { + static_cast(Command::self(command))->resume = true; + } + static void setResampleAlg(command_t *command) { GDALResampleAlg eResampleAlg; @@ -202,6 +208,8 @@ class TerrainBuild : public Command { endZoom, verbosity; + bool resume; + CPLStringList creationOptions; TilerOptions tilerOptions; }; @@ -325,6 +333,12 @@ showProgress(int currentIndex, string filename) { return progressFunc(currentIndex / (double) iteratorSize, message.c_str(), NULL); } +static bool +fileExists(const std::string& filename) { + VSIStatBufL statbuf; + return VSIStatExL(filename.c_str(), &statbuf, VSI_STAT_EXISTS_FLAG) == 0; +} + /// Output GDAL tiles represented by a tiler to a directory static void buildGDAL(const RasterTiler &tiler, TerrainBuild *command) { @@ -348,20 +362,24 @@ buildGDAL(const RasterTiler &tiler, TerrainBuild *command) { setIteratorSize(iter); while (!iter.exhausted()) { - GDALTile *tile = *iter; + const TileCoordinate *coordinate = iter.GridIterator::operator*(); GDALDataset *poDstDS; - const string filename = getTileFilename(tile, dirname, extension); + const string filename = getTileFilename(coordinate, dirname, extension); - poDstDS = poDriver->CreateCopy(filename.c_str(), tile->dataset, FALSE, - command->creationOptions.List(), NULL, NULL ); - delete tile; + + if( !command->resume || !fileExists(filename) ) { + GDALTile *tile = *iter; + poDstDS = poDriver->CreateCopy(filename.c_str(), tile->dataset, FALSE, + command->creationOptions.List(), NULL, NULL ); + delete tile; - // Close the datasets, flushing data to destination - if (poDstDS == NULL) { - throw CTBException("Could not create GDAL tile"); - } + // Close the datasets, flushing data to destination + if (poDstDS == NULL) { + throw CTBException("Could not create GDAL tile"); + } - GDALClose(poDstDS); + GDALClose(poDstDS); + } currentIndex = incrementIterator(iter, currentIndex); showProgress(currentIndex, filename); @@ -380,11 +398,15 @@ buildTerrain(const TerrainTiler &tiler, TerrainBuild *command) { setIteratorSize(iter); while (!iter.exhausted()) { - TerrainTile *tile = *iter; - const string filename = getTileFilename(tile, dirname, "terrain"); + const TileCoordinate *coordinate = iter.GridIterator::operator*(); + const string filename = getTileFilename(coordinate, dirname, "terrain"); - tile->writeFile(filename.c_str()); - delete tile; + if( !command->resume || !fileExists(filename) ) { + TerrainTile *tile = *iter; + + tile->writeFile(filename.c_str()); + delete tile; + } currentIndex = incrementIterator(iter, currentIndex); showProgress(currentIndex, filename); @@ -438,6 +460,7 @@ main(int argc, char *argv[]) { command.option("-n", "--creation-option