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/debug/logstream.hxx>
|
||||||
#include <simgear/io/lowlevel.hxx>
|
#include <simgear/io/lowlevel.hxx>
|
||||||
#include <simgear/misc/sg_path.hxx>
|
#include <simgear/misc/sg_path.hxx>
|
||||||
|
#include <simgear/misc/sg_dir.hxx>
|
||||||
|
|
||||||
#include <gdal.h>
|
#include <gdal.h>
|
||||||
|
#include <gdal_alg.h>
|
||||||
#include <gdal_priv.h>
|
#include <gdal_priv.h>
|
||||||
#include <gdalwarper.h>
|
#include <gdalwarper.h>
|
||||||
#include <ogr_spatialref.h>
|
#include <ogr_spatialref.h>
|
||||||
|
@ -124,7 +126,7 @@ public:
|
||||||
return pxSizeY * 3600;
|
return pxSizeY * 3600;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetDataChunk(int *buffer,
|
void GetDataChunk(const SGPath &work_dir, int *buffer,
|
||||||
double x, double y,
|
double x, double y,
|
||||||
double colstep, double rowstep,
|
double colstep, double rowstep,
|
||||||
int w, int h,
|
int w, int h,
|
||||||
|
@ -219,7 +221,7 @@ ImageInfo::ImageInfo(GDALDataset *dataset) :
|
||||||
" e=" << east << " w=" << west);
|
" e=" << east << " w=" << west);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImageInfo::GetDataChunk(int *buffer,
|
void ImageInfo::GetDataChunk(const SGPath &work_dir, int *buffer,
|
||||||
double x, double y,
|
double x, double y,
|
||||||
double colstep, double rowstep,
|
double colstep, double rowstep,
|
||||||
int w, int h,
|
int w, int h,
|
||||||
|
@ -247,9 +249,20 @@ void ImageInfo::GetDataChunk(int *buffer,
|
||||||
xformData.col_step = colstep;
|
xformData.col_step = colstep;
|
||||||
xformData.row_step = rowstep;
|
xformData.row_step = rowstep;
|
||||||
|
|
||||||
// Interpolate nodata pixels in the source band
|
// Interpolate nodata pixels in the source band.
|
||||||
GDALRasterBand *mask = dataset->GetRasterBand(srcband)->GetMaskBand();
|
// Create a temporary copy of the original for the processing.
|
||||||
GDALFillNodata(dataset->GetRasterBand(srcband), mask, 100, 0, 0, NULL, NULL, NULL);
|
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
|
// TODO: check if this image can actually cover part of the chunk
|
||||||
|
|
||||||
|
@ -300,6 +313,7 @@ void ImageInfo::GetDataChunk(int *buffer,
|
||||||
|
|
||||||
GDALDestroyGenImgProjTransformer( xformData.pTransformerArg );
|
GDALDestroyGenImgProjTransformer( xformData.pTransformerArg );
|
||||||
GDALDestroyWarpOptions( psWarpOptions );
|
GDALDestroyWarpOptions( psWarpOptions );
|
||||||
|
tmp_path.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_bucket(const std::string& work_dir, SGBucket bucket,
|
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);
|
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,
|
ImageInfo* images[], int imagecount,
|
||||||
bool forceWrite = false)
|
bool forceWrite = false)
|
||||||
{
|
{
|
||||||
|
@ -377,7 +391,7 @@ void process_bucket(const std::string& work_dir, SGBucket bucket,
|
||||||
::memset(buffer.get(), 0, cellcount * sizeof(int));
|
::memset(buffer.get(), 0, cellcount * sizeof(int));
|
||||||
|
|
||||||
for (int i = 0; i < imagecount; i++) {
|
for (int i = 0; i < imagecount; i++) {
|
||||||
images[i]->GetDataChunk(buffer.get(),
|
images[i]->GetDataChunk(work_dir, buffer.get(),
|
||||||
bwest, bsouth,
|
bwest, bsouth,
|
||||||
col_step / 3600.0, row_step / 3600.0,
|
col_step / 3600.0, row_step / 3600.0,
|
||||||
span_x, span_y );
|
span_x, span_y );
|
||||||
|
@ -405,7 +419,7 @@ void process_bucket(const std::string& work_dir, SGBucket bucket,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ...and write it out */
|
/* ...and write it out */
|
||||||
write_bucket(work_dir, bucket,
|
write_bucket(work_dir.str(), bucket,
|
||||||
buffer.get(),
|
buffer.get(),
|
||||||
min_x, min_y,
|
min_x, min_y,
|
||||||
span_x, span_y,
|
span_x, span_y,
|
||||||
|
@ -423,8 +437,9 @@ int main(int argc, const char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
SGPath work_dir(argv[1]);
|
SGPath work_dir(argv[1]);
|
||||||
|
simgear::Dir wd = simgear::Dir(work_dir);
|
||||||
work_dir.create_dir( 0755 );
|
wd.create(0755);
|
||||||
|
work_dir = wd.path();
|
||||||
|
|
||||||
GDALAllRegister();
|
GDALAllRegister();
|
||||||
|
|
||||||
|
@ -507,7 +522,7 @@ int main(int argc, const char **argv)
|
||||||
for (int y = 0; y <= dy; y++) {
|
for (int y = 0; y <= dy; y++) {
|
||||||
SGBucket bucket = sgBucketOffset(west, south, x, 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 {
|
} else {
|
||||||
|
@ -518,7 +533,7 @@ int main(int argc, const char **argv)
|
||||||
for (int i = 0; i < tilecount; i++) {
|
for (int i = 0; i < tilecount; i++) {
|
||||||
SGBucket bucket(atol(tilenames[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