PositionIterator.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/rp/PositionIterator.h
22 
23  \brief It implements several ways to retrieve positions inside a band with spatial restriction,
24  e.g. through a line, inside a bounding box or polygon, etc.
25 */
26 
27 #ifndef __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
28 #define __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
29 
30 // Terralib
31 #include "../common/STLUtils.h"
32 #include "../geometry.h"
33 #include "Band.h"
34 #include "BandProperty.h"
35 #include "BlockUtils.h"
36 #include "Grid.h"
37 #include "Exception.h"
38 
39 // STL
40 #include <iostream>
41 
42 namespace te
43 {
44  namespace rst
45  {
46 // Forward declaration.
47  class Band;
48 
49  /*!
50  \class AbstractPositionIterator
51 
52  \brief This class is the base for implementing ways to navigate over the band with spatial restriction,
53  e.g. through a line, inside a bounding box or polygon, etc.
54 
55  \ingroup rst
56  */
57  template<class T> class AbstractPositionIterator
58  {
59  public:
60 
61  /*! \brief Constructor. */
63 
64  /*!
65  \brief Constructor.
66 
67  \param b The band to iterate.
68  */
70 
71  /*!
72  \brief Copy constructor.
73 
74  \param rhs The right-hand-side copy used to copy from.
75  */
77 
78  /*! \brief Destructor. */
80 
81  /*! \brief Returns a vector of the values in current position (column, row) from iterator. */
82  virtual const std::vector<T> operator*() const = 0;
83 
84  /*!
85  \brief Returns the real value in current position (column, row, band) from iterator.
86 
87  \param i The band index.
88 
89  \return The pixel real value in current position.
90 
91  \note For complex values use operator()
92  */
93  virtual T operator[](const unsigned int i) const = 0;
94 
95  /*!
96  \brief Returns the complex value in current position (column, row, band) from iterator.
97 
98  \param i The band index.
99 
100  \return The pixel comples value in current position.
101 
102  \note For real values use operator[]
103  */
104  virtual std::complex< T > operator()(const unsigned int i) const = 0;
105 
106  /*! \brief Returns the current row in iterator. */
107  virtual unsigned int getRow() const = 0;
108 
109  /*! \brief Returns the current column in iterator. */
110  virtual unsigned int getColumn() const = 0;
111 
112  /*! \brief Advances to the next position. */
113  virtual void operator++() = 0;
114 
115  /*! \brief Returns to the previous position. */
116  virtual void operator--() = 0;
117 
118  /*!
119  \brief Assignment operator.
120 
121  \param rhs The right-hand-side copy used to copy from.
122 
123  \return A reference to this object.
124  */
126 
127  /*!
128  \brief Difference operator.
129 
130  \param rhs The right-hand side to compare.
131 
132  \return Returns true if the iterators are at different positions, or false otherwise.
133  */
134  virtual bool operator!=(const AbstractPositionIterator& rhs) const;
135 
136  /*!
137  \brief Sets the iterator position to the end of the current band.
138 
139  \param b The band to retrieve the end information.
140  */
141  virtual void setEnd() = 0;
142 
143  protected:
144 
145  const te::rst::Raster* m_raster; //!< The band from where to get the values.
146 
147  };
148 
149  /*!
150  \class PolygonIterator
151 
152  \brief This class implements the strategy to iterate with spatial restriction,
153  the iteration occurs inside a polygon.
154 
155  \ingroup rst
156  */
157  template<class T> class PolygonIterator: public AbstractPositionIterator<T>
158  {
159  public:
160 
161  PolygonIterator();
162 
163  /*!
164  \brief Constructor.
165 
166  \param b The band to iterate.
167  \param p The polygon from where the iteration will navigate.
168  \note Both raster and polygon must have the same SRID.
169  */
170  PolygonIterator(const te::rst::Raster* r, const te::gm::Polygon* p);
171 
172  /*!
173  \brief Copy constructor.
174 
175  \param rhs The right-hand-side copy used to copy from.
176  */
177  PolygonIterator(const PolygonIterator& rhs);
178 
180 
181  void setNextLine(bool updatecurrline = true);
182 
183  const std::vector<T> operator*() const;
184 
185  T operator[](const unsigned int i) const;
186 
187  std::complex< T > operator()(const unsigned int i) const;
188 
189  unsigned int getRow() const;
190 
191  unsigned int getColumn() const;
192 
193  void operator++();
194 
195  void operator--();
196 
198 
199  void setEnd();
200 
201  /*! \brief Returns an iterator referring to the first value of the band.*/
202  static PolygonIterator begin(const te::rst::Raster* r, const te::gm::Polygon* p);
203 
204  /*! \brief Returns an iterator referring to after the end of the iterator. */
205  static PolygonIterator end(const te::rst::Raster* r, const te::gm::Polygon* p);
206 
207  bool operator!=(const PolygonIterator<T>& rhs) const;
208 
209  protected:
210 
211  const te::gm::Polygon* m_polygon; //!< The spatial restriction to be applied in the iterator.
212  std::vector<te::gm::LineString*> m_intersections; //!< The points or lines of the intersection between the geometry and the current line.
213  te::gm::Line* m_currline; //!< The current line in the iterator.
214  int m_column; //!< The current column of the iterator.
215  int m_row; //!< The current row of the iterator.
216  int m_startingcolumn; //!< The starting column (in current line) to initialize the iteration.
217  int m_endingcolumn; //!< The column (in current line) to finalize the iteration.
218  int m_startingrow; //!< The starting row of the iteration.
219  int m_endingrow; //!< The ending row of the iteration.
220  int m_maxcolumns; //!< The number of columns in band.
221  int m_maxrows; //!< The number of rows in band.
222  int m_actualintersection; //!< The actual line of the iterator.
223  int m_nintersections; //!< The number number of intersected lines in current line of the iterator.
224  mutable double m_operatorBrackets_value; //!< Used by the operator[] method.
225  mutable std::complex< double > m_operatorParenthesis_value; //!< Used by the operator() method.
226 
227  /*!
228  \brief Decomponse one geometry collection in a vector of basic components (line, point).
229 
230  \param g The input geometry collection.
231 
232  \param decomposedGeoms The output geometries will be appended to this vector.
233 
234  \note The caller of this method must take the ownership of the returned objects.
235  */
236  void decompose(te::gm::Geometry const * const g,
237  std::vector<te::gm::LineString*>& decomposedGeoms ) const;
238 
239  /*! \brief Clear all internal allocated objects and reset back to the initial state. */
240  void clear();
241 
242  };
243 
244  /*!
245  \class LineIterator
246 
247  \brief This class implements the strategy to iterate with spatial restriction,
248  the iteration occurs inside a line.
249 
250  \ingroup rst
251  */
252  template<class T> class LineIterator: public AbstractPositionIterator<T>
253  {
254  public:
255 
256  LineIterator();
257 
258  /*!
259  \brief Constructor.
260 
261  \param b The band to iterate.
262  \param l The line from where the iteration will navigate.
263  */
264  LineIterator(const te::rst::Raster* r, const te::gm::Line* l);
265 
266  /*!
267  \brief Copy constructor.
268 
269  \param rhs The right-hand-side copy used to copy from.
270  */
271  LineIterator(const LineIterator& rhs);
272 
273  ~LineIterator();
274 
275  const std::vector<T> operator*() const;
276 
277  T operator[](const unsigned int i) const;
278 
279  std::complex< T > operator()(const unsigned int i) const;
280 
281  unsigned int getRow() const;
282 
283  unsigned int getColumn() const;
284 
285  void operator++();
286 
287  void operator--();
288 
289  LineIterator& operator=(const LineIterator& rhs);
290 
291  void setEnd();
292 
293  /*! \brief Returns an iterator referring to the first value of the band.*/
294  static LineIterator begin(const te::rst::Raster* r, const te::gm::Line* l);
295 
296  /*! \brief Returns an iterator referring to after the end of the iterator. */
297  static LineIterator end(const te::rst::Raster* r, const te::gm::Line* l);
298 
299  bool operator!=(const LineIterator<T>& rhs) const;
300 
301  protected:
302 
303  const te::gm::Line* m_line; //!< The spatial restriction to be applied in the iterator.
304  int m_currentpixelindex; //!< The index of the current pixel location.
305  std::vector<te::gm::Point*> m_pixelsinline; //!< A vector of pixel locations that intersects the line.
306  mutable double m_operatorBrackets_value; //!< Used by the operator[] method.
307  mutable std::complex< double > m_operatorParenthesis_value; //!< Used by the operator() method.
308 
309 
310  };
311 
312  /*!
313  \class PointSetIterator
314 
315  \brief This class implements the strategy to iterate with spatial restriction,
316  the iteration occurs inside a vector of points.
317 
318  \ingroup rst
319  */
320  template<class T> class PointSetIterator: public AbstractPositionIterator<T>
321  {
322  public:
323 
325 
326  /*!
327  \brief Constructor.
328 
329  \param b The band to iterate.
330  \param p The vector of points where the iteration will navigate.
331  */
332  PointSetIterator(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
333 
334  /*!
335  \brief Copy constructor.
336 
337  \param rhs The right-hand-side copy used to copy from.
338  */
340 
342 
343  const std::vector<T> operator*() const;
344 
345  T operator[](const unsigned int i) const;
346 
347  std::complex< T > operator()(const unsigned int i) const;
348 
349  unsigned int getRow() const;
350 
351  unsigned int getColumn() const;
352 
353  void operator++();
354 
355  void operator--();
356 
358 
359  void setEnd();
360 
361  /*! \brief Returns an iterator referring to the first value of the band.*/
362  static PointSetIterator begin(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
363 
364  /*! \brief Returns an iterator referring to after the end of the iterator. */
365  static PointSetIterator end(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
366 
367  bool operator!=(const PointSetIterator<T>& rhs) const;
368 
369  protected:
370 
371  std::vector<te::gm::Point*> m_pixelsinpointset; //!< The spatial restriction to be applied in the iterator.
372  int m_currentpixelindex; //!< The index of the current pixel location.
373  mutable double m_operatorBrackets_value; //!< Used by the operator[] method.
374  mutable std::complex< double > m_operatorParenthesis_value; //!< Used by the operator() method.
375 
376 
377  };
378 // implementation of abstract position iterator
380  : m_raster(0)
381  {
382  }
383 
385  : m_raster(r)
386  {
387  }
388 
390  : m_raster(rhs.m_raster)
391  {
392  }
393 
395  {
396  }
397 
399  {
400  if (this != &rhs)
401  m_raster = rhs.m_raster;
402 
403  return *this;
404  }
405 
407  {
408  return (m_raster != rhs.m_raster);
409  }
410 
411 // implementation of iteration strategy bounded by a polygon
414  m_polygon(0),
415  m_currline(0),
416  m_column(-1),
417  m_row(-1),
418  m_startingcolumn(0),
419  m_endingcolumn(0),
420  m_startingrow(0),
421  m_endingrow(0),
422  m_maxcolumns(0),
423  m_maxrows(0),
424  m_actualintersection(-1),
425  m_nintersections(0)
426  {
427  }
428 
430  : AbstractPositionIterator<T>(r),
431  m_polygon(p),
432  m_currline(0),
433  m_column(0),
434  m_row(-1),
435  m_startingcolumn(0),
436  m_endingcolumn(0),
437  m_startingrow(0),
438  m_endingrow(0),
439  m_maxcolumns(r->getNumberOfColumns()),
440  m_maxrows(r->getNumberOfRows()),
441  m_actualintersection(-1),
442  m_nintersections(0)
443  {
444  if( r->getSRID() != p->getSRID() )
445  {
446  throw te::rst::Exception( TE_TR("Invalid polygon SRID") );
447  }
448 
451 
452 // defining starting/ending rows
453  m_startingrow = (int) r->getGrid()->geoToGrid(ll.x, ur.y).y;
454  m_endingrow = (int) r->getGrid()->geoToGrid(ll.x, ll.y).y;
455 
456  int tmp;
458  {
459  tmp = m_startingrow;
461  m_endingrow = tmp;
462  }
463 
464 // avoiding bad access
467 
470 
472 
474  te::gm::Point(ur.x, ur.y, m_polygon->getSRID()),
476 
477 // in case of problems, we initialize the first line here
478  m_startingcolumn = 0;
480 
481 // defining starting/ending columns
482  setNextLine(false);
483 
485  }
486 
488  : AbstractPositionIterator<T>(rhs),
489  m_polygon(0),
490  m_currline(0),
491  m_column(-1),
492  m_row(-1),
493  m_startingcolumn(0),
494  m_endingcolumn(0),
495  m_startingrow(0),
496  m_endingrow(0),
497  m_maxcolumns(0),
498  m_maxrows(0),
499  m_actualintersection(-1),
500  m_nintersections(0)
501  {
502  operator=( rhs );
503  }
504 
506  {
507  clear();
508  }
509 
510  template<class T>
512  te::gm::Geometry const * const g,
513  std::vector<te::gm::LineString*>& decomposedGeoms ) const
514  {
515  te::gm::Geometry const * ing = g;
516  te::gm::GeometryCollection const* gc =
517  static_cast<te::gm::GeometryCollection const*> (g);
518  if (gc->getNumGeometries() == 1)
519  ing = gc->getGeometryN(0);
520 
521 // check if the geometry is a multi line string
523  {
524  te::gm::MultiLineString const* mls =
525  static_cast<te::gm::MultiLineString const*> (
526  ing);
527 
528  for (unsigned int i = 0; i < mls->getNumGeometries(); i++)
529  {
530  decomposedGeoms.push_back(static_cast<te::gm::LineString*> (
531  mls->getGeometryN(i)->clone() ));
532  }
533  }
534 // check if the geometry is a line
535  else if (ing->getGeomTypeId() == te::gm::LineStringType)
536  {
537  decomposedGeoms.push_back((te::gm::LineString*)ing->clone());
538  }
539 // check if the geometry is a multi point
540  else if (ing->getGeomTypeId() == te::gm::MultiPointType)
541  {
542  te::gm::MultiPoint const* mp = static_cast<te::gm::MultiPoint const*> (
543  ing);
544 
545  for (unsigned int i = 0; i < mp->getNumGeometries(); i++)
546  {
548  te::gm::Point* pointinter = static_cast<te::gm::Point*> (mp->getGeometryN(i));
549 
550  lineinter->setX(0, pointinter->getX());
551  lineinter->setY(0, pointinter->getY());
552  lineinter->setX(1, pointinter->getX());
553  lineinter->setY(1, pointinter->getY());
554 
555  decomposedGeoms.push_back(lineinter);
556  }
557  }
558 // check if the geometry is a point
559  else if (ing->getGeomTypeId() == te::gm::PointType)
560  {
561  te::gm::Point const* p = static_cast<te::gm::Point const*> (ing);
562 
564 
565  lineinter->setX(0, p->getX());
566  lineinter->setY(0, p->getY());
567  lineinter->setX(1, p->getX());
568  lineinter->setY(1, p->getY());
569 
570  decomposedGeoms.push_back(lineinter);
571  }
572 // check if the geometry is a geometry collection
574  {
575  for (unsigned int i = 0; i < gc->getNumGeometries(); i++)
576  {
577  decompose(gc->getGeometryN(i), decomposedGeoms);
578  }
579  }
580 // throw exception when other types?
581  else
582  {
583  throw te::rst::Exception( TE_TR(
584  "An exception has occurred in Polygon Iterator, with geometry " +
585  g->toString()) );
586  }
587 
588 // clean up (?)
589 // delete g;
590  }
591 
592  template<class T> void te::rst::PolygonIterator<T>::setNextLine(bool updatecurrline)
593  {
594  if (m_actualintersection == -1 || m_actualintersection >= m_nintersections)
595  {
596  if (updatecurrline)
597  {
598  double nexty = this->m_raster->getGrid()->gridToGeo(0, m_row).y;
599 
600  m_currline->setX(0, m_polygon->getMBR()->getLowerLeft().x);
601  m_currline->setX(1, m_polygon->getMBR()->getUpperRight().x);
602  m_currline->setY(0, nexty);
603  m_currline->setY(1, nexty);
604  }
605 
606  te::gm::Geometry* inter;
607 // in some cases the intersection presents an unhandled exception, in this case we do not paint the current line
608  try
609  {
610  inter = m_polygon->intersection(m_currline);
611 
612  if (inter->isEmpty())
613  {
614  delete inter;
615 
616  m_row++;
617  if (m_row > m_endingrow)
618  {
619  setEnd();
620 
621  return;
622  }
623 
624  setNextLine();
625 
626  return;
627  }
628  }
629  catch(const std::exception& e)
630  {
631  std::cout << "Unhandled exception, status:" << std::endl;
632  std::cout << " m_startingcolumn: " << m_startingcolumn << " m_endingcolumn: " << m_endingcolumn << std::endl;
633  std::cout << " m_startingrow: " << m_startingrow << " m_endingrow: " << m_endingrow<< std::endl;
634  std::cout << " m_column: " << m_column << " m_row: " << m_row << std::endl;
635  std::cout << " intersection line: " << m_currline->toString() << std::endl << std::endl;
636  std::cout << " current polygon: " << m_polygon->toString() << std::endl << std::endl;
637  std::cout << " exception message: " << e.what() << std::endl;
638  std::cout << std::endl;
639 
640  operator++();
641 
642  return;
643  }
644 
645  te::gm::GeometryCollection* intersections = new te::gm::GeometryCollection(0, inter->getGeomTypeId(), inter->getSRID());
646 
647  intersections->add(inter);
648 
649  m_actualintersection = 0;
650 
651  m_intersections.clear();
652  decompose(intersections,m_intersections);
653 
654  delete intersections;
655 
656  m_nintersections = m_intersections.size();
657  }
658 
659  te::gm::LineString* lineinter = m_intersections[m_actualintersection];
660 
661  m_startingcolumn = (int) this->m_raster->getGrid()->geoToGrid(lineinter->getStartPoint()->getX(), lineinter->getStartPoint()->getY()).x;
662 
663  m_endingcolumn = (int) this->m_raster->getGrid()->geoToGrid(lineinter->getEndPoint()->getX(), lineinter->getEndPoint()->getY()).x;
664 
665  int tmp;
666  if (m_startingcolumn > m_endingcolumn)
667  {
668  tmp = m_startingcolumn;
669  m_startingcolumn = m_endingcolumn;
670  m_endingcolumn = tmp;
671  }
672 
673 // avoiding bad access
674  m_startingcolumn = m_startingcolumn < 0? 0: m_startingcolumn;
675  m_startingcolumn = m_startingcolumn >= m_maxcolumns? m_maxcolumns - 1: m_startingcolumn;
676 
677  m_endingcolumn = m_endingcolumn < 0? 0: m_endingcolumn;
678  m_endingcolumn = m_endingcolumn >= m_maxcolumns? m_maxcolumns - 1: m_endingcolumn;
679  }
680 
681  template<class T> const std::vector<T> te::rst::PolygonIterator<T>::operator*() const
682  {
683  std::vector<T> values(this->m_raster->getNumberOfBands());
684  double value;
685 
686  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
687  {
688  this->m_raster->getValue(getColumn(), getRow(), value, b);
689  values[b] = ((T) value);
690  }
691 
692  return values;
693  }
694 
695  template<class T> T te::rst::PolygonIterator<T>::operator[](const unsigned int i) const
696  {
697  this->m_raster->getValue(m_column, m_row, m_operatorBrackets_value, i);
698 
699  return (T) m_operatorBrackets_value;
700  }
701 
702  template<class T> std::complex< T > te::rst::PolygonIterator<T>::operator()(const unsigned int i) const
703  {
704  this->m_raster->getValue(m_column, m_row, m_operatorParenthesis_value, i);
705 
706  return (std::complex< T >) m_operatorParenthesis_value;
707  }
708 
709  template<class T> unsigned int te::rst::PolygonIterator<T>::getRow() const
710  {
711  return m_row;
712  }
713 
714  template<class T> unsigned int te::rst::PolygonIterator<T>::getColumn() const
715  {
716  return m_column;
717  }
718 
720  {
721  m_column++;
722 
723  if (m_column > m_endingcolumn)
724  {
725  m_actualintersection++;
726 
727  if (m_actualintersection >= m_nintersections)
728  m_row++;
729 
730  if (m_row > m_endingrow)
731  {
732  setEnd();
733 
734  return;
735  }
736 
737  setNextLine();
738 
739  m_column = m_startingcolumn;
740  }
741  }
742 
744  {
745  m_column--;
746 
747  if (m_column < m_startingcolumn)
748  {
749  m_actualintersection--;
750 
751  if (m_actualintersection < 0)
752  m_row--;
753 
754  if (m_row < m_startingrow)
755  {
756  setEnd();
757 
758  return;
759  }
760 
761  setNextLine();
762 
763  m_column = m_endingcolumn;
764  }
765  }
766 
767  template<class T>
769  const te::rst::PolygonIterator<T>& rhs)
770  {
771  clear();
772 
773  if (this != &rhs)
774  {
776 
777  m_polygon = rhs.m_polygon;
778 
779  for( unsigned int intersectionsIdx = 0 ; intersectionsIdx <
780  rhs.m_intersections.size() ; ++intersectionsIdx )
781  {
782  m_intersections.push_back( (te::gm::LineString*)
783  rhs.m_intersections[ intersectionsIdx ]->clone() );
784  }
785 
786  if( rhs.m_currline )
787  {
788  m_currline = (te::gm::Line*)rhs.m_currline->clone();
789  }
790 
791  m_column = rhs.m_column;
792  m_row = rhs.m_row;
793  m_startingcolumn = rhs.m_startingcolumn;
794  m_endingcolumn = rhs.m_endingcolumn;
795  m_startingrow = rhs.m_startingrow;
796  m_endingrow = rhs.m_endingrow;
797  m_maxcolumns = rhs.m_maxcolumns;
798  m_maxrows = rhs.m_maxrows;
799  m_actualintersection = rhs.m_actualintersection;
800  m_nintersections = rhs.m_nintersections;
801  }
802 
803  return *this;
804  }
805 
806  template<class T> void te::rst::PolygonIterator<T>::setEnd()
807  {
808  this->m_column = -1;
809 
810  this->m_row = -1;
811  }
812 
814  {
815  return te::rst::PolygonIterator<T>(r, p);
816  }
817 
819  {
821 
822  it.setEnd();
823 
824  return it;
825  }
826 
828  {
829  return ( (this->m_row != rhs.m_row) && (this->m_column != rhs.m_column));
830  }
831 
832  template<class T> void te::rst::PolygonIterator<T>::clear()
833  {
834  m_polygon = 0;
835 
836  for( unsigned int intersectionsIdx = 0 ; intersectionsIdx <
837  m_intersections.size() ; ++intersectionsIdx )
838  {
839  delete m_intersections[ intersectionsIdx ];
840  }
841  m_intersections.clear();
842 
843  if( m_currline )
844  {
845  delete m_currline;
846  m_currline = 0;
847  }
848 
849  m_column = -1;
850  m_row = -1;
851  m_startingcolumn = 0;
852  m_endingcolumn = 0;
853  m_startingrow = 0;
854  m_endingrow = 0;
855  m_maxcolumns = 0;
856  m_maxrows = 0;
857  m_actualintersection = -1;
858  m_nintersections = 0;
859  }
860 
861 // implementation of iteration strategy bounded by a line
864  m_line(0),
865  m_currentpixelindex(0),
866  m_pixelsinline(0)
867  {
868  }
869 
871  : AbstractPositionIterator<T>(r),
872  m_line(l),
873  m_currentpixelindex(0),
874  m_pixelsinline(0)
875  {
876  if( r->getSRID() != l->getSRID() )
877  {
878  throw te::rst::Exception( TE_TR("Invalid line SRID") );
879  }
880 
881  int srid = this->m_raster->getSRID();
882 
883 // make intersection between line and band's envelope
884  te::gm::Geometry* bandEnvelope = te::gm::GetGeomFromEnvelope(this->m_raster->getExtent(), srid);
885  te::gm::Geometry* inter = bandEnvelope->intersection(m_line);
886 
887  if (inter->isEmpty())
888  {
889  setEnd();
890 
891  return;
892  }
893 
894 // create line that intersects only band's envelope
895  te::gm::Line* inrasterline = (te::gm::Line*) inter;
896 
897 // find starting and ending points
898  double startingcolumn;
899  double startingrow;
900  te::gm::Point* startpoint = inrasterline->getStartPoint();
901  this->m_raster->getGrid()->geoToGrid(startpoint->getX(), startpoint->getY(),
902  startingcolumn, startingrow);
903 
904  double endingcolumn;
905  double endingrow;
906  te::gm::Point* endpoint = inrasterline->getEndPoint();
907  this->m_raster->getGrid()->geoToGrid(endpoint->getX(), endpoint->getY(),
908  endingcolumn, endingrow);
909 
910 // creating one envelope per pixel, and intersects with line
911  const double resXdiv2 = this->m_raster->getResolutionX() / 2;
912  const double resYdiv2 = this->m_raster->getResolutionY() / 2;
913  double x1, x2, y1, y2, geoX, geoY;
914  for(int r = (int)startingrow; r <= (int)endingrow; r++)
915  for(int c = (int)startingcolumn; c <= (int)endingcolumn; c++)
916  {
917 // define envelope of pixel
918  this->m_raster->getGrid()->gridToGeo(c, r, geoX, geoY);
919  x1 = geoX - resXdiv2; y1 = geoY - resYdiv2;
920  x2 = geoX + resXdiv2; y2 = geoY + resYdiv2;
921 
922  te::gm::Envelope* pixelbox = new te::gm::Envelope(x1, y1, x2, y2);
923  te::gm::Geometry* pixelboxgeometry = GetGeomFromEnvelope(pixelbox, srid);
924 
925  if (te::gm::SatisfySpatialRelation(inrasterline, pixelboxgeometry, te::gm::INTERSECTS))
926  m_pixelsinline.push_back(new te::gm::Point(c, r, srid));
927  }
928 
929  if (m_pixelsinline.empty())
930  setEnd();
931  }
932 
934  : AbstractPositionIterator<T>(rhs),
935  m_currentpixelindex(rhs.m_currentpixelindex),
936  m_pixelsinline(rhs.m_pixelsinline)
937  {
938  }
939 
941  {
942  m_pixelsinline.clear();
943  }
944 
945  template<class T> const std::vector<T> te::rst::LineIterator<T>::operator*() const
946  {
947  std::vector<T> values(this->m_raster->getNumberOfBands());
948  double value;
949 
950  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
951  {
952  this->m_raster->getValue(getColumn(), getRow(), value, b);
953  values[b] = ((T) value);
954  }
955 
956  return values;
957  }
958 
959  template<class T> T te::rst::LineIterator<T>::operator[](const unsigned int i) const
960  {
961  this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
962 
963  return (T) m_operatorBrackets_value;
964  }
965 
966  template<class T> std::complex< T > te::rst::LineIterator<T>::operator()(const unsigned int i) const
967  {
968  this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
969 
970  return (std::complex< T >) m_operatorParenthesis_value;
971  }
972 
973  template<class T> unsigned int te::rst::LineIterator<T>::getRow() const
974  {
975  return (unsigned int)(m_pixelsinline[m_currentpixelindex]->getY());
976  }
977 
978  template<class T> unsigned int te::rst::LineIterator<T>::getColumn() const
979  {
980  return (unsigned int)(m_pixelsinline[m_currentpixelindex]->getX());
981  }
982 
983  template<class T> void te::rst::LineIterator<T>::operator++()
984  {
985  m_currentpixelindex++;
986 
987  if (m_currentpixelindex >= (int)(m_pixelsinline.size()))
988  setEnd();
989  }
990 
991  template<class T> void te::rst::LineIterator<T>::operator--()
992  {
993  m_currentpixelindex--;
994 
995  if (m_currentpixelindex < 0)
996  setEnd();
997  }
998 
1000  {
1001  if (this != &rhs)
1002  {
1004 
1005  m_line = rhs.m_line;
1006  m_currentpixelindex = rhs.m_currentpixelindex;
1007  m_pixelsinline = rhs.m_pixelsinline;
1008  }
1009 
1010  return *this;
1011  }
1012 
1013  template<class T> void te::rst::LineIterator<T>::setEnd()
1014  {
1015  this->m_currentpixelindex = -1;
1016  }
1017 
1019  {
1020  return te::rst::LineIterator<T>(r, l);
1021  }
1022 
1024  {
1025  te::rst::LineIterator<T> it(r, l);
1026 
1027  it.setEnd();
1028 
1029  return it;
1030  }
1031 
1032  template<class T> bool te::rst::LineIterator<T>::operator!=(const te::rst::LineIterator<T>& rhs) const
1033  {
1034  return ( (this->m_currentpixelindex != rhs.m_currentpixelindex) );
1035  }
1036 
1037 // implementation of iteration strategy bounded by a vector of points
1039  : AbstractPositionIterator<T>(),
1040  m_pixelsinpointset(0),
1041  m_currentpixelindex(0)
1042  {
1043  }
1044 
1045  template<class T> te::rst::PointSetIterator<T>::PointSetIterator(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1046  : AbstractPositionIterator<T>(r),
1047  m_pixelsinpointset(p),
1048  m_currentpixelindex(0)
1049  {
1050  const int rasterSRID = this->m_raster->getSRID();
1051 
1052  const te::gm::Envelope* rasterbox = r->getExtent();
1053  te::gm::Geometry* rasterboxgeometry = GetGeomFromEnvelope(rasterbox, rasterSRID);
1054 
1055 // remove points that are not inside the band's envelope
1056  std::vector<te::gm::Point*> inside_points;
1057  double column;
1058  double row;
1059  for (unsigned int i = 0; i < m_pixelsinpointset.size(); i++)
1060  {
1061  if( rasterSRID != m_pixelsinpointset[i]->getSRID() )
1062  {
1063  throw te::rst::Exception( TE_TR("Invalid point SRID") );
1064  }
1065 
1067  {
1068  this->m_raster->getGrid()->geoToGrid(m_pixelsinpointset[i]->getX(), m_pixelsinpointset[i]->getY(), column, row);
1069 
1070  inside_points.push_back(new te::gm::Point(column, row));
1071  }
1072  }
1073 
1074  m_pixelsinpointset.clear();
1075  m_pixelsinpointset = inside_points;
1076 
1077  if (m_pixelsinpointset.empty())
1078  setEnd();
1079  }
1080 
1082  : AbstractPositionIterator<T>(rhs),
1083  m_pixelsinpointset(rhs.m_pixelsinpointset),
1084  m_currentpixelindex(rhs.m_currentpixelindex)
1085  {
1086  }
1087 
1089  {
1090  m_pixelsinpointset.clear();
1091  }
1092 
1093  template<class T> const std::vector<T> te::rst::PointSetIterator<T>::operator*() const
1094  {
1095  std::vector<T> values(this->m_raster->getNumberOfBands());
1096  double value;
1097 
1098  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
1099  {
1100  this->m_raster->getValue(getColumn(), getRow(), value, b);
1101  values[b] = ((T) value);
1102  }
1103 
1104  return values;
1105  }
1106 
1107  template<class T> T te::rst::PointSetIterator<T>::operator[](const unsigned int i) const
1108  {
1109  this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
1110 
1111  return (T) m_operatorBrackets_value;
1112  }
1113 
1114  template<class T> std::complex< T > te::rst::PointSetIterator<T>::operator()(const unsigned int i) const
1115  {
1116  this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
1117 
1118  return (std::complex< T >) m_operatorParenthesis_value;
1119  }
1120 
1121  template<class T> unsigned int te::rst::PointSetIterator<T>::getRow() const
1122  {
1123  return (unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getY());
1124  }
1125 
1126  template<class T> unsigned int te::rst::PointSetIterator<T>::getColumn() const
1127  {
1128  return (unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getX());
1129  }
1130 
1132  {
1133  m_currentpixelindex++;
1134 
1135  if (m_currentpixelindex >= (int) m_pixelsinpointset.size())
1136  setEnd();
1137  }
1138 
1140  {
1141  m_currentpixelindex--;
1142 
1143  if (m_currentpixelindex < 0)
1144  setEnd();
1145  }
1146 
1148  {
1149  if (this != &rhs)
1150  {
1152 
1153  m_pixelsinpointset = rhs.m_pixelsinpointset;
1154  m_currentpixelindex = rhs.m_currentpixelindex;
1155  }
1156 
1157  return *this;
1158  }
1159 
1160  template<class T> void te::rst::PointSetIterator<T>::setEnd()
1161  {
1162  this->m_currentpixelindex = -1;
1163  }
1164 
1165  template<class T> te::rst::PointSetIterator<T> te::rst::PointSetIterator<T>::begin(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1166  {
1167  return te::rst::PointSetIterator<T>(r, p);
1168  }
1169 
1170  template<class T> te::rst::PointSetIterator<T> te::rst::PointSetIterator<T>::end(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1171  {
1173 
1174  it.setEnd();
1175 
1176  return it;
1177  }
1178 
1180  {
1181  return ( (this->m_currentpixelindex != rhs.m_currentpixelindex) );
1182  }
1183  } // end namespace rst
1184 } // end namespace te
1185 
1186 #endif // __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
virtual Geometry * intersection(const Geometry *const rhs) const
It returns a geometric object that represents the point set intersection with another geometry...
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
int m_row
The current row of the iterator.
void setEnd()
Sets the iterator position to the end of the current band.
void push_back(Curve *ring)
It adds the curve to the curve polygon.
Definition: CurvePolygon.h:279
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
Point * getStartPoint() const
The length of this Curve in its associated spatial reference.
int m_startingcolumn
The starting column (in current line) to initialize the iteration.
int m_startingrow
The starting row of the iteration.
A Line is LineString with 2 points.
Definition: Line.h:50
double y
y-coordinate.
Definition: Coord2D.h:114
It describes one band (or dimension) of a raster.
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
void setX(std::size_t i, const double &x)
It sets the n-th x coordinate value.
const te::gm::Line * m_line
The spatial restriction to be applied in the iterator.
int m_nintersections
The number number of intersected lines in current line of the iterator.
void setEnd()
Sets the iterator position to the end of the current band.
double x
x-coordinate.
Definition: Coord2D.h:113
void decompose(te::gm::Geometry const *const g, std::vector< te::gm::LineString * > &decomposedGeoms) const
Decomponse one geometry collection in a vector of basic components (line, point). ...
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
te::gm::Line * m_currline
The current line in the iterator.
double m_operatorBrackets_value
Used by the operator[] method.
virtual void operator--()=0
Returns to the previous position.
int m_maxcolumns
The number of columns in band.
bool operator!=(const PolygonIterator< T > &rhs) const
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
unsigned int getColumn() const
Returns the current column in iterator.
int m_currentpixelindex
The index of the current pixel location.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
virtual unsigned int getRow() const =0
Returns the current row in iterator.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
double m_operatorBrackets_value
Used by the operator[] method.
static PointSetIterator end(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to after the end of the iterator.
This class is the base for implementing ways to navigate over the band with spatial restriction...
unsigned int getRow() const
Returns the current row in iterator.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
bool operator!=(const PointSetIterator< T > &rhs) const
void operator++()
Advances to the next position.
virtual std::complex< T > operator()(const unsigned int i) const =0
Returns the complex value in current position (column, row, band) from iterator.
int m_actualintersection
The actual line of the iterator.
const te::gm::Polygon * m_polygon
The spatial restriction to be applied in the iterator.
bool operator!=(const LineIterator< T > &rhs) const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
PolygonIterator & operator=(const PolygonIterator &rhs)
PointSetIterator & operator=(const PointSetIterator &rhs)
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
int m_endingrow
The ending row of the iteration.
Grid * getGrid()
It returns the raster grid.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
int m_endingcolumn
The column (in current line) to finalize the iteration.
Utility functions for dealing with raster data blocks.
void setNextLine(bool updatecurrline=true)
virtual void operator++()=0
Advances to the next position.
void operator++()
Advances to the next position.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
virtual bool isEmpty() const
It returns true if this geometric object is the empty Geometry.
An exception class for the Raster module.
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
std::vector< te::gm::Point * > m_pixelsinline
A vector of pixel locations that intersects the line.
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
An abstract class for raster data strucutures.
Definition: Raster.h:71
virtual unsigned int getColumn() const =0
Returns the current column in iterator.
URI C++ Library.
int m_currentpixelindex
The index of the current pixel location.
TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry *g1, const Geometry *g2, SpatialRelation relation)
It returns if two geometries satisfy a given spatial relation.
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
int m_maxrows
The number of rows in band.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
void setEnd()
Sets the iterator position to the end of the current band.
It gives access to values in one band (dimension) of a raster.
void clear()
Clear all internal allocated objects and reset back to the initial state.
virtual void setEnd()=0
Sets the iterator position to the end of the current band.
virtual const std::vector< T > operator*() const =0
Returns a vector of the values in current position (column, row) from iterator.
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
double m_operatorBrackets_value
Used by the operator[] method.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
void operator--()
Returns to the previous position.
Point * getEndPoint() const
It returns the curve end point.
MultiLineString is a MultiCurve whose elements are LineStrings.
te::dt::AbstractData * clone() const
It clones the line.
int getSRID() const
Returns the raster spatial reference system identifier.
std::vector< te::gm::Point * > m_pixelsinpointset
The spatial restriction to be applied in the iterator.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
void operator--()
Returns to the previous position.
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to the first value of the band.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
int m_column
The current column of the iterator.
const te::rst::Raster * m_raster
The band from where to get the values.
void add(Geometry *g)
It adds the geometry into the collection.
unsigned int getColumn() const
Returns the current column in iterator.
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
static LineIterator end(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to after the end of the iterator.
std::string toString() const
It returns the data value in a WKT representation.
Definition: Geometry.h:858
virtual bool operator!=(const AbstractPositionIterator &rhs) const
Difference operator.
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
A rectified grid is the spatial support for raster data.
unsigned int getRow() const
Returns the current row in iterator.
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
AbstractPositionIterator & operator=(const AbstractPositionIterator &rhs)
Assignment operator.
void setY(std::size_t i, const double &y)
It sets the n-th y coordinate value.
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
void operator++()
Advances to the next position.
virtual T operator[](const unsigned int i) const =0
Returns the real value in current position (column, row, band) from iterator.
void operator--()
Returns to the previous position.
unsigned int getColumn() const
Returns the current column in iterator.
It is a collection of other geometric objects.
virtual AbstractData * clone() const =0
It returns a clone of this object.
std::vector< te::gm::LineString * > m_intersections
The points or lines of the intersection between the geometry and the current line.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
LineIterator & operator=(const LineIterator &rhs)
unsigned int getRow() const
Returns the current row in iterator.
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
static PointSetIterator begin(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to the first value of the band.
static LineIterator begin(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to the first value of the band.