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);
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);
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  private:
145 
146  std::vector<unsigned int> m_b; //!< The vector of bands to iterate.
147  Raster* m_raster; //!< The raster from where to get the values.
148  std::vector<BandIterator<T> > m_it; //!< The vector of band iterators.
149  };
150 
152  : m_b(0),
153  m_raster(0),
154  m_it(0)
155  {
156  }
157 
158  template<class T> te::rst::RasterIterator<T>::RasterIterator(Raster* r, const std::vector<unsigned int>& bands)
159  : m_b(bands),
160  m_raster(r)
161  {
162  for(unsigned int i = 0; i < m_b.size(); i++)
163  m_it.push_back(te::rst::BandIterator<T>::begin(r->getBand(m_b[i])));
164  }
165 
167  {
168  *this = rhs;
169  }
170 
172  {
173  }
174 
175  template<class T> unsigned int te::rst::RasterIterator<T>::getRow() const
176  {
177  return m_it[0].getRow();
178  }
179 
180  template<class T> unsigned int te::rst::RasterIterator<T>::getColumn() const
181  {
182  return m_it[0].getColumn();
183  }
184 
185  template<class T> T te::rst::RasterIterator<T>::operator[](const unsigned int i) const
186  {
187  return *m_it[i];
188  }
189 
190  template<class T> std::vector<T> te::rst::RasterIterator<T>::operator*() const
191  {
192  std::vector<T> values(m_b.size());
193 
194  getValues(values);
195 
196  return values;
197  }
198 
199  template<class T> void te::rst::RasterIterator<T>::getValues(std::vector<T>& v) const
200  {
201  for (unsigned int b = 0; b < m_b.size(); b++)
202  v[b] = *m_it[b];
203  }
204 
205  template<class T> void te::rst::RasterIterator<T>::operator++()
206  {
207  for (unsigned int b = 0; b < m_b.size(); b++)
208  ++m_it[b];
209  }
210 
211  template<class T> void te::rst::RasterIterator<T>::operator--()
212  {
213  for (unsigned int b = 0; b < m_b.size(); b++)
214  --m_it[b];
215  }
216 
218  {
219  if (this != &rhs)
220  {
221  m_b = rhs.m_b;
222 
223  m_raster = rhs.m_raster;
224 
225  m_it = rhs.m_it;
226  }
227 
228  return *this;
229  }
230 
231  template<class T> bool te::rst::RasterIterator<T>::operator!=(const RasterIterator& rhs) const
232  {
233  return (m_it[0] != rhs.m_it[0]);
234  }
235 
236  template<class T> te::rst::RasterIterator<T> te::rst::RasterIterator<T>::begin(Raster* r, const std::vector<unsigned int>& bands)
237  {
238  return te::rst::RasterIterator<T>(r, bands);
239  }
240 
241  template<class T> te::rst::RasterIterator<T> te::rst::RasterIterator<T>::end(Raster* r, const std::vector<unsigned int>& bands)
242  {
244 
245  for(unsigned int i = 0; i < bands.size(); i++)
246  it.m_it.push_back(te::rst::BandIterator<T>::end(r->getBand(bands[i])));
247 
248  return it;
249  }
250 
251  } // end namespace rst
252 } // end namespace te
253 
254 #endif // __TERRALIB_RASTER_INTERNAL_RASTERITERATOR_H
void operator++()
Advances to the next position.
RasterIterator()
Empty constructor.
It describes one band (or dimension) of a raster.
unsigned int getRow() const
Returns the current row in iterator.
void getValues(std::vector< T > &v) const
Gets the values from all bands in current position (column, row) from iterator.
std::vector< BandIterator< T > > m_it
The vector of band iterators.
This class implements and iterator to "navigate" over a raster, with a predefined number of bands...
T operator[](const unsigned int i) const
Returns the value in current position (column, row, band) from iterator.
void operator--()
Returns to the previous position.
std::vector< T > operator*() const
Returns a vector with pixel values from all bands in current position (column, row) from iterator...
An abstract class for raster data strucutures.
Definition: Raster.h:71
static RasterIterator begin(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to the first value.
URI C++ Library.
It implements an iterator to "navigate" over a single band (const or not const).
std::vector< unsigned int > m_b
The vector of bands to iterate.
It gives access to values in one band (dimension) of a raster.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
RasterIterator & operator=(const RasterIterator &rhs)
Assignment operator.
This class implements an iterator to "navigate" over a single band.
Definition: BandIterator.h:148
bool operator!=(const RasterIterator &rhs) const
Difference operator.
static RasterIterator end(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to after the end of the iterator.
unsigned int getColumn() const
Returns the current column in iterator.
~RasterIterator()
Destructor.
Raster * m_raster
The raster from where to get the values.