65 d_dest(0), d_dds(dds), d_localfile(localfile),
66 d_geo_transform_set(false), d_width(0.0), d_height(0.0), d_top(0.0), d_left(0.0),
67 d_bottom(0.0), d_right(0.0), d_no_data(0.0), d_no_data_type(none), d_num_bands(0)
69 BESDEBUG(
"fong3",
"dds numvars = " << dds->num_var() << endl);
70 if (localfile.empty())
71 throw BESInternalError(
"Empty local file name passed to constructor", __FILE__, __LINE__);
322 find_vars(d_dds, *
this);
324 for (
int i = 0; i < num_bands(); ++i)
325 if (!effectively_two_D(var(i)))
326 throw Error(
"GeoTiff responses can consist of two-dimensional variables only; use constraints to reduce the size of Grids as needed.");
328 GDALDriver *Driver = GetGDALDriverManager()->GetDriverByName(
"MEM");
330 throw Error(
"Could not get the MEM driver from/for GDAL: " +
string(CPLGetLastErrorMsg()));
332 char **Metadata = Driver->GetMetadata();
333 if (!CSLFetchBoolean(Metadata, GDAL_DCAP_CREATE, FALSE))
334 throw Error(
"Could not make output format.");
336 BESDEBUG(
"fong3",
"num_bands: " << num_bands() <<
"." << endl);
346 d_dest = Driver->Create(
"in_memory_dataset", width(), height(), num_bands(), GDT_Byte, 0);
348 d_dest = Driver->Create(
"in_memory_dataset", width(), height(), num_bands(), GDT_Float32, 0);
351 throw Error(
"Could not create the geotiff dataset: " +
string(CPLGetLastErrorMsg()));
355 BESDEBUG(
"fong3",
"Made new temp file and set georeferencing (" << num_bands() <<
" vars)." << endl);
357 bool projection_set =
false;
359 for (
int i = 0; i < num_bands(); ++i) {
362 if (!projection_set) {
364 if (d_dest->SetProjection(wkt.c_str()) != CPLE_None)
365 throw Error(
"Could not set the projection: " +
string(CPLGetLastErrorMsg()));
366 projection_set =
true;
371 throw Error(
"In building a multiband response, different bands had different projection information.");
374 GDALRasterBand *band = d_dest->GetRasterBand(i+1);
376 throw Error(
"Could not get the " + long_to_string(i+1) +
"th band: " +
string(CPLGetLastErrorMsg()));
388 vector<double> local_lat;
390 extract_double_array(fbtp->d_lat, local_lat);
395 if (local_lat[0] < local_lat[local_lat.size() - 1]) {
396 BESDEBUG(
"fong3",
"Writing reversed raster. Lat[0] = " << local_lat[0] << endl);
398 for (
int row = 0; row <= height()-1; ++row) {
399 int offsety=height()-row-1;
400 CPLErr error_write = band->RasterIO(GF_Write, 0, offsety, width(), 1, data+(row*width()), width(), 1, GDT_Float64, 0, 0);
401 if (error_write != CPLE_None)
402 throw Error(
"Could not write data for band: " + long_to_string(i + 1) +
": " +
string(CPLGetLastErrorMsg()));
406 BESDEBUG(
"fong3",
"calling band->RasterIO" << endl);
407 CPLErr error = band->RasterIO(GF_Write, 0, 0, width(), height(), data, width(), height(), GDT_Float64, 0, 0);
408 if (error != CPLE_None)
409 throw Error(
"Could not write data for band: " + long_to_string(i+1) +
": " +
string(CPLGetLastErrorMsg()));
422 GDALDataset *tif_dst = 0;
424 Driver = GetGDALDriverManager()->GetDriverByName(
"GTiff");
426 throw Error(
"Could not get driver for GeoTiff: " +
string(CPLGetLastErrorMsg()));
429 char **Metadata = Driver->GetMetadata();
430 if (!CSLFetchBoolean(Metadata, GDAL_DCAP_CREATECOPY, FALSE))
431 BESDEBUG(
"fong",
"Driver does not support dataset creation via 'CreateCopy()'." << endl);
435 char **options = NULL;
436 options = CSLSetNameValue(options,
"PHOTOMETRIC",
"MINISBLACK" );
442 BESDEBUG(
"fong3",
"Before CreateCopy, number of bands: " << d_dest->GetRasterCount() << endl);
446 argv = CSLAddString(argv,
"-scale");
447 GDALTranslateOptions *opts = GDALTranslateOptionsNew(argv, NULL );
448 int usage_error = CE_None;
449 GDALDatasetH dst_handle = GDALTranslate(d_localfile.c_str(), d_dest, opts, &usage_error);
450 if (!dst_handle || usage_error != CE_None) {
451 GDALClose(dst_handle);
452 GDALTranslateOptionsFree(opts);
453 string msg = string(
"Error calling GDAL translate: ") + CPLGetLastErrorMsg();
454 BESDEBUG(
"fong3",
"ERROR transform_to_geotiff(): " << msg << endl);
455 throw BESError(msg, BES_INTERNAL_ERROR, __FILE__, __LINE__);
458 tif_dst = Driver->CreateCopy(d_localfile.c_str(),
reinterpret_cast<GDALDataset*
>(dst_handle), FALSE,
459 options, NULL, NULL);
461 GDALClose(dst_handle);
462 GDALTranslateOptionsFree(opts);
465 throw Error(
"Could not create the GeoTiff dataset: " +
string(CPLGetLastErrorMsg()));
494 find_vars(d_dds, *
this);
496 for (
int i = 0; i < num_bands(); ++i)
497 if (!effectively_two_D(var(i)))
498 throw Error(
"GeoTiff responses can consist of two-dimensional variables only; use constraints to reduce the size of Grids as needed.");
500 GDALDriver *Driver = GetGDALDriverManager()->GetDriverByName(
"MEM");
502 throw Error(
"Could not get the MEM driver from/for GDAL: " +
string(CPLGetLastErrorMsg()));
504 char **Metadata = Driver->GetMetadata();
505 if (!CSLFetchBoolean(Metadata, GDAL_DCAP_CREATE, FALSE))
506 throw Error(
"Driver JP2OpenJPEG does not support dataset creation.");
510 d_dest = Driver->Create(
"in_memory_dataset", width(), height(), num_bands(), GDT_Byte, 0 );
512 throw Error(
"Could not create in-memory dataset: " +
string(CPLGetLastErrorMsg()));
516 BESDEBUG(
"fong3",
"Made new temp file and set georeferencing (" << num_bands() <<
" vars)." << endl);
518 bool projection_set =
false;
520 for (
int i = 0; i < num_bands(); ++i) {
523 if (!projection_set) {
525 if (d_dest->SetProjection(wkt.c_str()) != CPLE_None)
526 throw Error(
"Could not set the projection: " +
string(CPLGetLastErrorMsg()));
527 projection_set =
true;
532 throw Error(
"In building a multiband response, different bands had different projection information.");
535 GDALRasterBand *band = d_dest->GetRasterBand(i+1);
537 throw Error(
"Could not get the " + long_to_string(i+1) +
"th band: " +
string(CPLGetLastErrorMsg()));
546 vector<double> local_lat;
548 extract_double_array(fbtp->d_lat, local_lat);
553 if (local_lat[0] < local_lat[local_lat.size() - 1]) {
554 BESDEBUG(
"fong3",
"Writing reversed raster. Lat[0] = " << local_lat[0] << endl);
556 for (
int row = 0; row <= height()-1; ++row) {
557 int offsety=height()-row-1;
558 CPLErr error_write = band->RasterIO(GF_Write, 0, offsety, width(), 1, data+(row*width()), width(), 1, GDT_Float64, 0, 0);
559 if (error_write != CPLE_None)
560 throw Error(
"Could not write data for band: " + long_to_string(i + 1) +
": " +
string(CPLGetLastErrorMsg()));
564 BESDEBUG(
"fong3",
"calling band->RasterIO" << endl);
565 CPLErr error = band->RasterIO(GF_Write, 0, 0, width(), height(), data, width(), height(), GDT_Float64, 0, 0);
566 if (error != CPLE_None)
567 throw Error(
"Could not write data for band: " + long_to_string(i+1) +
": " +
string(CPLGetLastErrorMsg()));
581 GDALDataset *jpeg_dst = 0;
583 Driver = GetGDALDriverManager()->GetDriverByName(
"JP2OpenJPEG");
585 throw Error(
"Could not get driver for JP2OpenJPEG: " +
string(CPLGetLastErrorMsg()));
588 char **Metadata = Driver->GetMetadata();
589 if (!CSLFetchBoolean(Metadata, GDAL_DCAP_CREATECOPY, FALSE))
590 BESDEBUG(
"fong",
"Driver JP2OpenJPEG does not support dataset creation via 'CreateCopy()'." << endl);
593 char **options = NULL;
594 options = CSLSetNameValue(options,
"CODEC",
"JP2");
595 options = CSLSetNameValue(options,
"GMLJP2",
"YES");
596 options = CSLSetNameValue(options,
"GeoJP2",
"NO");
597 options = CSLSetNameValue(options,
"QUALITY",
"100");
598 options = CSLSetNameValue(options,
"REVERSIBLE",
"YES");
600 BESDEBUG(
"fong3",
"Before JPEG2000 CreateCopy, number of bands: " << d_dest->GetRasterCount() << endl);
604 argv = CSLAddString(argv,
"-scale");
605 GDALTranslateOptions *opts = GDALTranslateOptionsNew(argv, NULL );
606 int usage_error = CE_None;
607 GDALDatasetH dst_h = GDALTranslate(d_localfile.c_str(), d_dest, opts, &usage_error);
608 if (!dst_h || usage_error != CE_None) {
610 GDALTranslateOptionsFree(opts);
611 string msg = string(
"Error calling GDAL translate: ") + CPLGetLastErrorMsg();
612 BESDEBUG(
"fong3",
"ERROR transform_to_jpeg2000(): " << msg << endl);
613 throw BESError(msg, BES_INTERNAL_ERROR, __FILE__, __LINE__);
616 jpeg_dst = Driver->CreateCopy(d_localfile.c_str(),
reinterpret_cast<GDALDataset*
>(dst_h), FALSE,
617 options, NULL, NULL);
620 GDALTranslateOptionsFree(opts);
623 throw Error(
"Could not create the JPEG200 dataset: " +
string(CPLGetLastErrorMsg()));
627 GDALClose (jpeg_dst);