summaryrefslogtreecommitdiffstats
path: root/rasodmg/storagelayout.cc
blob: 847759d3db4293f3d30f9fa3b405990878ab586d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: storagelayout.cc
 *
 * MODULE: rasodmg
 * CLASS:  r_StorageLayout, r_Domain_Storage_Layout
 *
 * COMMENTS:
 *   		None
 *
*/

// Because of NT port
#include <string.h>
#ifdef __VISUALC__
#include <strstrea.h>
#else
#include <strstream>
#endif
#include <math.h>
#include <vector>

#include "rasodmg/storagelayout.hh"
#include "rasodmg/iterator.hh"
#include "rasodmg/tiling.hh"
#include "rasodmg/alignedtiling.hh"
#include "raslib/rmdebug.hh"
#include "raslib/rminit.hh"
#include "raslib/minterval.hh"
#include "rasodmg/gmarray.hh"

r_Storage_Layout::r_Storage_Layout(r_Data_Format init_format, const char* formatParams)
	:	til(0),
		storage_format(init_format),
		storage_params(0)
{
  til = new r_Size_Tiling();
  if (formatParams != NULL)
    storage_params = strdup(formatParams);
}

r_Storage_Layout::r_Storage_Layout(r_Tiling* ts, r_Data_Format init_format, const char* formatParams)
	:	til(ts),
		storage_format(init_format),
		storage_params(0)
{
  if (til == NULL)
    til = new r_Size_Tiling();
  if (formatParams != NULL)
    storage_params = strdup(formatParams);
}

r_Storage_Layout::r_Storage_Layout(const r_Storage_Layout& sl)
	:	til(sl.get_tiling()->clone()),
		storage_format(sl.get_storage_format()),
		storage_params(0)
{
  if (sl.storage_params != NULL)
    storage_params = strdup(sl.storage_params);
}

r_Storage_Layout*
r_Storage_Layout::clone() const
{
  r_Storage_Layout* newSL = new r_Storage_Layout(til->clone(), storage_format, storage_params);
  return newSL;
}

r_Storage_Layout::~r_Storage_Layout()
{
  if(til)
  {
    delete til;
    til = NULL;
  }
  if (storage_params)
  {
    free(storage_params);
    storage_params = NULL;
  }
}

const r_Tiling*
r_Storage_Layout::get_tiling() const
{
  return til;
}

r_Data_Format
r_Storage_Layout::get_storage_format() const
{
  return storage_format;
}

const char*
r_Storage_Layout::get_storage_format_params() const
{
  return storage_params;
}

r_Set< r_GMarray* >* 
r_Storage_Layout::decomposeMDD(const r_GMarray* mar) const throw (r_Error)
{
  r_Bytes cell_size = mar->get_type_length();
  std::vector<r_Minterval>* tiles=NULL;
  r_Set<r_GMarray*>* result=NULL;
  
  if (!til->is_compatible(mar->spatial_domain(), cell_size))
    {
    RMInit::logOut << "r_Storage_Layout::decomposeMDD() gmarray is not compatible with tiling" << endl;
    RMInit::logOut << "\tgmarray domain   : " << mar->spatial_domain() << endl;
    RMInit::logOut << "\tgmarray type size: " << mar->get_type_length() << endl;
    RMInit::logOut << "\tstorage layout   : " << *this << endl;
    throw r_Error(STORAGERLAYOUTINCOMPATIBLEWITHGMARRAY);
    }



  try
  {
   tiles = til->compute_tiles(mar->spatial_domain(), cell_size);
  }
  catch(r_Error& err)
  {
   throw;
  }
  
  result = new r_Set<r_GMarray*>;
  
  for (std::vector<r_Minterval>::iterator it = tiles->begin(); it != tiles->end(); it++)
    result->insert_element(mar->intersect(*it));

  delete tiles;
  return result;
}

void 
r_Storage_Layout::print_status(std::ostream& os) const
{
  os << "r_Storage_Layout[ tiling = "<< *til << " storage format = " << storage_format << " storage parameters = ";
  if (storage_params != NULL)
    os << "upps, not here";
    //os << storage_params;
  else
    os << "none defined";
  os << " ]";
}
  
bool
r_Storage_Layout::is_compatible(const r_Minterval& obj_domain, r_Bytes cellTypeSize) const
{
  return til->is_compatible(obj_domain, cellTypeSize);
}
     
std::ostream& 
operator<<(std::ostream& s, const r_Storage_Layout& sl)
{
  sl.print_status(s);
  return s;
}