All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Band.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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* rst, std::size_t idx)
43  : te::rst::Band(0, idx),
44  m_raster(rst),
45  m_rasterBand(0),
46  m_getBuff(0)
47 {
48  m_rasterBand =
49  ( rst->getGDALDataset()->GetRasterBand(1)->GetColorInterpretation() ==
50  GCI_PaletteIndex ) ? rst->getGDALDataset()->GetRasterBand(1) :
51  rst->getGDALDataset()->GetRasterBand( idx + 1 );
52 
53  m_gdaltype = m_rasterBand->GetRasterDataType();
54 
56 
58 
59  m_buffer = new unsigned char[getBlockSize()];
60 
61  m_x = std::numeric_limits<int>::max();
62 
63  m_y = std::numeric_limits<int>::max();
64 
65  m_update_buffer = false;
66 }
67 
69  : te::rst::Band(rhs),
70  m_raster(rhs.m_raster),
71  m_rasterBand(rhs.m_rasterBand),
72  m_getBuff(rhs.m_getBuff),
73  m_gdaltype(rhs.m_gdaltype)
74 {
75  m_buffer = new unsigned char[getBlockSize()];
76 
77  m_x = std::numeric_limits<int>::max();
78 
79  m_y = std::numeric_limits<int>::max();
80 
81  m_update_buffer = false;
82 }
83 
85 {
86  if (m_update_buffer)
87  {
88  m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
89 
90  m_rasterBand->FlushCache();
91  }
92 
93  unsigned char* buff = (unsigned char*)m_buffer;
94 
95  delete [] buff;
96 }
97 
99 {
100  return m_raster;
101 }
102 
104 {
105  assert(m_property->getType() == rhs.m_property->getType());
106 
107  if(this != &rhs)
108  {
110 
111  m_getBuff = rhs.m_getBuff;
112 
113  unsigned char* buffer = new unsigned char[rhs.getBlockSize()];
114 
115  for (int x = 0; x < m_property->m_nblocksx; ++x)
116  for (int y = 0; y < m_property->m_nblocksy; ++y)
117  {
118  rhs.read(x, y, buffer);
119 
120  write(x, y, buffer);
121  }
122 
123  delete [] buffer;
124  }
125 
126  return *this;
127 }
128 
129 void te::gdal::Band::getValue(unsigned int c, unsigned int r, double& value) const
130 {
131  m_i = placeBuffer(c, r);
132 
133  m_getBuff(m_i, m_buffer, &value);
134 }
135 
136 void te::gdal::Band::setValue(unsigned int c, unsigned int r, const double value)
137 {
138  m_i = placeBuffer(c, r);
139 
140  m_setBuff(m_i, m_buffer, &value);
141 
142  m_update_buffer = true;
143 }
144 
145 void te::gdal::Band::getIValue(unsigned int c, unsigned int r, double& value) const
146 {
147  m_i = placeBuffer(c, r);
148 
149  m_getBuffI(m_i, m_buffer, &value);
150 }
151 
152 void te::gdal::Band::setIValue(unsigned int c, unsigned int r, const double value)
153 {
154  m_i = placeBuffer(c, r);
155 
156  m_setBuffI(m_i, m_buffer, &value);
157 
158  m_update_buffer = true;
159 }
160 
161 void te::gdal::Band::read(int x, int y, void* buffer) const
162 {
163  m_rasterBand->ReadBlock(x, y, buffer);
164 
165  // If there is a palette, the values must be interpreted as
166  // Palette indexes
167 
168  if( m_rasterBand->GetColorInterpretation() == GCI_PaletteIndex )
169  {
170  const int bufferSize = m_property->m_blkh * m_property->m_blkw;
171 
172  const GDALColorTable& cTable = *( m_rasterBand->GetColorTable() );
173  double value = 0;
174  GDALColorEntry const * cEntryPtr = 0;
175 
176  switch( te::rst::Band::m_idx )
177  {
178  case 0 :
179  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
180  {
181  m_getBuff( bufferIdx, buffer, &value );
182 
183  cEntryPtr = cTable.GetColorEntry( (int)value );
184  assert(cEntryPtr );
185 
186  value = (double)cEntryPtr->c1;
187 
188  m_setBuff( bufferIdx, buffer, &value );
189  }
190  break;
191  case 1 :
192  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
193  {
194  m_getBuff( bufferIdx, buffer, &value );
195 
196  cEntryPtr = cTable.GetColorEntry( (int)value );
197  assert(cEntryPtr );
198 
199  value = (double)cEntryPtr->c2;
200 
201  m_setBuff( bufferIdx, buffer, &value );
202  }
203  break;
204  case 2 :
205  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
206  {
207  m_getBuff( bufferIdx, buffer, &value );
208 
209  cEntryPtr = cTable.GetColorEntry( (int)value );
210  assert(cEntryPtr );
211 
212  value = (double)cEntryPtr->c3;
213 
214  m_setBuff( bufferIdx, buffer, &value );
215  }
216  break;
217  case 3 :
218  for( int bufferIdx = 0 ; bufferIdx < bufferSize ; ++bufferIdx )
219  {
220  m_getBuff( bufferIdx, buffer, &value );
221 
222  cEntryPtr = cTable.GetColorEntry( (int)value );
223  assert(cEntryPtr );
224 
225  value = (double)cEntryPtr->c4;
226 
227  m_setBuff( bufferIdx, buffer, &value );
228  }
229  break;
230  default :
231  throw Exception(TR_GDAL("Invalid band index"));
232  break;
233  }
234  }
235 }
236 
237 void* te::gdal::Band::read(int x, int y)
238 {
239  if( ( x != m_x ) || ( y != m_y ) )
240  {
241  if (m_update_buffer)
242  {
243  m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
244 
245  m_rasterBand->FlushCache();
246 
247  m_update_buffer = false;
248  }
249 
250  read( x, y, m_buffer );
251  m_x = x;
252  m_y = y;
253  }
254 
255  return m_buffer;
256 }
257 
258 void te::gdal::Band::write(int x, int y, void* buffer)
259 {
260  m_rasterBand->WriteBlock(x, y, buffer);
261 }
262 
263 int te::gdal::Band::placeBuffer(unsigned c, unsigned r) const
264 {
265  assert(c >= 0 && c < m_raster->getNumberOfColumns());
266  assert(r >= 0 && r < m_raster->getNumberOfRows());
267 
268  m_currX = c / m_property->m_blkw;
269 
270  m_currY = r / m_property->m_blkh;
271 
272  m_currC = c % m_property->m_blkw;
273 
274  m_currR = r % m_property->m_blkh;
275 
276  if (m_currX != m_x || m_currY != m_y)
277  {
278  if (m_update_buffer)
279  {
280  m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
281 
282  m_rasterBand->FlushCache();
283 
284  m_update_buffer = false;
285  }
286 
287  read(m_currX, m_currY, m_buffer);
288 
289  m_x = m_currX;
290 
291  m_y = m_currY;
292  }
293 
294 // calculates and returns the value of m_i
295  return (m_currC + m_currR * m_property->m_blkw);
296 }
297 
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:119
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:145
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
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:121
Band(Raster *rst, std::size_t idx)
Definition: Band.cpp:42
This class represents Raster data.
Definition: Raster.h:59
void * m_buffer
An internal buffer.
Definition: Band.h:124
GDALDataType m_gdaltype
The GDAL Data type.
Definition: Band.h:123
GDALRasterBand * m_rasterBand
GDAL Raster band.
Definition: Band.h:118
It gives access to values in one band (dimension) of a raster.
te::rst::Raster * getRaster() const
Returns the associated raster.
Definition: Band.cpp:98
void getValue(unsigned int c, unsigned int r, double &value) const
Returns the cell attribute value.
Definition: Band.cpp:129
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:122
bool m_update_buffer
Flag to update buffer.
Definition: Band.h:127
int m_x
Actual x buffer position.
Definition: Band.h:125
virtual Band & operator=(const Band &rhs)
Assignment operator.
Definition: Band.cpp:46
void read(int x, int y, void *buffer) const
It reads a data block to the specified buffer.
Definition: Band.cpp:161
int placeBuffer(unsigned c, unsigned r) const
Places the buffer in position adequate to obtain row/column values.
Definition: Band.cpp:263
void setValue(unsigned int c, unsigned int r, const double value)
Sets the cell attribute value.
Definition: Band.cpp:136
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
An exception class for the GDAL module.
Band & operator=(const Band &rhs)
Definition: Band.cpp:103
This class represents raster band description.
Definition: Band.h:64
std::size_t m_idx
The band index.
Definition: Band.h:414
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:120
void write(int x, int y, void *buffer)
It writes a data block from the specified buffer.
Definition: Band.cpp:258
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
Definition: Band.cpp:490
This is a class that represents a GDAL Raster.
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
~Band()
Virtual destructor.
Definition: Band.cpp:84
An abstract class for raster data strucutures.
Definition: Raster.h:70
GDALDataset * getGDALDataset() const
Returns the raster GDAL handler.
Definition: Raster.cpp:250
int m_y
Actual y buffer position.
Definition: Band.h:126
te::rst::BandProperty * GetBandProperty(GDALRasterBand *gband, const unsigned int bandIndex)
Gets the properties of a single band from a GDAL dataset.
Definition: Utils.cpp:187
BandProperty * m_property
The band information.
Definition: Band.h:413
#define TR_GDAL(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...
Definition: Config.h:64
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:152