30 #include <gdal_priv.h>
33 #include <ConstraintEvaluator.h>
35 #include <Structure.h>
63 d_dds(dds), d_localfile(localfile),
64 d_geo_transform_set(false), d_no_data_type(none), d_num_bands(0)
66 if (localfile.empty())
67 throw BESInternalError(
"Empty local file name passed to constructor", __FILE__, __LINE__);
76 vector<FONgBaseType *>::iterator i = d_fong_vars.begin();
77 vector<FONgBaseType *>::iterator e = d_fong_vars.end();
87 is_convertable_type(
const BaseType *b)
110 return new FONgGrid(static_cast<Grid*>(v));
113 throw BESInternalError(
"file out GeoTiff, unable to write unknown variable type", __FILE__, __LINE__);
134 void FONgTransform::m_scale_data(
double *data)
139 hist.insert(data[i]);
141 BESDEBUG(
"fong3",
"Hist count: " << hist.size() << endl);
150 set<double>::iterator i = hist.begin();
151 double smallest = *(++i);
152 if (fabs(smallest +
no_data()) > 1) {
155 BESDEBUG(
"fong3",
"New no_data value: " << smallest << endl);
165 set<double>::reverse_iterator r = hist.rbegin();
166 double biggest = *(++r);
167 if (fabs(
no_data() - biggest) > 1) {
170 BESDEBUG(
"fong3",
"New no_data value: " << biggest << endl);
195 BESDEBUG(
"fong3",
"left: " << d_left <<
", top: " << d_top <<
", right: " << d_right <<
", bottom: " << d_bottom << endl);
196 BESDEBUG(
"fong3",
"width: " << d_width <<
", height: " << d_height << endl);
208 d_gt[1] = (d_right - d_left) / d_width;
209 d_gt[5] = (d_bottom - d_top) / d_height;
211 BESDEBUG(
"fong3",
"gt[0]: " << d_gt[0] <<
", gt[1]: " << d_gt[1] <<
", gt[2]: " << d_gt[2] \
212 <<
", gt[3]: " << d_gt[3] <<
", gt[4]: " << d_gt[4] <<
", gt[5]: " << d_gt[5] << endl);
228 bool FONgTransform::effectively_two_D(
FONgBaseType *fbtp)
230 if (fbtp->
type() == dods_grid_c) {
231 Grid *g =
static_cast<FONgGrid*
>(fbtp)->grid();
233 if (g->get_array()->dimensions() == 2)
237 Array *a = g->get_array();
239 for (Array::Dim_iter d = a->dim_begin(); d != a->dim_end(); ++d) {
240 if (a->dimension_size(d,
true) > 1)
252 if (btp->send_p() && is_convertable_type(btp)) {
253 BESDEBUG(
"fong3",
"converting " << btp->name() << endl);
262 fb->extract_coordinates(t);
270 Structure::Vars_iter vi = s->var_begin();
271 while (vi != s->var_end()) {
272 if ((*vi)->send_p() && is_convertable_type(*vi)) {
273 build_delegate(*vi, t);
275 else if ((*vi)->type() == dods_structure_c) {
276 find_vars_helper(static_cast<Structure*>(*vi), t);
290 DDS::Vars_iter vi = dds->var_begin();
291 while (vi != dds->var_end()) {
292 BESDEBUG(
"fong3",
"looking at: " << (*vi)->name() <<
" and it is/isn't selected: " << (*vi)->send_p() << endl);
293 if ((*vi)->send_p() && is_convertable_type(*vi)) {
294 BESDEBUG(
"fong3",
"converting " << (*vi)->name() << endl);
295 build_delegate(*vi, t);
297 else if ((*vi)->type() == dods_structure_c) {
298 find_vars_helper(static_cast<Structure*>(*vi), t);
315 find_vars(d_dds, *
this);
318 if (!effectively_two_D(
var(i)))
319 throw Error(
"GeoTiff responses can consist of two-dimensional variables only; use constraints to reduce the size of Grids as needed.");
322 CPLSetErrorHandler(CPLQuietErrorHandler);
324 GDALDriver *Driver = GetGDALDriverManager()->GetDriverByName(
"GTiff");
326 throw Error(
"Could not get the GTiff driver from/for GDAL: " +
string(CPLGetLastErrorMsg()));
328 char **Metadata = Driver->GetMetadata();
329 if (!CSLFetchBoolean(Metadata, GDAL_DCAP_CREATE,
FALSE))
330 throw Error(
"Could not make output format.");
336 char **options =
NULL;
337 options = CSLSetNameValue(options,
"PHOTOMETRIC",
"MINISBLACK" );
338 d_dest = Driver->Create(d_localfile.c_str(),
width(),
height(),
num_bands(), GDT_Float64, options);
340 throw Error(
"Could not create the geotiff dataset: " +
string(CPLGetLastErrorMsg()));
346 BESDEBUG(
"fong3",
"Made new temp file and set georeferencing (" <<
num_bands() <<
" vars)." << endl);
348 bool projection_set =
false;
353 if (!projection_set) {
355 if (d_dest->SetProjection(wkt.c_str()) != CPLE_None)
356 throw Error(
"Could not set the projection: " +
string(CPLGetLastErrorMsg()));
357 projection_set =
true;
362 throw Error(
"In building a multiband response, different bands had different projection information.");
365 d_dest->AddBand(GDT_Float64, 0);
366 GDALRasterBand *band = d_dest->GetRasterBand(d_dest->GetRasterCount());
368 throw Error(
"Could not get the " + long_to_string(i) +
"th band: " +
string(CPLGetLastErrorMsg()));
370 GDALRasterBand *band = d_dest->GetRasterBand(i+1);
372 throw Error(
"Could not get the " + long_to_string(i+1) +
"th band: " +
string(CPLGetLastErrorMsg()));
386 BESDEBUG(
"fong3",
"calling band->RasterIO" << endl);
387 CPLErr error = band->RasterIO(GF_Write, 0, 0,
width(),
height(),
391 if (error != CPLE_None)
392 throw Error(
"Could not write data for band: " + long_to_string(i+1) +
": " +
string(CPLGetLastErrorMsg()));
421 find_vars(d_dds, *
this);
424 if (!effectively_two_D(
var(i)))
425 throw Error(
"GeoTiff responses can consist of two-dimensional variables only; use constraints to reduce the size of Grids as needed.");
428 CPLSetErrorHandler(CPLQuietErrorHandler);
430 GDALDriver *Driver = GetGDALDriverManager()->GetDriverByName(
"MEM");
432 throw Error(
"Could not get the MEM driver from/for GDAL: " +
string(CPLGetLastErrorMsg()));
434 char **Metadata = Driver->GetMetadata();
435 if (!CSLFetchBoolean(Metadata, GDAL_DCAP_CREATE,
FALSE))
436 throw Error(
"Driver JP2OpenJPEG does not support dataset creation.");
442 throw Error(
"Could not create in-memory dataset: " +
string(CPLGetLastErrorMsg()));
446 BESDEBUG(
"fong3",
"Made new temp file and set georeferencing (" <<
num_bands() <<
" vars)." << endl);
448 bool projection_set =
false;
453 if (!projection_set) {
455 if (d_dest->SetProjection(wkt.c_str()) != CPLE_None)
456 throw Error(
"Could not set the projection: " +
string(CPLGetLastErrorMsg()));
457 projection_set =
true;
462 throw Error(
"In building a multiband response, different bands had different projection information.");
465 GDALRasterBand *band = d_dest->GetRasterBand(i+1);
467 throw Error(
"Could not get the " + long_to_string(i+1) +
"th band: " +
string(CPLGetLastErrorMsg()));
481 BESDEBUG(
"fong3",
"calling band->RasterIO" << endl);
485 CPLErr error = band->RasterIO(GF_Write, 0, 0,
width(),
height(),
489 if (error != CPLE_None)
490 throw Error(CPLGetLastErrorMsg());
499 GDALDataset *jpeg_dst = 0;
501 Driver = GetGDALDriverManager()->GetDriverByName(
"JP2OpenJPEG");
503 throw Error(
"Could not get driver for JP2OpenJPEG: " +
string(CPLGetLastErrorMsg()));
506 char **Metadata = Driver->GetMetadata();
507 if (!CSLFetchBoolean(Metadata, GDAL_DCAP_CREATECOPY,
FALSE))
508 BESDEBUG(
"fong",
"Driver JP2OpenJPEG does not support dataset creation via 'CreateCopy()'." << endl);
511 char **options =
NULL;
512 options = CSLSetNameValue(options,
"CODEC",
"JP2");
513 options = CSLSetNameValue(options,
"GMLJP2",
"YES");
514 options = CSLSetNameValue(options,
"GeoJP2",
"NO");
515 options = CSLSetNameValue(options,
"QUALITY",
"100");
516 options = CSLSetNameValue(options,
"REVERSIBLE",
"YES");
518 BESDEBUG(
"fong3",
"Before JPEG2000 CreateCopy, number of bands: " << d_dest->GetRasterCount() << endl);
520 jpeg_dst = Driver->CreateCopy(d_localfile.c_str(), d_dest,
FALSE,
521 options,
NULL, NULL);
524 throw Error(
"Could not create the JPEG200 dataset: " +
string(CPLGetLastErrorMsg()));
528 GDALClose (jpeg_dst);
exception thrown if inernal error encountered
A DAP Grid with file out netcdf information included.
virtual string get_projection(libdap::DDS *dds)=0
Get the GDAL/OGC WKT projection string.
static class NCMLUtil overview
virtual libdap::Type type()
A DAP BaseType with file out gdal information included.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
virtual double * get_data()=0
Get the data values for the band(s). Call must delete.