32 #include <sys/types.h>
56 #define FONG_TEMP_DIR "/tmp"
57 #define FONG_GCS "WGS84"
59 string GeoTiffTransmitter::temp_dir;
83 if (GeoTiffTransmitter::temp_dir.empty()) {
86 string key =
"FONg.Tempdir";
88 if (!found || GeoTiffTransmitter::temp_dir.empty()) {
91 string::size_type len = GeoTiffTransmitter::temp_dir.length();
92 if (GeoTiffTransmitter::temp_dir[len - 1] ==
'/') {
93 GeoTiffTransmitter::temp_dir = GeoTiffTransmitter::temp_dir.substr(0, len - 1);
100 string key =
"FONg.Default_GCS";
129 DataDDS *dds = bdds->
get_dds();
131 throw BESInternalError(
"No DataDDS has been created for transmit", __FILE__, __LINE__);
135 throw BESInternalError(
"Output stream is not set, cannot return as", __FILE__, __LINE__);
137 BESDEBUG(
"fong2",
"GeoTiffTransmitter::send_data - parsing the constraint" << endl);
142 bdds->
get_ce().parse_constraint(ce, *dds);
145 throw BESInternalError(
"Failed to parse the constraint expression: " + e.get_error_message(), __FILE__, __LINE__);
148 throw BESInternalError(
"Failed to parse the constraint expression: Unknown exception caught", __FILE__, __LINE__);
152 BESDEBUG(
"fong2",
"GeoTiffTransmitter::send_data - reading data into DataDDS" << endl);
156 if (bdds->
get_ce().function_clauses()) {
157 BESDEBUG(
"fonc",
"processing a functional constraint clause(s)." << endl);
158 DataDDS *tmp_dds = bdds->
get_ce().eval_function_clauses(*dds);
166 for (DDS::Vars_iter i = dds->var_begin(); i != dds->var_end(); i++) {
167 if ((*i)->send_p()) {
168 (*i)->intern_data(bdds->
get_ce(), *dds);
174 throw BESInternalError(
"Failed to read data: " + e.get_error_message(), __FILE__, __LINE__);
177 throw BESInternalError(
"Failed to read data: Unknown exception caught", __FILE__, __LINE__);
182 string temp_file_name = GeoTiffTransmitter::temp_dir +
'/' +
"geotiffXXXXXX";
183 vector<char> temp_file(temp_file_name.length() + 1);
184 string::size_type len = temp_file_name.copy(&temp_file[0], temp_file_name.length());
185 temp_file[len] =
'\0';
189 mode_t original_mode = umask(077);
192 int fd = mkstemp(&temp_file[0]);
193 umask(original_mode);
196 throw BESInternalError(
"Failed to open the temporary file: " + temp_file_name, __FILE__, __LINE__);
199 BESDEBUG(
"fong2",
"GeoTiffTransmitter::send_data - transforming into temporary file " << &temp_file[0] << endl);
207 BESDEBUG(
"fong2",
"GeoTiffTransmitter::send_data - transmitting temp file " << &temp_file[0] << endl );
209 GeoTiffTransmitter::return_temp_stream(&temp_file[0], strm);
213 (void) unlink(&temp_file[0]);
218 (void) unlink(&temp_file[0]);
224 (void) unlink(&temp_file[0]);
225 throw BESInternalError(
"Fileout GeoTiff, was not able to transform to geotiff, unknown error", __FILE__, __LINE__);
229 (void) unlink(&temp_file[0]);
231 BESDEBUG(
"fong2",
"GeoTiffTransmitter::send_data - done transmitting to geotiff" << endl);
243 void GeoTiffTransmitter::return_temp_stream(
const string &filename, ostream &strm)
246 os.open(filename.c_str(), ios::binary | ios::in);
248 throw BESInternalError(
"Cannot connect to data source", __FILE__, __LINE__);
251 os.read(block,
sizeof block);
252 int nbytes = os.gcount();
255 throw BESInternalError(
"Internal server error, got zero count on stream buffer.", __FILE__, __LINE__);
259 string context =
"transmit_protocol";
261 if (protocol ==
"HTTP") {
262 strm <<
"HTTP/1.0 200 OK\n";
263 strm <<
"Content-type: application/octet-stream\n";
264 strm <<
"Content-Description: " <<
"BES dataset" <<
"\n";
265 strm <<
"Content-Disposition: filename=" << filename <<
".tif;\n\n";
268 strm.write(block, nbytes);
271 os.read(block,
sizeof block);
272 nbytes = os.gcount();
273 strm.write(block, nbytes);
void set_dds(DataDDS *ddsIn)
Set the response object's DDS.
exception thrown if inernal error encountered
static string default_gcs
ostream & get_output_stream()
virtual bool add_method(string method_name, p_transmitter trans_method)
virtual string get_context(const string &name, bool &found)
retrieve the value of the specified context from the BES
static class NCMLUtil overview
Abstract exception class for the BES with basic string message.
GeoTiffTransmitter()
Construct the GeoTiffTransmitter, adding it with name geotiff to be able to transmit a data response...
Represents an OPeNDAP DataDDS DAP2 data object within the BES.
static BESContextManager * TheManager()
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
ConstraintEvaluator & get_ce()
Structure storing information used by the BES to handle the request.
map< string, string > data
the map of string data that will be required for the current request.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
static void send_data_as_geotiff(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a netcdf file.
static BESKeys * TheKeys()
Abstract base class representing a specific set of information in response to a request to the BES...