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.
This commit is contained in:
parent
6d50ec366c
commit
5b6329e5ad
1 changed files with 28 additions and 13 deletions
|
@ -31,8 +31,10 @@
|
|||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/io/lowlevel.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
|
||||
#include <gdal.h>
|
||||
#include <gdal_alg.h>
|
||||
#include <gdal_priv.h>
|
||||
#include <gdalwarper.h>
|
||||
#include <ogr_spatialref.h>
|
||||
|
@ -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,
|
||||
|
@ -423,8 +437,9 @@ int main(int argc, const char **argv)
|
|||
}
|
||||
|
||||
SGPath work_dir(argv[1]);
|
||||
|
||||
work_dir.create_dir( 0755 );
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue