38 #include <gdal_version.h>
40 #include <gdal_priv.h>
41 #include <cpl_string.h>
42 #include <gdalwarper.h>
67 (
sizeof(
Scalar) == 4) ? GDT_Float32 : GDT_Float64,
99 (
sizeof(
Scalar) == 4) ? GDT_Float32 : GDT_Float64,
103 static const GDALDataType
f_type = (
sizeof(
Scalar) == 4) ? GDT_Float32 : GDT_Float64;
133 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
138 f_output_file = output_file;
284 if (((rank != 0) && (strcmp (
f_file.c_str(), f_output_file.c_str()) != 0)) || (rank == 0)){
311 for (
int i = 1; i <= nband; i++, buf += size ) {
314 GDALRasterBand *band =
f_ds->GetRasterBand( i );
317 int ret = band->RasterIO( GF_Read, 0, frow,
f_size, nrow, buf,
f_size, nrow,
f_type, 0, 0 );
319 if ( ret == CE_Failure ) {
322 for (
int j = 0 ; j <
f_size; j++ ) {
341 for (
int i = 1; i <= nband; i++, buf += size ) {
344 GDALRasterBand *band =
f_ds->GetRasterBand( i );
347 int ret = band->RasterIO( GF_Write, 0, frow,
f_size, nrow, buf,
f_size, nrow,
f_type, 0, 0 );
349 if ( ret == CE_Failure )
363 GDALAccess gmod = (mode ==
'w') ? GA_Update : GA_ReadOnly;
366 f_ds = (GDALDataset *)GDALOpenShared(
f_file.c_str(), gmod );
378 const char * projwkt = (
char *)
f_ds->GetProjectionRef();
391 GDALRasterBand *band =
f_ds->GetRasterBand( 1 );
400 f_ds->GetGeoTransform( cf );
410 for (
int i=0; i<6; ++i ) {
430 GDALWarpOptions *warpOptions = GDALCreateWarpOptions();
447 GDALDriver *poDriver;
448 char **papszMetadata;
453 poDriver = GetGDALDriverManager()->GetDriverByName(fmt);
455 if ( poDriver == NULL ) {
457 std::string msg =
"Unable to load GDAL driver " + string(fmt) +
" for file " +
f_file;
464 papszMetadata = poDriver->GetMetadata();
466 if ( ! CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) ) {
469 poDriver->GetDescription(),
484 char **papszOptions = NULL;
486 papszOptions = CSLSetNameValue( papszOptions,
"COMPRESS",
"YES" );
498 CSLDestroy( papszOptions );
507 char **papszOptions = NULL;
508 papszOptions = CSLSetNameValue( papszOptions,
"NBITS",
"7" );
509 papszOptions = CSLSetNameValue( papszOptions,
"COMPRESS",
"LZW" );
521 CSLDestroy( papszOptions );
527 std::string temp_file =
f_file +
".tmp";
532 f_ds = poDriver->Create( temp_file.c_str(),
545 std::string temp_file =
f_file +
".tmp";
550 f_ds = poDriver->Create( temp_file.c_str(),
583 if ( Formats[format].hasMeta ) {
596 if ( ret == CE_Failure ) {
598 Log::instance()->
warn(
"Raster %s (%s) does not support nodata value assignment. Nodata values will correspond to %f anyway, but this will not be stored as part of the raster metadata.\n",
f_file.c_str(), fmt,
f_hdr.
noval );
632 static int first = 1;
669 for (
int i = 0; i < nband; i++, x +=
f_size ) {
704 return iget( x, y, val );
718 Log::instance()->
warn(
"Coordinate (%f, %f) corresponds to a cell (%d, %d) outside the raster boundaries. It will be ignored.\n", px, py, x, y );
738 Log::instance()->
warn(
"Coordinate (%f, %f) corresponds to a cell (%d, %d) outside the raster boundaries. It will be ignored.\n", px, py, x, y );
742 return iput( x, y, val );
774 Scalar *val = bands + band;
778 bool initialized =
false;
785 if (
iget( x, y, bands ) ) {
787 if ( !initialized ) {
797 else if ( max < *val ) {
807 if ( ! initialized ) {
817 f_hdrNoConst->
vmax = max;
836 if ( writeOperation ) {
839 for (
int i = 0 ; i <
f_size; i++ ) {
881 std::string temp_file =
f_file +
".tmp";
885 hDriver = GetGDALDriverManager()->GetDriverByName(
"AAIGrid");
887 if ( hDriver == NULL ) {
889 std::string msg =
"Unable to load AAIGrid GDAL driver";
896 GDALDataset * new_ds = hDriver->CreateCopy(
f_file.c_str(),
f_ds, FALSE, NULL, NULL, NULL );
905 #if GDAL_VERSION_MAJOR > 1 || ( GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR >= 5 )
909 if ( GDALDriver::QuietDelete( temp_file.c_str() ) == CE_Failure )
911 Log::instance()->
warn(
"Could not delete temporary file %s", temp_file.c_str() );
914 GDALDriver * temp_driver =
f_ds->GetDriver();
916 if ( temp_driver->Delete( temp_file.c_str() ) == CE_Failure )
918 Log::instance()->
warn(
"Could not delete temporary file %s", temp_file.c_str() );
933 GDALDriver * driver =
f_ds->GetDriver();
935 int ret = driver->Delete(
f_file.c_str() );
937 if ( ret == CE_Failure ) {
957 GDALRasterBand *band =
f_warped_ds->GetRasterBand( 1 );
959 int xd = band->GetXSize();
960 int yd = band->GetYSize();
966 *ymin = cf[3] + xd * cf[4] + yd * cf[5];
967 *xmax = cf[0] + xd * cf[1] + yd * cf[2];
GDALDataset * f_warped_ds
void warn(const char *format,...)
'Warn' level.
double Scalar
Type of map values.
static const GDALDataType f_type
int getExtentInStandardCs(Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax)
static Log * instance()
Returns the instance pointer, creating the object on the first call.
int get(Coord x, Coord y, Scalar *val)
int iput(int x, int y, Scalar val)
void error(const char *format,...)
'Error' level.
void read(Scalar *buf, int first_row, int num_rows)
void write(Scalar *buf, int first_row, int num_rows)
void createRaster(const std::string &file, int categ=0)
int put(Coord x, Coord y, Scalar val)
void loadRow(int row, bool writeOperation=false)
int getMinMax(Scalar *min, Scalar *max)
int iget(int x, int y, Scalar *val)
double Coord
Type of map coordinates.
int calcMinMax(int band=0)
void debug(const char *format,...)
'Debug' level.