src/terralib/gdal/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 "../core/translator/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(nullptr, idx),
44  m_raster(rstPtr),
45  m_rasterBand(gdalRasterBandPtr)
46 {
47  m_gdaltype = m_rasterBand->GetRasterDataType();
48 
49  m_property = GetBandProperty(m_rasterBand, static_cast<unsigned int>(idx));
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),
66  m_getBuff(rhs.m_getBuff),
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  CPLErr error = m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
83 
84  if(error == CE_Failure || error == CE_Fatal)
85  {
86  //TODO There are codes that describes the error. Send a message according to it.
87  }
88 
89  m_rasterBand->FlushCache();
90  }
91 
92  unsigned char* buff = reinterpret_cast<unsigned char*>(m_buffer);
93 
94  delete [] buff;
95 }
96 
98 {
99  return m_raster;
100 }
101 
103 {
104  assert(m_property->getType() == rhs.m_property->getType());
105 
106  if(this != &rhs)
107  {
109 
110  m_getBuff = rhs.m_getBuff;
111 
112  unsigned char* buffer = new unsigned char[rhs.getBlockSize()];
113 
114  for (int x = 0; x < m_property->m_nblocksx; ++x)
115  for (int y = 0; y < m_property->m_nblocksy; ++y)
116  {
117  rhs.read(x, y, buffer);
118 
119  write(x, y, buffer);
120  }
121 
122  delete [] buffer;
123  }
124 
125  return *this;
126 }
127 
128 void te::gdal::Band::getValue(unsigned int c, unsigned int r, double& value) const
129 {
130  m_i = placeBuffer(c, r);
131 
132  m_getBuff(m_i, m_buffer, &value);
133 }
134 
135 void te::gdal::Band::setValue(unsigned int c, unsigned int r, const double value)
136 {
137  m_i = placeBuffer(c, r);
138 
139  m_setBuff(m_i, m_buffer, &value);
140 
141  m_update_buffer = true;
142 }
143 
144 void te::gdal::Band::getIValue(unsigned int c, unsigned int r, double& value) const
145 {
146  m_i = placeBuffer(c, r);
147 
148  m_getBuffI(m_i, m_buffer, &value);
149 }
150 
151 void te::gdal::Band::setIValue(unsigned int c, unsigned int r, const double value)
152 {
153  m_i = placeBuffer(c, r);
154 
155  m_setBuffI(m_i, m_buffer, &value);
156 
157  m_update_buffer = true;
158 }
159 
160 void te::gdal::Band::read(int x, int y, void* buffer) const
161 {
162  if (m_update_buffer)
163  {
164  CPLErr error = m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
165 
166  if(error == CE_Failure || error == CE_Fatal)
167  {
168  //TODO There are codes that describes the error. Send a message according to it.
169  }
170 
171  m_rasterBand->FlushCache();
172 
173  m_update_buffer = false;
174  }
175 
176  CPLErr error = m_rasterBand->ReadBlock(x, y, buffer);
177 
178  if(error == CE_Failure || error == CE_Fatal)
179  {
180  //TODO There are codes that describes the error. Send a message according to it.
181  }
182 }
183 
184 void* te::gdal::Band::read(int x, int y)
185 {
186  if( ( x != m_x ) || ( y != m_y ) )
187  {
188  read( x, y, m_buffer );
189  m_x = x;
190  m_y = y;
191  }
192 
193  return m_buffer;
194 }
195 
196 void te::gdal::Band::write(int x, int y, void* buffer)
197 {
198  if (m_update_buffer)
199  {
200  CPLErr error = m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
201 
202  if(error == CE_Failure || error == CE_Fatal)
203  {
204  //TODO There are codes that describes the error. Send a message according to it.
205  }
206 
207  m_rasterBand->FlushCache();
208 
209  m_update_buffer = false;
210  }
211 
212  CPLErr error = m_rasterBand->WriteBlock(x, y, buffer);
213 
214  if(error == CE_Failure || error == CE_Fatal)
215  {
216  //TODO There are codes that describes the error. Send a message according to it.
217  }
218 }
219 
220 std::complex<double> te::gdal::Band::getMinValue(bool readall, unsigned int rs, unsigned int cs, unsigned int rf, unsigned int cf) const
221 {
222  if (rs == 0, cs == 0, rf == 0, cf == 0)
223  {
224  double min = 0;
225  double max = 0;
226  double mean = 0;
227  double stdDev = 0;
228 
229  if (m_rasterBand->GetStatistics(!readall, true, &min, &max, &mean, &stdDev) == CE_None)
230  {
231  return std::complex<double>(min, 0.0);
232  }
233  }
234 
235  return te::rst::Band::getMinValue(readall, rs, cs, rf, cf);
236 }
237 
238 std::complex<double> te::gdal::Band::getMaxValue(bool readall, unsigned int rs, unsigned int cs, unsigned int rf, unsigned int cf) const
239 {
240  if (rs == 0, cs == 0, rf == 0, cf == 0)
241  {
242  double min = 0;
243  double max = 0;
244  double mean = 0;
245  double stdDev = 0;
246 
247  if (m_rasterBand->GetStatistics(!readall, true, &min, &max, &mean, &stdDev) == CE_None)
248  {
249  return std::complex<double>(max, 0.0);
250  }
251  }
252 
253  return te::rst::Band::getMaxValue(readall, rs, cs, rf, cf);
254 }
255 
256 std::complex<double> te::gdal::Band::getStdValue(unsigned int rs, unsigned int cs, unsigned int rf, unsigned int cf) const
257 {
258  if (rs == 0, cs == 0, rf == 0, cf == 0)
259  {
260  double min = 0;
261  double max = 0;
262  double mean = 0;
263  double stdDev = 0;
264 
265  if (m_rasterBand->GetStatistics(false, true, &min, &max, &mean, &stdDev) == CE_None)
266  {
267  return std::complex<double>(stdDev, 0.0);
268  }
269  }
270 
271  return te::rst::Band::getStdValue(rs, cs, rf, cf);
272 }
273 
274 std::complex<double> te::gdal::Band::getMeanValue(unsigned int rs, unsigned int cs, unsigned int rf, unsigned int cf) const
275 {
276  if (rs == 0, cs == 0, rf == 0, cf == 0)
277  {
278  double min = 0;
279  double max = 0;
280  double mean = 0;
281  double stdDev = 0;
282 
283  if (m_rasterBand->GetStatistics(false, true, &min, &max, &mean, &stdDev) == CE_None)
284  {
285  return std::complex<double>(mean, 0.0);
286  }
287  }
288 
289  return te::rst::Band::getMeanValue(rs, cs, rf, cf);
290 }
291 
292 int te::gdal::Band::placeBuffer(unsigned c, unsigned r) const
293 {
294  assert(c >= 0 && c < m_raster->getNumberOfColumns());
295  assert(r >= 0 && r < m_raster->getNumberOfRows());
296 
297  m_currX = static_cast<int>(c) / m_property->m_blkw;
298 
299  m_currY = static_cast<int>(r) / m_property->m_blkh;
300 
301  m_currC = static_cast<int>(c) % m_property->m_blkw;
302 
303  m_currR = static_cast<int>(r) % m_property->m_blkh;
304 
305  if (m_currX != m_x || m_currY != m_y)
306  {
307  if (m_update_buffer)
308  {
309  CPLErr error = m_rasterBand->WriteBlock(m_x, m_y, m_buffer);
310 
311  if(error == CE_Failure || error == CE_Fatal)
312  {
313  //TODO There are codes that describes the error. Send a message according to it.
314  }
315 
316  m_rasterBand->FlushCache();
317 
318  m_update_buffer = false;
319  }
320 
322 
323  m_x = m_currX;
324 
325  m_y = m_currY;
326  }
327 
328 // calculates and returns the value of m_i
329  return (m_currC + m_currR * m_property->m_blkw);
330 }
void write(int x, int y, void *buffer)
It writes a data block from the specified buffer.
void read(int x, int y, void *buffer) const
It reads a data block to the specified buffer.
int m_x
Actual x buffer position.
int m_currC
Block column position.
te::rst::GetBufferValueFPtr m_getBuffI
A pointer to a function that helps to extract the imaginary part value from a specific buffer data ty...
void getIValue(unsigned int c, unsigned int r, double &value) const
Returns the imaginary attribute value in a complex band of a cell.
void * m_buffer
An internal buffer.
This class represents Raster data.
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
virtual Band & operator=(const Band &rhs)
Assignment operator.
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
Band & operator=(const Band &rhs)
virtual std::complex< double > getStdValue(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the standard deviation of the occurring values in a window of the band...
te::rst::BandProperty * GetBandProperty(GDALRasterBand *gband, const unsigned int bandIndex)
Gets the properties of a single band from a GDAL dataset.
This is a class that represents a GDAL Raster.
int m_currR
Block row position.
int m_currY
Block y position.
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...
This class represents raster band description.
te::rst::SetBufferValueFPtr m_setBuffI
A pointer to a function that helps to insert the imaginary part value into a specific buffer data typ...
Band(Raster *rstPtr, std::size_t idx, GDALRasterBand *gdalRasterBandPtr)
Constructor.
virtual std::complex< double > getMaxValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the maximum occurring value in a window of the band.
virtual std::complex< double > getStdValue(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the standard deviation of the occurring values in a window of the band...
void setValue(unsigned int c, unsigned int r, const double value)
Sets the cell attribute value.
An abstract class for raster data strucutures.
URI C++ Library.
Definition: Attributes.h:37
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
GDALRasterBand * m_rasterBand
GDAL Raster band.
int m_y
Actual y buffer position.
virtual std::complex< double > getMeanValue(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the mean of the occurring values in a window of the band. ...
te::rst::Raster * m_raster
The type of function used to insert/extract data from buffers.
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...
void getValue(unsigned int c, unsigned int r, double &value) const
Returns the cell attribute value.
~Band()
Virtual destructor.
virtual std::complex< double > getMaxValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the maximum occurring value in a window of the band.
void setIValue(unsigned int c, unsigned int r, const double value)
Sets the imaginary attribute value in a complex band of a cell.
int m_i
Block index.
virtual std::complex< double > getMinValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the minimum occurring value in a window of the band.
virtual std::complex< double > getMeanValue(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the mean of the occurring values in a window of the band. ...
GDALDataType m_gdaltype
The GDAL Data type.
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
int m_currX
Block x position.
bool m_update_buffer
Flag to update buffer.
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
It gives access to values in one band (dimension) of a raster.
An exception class for the GDAL module.
virtual std::complex< double > getMinValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the minimum occurring value in a window of the band.
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
te::rst::Raster * getRaster() const
Returns the associated raster.
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.
BandProperty * m_property
The band information.
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.