OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
roi_utils.cc
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2015 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include "config.h"
26 
27 #include <memory>
28 
29 #include <BaseType.h>
30 #include <Structure.h>
31 #include <Array.h>
32 #include <Int32.h>
33 #include <Str.h>
34 
35 #include <util.h>
36 
37 #include "roi_utils.h"
38 
39 namespace libdap {
40 
56 void roi_bbox_valid_slice(BaseType *btp)
57 {
58  // we know it's a Structure * and it has one element because the test above passed
59  if (btp->type() != dods_structure_c)
60  throw Error("In function roi(): Expected an Array of Structures for the slice information.");
61 
62  Structure *slice = static_cast<Structure*>(btp);
63 
64  Constructor::Vars_iter i = slice->var_begin();
65  if (i == slice->var_end() || (*i)->name() != "start" || (*i)->type() != dods_int32_c)
66  throw Error("In function roi(): Could not find valid 'start' field in slice information");
67 
68  ++i;
69  if (i == slice->var_end() || (*i)->name() != "stop" || (*i)->type() != dods_int32_c)
70  throw Error("In function roi(): Could not find valid 'stop' field in slice information");
71 
72  ++i;
73  if (i == slice->var_end() || (*i)->name() != "name" || (*i)->type() != dods_str_c)
74  throw Error("In function roi(): Could not find valid 'name' field in slice information");
75 }
76 
90 unsigned int roi_valid_bbox(BaseType *btp)
91 {
92  if (!btp)
93  throw InternalErr(__FILE__, __LINE__, "Function called with null slice array.");
94 
95  if (btp->type() != dods_array_c)
96  throw Error("Function expected last argument to be a Bounding Box (i.e., an Array of Structures) (1).");
97 
98  Array *slices = static_cast<Array*>(btp);
99  if (slices->dimensions() != 1)
100  throw Error("Function expected last argument to be a Bounding Box (i.e., an Array of Structures) (2).");
101 
102  int rank = slices->dimension_size(slices->dim_begin());
103  for (int i = 0; i < rank; ++i) {
104  roi_bbox_valid_slice(slices->var(i));
105  }
106 
107  return rank;
108 }
109 
122 void roi_bbox_get_slice_data(Array *slices, unsigned int i, int &start, int &stop, string &name)
123 {
124  BaseType *btp = slices->var(i);
125 
126  Structure *slice = static_cast<Structure*>(btp);
127  Constructor::Vars_iter vi = slice->var_begin();
128 
129  start = static_cast<Int32*>(*vi++)->value();
130  stop = static_cast<Int32*>(*vi++)->value();
131  name = static_cast<Str*>(*vi++)->value();
132 }
133 
153 Structure *roi_bbox_build_slice(unsigned int start_value, unsigned int stop_value, const string &dim_name)
154 {
155  Structure *slice = new Structure("slice"); // This name is superfluous
156 
157  Int32 *start = new Int32("start");
158  start->set_value(start_value);
159  slice->add_var_nocopy(start);
160 
161  Int32 *stop = new Int32("stop");
162  stop->set_value(stop_value);
163  slice->add_var_nocopy(stop);
164 
165  Str *name = new Str("name");
166  name->set_value(dim_name);
167  slice->add_var_nocopy(name);
168 
169  slice->set_read_p(true); // Sets all children too, as does set_send_p()
170  slice->set_send_p(true);
171 
172  return slice;
173 }
174 
192 auto_ptr<Array> roi_bbox_build_empty_bbox(unsigned int num_dim, const string &bbox_name)
193 {
194  // Build the Structure and load it with the needed fields. The
195  // Array instances will have the same fields, but each instance
196  // will also be loaded with values.
197  Structure *proto = new Structure(bbox_name);
198  proto->add_var_nocopy(new Int32("start"));
199  proto->add_var_nocopy(new Int32("stop"));
200  proto->add_var_nocopy(new Str("name"));
201  // Using auto_ptr and not unique_ptr because of OS/X 10.7. jhrg 2/24/15
202  auto_ptr<Array> response(new Array(bbox_name, proto));
203 
204  response->append_dim(num_dim, bbox_name);
205 
206  return response;
207 }
208 
209 } //namespace libdap
void roi_bbox_valid_slice(BaseType *btp)
Each Bounding Box is made up of a number of 'slices' - test if a given one is valid.
Definition: roi_utils.cc:56
void roi_bbox_get_slice_data(Array *slices, unsigned int i, int &start, int &stop, string &name)
This method extracts values from one element of the Bounding Box (i.e., Array of Structures).
Definition: roi_utils.cc:122
unsigned int roi_valid_bbox(BaseType *btp)
Is the bound box valid?
Definition: roi_utils.cc:90
Structure * roi_bbox_build_slice(unsigned int start_value, unsigned int stop_value, const string &dim_name)
Build a single element of a bounding box.
Definition: roi_utils.cc:153
static class NCMLUtil overview
auto_ptr< Array > roi_bbox_build_empty_bbox(unsigned int num_dim, const string &bbox_name)
Build an empty Bounding Box using DAP variables.
Definition: roi_utils.cc:192