All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Band.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/gdal/Band.cpp
22 
23  \brief This class represents raster band description.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../raster/RasterProperty.h"
29 #include "../raster/Utils.h"
30 #include "Band.h"
31 #include "Exception.h"
32 #include "Raster.h"
33 #include "Utils.h"
34 
35 // STL
36 #include <cassert>
37 #include <limits>
38 
39 // GDAL
40 #include <gdal_priv.h>
41 
42 te::gdal::Band::Band(Raster* rstPtr, std::size_t idx, GDALRasterBand* gdalRasterBandPtr )
43  : te::rst::Band(0, idx),
44  m_raster(rstPtr),
45  m_rasterBand(gdalRasterBandPtr)
46 {
47  m_gdaltype = m_rasterBand->GetRasterDataType();
48 
50 
52 
53  m_buffer = new unsigned char[getBlockSize()];
54 
55  m_x = std::numeric_limits<int>::max();
56 
57  m_y = std::numeric_limits<int>::max();
58 
59  m_update_buffer = false;
60 }
61 
63  : te::rst::Band(rhs),
64  m_raster(rhs.m_raster),
65  m_rasterBand(rhs.m_rasterBand),
66  m_getBuff(rhs.m_getBuff),
67  m_gdaltype(rhs.m_gdaltype)
68 {
69  m_buffer = new unsigned char[getBlockSize()];
70 
71  m_x = std::numeric_limits<int>::max();
72 
73  m_y = std::numeric_limits<int>::max();
74 
75  m_update_buffer = false;
76 }
77 
79 {
80  if (m_update_buffer)
81  {
82  m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
83 
84  m_rasterBand->FlushCache();
85  }
86 
87  unsigned char* buff = (unsigned char*)m_buffer;
88 
89  delete [] buff;
90 }
91 
93 {
94  return m_raster;
95 }
96 
98 {
99  assert(m_property->getType() == rhs.m_property->getType());
100 
101  if(this != &rhs)
102  {
104 
105  m_getBuff = rhs.m_getBuff;
106 
107  unsigned char* buffer = new unsigned char[rhs.getBlockSize()];
108 
109  for (int x = 0; x < m_property->m_nblocksx; ++x)
110  for (int y = 0; y < m_property->m_nblocksy; ++y)
111  {
112  rhs.read(x, y, buffer);
113 
114  write(x, y, buffer);
115  }
116 
117  delete [] buffer;
118  }
119 
120  return *this;
121 }
122 
123 void te::gdal::Band::getValue(unsigned int c, unsigned int r, double& value) const
124 {
125  m_i = placeBuffer(c, r);
126 
127  m_getBuff(m_i, m_buffer, &value);
128 }
129 
130 void te::gdal::Band::setValue(unsigned int c, unsigned int r, const double value)
131 {
132  m_i = placeBuffer(c, r);
133 
134  m_setBuff(m_i, m_buffer, &value);
135 
136  m_update_buffer = true;
137 }
138 
139 void te::gdal::Band::getIValue(unsigned int c, unsigned int r, double& value) const
140 {
141  m_i = placeBuffer(c, r);
142 
143  m_getBuffI(m_i, m_buffer, &value);
144 }
145 
146 void te::gdal::Band::setIValue(unsigned int c, unsigned int r, const double value)
147 {
148  m_i = placeBuffer(c, r);
149 
150  m_setBuffI(m_i, m_buffer, &value);
151 
152  m_update_buffer = true;
153 }
154 
155 void te::gdal::Band::read(int x, int y, void* buffer) const
156 {
157  m_rasterBand->ReadBlock(x, y, buffer);
158 
159  // If there is a palette, the values must be interpreted as
160  // Palette indexes
161 
162  if( m_rasterBand->GetColorInterpretation() == GCI_PaletteIndex )
163  {
164  const int bufferSize = m_property->m_blkh * m_property->m_blkw;
165 
166  const GDALColorTable& cTable = *( m_rasterBand->GetColorTable() );
167  double value = 0;
168  GDALColorEntry const * cEntryPtr = 0;
169 
170  switch( te::rst::Band::m_idx )
171  {
172  case 0 :
173  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
174  {
175  m_getBuff( bufferIdx, buffer, &value );
176 
177  cEntryPtr = cTable.GetColorEntry( (int)value );
178  assert(cEntryPtr );
179 
180  value = (double)cEntryPtr->c1;
181 
182  m_setBuff( bufferIdx, buffer, &value );
183  }
184  break;
185  case 1 :
186  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
187  {
188  m_getBuff( bufferIdx, buffer, &value );
189 
190  cEntryPtr = cTable.GetColorEntry( (int)value );
191  assert(cEntryPtr );
192 
193  value = (double)cEntryPtr->c2;
194 
195  m_setBuff( bufferIdx, buffer, &value );
196  }
197  break;
198  case 2 :
199  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
200  {
201  m_getBuff( bufferIdx, buffer, &value );
202 
203  cEntryPtr = cTable.GetColorEntry( (int)value );
204  assert(cEntryPtr );
205 
206  value = (double)cEntryPtr->c3;
207 
208  m_setBuff( bufferIdx, buffer, &value );
209  }
210  break;
211  case 3 :
212  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
213  {
214  m_getBuff( bufferIdx, buffer, &value );
215 
216  cEntryPtr = cTable.GetColorEntry( (int)value );
217  assert(cEntryPtr );
218 
219  value = (double)cEntryPtr->c4;
220 
221  m_setBuff( bufferIdx, buffer, &value );
222  }
223  break;
224  default :
225  throw Exception(TE_TR("Invalid band index"));
226  break;
227  }
228  }
229 }
230 
231 void* te::gdal::Band::read(int x, int y)
232 {
233  if( ( x != m_x ) || ( y != m_y ) )
234  {
235  if (m_update_buffer)
236  {
237  m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
238 
239  m_rasterBand->FlushCache();
240 
241  m_update_buffer = false;
242  }
243 
244  read( x, y, m_buffer );
245  m_x = x;
246  m_y = y;
247  }
248 
249  return m_buffer;
250 }
251 
252 void te::gdal::Band::write(int x, int y, void* buffer)
253 {
254  m_rasterBand->WriteBlock(x, y, buffer);
255 }
256 
257 int te::gdal::Band::placeBuffer(unsigned c, unsigned r) const
258 {
259  assert(c >= 0 && c < m_raster->getNumberOfColumns());
260  assert(r >= 0 && r < m_raster->getNumberOfRows());
261 
262  m_currX = c / m_property->m_blkw;
263 
264  m_currY = r / m_property->m_blkh;
265 
266  m_currC = c % m_property->m_blkw;
267 
268  m_currR = r % m_property->m_blkh;
269 
270  if (m_currX != m_x || m_currY != m_y)
271  {
272  if (m_update_buffer)
273  {
274  m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
275 
276  m_rasterBand->FlushCache();
277 
278  m_update_buffer = false;
279  }
280 
281  read(m_currX, m_currY, m_buffer);
282 
283  m_x = m_currX;
284 
285  m_y = m_currY;
286  }
287 
288 // calculates and returns the value of m_i
289  return (m_currC + m_currR * m_property->m_blkw);
290 }
void write(int x, int y, void *buffer)
It writes a data block from the specified buffer.
Definition: Band.cpp:252
This is a class that represents a GDAL Raster.
void read(int x, int y, void *buffer) const
It reads a data block to the specified buffer.
Definition: Band.cpp:155
int m_x
Actual x buffer position.
Definition: Band.h:134
te::rst::GetBufferValueFPtr m_getBuffI
A pointer to a function that helps to extract the imaginary part value from a specific buffer data ty...
Definition: Band.h:129
void getIValue(unsigned int c, unsigned int r, double &value) const
Returns the imaginary attribute value in a complex band of a cell.
Definition: Band.cpp:139
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
void * m_buffer
An internal buffer.
Definition: Band.h:133
This class represents Raster data.
Definition: Raster.h:61
virtual Band & operator=(const Band &rhs)
Assignment operator.
Definition: Band.cpp:48
It gives access to values in one band (dimension) of a raster.
Band & operator=(const Band &rhs)
Definition: Band.cpp:97
te::rst::BandProperty * GetBandProperty(GDALRasterBand *gband, const unsigned int bandIndex)
Gets the properties of a single band from a GDAL dataset.
Definition: Utils.cpp:243
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
te::rst::GetBufferValueFPtr m_getBuff
A pointer to a function that helps to extract a double or complex value from a specific buffer data t...
Definition: Band.h:128
This class represents raster band description.
Definition: Band.h:64
te::rst::SetBufferValueFPtr m_setBuffI
A pointer to a function that helps to insert the imaginary part value into a specific buffer data typ...
Definition: Band.h:131
Band(Raster *rstPtr, std::size_t idx, GDALRasterBand *gdalRasterBandPtr)
Constructor.
Definition: Band.cpp:42
An exception class for the GDAL module.
void setValue(unsigned int c, unsigned int r, const double value)
Sets the cell attribute value.
Definition: Band.cpp:130
An abstract class for raster data strucutures.
Definition: Raster.h:71
GDALRasterBand * m_rasterBand
GDAL Raster band.
Definition: Band.h:127
int m_y
Actual y buffer position.
Definition: Band.h:135
te::rst::SetBufferValueFPtr m_setBuff
A pointer to a function that helps to insert a double or complex value into a specific buffer data ty...
Definition: Band.h:130
void getValue(unsigned int c, unsigned int r, double &value) const
Returns the cell attribute value.
Definition: Band.cpp:123
~Band()
Virtual destructor.
Definition: Band.cpp:78
void setIValue(unsigned int c, unsigned int r, const double value)
Sets the imaginary attribute value in a complex band of a cell.
Definition: Band.cpp:146
std::size_t m_idx
The band index.
Definition: Band.h:475
GDALDataType m_gdaltype
The GDAL Data type.
Definition: Band.h:132
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
bool m_update_buffer
Flag to update buffer.
Definition: Band.h:136
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
Definition: Band.cpp:630
te::rst::Raster * getRaster() const
Returns the associated raster.
Definition: Band.cpp:92
TERASTEREXPORT void SetBlockFunctions(GetBufferValueFPtr *gb, GetBufferValueFPtr *gbi, SetBufferValueFPtr *sb, SetBufferValueFPtr *sbi, int type)
Sets the pointers to functions that helps to extract a double or complex value from a specific buffer...
Definition: BlockUtils.cpp:295
int placeBuffer(unsigned c, unsigned r) const
Places the buffer in position adequate to obtain row/column values.
Definition: Band.cpp:257
BandProperty * m_property
The band information.
Definition: Band.h:474