From 5b6329e5add77febca9ae4a89db01761a8a9c213 Mon Sep 17 00:00:00 2001 From: Christian Schmitt Date: Thu, 20 Dec 2012 18:53:20 +0100 Subject: [PATCH] The previous commit brought up some issues with certain source formats. We now create a copy of the original dataset and use that for processing, as GDAL needs to write some changes. --- src/Prep/GDALChop/gdalchop.cxx | 41 +++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/Prep/GDALChop/gdalchop.cxx b/src/Prep/GDALChop/gdalchop.cxx index cb4b4f54..7fe38073 100644 --- a/src/Prep/GDALChop/gdalchop.cxx +++ b/src/Prep/GDALChop/gdalchop.cxx @@ -31,8 +31,10 @@ #include #include #include +#include #include +#include #include #include #include @@ -124,7 +126,7 @@ public: return pxSizeY * 3600; } - void GetDataChunk(int *buffer, + void GetDataChunk(const SGPath &work_dir, int *buffer, double x, double y, double colstep, double rowstep, int w, int h, @@ -219,7 +221,7 @@ ImageInfo::ImageInfo(GDALDataset *dataset) : " e=" << east << " w=" << west); } -void ImageInfo::GetDataChunk(int *buffer, +void ImageInfo::GetDataChunk(const SGPath &work_dir, int *buffer, double x, double y, double colstep, double rowstep, int w, int h, @@ -247,9 +249,20 @@ void ImageInfo::GetDataChunk(int *buffer, xformData.col_step = colstep; xformData.row_step = rowstep; - // Interpolate nodata pixels in the source band - GDALRasterBand *mask = dataset->GetRasterBand(srcband)->GetMaskBand(); - GDALFillNodata(dataset->GetRasterBand(srcband), mask, 100, 0, 0, NULL, NULL, NULL); + // Interpolate nodata pixels in the source band. + // Create a temporary copy of the original for the processing. + SGPath tmp_path = work_dir; + tmp_path.append(".gdal.tif"); + + const char *pszFormat = "GTiff"; + GDALDriver *poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat); + GDALDataset *interpolate = poDriver->CreateCopy( tmp_path.c_str(), dataset, FALSE, + NULL, NULL, NULL ); + GDALFillNodata( interpolate->GetRasterBand(srcband), + interpolate->GetRasterBand(srcband)->GetMaskBand(), + 100.0, 0, 0, NULL, NULL, NULL ); + + dataset = interpolate; // TODO: check if this image can actually cover part of the chunk @@ -300,6 +313,7 @@ void ImageInfo::GetDataChunk(int *buffer, GDALDestroyGenImgProjTransformer( xformData.pTransformerArg ); GDALDestroyWarpOptions( psWarpOptions ); + tmp_path.remove(); } void write_bucket(const std::string& work_dir, SGBucket bucket, @@ -338,7 +352,7 @@ void write_bucket(const std::string& work_dir, SGBucket bucket, gzclose(fp); } -void process_bucket(const std::string& work_dir, SGBucket bucket, +void process_bucket(const SGPath& work_dir, SGBucket bucket, ImageInfo* images[], int imagecount, bool forceWrite = false) { @@ -377,7 +391,7 @@ void process_bucket(const std::string& work_dir, SGBucket bucket, ::memset(buffer.get(), 0, cellcount * sizeof(int)); for (int i = 0; i < imagecount; i++) { - images[i]->GetDataChunk(buffer.get(), + images[i]->GetDataChunk(work_dir, buffer.get(), bwest, bsouth, col_step / 3600.0, row_step / 3600.0, span_x, span_y ); @@ -405,7 +419,7 @@ void process_bucket(const std::string& work_dir, SGBucket bucket, } /* ...and write it out */ - write_bucket(work_dir, bucket, + write_bucket(work_dir.str(), bucket, buffer.get(), min_x, min_y, span_x, span_y, @@ -422,9 +436,10 @@ int main(int argc, const char **argv) exit(-1); } - SGPath work_dir( argv[1] ); - - work_dir.create_dir( 0755 ); + SGPath work_dir(argv[1]); + simgear::Dir wd = simgear::Dir(work_dir); + wd.create(0755); + work_dir = wd.path(); GDALAllRegister(); @@ -507,7 +522,7 @@ int main(int argc, const char **argv) for (int y = 0; y <= dy; y++) { SGBucket bucket = sgBucketOffset(west, south, x, y); - process_bucket(work_dir.str(), bucket, images.get(), datasetcount); + process_bucket(work_dir, bucket, images.get(), datasetcount); } } } else { @@ -518,7 +533,7 @@ int main(int argc, const char **argv) for (int i = 0; i < tilecount; i++) { SGBucket bucket(atol(tilenames[i])); - process_bucket(work_dir.str(), bucket, images.get(), datasetcount, true); + process_bucket(work_dir, bucket, images.get(), datasetcount, true); } }