RasterIterator.h
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/raster/RasterIterator.h
22 
23  \brief It implements and iterator to "navigate" over a raster,
24  with a predefined number of bands.
25 */
26 
27 #ifndef __TERRALIB_RASTER_INTERNAL_RASTERITERATOR_H
28 #define __TERRALIB_RASTER_INTERNAL_RASTERITERATOR_H
29 
30 // Terralib
31 #include "Band.h"
32 #include "BandIterator.h"
33 #include "BandProperty.h"
34 
35 namespace te
36 {
37  namespace rst
38  {
39 // Forward declaration.
40  class Raster;
41 
42  /*!
43  \class RasterIterator
44 
45  \brief This class implements and iterator to "navigate" over a raster,
46  with a predefined number of bands.
47 
48  \ingroup rst
49 
50  \sa te::rst::Raster
51  */
52  template<class T> class RasterIterator
53  {
54  public:
55 
56  /*! \brief Empty constructor. */
58 
59  /*!
60  \brief Constructor.
61 
62  \param r The raster to iterate.
63  \param bands The vector of band indices that the iterator will use.
64  */
65  RasterIterator(Raster* r, const std::vector<unsigned int>& bands, bool ignoreNoDataValue = false);
66 
67  /*!
68  \brief Copy constructor.
69 
70  \param rhs The right-hand-side copy used to copy from.
71  */
72  RasterIterator(const RasterIterator& rhs);
73 
74  /*! \brief Destructor. */
76 
77  /*! \brief Returns the current row in iterator. */
78  unsigned int getRow() const;
79 
80  /*! \brief Returns the current column in iterator. */
81  unsigned int getColumn() const;
82 
83  /*!
84  \brief Returns the value in current position (column, row, band) from iterator.
85 
86  \param i The band index.
87 
88  \return The pixel value in current position.
89  */
90  T operator[](const unsigned int i) const;
91 
92  /*! \brief Returns a vector with pixel values from all bands in current position (column, row) from iterator. */
93  std::vector<T> operator*() const;
94 
95  /*!
96  \brief Gets the values from all bands in current position (column, row) from iterator.
97 
98  \param v A vector previously created to be filled with values.
99 
100  \warning The caller is responsible for creating the vector with enough space.
101  */
102  void getValues(std::vector<T>& v) const;
103 
104  /*! \brief Advances to the next position. */
105  void operator++();
106 
107  /*! \brief Returns to the previous position. */
108  void operator--();
109 
110  /*!
111  \brief Assignment operator.
112 
113  \param rhs The right-hand-side copy used to copy from.
114 
115  \return A reference to this object.
116  */
118 
119  /*!
120  \brief Difference operator.
121 
122  \param rhs The right-hand side to compare.
123 
124  \return Returns true if the iterators are at different positions, or false otherwise.
125  */
126  bool operator!=(const RasterIterator& rhs) const;
127 
128  /*!
129  \brief Returns an iterator referring to the first value.
130 
131  \param r The raster to iterate.
132  \param bands The vector of band indices that the iterator will use.
133  */
134  static RasterIterator begin(Raster* r, const std::vector<unsigned int>& bands, bool ignoreNoDataValue = false);
135 
136  /*!
137  \brief Returns an iterator referring to after the end of the iterator.
138 
139  \param r The raster to iterate.
140  \param bands The vector of band indices that the iterator will use.
141  */
142  static RasterIterator end(Raster* r, const std::vector<unsigned int>& bands);
143 
144  protected:
145 
146  /*!
147  \brief Check if current position from iterartor represents a pixel with no data value.
148 
149  \return Returns true if the all values from vector matchs is no data value
150  */
151  bool isNoDataValue();
152 
153  private:
154 
155  std::vector<unsigned int> m_b; //!< The vector of bands to iterate.
156  Raster* m_raster; //!< The raster from where to get the values.
157  std::vector<BandIterator<T> > m_it; //!< The vector of band iterators.
158  bool m_ignoreNoDataValue; //!< Flag used to not iterate over pixels with no data value
159  };
160 
162  : m_b(0),
163  m_raster(0),
164  m_it(0)
165  {
166  }
167 
168  template<class T> te::rst::RasterIterator<T>::RasterIterator(Raster* r, const std::vector<unsigned int>& bands, bool ignoreNoDataValue)
169  : m_b(bands),
170  m_raster(r),
171  m_ignoreNoDataValue(ignoreNoDataValue)
172  {
173  for(unsigned int i = 0; i < m_b.size(); i++)
174  m_it.push_back(te::rst::BandIterator<T>::begin(r->getBand(m_b[i])));
175 
176  if (m_ignoreNoDataValue == true)
177  {
178  bool isDummyValue = isNoDataValue();
179 
180  if(isDummyValue == true)
181  operator++();
182  }
183  }
184 
186  {
187  *this = rhs;
188  }
189 
191  {
192  }
193 
194  template<class T> unsigned int te::rst::RasterIterator<T>::getRow() const
195  {
196  return m_it[0].getRow();
197  }
198 
199  template<class T> unsigned int te::rst::RasterIterator<T>::getColumn() const
200  {
201  return m_it[0].getColumn();
202  }
203 
204  template<class T> T te::rst::RasterIterator<T>::operator[](const unsigned int i) const
205  {
206  return *m_it[i];
207  }
208 
209  template<class T> std::vector<T> te::rst::RasterIterator<T>::operator*() const
210  {
211  std::vector<T> values(m_b.size());
212 
213  getValues(values);
214 
215  return values;
216  }
217 
218  template<class T> void te::rst::RasterIterator<T>::getValues(std::vector<T>& v) const
219  {
220  for (unsigned int b = 0; b < m_b.size(); b++)
221  v[b] = *m_it[b];
222  }
223 
224  template<class T> void te::rst::RasterIterator<T>::operator++()
225  {
226  if (m_ignoreNoDataValue == false)
227  {
228  for (unsigned int b = 0; b < m_b.size(); b++)
229  ++m_it[b];
230  }
231  else
232  {
233  bool isDummyValue = true;
234 
235  while (isDummyValue)
236  {
237  for (unsigned int b = 0; b < m_b.size(); b++)
238  ++m_it[b];
239 
240  isDummyValue = isNoDataValue();
241 
242  if (!(m_it[0] != te::rst::BandIterator<T>::end(m_raster->getBand(0))))
243  {
244  break;
245  }
246  }
247  }
248  }
249 
250  template<class T> void te::rst::RasterIterator<T>::operator--()
251  {
252  if (m_ignoreNoDataValue == false)
253  {
254  for (unsigned int b = 0; b < m_b.size(); b++)
255  --m_it[b];
256  }
257  else
258  {
259  bool isDummyValue = true;
260 
261  while (isDummyValue)
262  {
263  for (unsigned int b = 0; b < m_b.size(); b++)
264  --m_it[b];
265 
266  isDummyValue = isNoDataValue();
267 
268  if (!(m_it[0] != te::rst::BandIterator<T>::begin(m_raster->getBand(0))))
269  {
270  break;
271  }
272  }
273  }
274  }
275 
277  {
278  if (this != &rhs)
279  {
280  m_b = rhs.m_b;
281 
282  m_raster = rhs.m_raster;
283 
284  m_it = rhs.m_it;
285 
286  m_ignoreNoDataValue = rhs.m_ignoreNoDataValue;
287  }
288 
289  return *this;
290  }
291 
292  template<class T> bool te::rst::RasterIterator<T>::operator!=(const RasterIterator& rhs) const
293  {
294  return (m_it[0] != rhs.m_it[0]);
295  }
296 
297  template<class T> te::rst::RasterIterator<T> te::rst::RasterIterator<T>::begin(Raster* r, const std::vector<unsigned int>& bands, bool ignoreNoDataValue)
298  {
299  return te::rst::RasterIterator<T>(r, bands, ignoreNoDataValue);
300  }
301 
302  template<class T> te::rst::RasterIterator<T> te::rst::RasterIterator<T>::end(Raster* r, const std::vector<unsigned int>& bands)
303  {
305 
306  for(unsigned int i = 0; i < bands.size(); i++)
307  it.m_it.push_back(te::rst::BandIterator<T>::end(r->getBand(bands[i])));
308 
309  return it;
310  }
311 
313  {
314  std::vector<T> values(m_b.size());
315 
316  getValues(values);
317 
318  for (std::size_t i = 0; i < m_b.size(); ++i)
319  {
320  if (values[i] != m_raster->getBand(i)->getProperty()->m_noDataValue)
321  {
322  return false;
323  }
324  }
325 
326  return true;
327  }
328 
329  } // end namespace rst
330 } // end namespace te
331 
332 #endif // __TERRALIB_RASTER_INTERNAL_RASTERITERATOR_H
BandIterator.h
It implements an iterator to "navigate" over a single band (const or not const).
te::rst::RasterIterator
This class implements and iterator to "navigate" over a raster, with a predefined number of bands.
Definition: RasterIterator.h:53
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::rst::RasterIterator::end
static RasterIterator end(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to after the end of the iterator.
Definition: RasterIterator.h:302
te::rst::RasterIterator::~RasterIterator
~RasterIterator()
Destructor.
Definition: RasterIterator.h:190
te::rst::RasterIterator::m_raster
Raster * m_raster
The raster from where to get the values.
Definition: RasterIterator.h:156
te::rst::Raster
An abstract class for raster data strucutures.
Definition: Raster.h:72
te::rst::RasterIterator::m_b
std::vector< unsigned int > m_b
The vector of bands to iterate.
Definition: RasterIterator.h:155
te::rst::RasterIterator::operator=
RasterIterator & operator=(const RasterIterator &rhs)
Assignment operator.
Definition: RasterIterator.h:276
te::rst::RasterIterator::operator!=
bool operator!=(const RasterIterator &rhs) const
Difference operator.
Definition: RasterIterator.h:292
te::rst::RasterIterator::getValues
void getValues(std::vector< T > &v) const
Gets the values from all bands in current position (column, row) from iterator.
Definition: RasterIterator.h:218
Band.h
Band implementation for TerraLib 4.x.
te::rst::RasterIterator::m_ignoreNoDataValue
bool m_ignoreNoDataValue
Flag used to not iterate over pixels with no data value.
Definition: RasterIterator.h:158
te::rst::Raster::getBand
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
te::rst::RasterIterator::operator[]
T operator[](const unsigned int i) const
Returns the value in current position (column, row, band) from iterator.
Definition: RasterIterator.h:204
te::rst::RasterIterator::operator*
std::vector< T > operator*() const
Returns a vector with pixel values from all bands in current position (column, row) from iterator.
Definition: RasterIterator.h:209
te::rst::RasterIterator::begin
static RasterIterator begin(Raster *r, const std::vector< unsigned int > &bands, bool ignoreNoDataValue=false)
Returns an iterator referring to the first value.
Definition: RasterIterator.h:297
te::rst::BandIterator
This class implements an iterator to "navigate" over a single band.
Definition: BandIterator.h:154
te::rst::RasterIterator::operator++
void operator++()
Advances to the next position.
Definition: RasterIterator.h:224
te::rst::RasterIterator::getRow
unsigned int getRow() const
Returns the current row in iterator.
Definition: RasterIterator.h:194
te::rst::RasterIterator::operator--
void operator--()
Returns to the previous position.
Definition: RasterIterator.h:250
te::rst::RasterIterator::isNoDataValue
bool isNoDataValue()
Check if current position from iterartor represents a pixel with no data value.
Definition: RasterIterator.h:312
te::rst::RasterIterator::RasterIterator
RasterIterator()
Empty constructor.
Definition: RasterIterator.h:161
BandProperty.h
It describes one band (or dimension) of a raster.
te::rst::RasterIterator::getColumn
unsigned int getColumn() const
Returns the current column in iterator.
Definition: RasterIterator.h:199
te::rst::RasterIterator::m_it
std::vector< BandIterator< T > > m_it
The vector of band iterators.
Definition: RasterIterator.h:157