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 "../common/MathUtils.h"
33 #include "../core/logger/Logger.h"
34 #include "../geometry.h"
35 #include "Band.h"
36 #include "BandProperty.h"
37 #include "BlockUtils.h"
38 #include "Grid.h"
39 #include "Exception.h"
40 
41 // STL
42 #include <iostream>
43 #include <cmath>
44 
45 namespace te
46 {
47  namespace rst
48  {
49 // Forward declaration.
50  class Band;
51 
53  {
54  return *p1 < *p2;
55  }
56 
57  /*!
58  \class AbstractPositionIterator
59 
60  \brief This class is the base for implementing ways to navigate over the band with spatial restriction,
61  e.g. through a line, inside a bounding box or polygon, etc.
62 
63  \ingroup rst
64  */
65  template<typename T> class AbstractPositionIterator
66  {
67  public:
68 
69  /*! \brief Destructor. */
70  virtual ~AbstractPositionIterator();
71 
72  /*! \brief Returns a vector of the values in current position (column, row) from iterator. */
73  virtual const std::vector<T> operator*() const = 0;
74 
75  /*!
76  \brief Returns the real value in current position (column, row, band) from iterator.
77 
78  \param i The band index.
79 
80  \return The pixel real value in current position.
81 
82  \note For complex values use operator()
83  */
84  virtual T operator[](const unsigned int i) const = 0;
85 
86  /*!
87  \brief Returns the complex value in current position (column, row, band) from iterator.
88 
89  \param i The band index.
90 
91  \return The pixel comples value in current position.
92 
93  \note For real values use operator[]
94  */
95  virtual std::complex< T > operator()(const unsigned int i) const = 0;
96 
97  /*! \brief Returns the current row in iterator. */
98  virtual unsigned int getRow() const = 0;
99 
100  /*! \brief Returns the current column in iterator. */
101  virtual unsigned int getColumn() const = 0;
102 
103  /*! \brief Advances to the next position. */
104  virtual void operator++() = 0;
105 
106  /*! \brief Returns to the previous position. */
107  virtual void operator--() = 0;
108 
109  /*!
110  \brief Assignment operator.
111 
112  \param rhs The right-hand-side copy used to copy from.
113 
114  \return A reference to this object.
115  */
117 
118  /*!
119  \brief Difference operator.
120 
121  \param rhs The right-hand side to compare.
122 
123  \return Returns true if the iterators are at different positions, or false otherwise.
124  */
125  virtual bool operator!=(const AbstractPositionIterator<T>& rhs) const = 0;
126 
127  /*!
128  \brief Sets the iterator position to the end of the current band.
129 
130  \param b The band to retrieve the end information.
131  */
132  virtual void setEnd() = 0;
133 
134  protected:
135 
136  /*! \brief Constructor. */
138 
139  /*!
140  \brief Copy constructor.
141 
142  \param rhs The right-hand-side copy used to copy from.
143  */
145 
146  };
147 
148  /*!
149  \class PolygonIterator
150 
151  \brief This class implements the strategy to iterate with spatial restriction,
152  the iteration occurs inside a polygon.
153 
154  \ingroup rst
155  */
156  template<typename T> class PolygonIterator: public AbstractPositionIterator<T>
157  {
158  public:
159 
160  /*! \enum BlendMethod Pixel Blend methods. */
162  {
163  ScanLineIterationT, //!< Scan line iteration type.
164  NDisjointBBOXIterationT, //!< Itaration over pixels whose bounding boxes are not disjoint in relation with the polygon.
165  DisjointBBOXIterationT //!< Itaration over pixels whose bounding boxes are disjoint in relation with the polygon.
166  };
167 
168  PolygonIterator();
169 
170  /*!
171  \brief Constructor.
172 
173  \param b The band to iterate.
174  \param p The polygon from where the iteration will navigate.
175  \param iterationType Iteration type.
176  \note Both raster and polygon must have the same SRID.
177  */
179  const IterationType iterationType );
180 
181  /*!
182  \brief Constructor.
183 
184  \param b The band to iterate.
185  \param p The polygon from where the iteration will navigate.
186  \note Both raster and polygon must have the same SRID.
187  \note Iteration type: ScanLineIterationT.
188  */
189  PolygonIterator(const te::rst::Raster* r, const te::gm::Polygon* p);
190 
191  /*!
192  \brief Copy constructor.
193 
194  \param rhs The right-hand-side copy used to copy from.
195  */
196  PolygonIterator(const PolygonIterator& rhs);
197 
199 
200  const std::vector<T> operator*() const;
201 
202  T operator[](const unsigned int i) const;
203 
204  std::complex< T > operator()(const unsigned int i) const;
205 
206  unsigned int getRow() const;
207 
208  unsigned int getColumn() const;
209 
210  void operator++();
211 
212  void operator--();
213 
215 
217 
218  void setEnd();
219 
220  /*! \brief Returns an iterator referring to the first value of the band.*/
221  static PolygonIterator begin(const te::rst::Raster* r, const te::gm::Polygon* p,
222  const IterationType iterationType );
223 
224  /*! \brief Returns an iterator referring to the first value of the band.*/
225  static PolygonIterator begin(const te::rst::Raster* r, const te::gm::Polygon* p);
226 
227  /*! \brief Returns an iterator referring to after the end of the iterator. */
228  static PolygonIterator end(const te::rst::Raster* r, const te::gm::Polygon* p);
229 
230  /*! \brief Returns an iterator referring to after the end of the iterator. */
231  static PolygonIterator end(const te::rst::Raster* r, const te::gm::Polygon* p,
232  const IterationType iterationType );
233 
234  bool operator!=(const PolygonIterator<T>& rhs) const;
235 
236  bool operator!=(const AbstractPositionIterator<T>& rhs) const;
237 
238  protected:
239 
240  const te::rst::Raster* m_raster; //!< The band from where to get the values (default 0).
241  const te::gm::Polygon* m_polygon; //!< The spatial restriction to be applied in the iterator (default 0).
242  IterationType m_iterationType; //!< Current iteration type (default ScanLineIterationT).
243  int m_totalRasterColumns; //!< The number of columns in band (default: 0).
244  int m_totalRasterRows; //!< The number of rows in band (default: 0).
245  int m_polygonStartingColumn; //!< Polygon first raster column (default: -1).
246  int m_polygonEndingColumn; //!< Polygon last raster column (default: -1).
247  int m_polygonStartingRow; //!< Polygon first raster row (default: -1).
248  int m_polygonEndingRow;//!< Polygon last raster row (default: -1).
249 
250  int m_column; //!< The current column of the iterator (default -1).
251  int m_row; //!< The current row of the iterator (default -1).
252  int m_currentRangeStartingColumn; //!< The current line iteration range starting column (default: -1).
253  int m_currentRangeEndingColumn; //!< The current line iteration range column to finalize the iteration (default: -1).
254  int m_currentLineIntersectionRangesIndex; //!< The actual line of the iterator (default -1).
255  std::unique_ptr<te::rst::TileIndexer> m_tileIndexerPtr; //!< Tile indexer used to optimize the geometric operations
256  std::vector<std::pair<int, int> > m_currentLineIntersectionRanges; //!< Coordinates of the columns to be transversed
257 
258  // Variables used by the operator[] method.
259  mutable double m_operatorBrackets_value;
260  mutable std::complex< double > m_operatorParenthesis_value;
261 
262  /*! \brief Initialize all internal variables to initial states. */
263  void initiateVariables();
264 
265  /*! \brief Clear all internal allocated objects and reset back to the initial state. */
266  void clear();
267 
268  /*!
269  \brief Returns the given line intersection ranges (Coordinates of the columns to be transversed) using the current iteration type.
270  \param lineIndex Line index.
271  \param intersectionRanges Line intersection ranges (first:initial column, second:ending column) or an empty vector if no intersections exist.
272  */
273  void getIntersectionRanges( int lineIndex,
274  std::vector<std::pair<int, int> >& intersectionRanges ) const;
275 
276  /*!
277  \brief Returns the given line intersection ranges (Coordinates of the columns to be transversed) using the ScanLineIterationT mode.
278  \param lineIndex Line index.
279  \param intersectionRanges Line intersection ranges (first:initial column, second:ending column) or an empty vector if no intersections exist.
280  */
281  void getScanLineIntersectionRanges( int lineIndex,
282  std::vector<std::pair<int, int> >& intersectionRanges ) const;
283 
284  /*!
285  \brief Returns the given line intersection ranges using the pixel bounding box as reference.
286  \param lineIndex Line index.
287  \param areDisjoint If true, returns only thos pixels disjoint with the polygon, otherwise returns those pixels not disjoint with the polygon.
288  \param intersectionRanges Line intersection ranges (first:initial column, second:ending column) or an empty vector if no intersections exist.
289  */
290  void getBBOXIntersectionRanges( int lineIndex, const bool areDisjoint,
291  std::vector<std::pair<int, int> >& intersectionRanges ) const;
292 
293 
294  /*!
295  \brief Initialize this instance.
296 
297  \param r The raster to iterate.
298  \param p The polygon from where the iteration will navigate.
299  \param iterationType The iteration type
300  \note Both raster and polygon must have the same SRID.
301  */
302  bool initialize(const te::rst::Raster* r, const te::gm::Polygon* p,
303  const IterationType iterationType );
304 
305  };
306 
307  /*!
308  \class LineIterator
309 
310  \brief This class implements the strategy to iterate with spatial restriction,
311  the iteration occurs inside a line.
312 
313  \ingroup rst
314  */
315  template<typename T> class LineIterator: public AbstractPositionIterator<T>
316  {
317  public:
318 
319  LineIterator();
320 
321  /*!
322  \brief Constructor.
323 
324  \param b The band to iterate.
325  \param l The line from where the iteration will navigate.
326  */
327  LineIterator(const te::rst::Raster* r, const te::gm::Line* l);
328 
329  /*!
330  \brief Copy constructor.
331 
332  \param rhs The right-hand-side copy used to copy from.
333  */
334  LineIterator(const LineIterator& rhs);
335 
336  ~LineIterator();
337 
338  const std::vector<T> operator*() const;
339 
340  T operator[](const unsigned int i) const;
341 
342  std::complex< T > operator()(const unsigned int i) const;
343 
344  unsigned int getRow() const;
345 
346  unsigned int getColumn() const;
347 
348  void operator++();
349 
350  void operator--();
351 
352  LineIterator& operator=(const LineIterator& rhs);
353 
355 
356  void setEnd();
357 
358  /*! \brief Returns an iterator referring to the first value of the band.*/
359  static LineIterator begin(const te::rst::Raster* r, const te::gm::Line* l);
360 
361  /*! \brief Returns an iterator referring to after the end of the iterator. */
362  static LineIterator end(const te::rst::Raster* r, const te::gm::Line* l);
363 
364  bool operator!=(const LineIterator<T>& rhs) const;
365 
366  bool operator!=(const AbstractPositionIterator<T>& rhs) const;
367 
368  protected:
369 
370  const te::rst::Raster* m_raster; //!< The band from where to get the values.
371  const te::gm::Line* m_line; //!< The spatial restriction to be applied in the iterator.
372  int m_currentpixelindex; //!< The index of the current pixel location.
373  std::vector<te::gm::Point*> m_pixelsinline; //!< A vector of pixel locations that intersects the line.
374  mutable double m_operatorBrackets_value; //!< Used by the operator[] method.
375  mutable std::complex< double > m_operatorParenthesis_value; //!< Used by the operator() method.
376 
377 
378  };
379 
380  /*!
381  \class PointSetIterator
382 
383  \brief This class implements the strategy to iterate with spatial restriction,
384  the iteration occurs inside a vector of points.
385 
386  \ingroup rst
387  */
388  template<typename T> class PointSetIterator: public AbstractPositionIterator<T>
389  {
390  public:
391 
393 
394  /*!
395  \brief Constructor.
396 
397  \param b The band to iterate.
398  \param p The vector of points where the iteration will navigate.
399  */
400  PointSetIterator(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
401 
402  /*!
403  \brief Copy constructor.
404 
405  \param rhs The right-hand-side copy used to copy from.
406  */
408 
410 
411  const std::vector<T> operator*() const;
412 
413  T operator[](const unsigned int i) const;
414 
415  std::complex< T > operator()(const unsigned int i) const;
416 
417  unsigned int getRow() const;
418 
419  unsigned int getColumn() const;
420 
421  void operator++();
422 
423  void operator--();
424 
426 
428 
429  void setEnd();
430 
431  /*! \brief Returns an iterator referring to the first value of the band.*/
432  static PointSetIterator begin(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
433 
434  /*! \brief Returns an iterator referring to after the end of the iterator. */
435  static PointSetIterator end(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
436 
437  bool operator!=(const PointSetIterator<T>& rhs) const;
438 
439  bool operator!=(const AbstractPositionIterator<T>& rhs) const;
440 
441  protected:
442 
443  const te::rst::Raster* m_raster; //!< The band from where to get the values.
444  std::vector<te::gm::Point*> m_pixelsinpointset; //!< The spatial restriction to be applied in the iterator.
445  int m_currentpixelindex; //!< The index of the current pixel location.
446  mutable double m_operatorBrackets_value; //!< Used by the operator[] method.
447  mutable std::complex< double > m_operatorParenthesis_value; //!< Used by the operator() method.
448 
449 
450  };
451 
452  // ------------------------------------------------------------------------
453 
454 // implementation of abstract position iterator
455 
457  {
458  }
459 
461  {
462  }
463 
465  {
466  }
467 
468  template<typename T>
471  {
472  return *this;
473  }
474 
475  // ------------------------------------------------------------------------
476 
477 // implementation of iteration strategy bounded by a polygon
479  {
480  initiateVariables();
481  }
482 
484  const te::rst::Raster* r, const te::gm::Polygon* p,
485  const IterationType iterationType)
486  {
487  initiateVariables();
488 
489  if( ! initialize( r, p, iterationType ) )
490  {
491  throw te::rst::Exception( TE_TR("Iterator initialization error") );
492  }
493  }
494 
496  {
497  initiateVariables();
498 
499  if( ! initialize( r, p, ScanLineIterationT ) )
500  {
501  throw te::rst::Exception( TE_TR("Iterator initialization error") );
502  }
503  }
504 
506  {
507  initiateVariables();
508  operator=( rhs );
509  }
510 
512  {
513  clear();
514  }
515 
516  template<typename T> const std::vector<T> te::rst::PolygonIterator<T>::operator*() const
517  {
518  std::vector<T> values( m_raster->getNumberOfBands());
519  double value;
520 
521  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
522  {
523  m_raster->getValue( m_column, m_row, value, b);
524  values[b] = ((T) value);
525  }
526 
527  return values;
528  }
529 
530  template<typename T> T te::rst::PolygonIterator<T>::operator[](const unsigned int i) const
531  {
532  this->m_raster->getValue(m_column, m_row, m_operatorBrackets_value, i);
533 
534  return (T) m_operatorBrackets_value;
535  }
536 
537  template<typename T> std::complex< T > te::rst::PolygonIterator<T>::operator()(const unsigned int i) const
538  {
539  this->m_raster->getValue(m_column, m_row, m_operatorParenthesis_value, i);
540 
541  return (std::complex< T >) m_operatorParenthesis_value;
542  }
543 
544  template<typename T> unsigned int te::rst::PolygonIterator<T>::getRow() const
545  {
546  return m_row;
547  }
548 
549  template<typename T> unsigned int te::rst::PolygonIterator<T>::getColumn() const
550  {
551  return m_column;
552  }
553 
554  template<typename T> void te::rst::PolygonIterator<T>::operator++()
555  {
556  if( m_column < m_currentRangeEndingColumn )
557  {
558  ++m_column;
559  }
560  else
561  {
562  if ( ( m_currentLineIntersectionRangesIndex + 1 ) < (int)( m_currentLineIntersectionRanges.size() ) )
563  {
564  ++m_currentLineIntersectionRangesIndex;
565  m_column = m_currentRangeStartingColumn = m_currentLineIntersectionRanges[ m_currentLineIntersectionRangesIndex ].first;
566  m_currentRangeEndingColumn = m_currentLineIntersectionRanges[ m_currentLineIntersectionRangesIndex ].second;
567  }
568  else
569  {
570  if( m_row < m_polygonEndingRow )
571  {
572  m_currentLineIntersectionRanges.clear();
573 
574  while( m_row <= m_polygonEndingRow )
575  {
576  ++m_row;
577 
578  getIntersectionRanges( m_row, m_currentLineIntersectionRanges );
579 
580  if( ! m_currentLineIntersectionRanges.empty() )
581  {
582  break;
583  }
584  }
585 
586  if( m_currentLineIntersectionRanges.empty() )
587  {
588  setEnd();
589  }
590  else
591  {
592  m_column = m_currentRangeStartingColumn = m_currentLineIntersectionRanges[ 0 ].first;
593  m_currentRangeEndingColumn = m_currentLineIntersectionRanges[ 0 ].second;
594  m_currentLineIntersectionRangesIndex = 0;
595  }
596  }
597  else
598  {
599  setEnd();
600  }
601  }
602  }
603  }
604 
605  template<typename T> void te::rst::PolygonIterator<T>::operator--()
606  {
607  if(m_column > m_currentRangeStartingColumn )
608  {
609  --m_column;
610  }
611  else
612  {
613  if( m_currentLineIntersectionRangesIndex > 0 )
614  {
615  --m_currentLineIntersectionRangesIndex;
616  m_currentRangeStartingColumn = m_currentLineIntersectionRanges[ m_currentLineIntersectionRangesIndex ].first;
617  m_column = m_currentRangeEndingColumn = m_currentLineIntersectionRanges[ m_currentLineIntersectionRangesIndex ].second;
618  }
619  else
620  {
621  if( m_row > m_polygonStartingRow )
622  {
623  m_currentLineIntersectionRanges.clear();
624 
625  while( m_row >= m_polygonStartingRow )
626  {
627  --m_row;
628 
629  getIntersectionRanges( m_row, m_currentLineIntersectionRanges );
630 
631  if( ! m_currentLineIntersectionRanges.empty() )
632  {
633  break;
634  }
635  }
636 
637  if( m_currentLineIntersectionRanges.empty() )
638  {
639  setEnd();
640  }
641  else
642  {
643  m_currentLineIntersectionRangesIndex = m_currentLineIntersectionRanges.size() - 1;
644  m_currentRangeStartingColumn = m_currentLineIntersectionRanges[ m_currentLineIntersectionRangesIndex ].first;
645  m_column = m_currentRangeEndingColumn = m_currentLineIntersectionRanges[ m_currentLineIntersectionRangesIndex ].second;
646  }
647  }
648  else
649  {
650  setEnd();
651  }
652  }
653  }
654  }
655 
656  template<typename T>
658  const te::rst::PolygonIterator<T>& rhs)
659  {
660  clear();
661 
662  if (this != &rhs)
663  {
665 
666  m_raster = rhs.m_raster;
667  m_polygon = rhs.m_polygon;
668  m_iterationType = rhs.m_iterationType;
669  m_totalRasterColumns = rhs.m_totalRasterColumns;
670  m_totalRasterRows = rhs.m_totalRasterRows;
671  m_polygonStartingColumn = rhs.m_polygonStartingColumn;
672  m_polygonEndingColumn = rhs.m_polygonEndingColumn;
673  m_polygonStartingRow = rhs.m_polygonStartingRow;
674  m_polygonEndingRow = rhs.m_polygonEndingRow;
675 
676  m_column = rhs.m_column;
677  m_row = rhs.m_row;
678  m_currentRangeStartingColumn = rhs.m_currentRangeStartingColumn;
679  m_currentRangeEndingColumn = rhs.m_currentRangeEndingColumn;
680  m_currentLineIntersectionRangesIndex = rhs.m_currentLineIntersectionRangesIndex;
681 
682  if( rhs.m_tileIndexerPtr.get() )
683  {
684  m_tileIndexerPtr.reset(rhs.m_tileIndexerPtr->clone());
685  }
686 
687  m_currentLineIntersectionRanges = rhs.m_currentLineIntersectionRanges;
688  }
689 
690  return *this;
691  }
692 
693  template<typename T>
696  {
697  return operator=( *((te::rst::PolygonIterator<T>*)&rhs) );
698  }
699 
700  template<typename T> void te::rst::PolygonIterator<T>::setEnd()
701  {
702  m_column = -1;
703  m_row = -1;
704  }
705 
707  const te::rst::Raster* r, const te::gm::Polygon* p, const IterationType iterationType )
708  {
709  return te::rst::PolygonIterator<T>(r, p, iterationType);
710  }
711 
713  {
714  return te::rst::PolygonIterator<T>(r, p);
715  }
716 
718  const te::rst::Raster* r, const te::gm::Polygon* p, const IterationType iterationType)
719  {
720  te::rst::PolygonIterator<T> it(r, p, iterationType);
721 
722  it.setEnd();
723 
724  return it;
725  }
726 
728  {
730 
731  it.setEnd();
732 
733  return it;
734  }
735 
736  template<typename T> bool te::rst::PolygonIterator<T>::operator!=(const te::rst::PolygonIterator<T>& rhs) const
737  {
738  return ( (this->m_row != rhs.m_row) && (this->m_column != rhs.m_column));
739  }
740 
741  template<typename T>
743  const AbstractPositionIterator<T>& rhs) const
744  {
745  return operator!=( *((te::rst::PolygonIterator<T>*)&rhs) );
746  }
747 
749  {
750  m_raster = 0;
751  m_polygon = 0;
752  m_iterationType = ScanLineIterationT;
753  m_totalRasterColumns = 0;
754  m_totalRasterRows = 0;
755  m_polygonStartingColumn = -1;
756  m_polygonEndingColumn = -1;
757  m_polygonStartingRow = -1;
758  m_polygonEndingRow = -1;
759 
760  m_column = -1;
761  m_row = -1;
762  m_currentRangeStartingColumn = -1;
763  m_currentRangeEndingColumn = -1;
764  m_currentLineIntersectionRangesIndex = -1;
765  }
766 
767  template<typename T> void te::rst::PolygonIterator<T>::clear()
768  {
769  m_tileIndexerPtr.reset();
770  m_currentLineIntersectionRanges.clear();
771 
772  initiateVariables();
773  }
774 
775  template<typename T>
777  std::vector<std::pair<int, int> >& intersectionRanges ) const
778  {
779  switch( m_iterationType )
780  {
781  case ScanLineIterationT :
782  {
783  getScanLineIntersectionRanges( lineIndex, intersectionRanges );
784  break;
785  }
786  case NDisjointBBOXIterationT :
787  {
788  getBBOXIntersectionRanges( lineIndex, false, intersectionRanges );
789  break;
790  }
791  case DisjointBBOXIterationT :
792  {
793  getBBOXIntersectionRanges( lineIndex, true, intersectionRanges );
794  break;
795  }
796  default :
797  {
798  throw te::rst::Exception( TE_TR("Invalid iteration type") );
799  break;
800  }
801  }
802  }
803 
804  template<typename T>
806  std::vector<std::pair<int, int> >& intersectionRanges ) const
807  {
808  intersectionRanges.clear();
809 
810  // Globals
811 
812  const double& rasterEnvelopeLLX =
813  m_raster->getGrid()->getExtent()->m_llx;
814  const double& rasterEnvelopeURX =
815  m_raster->getGrid()->getExtent()->m_urx;
816 
817  // current line
818 
819  const double lineY = this->m_raster->getGrid()->gridToGeo(0, (double)lineIndex).y;
820 
821  te::gm::Line currline( te::gm::Point( m_polygon->getSRID() ),
822  te::gm::Point( m_polygon->getSRID() ), te::gm::LineStringType,
823  m_polygon->getSRID() );
824 
825  currline.setX(0, std::min(
826  rasterEnvelopeLLX,
827  m_polygon->getMBR()->getLowerLeft().x) );
828  currline.setY(0, lineY);
829 
830  currline.setX(1, std::max(
831  rasterEnvelopeURX,
832  m_polygon->getMBR()->getUpperRight().x ) );
833  currline.setY(1, lineY);
834 
835  // Vector to store the points where the current line intersects the current tile of the polygon
836  std::vector<te::gm::Point*> intersectionPoints;
837 
838  // Retrieve the tile corresponding to the current line and store the points where the line
839  // intersects the tile
840 
841  te::rst::TileIndexer::TileSegIndex* currentTilePtr = 0;
842 
843  if(
844  m_tileIndexerPtr->getTile(currline.getY(0), &currentTilePtr)
845  &&
846  ( currentTilePtr != 0 )
847  )
848  {
849  te::gm::LinearRing const* ringTile;
850  std::unique_ptr<te::gm::Line> tileSeg;
851  std::unique_ptr< te::gm::Geometry > interResultPtr;
852  std::vector< te::gm::Geometry * > singleGeomsPtrs;
853  std::size_t singleGeomsPtrsIdx = 0;
854 
855  try
856  {
857  // in some cases the intersection presents an unhandled exception, in this case we do not paint the current line
858  // Transverse the segments of the tile retrieving the intersection between them and the current line
859 
860  te::gm::LineString* lineStrPtr = 0;
861 
862  for (std::size_t i = 0; i < currentTilePtr->size(); i++)
863  {
864  assert((*currentTilePtr)[i].first < m_polygon->getNumRings());
865 
866  // Retrieve the current ring of the current tile
867  assert(dynamic_cast<te::gm::LinearRing const*>((*m_polygon)[(*currentTilePtr)[i].first]));
868  ringTile = (te::gm::LinearRing const*)(*m_polygon)[(*currentTilePtr)[i].first];
869 
870  assert((*currentTilePtr)[i].second < m_polygon->getNPoints());
871 
872  // Retrieve the current segment of the current tile
873  tileSeg.reset(new te::gm::Line(te::gm::Point(ringTile->getX((*currentTilePtr)[i].second),
874  ringTile->getY((*currentTilePtr)[i].second),
875  ringTile->getSRID()),
876  te::gm::Point(ringTile->getX((*currentTilePtr)[i].second + 1),
877  ringTile->getY((*currentTilePtr)[i].second + 1),
878  ringTile->getSRID()),
879  te::gm::LineStringType, ringTile->getSRID()));
880 
881  // Computes the intersection point between the current segment and the current line
882 
883  interResultPtr.reset( tileSeg->intersection( &currline ) );
884 
885  if( interResultPtr.get() != 0 )
886  {
887  for(auto p : singleGeomsPtrs)
888  delete p;
889  singleGeomsPtrs.clear();
890  te::gm::Multi2Single( interResultPtr.get(), singleGeomsPtrs );
891 
892  for( singleGeomsPtrsIdx = 0 ; singleGeomsPtrsIdx < singleGeomsPtrs.size() ;
893  ++singleGeomsPtrsIdx )
894  {
895  if( singleGeomsPtrs[ singleGeomsPtrsIdx ]->getGeomTypeId() ==
897  {
898  intersectionPoints.push_back( (te::gm::Point*)(
899  singleGeomsPtrs[ singleGeomsPtrsIdx ]->clone() ) );
900  }
901  else if( singleGeomsPtrs[ singleGeomsPtrsIdx ]->getGeomTypeId() ==
903  {
904  lineStrPtr = ((te::gm::LineString*) singleGeomsPtrs[
905  singleGeomsPtrsIdx ]);
906  intersectionPoints.push_back( lineStrPtr->getStartPoint().release() );
907  intersectionPoints.push_back( lineStrPtr->getEndPoint().release() );
908  }
909  }
910  }
911  }
912  }
913  catch(const std::exception& e)
914  {
915  TE_LOG_WARN( "Geometry intersection error:" + e.what() );
916 
917  // deleting the intersections points
918 
919  for( std::size_t intersectionPointsIdx = 0; intersectionPointsIdx <
920  intersectionPoints.size() ; ++intersectionPointsIdx )
921  {
922  delete intersectionPoints[intersectionPointsIdx];
923  }
924 
925  intersectionPoints.clear();
926  }
927  catch(...)
928  {
929  TE_LOG_WARN( "Geometry intersection error" );
930 
931  // deleting the intersections points
932 
933  for( std::size_t intersectionPointsIdx = 0; intersectionPointsIdx <
934  intersectionPoints.size() ; ++intersectionPointsIdx )
935  {
936  delete intersectionPoints[intersectionPointsIdx];
937  }
938 
939  intersectionPoints.clear();
940  }
941 
942  for(auto p : singleGeomsPtrs)
943  delete p;
944  singleGeomsPtrs.clear();
945  }
946 
947  // Removing duplicated points
948 
949  {
950  std::size_t positionBegin = 0;
951  std::size_t positionEnd = 0;
952  std::vector<te::gm::Point*> intersectionPointsAux = intersectionPoints;
953 
954  intersectionPoints.clear();
955 
956  while( positionBegin < intersectionPointsAux.size() )
957  {
958  if( intersectionPointsAux[positionBegin] )
959  {
960  positionEnd = positionBegin + 1;
961 
962  while( positionEnd < intersectionPointsAux.size() )
963  {
964  if(
965  ( intersectionPointsAux[positionEnd] != 0 )
966  &&
967  intersectionPointsAux[positionBegin]->equals(
968  intersectionPointsAux[positionEnd], true)
969  )
970  {
971  delete intersectionPointsAux[positionEnd];
972  intersectionPointsAux[positionEnd] = 0;
973  }
974 
975  positionEnd++;
976  }
977 
978  intersectionPoints.push_back( intersectionPointsAux[positionBegin] );
979  }
980 
981  ++positionBegin;
982  }
983  }
984 
985  // Sort the intersection points through its Y coordinates (column)
986  std::sort(intersectionPoints.begin(), intersectionPoints.end(),
988 
989  // Using the intersection points, build a vector of coordinates (columns) with the start and
990  // end column of the current line for each stretch
991 
992  std::size_t positionBegin = 0;
993  std::size_t positionEnd = 0;
994  int startingCol = 0;
995  int endingCol = 0;
996  double startingX = 0;
997  double startingY = 0;
998  double endingX = 0;
999  double endingY = 0;
1000 
1001  while( ( positionBegin + 1 ) < intersectionPoints.size() )
1002  {
1003  positionEnd = positionBegin + 1;
1004 
1005  startingX = intersectionPoints[positionBegin]->getX();
1006  startingX = std::max( startingX, rasterEnvelopeLLX );
1007 
1008  startingY = intersectionPoints[positionBegin]->getY();
1009 
1010  endingX = intersectionPoints[positionEnd]->getX();
1011  endingX = std::min( endingX, rasterEnvelopeURX );
1012 
1013  endingY = intersectionPoints[positionEnd]->getY();
1014 
1015  // Build the vector of coordinates with the folowing structure:
1016  // vector<pair<startcolumn, endcolumn>>;
1017  // where each pair represents a stretch to be transversed
1018 
1019  if(
1020  ( startingX != endingX )
1021  &&
1022  (
1023  m_tileIndexerPtr->withinOrTouches(te::gm::Point(
1024  (startingX + (endingX - startingX) / 2.0 ),
1025  (startingY +(endingY - startingY) / 2.0 ),
1026  m_polygon->getSRID()))
1027  )
1028  )
1029  {
1030  // The middle-point (between the start end end point)
1031  // inside the current polygon
1032 
1033  startingCol = (int)std::ceil( m_raster->getGrid()->geoToGrid(startingX, startingY).x );
1034  startingCol = std::max( 0, startingCol );
1035  startingCol = std::min( m_totalRasterColumns - 1, startingCol );
1036 
1037  endingCol = (int)std::floor( m_raster->getGrid()->geoToGrid(endingX, endingY).x );
1038  endingCol = std::max( 0, endingCol );
1039  endingCol = std::min( m_totalRasterColumns - 1, endingCol );
1040 
1041  if( endingCol >= startingCol )
1042  {
1043  intersectionRanges.push_back(std::pair<int, int>(startingCol, endingCol));
1044  }
1045  }
1046 
1047  positionBegin = positionEnd;
1048  }
1049 
1050  // deleting the intersections points
1051 
1052  positionBegin = 0;
1053 
1054  while ( positionBegin < intersectionPoints.size() )
1055  {
1056  delete intersectionPoints[positionBegin];
1057  ++positionBegin;
1058  }
1059  }
1060 
1061  template<typename T>
1063  const bool areDisjoint, std::vector<std::pair<int, int> >& intersectionRanges ) const
1064  {
1065  intersectionRanges.clear();
1066 
1067  double ulXCoord = 0;
1068  double ulYCoord = 0;
1069  m_raster->getGrid()->gridToGeo( ((double)m_polygonStartingColumn) - 0.5,
1070  ((double)lineIndex) - 0.5, ulXCoord, ulYCoord);
1071 
1072  const double yRes = this->m_raster->getGrid()->getResolutionY();
1073  const double xRes = this->m_raster->getGrid()->getResolutionX();
1074 
1076  m_polygon->getSRID(), 0 );
1077  currBBOX.setNumCoordinates( 5 );
1078  te::gm::Coord2D* currBBOXCoordsPtr = currBBOX.getCoordinates();
1079 
1080  bool rangeStarted = false;
1081  std::pair<int, int> range;
1082 
1083  for( int col = m_polygonStartingColumn ; col <= m_polygonEndingColumn ; ++col )
1084  {
1085  // UL
1086  currBBOXCoordsPtr[ 0 ].x = currBBOXCoordsPtr[ 4 ].x =
1087  ulXCoord + ( xRes * (double)( col - m_polygonStartingColumn ) );
1088  currBBOXCoordsPtr[ 0 ].y = currBBOXCoordsPtr[ 4 ].y = ulYCoord;
1089 
1090  // UR
1091  currBBOXCoordsPtr[ 1 ].x = currBBOXCoordsPtr[ 0 ].x + xRes;
1092  currBBOXCoordsPtr[ 1 ].y = ulYCoord;
1093 
1094  // LR
1095  currBBOXCoordsPtr[ 2 ].x = currBBOXCoordsPtr[ 1 ].x;
1096  currBBOXCoordsPtr[ 2 ].y = ulYCoord - yRes;
1097 
1098  // LL
1099  currBBOXCoordsPtr[ 3 ].x = currBBOXCoordsPtr[ 0 ].x;
1100  currBBOXCoordsPtr[ 3 ].y = ulYCoord - yRes;
1101 
1102  if( areDisjoint == currBBOX.disjoint( m_polygon ) )
1103  { // this pixel must be considered
1104  if( rangeStarted )
1105  {
1106  range.second = col;
1107  }
1108  else
1109  {
1110  rangeStarted = true;
1111  range.first = range.second = col;
1112  }
1113  }
1114  else
1115  { // this pixel must NOT be considered
1116  if( rangeStarted )
1117  {
1118  rangeStarted = false;
1119  intersectionRanges.push_back( range );
1120  }
1121  }
1122  }
1123 
1124  // finalizing the last range
1125  if( rangeStarted )
1126  {
1127  intersectionRanges.push_back( range );
1128  }
1129  }
1130 
1131  template<typename T>
1133  const te::gm::Polygon* p, const IterationType iterationType )
1134  {
1135  clear();
1136 
1137  m_raster = r;
1138  m_polygon = p;
1139  m_totalRasterColumns = r->getNumberOfColumns();
1140  m_totalRasterRows = r->getNumberOfRows();
1141  m_iterationType = iterationType;
1142 
1143  if( r->getSRID() != p->getSRID() )
1144  {
1145  return false;
1146  }
1147 
1148  const te::rst::Grid& rasterGrid = *r->getGrid();
1149  const te::gm::Envelope& rasterEnvelope = *rasterGrid.getExtent();
1150 
1151  te::gm::Coord2D ll = m_polygon->getMBR()->getLowerLeft();
1152  te::gm::Coord2D ur = m_polygon->getMBR()->getUpperRight();
1153 
1154  // Is the polygon extent is outside the current raster area
1155 
1156  if(
1157  ( ll.x > rasterEnvelope.m_urx )
1158  ||
1159  ( ur.x < rasterEnvelope.m_llx )
1160  ||
1161  ( ur.y < rasterEnvelope.m_lly )
1162  ||
1163  ( ll.y > rasterEnvelope.m_ury )
1164  )
1165  {
1166  setEnd();
1167  }
1168  else
1169  {
1170  // defining starting/ending rows
1171 
1172  te::gm::Coord2D llIndexed;
1173  rasterGrid.geoToGrid(ll.x, ll.y, llIndexed.x, llIndexed.y);
1174  te::gm::Coord2D urIndexed;
1175  rasterGrid.geoToGrid(ur.x, ur.y, urIndexed.x, urIndexed.y);
1176 
1177  m_polygonStartingRow = (int)std::floor( urIndexed.y );
1178  m_polygonStartingRow = std::max( 0, m_polygonStartingRow );
1179  m_polygonStartingRow = std::min( m_polygonStartingRow, ((int)rasterGrid.getNumberOfRows()) - 1 );
1180 
1181  m_polygonEndingRow = (int)std::ceil( llIndexed.y );
1182  m_polygonEndingRow = std::max( 0, m_polygonEndingRow );
1183  m_polygonEndingRow = std::min( m_polygonEndingRow, ((int)rasterGrid.getNumberOfRows()) - 1 );
1184 
1185  m_polygonStartingColumn = (int)std::floor( llIndexed.x );
1186  m_polygonStartingColumn = std::max( 0, m_polygonStartingColumn );
1187  m_polygonStartingColumn = std::min( m_polygonStartingColumn, ((int)rasterGrid.getNumberOfColumns()) - 1 );
1188 
1189  m_polygonEndingColumn = (int)std::ceil( urIndexed.x );
1190  m_polygonEndingColumn = std::max( 0, m_polygonEndingColumn );
1191  m_polygonEndingColumn = std::min( m_polygonEndingColumn, ((int)rasterGrid.getNumberOfColumns()) - 1 );
1192 
1193  if(
1194  ( m_polygonEndingRow < m_polygonStartingRow )
1195  ||
1196  ( m_polygonEndingColumn < m_polygonStartingColumn )
1197  )
1198  {
1199  setEnd();
1200  }
1201  else
1202  {
1203  // initialize the TileIndexer
1204 
1205  if( m_iterationType == ScanLineIterationT )
1206  {
1207  m_tileIndexerPtr = std::unique_ptr<te::rst::TileIndexer>(new te::rst::TileIndexer(*m_polygon, rasterGrid.getResolutionY()));
1208  }
1209 
1210  // defining initial state
1211 
1212  m_row = m_polygonStartingRow;
1213 
1214  while( m_row <= m_polygonEndingRow )
1215  {
1216  getIntersectionRanges( m_row, m_currentLineIntersectionRanges );
1217 
1218  if( ! m_currentLineIntersectionRanges.empty() )
1219  {
1220  break;
1221  }
1222 
1223  ++m_row;
1224  }
1225 
1226  if( m_currentLineIntersectionRanges.empty() )
1227  {
1228  setEnd();
1229  }
1230  else
1231  {
1232  m_column = m_currentRangeStartingColumn = m_currentLineIntersectionRanges[ 0 ].first;
1233  m_currentRangeEndingColumn = m_currentLineIntersectionRanges[ 0 ].second;
1234  m_currentLineIntersectionRangesIndex = 0;
1235  }
1236  }
1237  }
1238 
1239  return true;
1240  }
1241 
1242  // ------------------------------------------------------------------------
1243 
1244 
1245 // implementation of iteration strategy bounded by a line
1247  : m_raster(0),
1248  m_line(0),
1249  m_currentpixelindex(0),
1250  m_pixelsinline(0)
1251  {
1252  }
1253 
1255  : m_raster(r),
1256  m_line(l),
1257  m_currentpixelindex(0),
1258  m_pixelsinline(0)
1259  {
1260  if( r->getSRID() != l->getSRID() )
1261  {
1262  throw te::rst::Exception( TE_TR("Invalid line SRID") );
1263  }
1264 
1265  int srid = this->m_raster->getSRID();
1266 
1267 // make intersection between line and band's envelope
1268  te::gm::Geometry* bandEnvelope = te::gm::GetGeomFromEnvelope(this->m_raster->getExtent(), srid);
1269  te::gm::Geometry* inter = bandEnvelope->intersection(m_line);
1270 
1271  if (inter->isEmpty())
1272  {
1273  setEnd();
1274 
1275  return;
1276  }
1277 
1278 // create line that intersects only band's envelope
1279  te::gm::Line* inrasterline = (te::gm::Line*) inter;
1280 
1281 // find starting and ending points
1282  double startingcolumn;
1283  double startingrow;
1284  std::unique_ptr<te::gm::Point> startpoint = inrasterline->getStartPoint();
1285  this->m_raster->getGrid()->geoToGrid(startpoint->getX(), startpoint->getY(),
1286  startingcolumn, startingrow);
1287 
1288  double endingcolumn;
1289  double endingrow;
1290  std::unique_ptr<te::gm::Point> endpoint = inrasterline->getEndPoint();
1291  this->m_raster->getGrid()->geoToGrid(endpoint->getX(), endpoint->getY(),
1292  endingcolumn, endingrow);
1293 
1294 // creating one envelope per pixel, and intersects with line
1295  const double resXdiv2 = this->m_raster->getResolutionX() / 2;
1296  const double resYdiv2 = this->m_raster->getResolutionY() / 2;
1297  double x1, x2, y1, y2, geoX, geoY;
1298  for(int r = (int)startingrow; r <= (int)endingrow; r++)
1299  for(int c = (int)startingcolumn; c <= (int)endingcolumn; c++)
1300  {
1301 // define envelope of pixel
1302  this->m_raster->getGrid()->gridToGeo(c, r, geoX, geoY);
1303  x1 = geoX - resXdiv2; y1 = geoY - resYdiv2;
1304  x2 = geoX + resXdiv2; y2 = geoY + resYdiv2;
1305 
1306  te::gm::Envelope* pixelbox = new te::gm::Envelope(x1, y1, x2, y2);
1307  te::gm::Geometry* pixelboxgeometry = GetGeomFromEnvelope(pixelbox, srid);
1308 
1309  if (te::gm::SatisfySpatialRelation(inrasterline, pixelboxgeometry, te::gm::INTERSECTS))
1310  m_pixelsinline.push_back(new te::gm::Point(c, r, srid));
1311  }
1312 
1313  if (m_pixelsinline.empty())
1314  setEnd();
1315  }
1316 
1318  : m_raster(rhs.m_raster),
1319  m_currentpixelindex(rhs.m_currentpixelindex),
1320  m_pixelsinline(rhs.m_pixelsinline)
1321  {
1322  }
1323 
1325  {
1326  m_pixelsinline.clear();
1327  }
1328 
1329  template<typename T> const std::vector<T> te::rst::LineIterator<T>::operator*() const
1330  {
1331  std::vector<T> values(this->m_raster->getNumberOfBands());
1332  double value;
1333 
1334  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
1335  {
1336  this->m_raster->getValue(getColumn(), getRow(), value, b);
1337  values[b] = ((T) value);
1338  }
1339 
1340  return values;
1341  }
1342 
1343  template<typename T> T te::rst::LineIterator<T>::operator[](const unsigned int i) const
1344  {
1345  this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
1346 
1347  return (T) m_operatorBrackets_value;
1348  }
1349 
1350  template<typename T> std::complex< T > te::rst::LineIterator<T>::operator()(const unsigned int i) const
1351  {
1352  this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
1353 
1354  return (std::complex< T >) m_operatorParenthesis_value;
1355  }
1356 
1357  template<typename T> unsigned int te::rst::LineIterator<T>::getRow() const
1358  {
1359  return (unsigned int)(m_pixelsinline[m_currentpixelindex]->getY());
1360  }
1361 
1362  template<typename T> unsigned int te::rst::LineIterator<T>::getColumn() const
1363  {
1364  return (unsigned int)(m_pixelsinline[m_currentpixelindex]->getX());
1365  }
1366 
1367  template<typename T> void te::rst::LineIterator<T>::operator++()
1368  {
1369  m_currentpixelindex++;
1370 
1371  if (m_currentpixelindex >= (int)(m_pixelsinline.size()))
1372  setEnd();
1373  }
1374 
1375  template<typename T> void te::rst::LineIterator<T>::operator--()
1376  {
1377  m_currentpixelindex--;
1378 
1379  if (m_currentpixelindex < 0)
1380  setEnd();
1381  }
1382 
1384  {
1385  if (this != &rhs)
1386  {
1388 
1389  m_line = rhs.m_line;
1390  m_currentpixelindex = rhs.m_currentpixelindex;
1391  m_pixelsinline = rhs.m_pixelsinline;
1392  }
1393 
1394  return *this;
1395  }
1396 
1397  template<typename T>
1399  const AbstractPositionIterator<T>& rhs)
1400  {
1401  return operator=( *((LineIterator<T>*)&rhs) );
1402  }
1403 
1404  template<typename T> void te::rst::LineIterator<T>::setEnd()
1405  {
1406  this->m_currentpixelindex = -1;
1407  }
1408 
1410  {
1411  return te::rst::LineIterator<T>(r, l);
1412  }
1413 
1415  {
1416  te::rst::LineIterator<T> it(r, l);
1417 
1418  it.setEnd();
1419 
1420  return it;
1421  }
1422 
1423  template<typename T> bool te::rst::LineIterator<T>::operator!=(const te::rst::LineIterator<T>& rhs) const
1424  {
1425  return ( (this->m_currentpixelindex != rhs.m_currentpixelindex) );
1426  }
1427 
1428  template<typename T>
1430  const AbstractPositionIterator<T>& rhs) const
1431  {
1432  return operator!=( *((te::rst::LineIterator<T>*)&rhs) );
1433  }
1434 
1435 // implementation of iteration strategy bounded by a vector of points
1437  : m_raster(0),
1438  m_pixelsinpointset(0),
1439  m_currentpixelindex(0)
1440  {
1441  }
1442 
1443  template<typename T> te::rst::PointSetIterator<T>::PointSetIterator(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1444  : m_raster(r),
1445  m_pixelsinpointset(p),
1446  m_currentpixelindex(0)
1447  {
1448  const int rasterSRID = this->m_raster->getSRID();
1449 
1450  const te::gm::Envelope* rasterbox = r->getExtent();
1451  te::gm::Geometry* rasterboxgeometry = GetGeomFromEnvelope(rasterbox, rasterSRID);
1452 
1453 // remove points that are not inside the band's envelope
1454  std::vector<te::gm::Point*> inside_points;
1455  double column;
1456  double row;
1457  for (unsigned int i = 0; i < m_pixelsinpointset.size(); i++)
1458  {
1459  if( rasterSRID != m_pixelsinpointset[i]->getSRID() )
1460  {
1461  throw te::rst::Exception( TE_TR("Invalid point SRID") );
1462  }
1463 
1465  {
1466  this->m_raster->getGrid()->geoToGrid(m_pixelsinpointset[i]->getX(), m_pixelsinpointset[i]->getY(), column, row);
1467 
1468  inside_points.push_back(new te::gm::Point(column, row));
1469  }
1470  }
1471 
1472  m_pixelsinpointset.clear();
1473  m_pixelsinpointset = inside_points;
1474 
1475  if (m_pixelsinpointset.empty())
1476  setEnd();
1477  }
1478 
1480  : m_raster(rhs.m_raster),
1481  m_pixelsinpointset(rhs.m_pixelsinpointset),
1482  m_currentpixelindex(rhs.m_currentpixelindex)
1483  {
1484  }
1485 
1487  {
1488  m_pixelsinpointset.clear();
1489  }
1490 
1491  template<typename T> const std::vector<T> te::rst::PointSetIterator<T>::operator*() const
1492  {
1493  std::vector<T> values(this->m_raster->getNumberOfBands());
1494  double value;
1495 
1496  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
1497  {
1498  this->m_raster->getValue(getColumn(), getRow(), value, b);
1499  values[b] = ((T) value);
1500  }
1501 
1502  return values;
1503  }
1504 
1505  template<typename T> T te::rst::PointSetIterator<T>::operator[](const unsigned int i) const
1506  {
1507  this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
1508 
1509  return (T) m_operatorBrackets_value;
1510  }
1511 
1512  template<typename T> std::complex< T > te::rst::PointSetIterator<T>::operator()(const unsigned int i) const
1513  {
1514  this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
1515 
1516  return (std::complex< T >) m_operatorParenthesis_value;
1517  }
1518 
1519  template<typename T> unsigned int te::rst::PointSetIterator<T>::getRow() const
1520  {
1521  return (unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getY());
1522  }
1523 
1524  template<typename T> unsigned int te::rst::PointSetIterator<T>::getColumn() const
1525  {
1526  return (unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getX());
1527  }
1528 
1529  template<typename T> void te::rst::PointSetIterator<T>::operator++()
1530  {
1531  m_currentpixelindex++;
1532 
1533  if (m_currentpixelindex >= (int) m_pixelsinpointset.size())
1534  setEnd();
1535  }
1536 
1537  template<typename T> void te::rst::PointSetIterator<T>::operator--()
1538  {
1539  m_currentpixelindex--;
1540 
1541  if (m_currentpixelindex < 0)
1542  setEnd();
1543  }
1544 
1546  {
1547  if (this != &rhs)
1548  {
1550 
1551  m_pixelsinpointset = rhs.m_pixelsinpointset;
1552  m_currentpixelindex = rhs.m_currentpixelindex;
1553  }
1554 
1555  return *this;
1556  }
1557 
1558  template<typename T>
1560  const AbstractPositionIterator<T>& rhs)
1561  {
1562  return operator=( *((te::rst::PointSetIterator<T>*)&rhs) );
1563  }
1564 
1565  template<typename T> void te::rst::PointSetIterator<T>::setEnd()
1566  {
1567  this->m_currentpixelindex = -1;
1568  }
1569 
1570  template<typename T> te::rst::PointSetIterator<T> te::rst::PointSetIterator<T>::begin(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1571  {
1572  return te::rst::PointSetIterator<T>(r, p);
1573  }
1574 
1575  template<typename T> te::rst::PointSetIterator<T> te::rst::PointSetIterator<T>::end(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1576  {
1578 
1579  it.setEnd();
1580 
1581  return it;
1582  }
1583 
1585  {
1586  return ( (this->m_currentpixelindex != rhs.m_currentpixelindex) );
1587  }
1588 
1589  template<typename T>
1591  const AbstractPositionIterator<T>& rhs) const
1592  {
1593  return operator!=( *((te::rst::PointSetIterator<T>*)&rhs) );
1594  }
1595 
1596  } // end namespace rst
1597 } // end namespace te
1598 
1599 #endif // __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
int m_row
The current row of the iterator (default -1).
void setEnd()
Sets the iterator position to the end of the current band.
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
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.
Polygon tile indexing class for optmized geometrical relational tests.
Definition: TileIndexer.h:54
A Line is LineString with 2 points.
Definition: Line.h:50
unsigned int getNumberOfRows() const
Returns the raster number of rows.
int m_polygonEndingColumn
Polygon last raster column (default: -1).
double y
y-coordinate.
Definition: Coord2D.h:114
It describes one band (or dimension) of a raster.
int m_currentRangeEndingColumn
The current line iteration range column to finalize the iteration (default: -1).
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.
void getBBOXIntersectionRanges(int lineIndex, const bool areDisjoint, std::vector< std::pair< int, int > > &intersectionRanges) const
Returns the given line intersection ranges using the pixel bounding box as reference.
void setEnd()
Sets the iterator position to the end of the current band.
double x
x-coordinate.
Definition: Coord2D.h:113
int m_currentLineIntersectionRangesIndex
The actual line of the iterator (default -1).
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
double m_operatorBrackets_value
Used by the operator[] method.
unsigned int getNumberOfRows() const
Returns the grid number of rows.
virtual void operator--()=0
Returns to the previous position.
bool operator!=(const PointSetIterator< T > &rhs) const
Itaration over pixels whose bounding boxes are disjoint in relation with the polygon.
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...
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
double m_operatorBrackets_value
Used by the operator[] method.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
unsigned int getColumn() const
Returns the current column in iterator.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
bool StdSortPointPointerComparison(te::gm::Point *p1, te::gm::Point *p2)
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
This class is the base for implementing ways to navigate over the band with spatial restriction...
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
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.
TEDATAACCESSEXPORT te::da::Expression * operator!=(const te::da::Expression &e1, const te::da::Expression &e2)
const te::gm::Polygon * m_polygon
The spatial restriction to be applied in the iterator (default 0).
int m_polygonStartingColumn
Polygon first raster column (default: -1).
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
PolygonIterator & operator=(const PolygonIterator &rhs)
PointSetIterator & operator=(const PointSetIterator &rhs)
virtual bool operator!=(const AbstractPositionIterator< T > &rhs) const =0
Difference operator.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
Grid * getGrid()
It returns the raster grid.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
unsigned int getRow() const
Returns the current row in iterator.
Utility functions for dealing with raster data blocks.
virtual void operator++()=0
Advances to the next position.
void operator++()
Advances to the next position.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
std::unique_ptr< Point > getStartPoint() const
The length of this Curve in its associated spatial reference.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
An exception class for the Raster module.
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:240
std::vector< te::gm::Point * > m_pixelsinline
A vector of pixel locations that intersects the line.
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
const te::rst::Raster * m_raster
The band from where to get the values.
int m_totalRasterRows
The number of rows in band (default: 0).
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 te::rst::Raster * m_raster
The band from where to get the values.
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
int m_currentRangeStartingColumn
The current line iteration range starting column (default: -1).
A point with x and y coordinate values.
Definition: Point.h:50
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
An abstract class for raster data strucutures.
Definition: Raster.h:71
void getIntersectionRanges(int lineIndex, std::vector< std::pair< int, int > > &intersectionRanges) const
Returns the given line intersection ranges (Coordinates of the columns to be transversed) using the c...
virtual unsigned int getColumn() const =0
Returns the current column in iterator.
const te::rst::Raster * m_raster
The band from where to get the values (default 0).
unsigned int getColumn() const
Returns the current column in iterator.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
TerraLib.
std::unique_ptr< Point > getEndPoint() const
It returns the curve end point.
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
int m_currentpixelindex
The index of the current pixel location.
void setNumCoordinates(std::size_t size)
It reserves room for the number of coordinates in this LineString.
TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry *g1, const Geometry *g2, SpatialRelation relation)
It returns if two geometries satisfy a given spatial relation.
Itaration over pixels whose bounding boxes are not disjoint in relation with the polygon.
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
void setEnd()
Sets the iterator position to the end of the current band.
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
It gives access to values in one band (dimension) of a raster.
void initiateVariables()
Initialize all internal variables to initial states.
void clear()
Clear all internal allocated objects and reset back to the initial state.
int m_polygonStartingRow
Polygon first raster row (default: -1).
virtual void setEnd()=0
Sets the iterator position to the end of the current band.
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
virtual const std::vector< T > operator*() const =0
Returns a vector of the values in current position (column, row) from iterator.
int getSRID() const
Returns the raster spatial reference system identifier.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:75
void operator--()
Returns to the previous position.
void getScanLineIntersectionRanges(int lineIndex, std::vector< std::pair< int, int > > &intersectionRanges) const
Returns the given line intersection ranges (Coordinates of the columns to be transversed) using the S...
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p, const IterationType iterationType)
Returns an iterator referring to the first value of the band.
IterationType m_iterationType
Current iteration type (default ScanLineIterationT).
std::vector< std::pair< int, int > > m_currentLineIntersectionRanges
Coordinates of the columns to be transversed.
bool operator!=(const LineIterator< T > &rhs) const
virtual Geometry * intersection(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns a geometric object that represents the point set intersection with another geometry...
std::vector< te::gm::Point * > m_pixelsinpointset
The spatial restriction to be applied in the iterator.
unsigned int getColumn() const
Returns the current column in iterator.
virtual ~AbstractPositionIterator()
Destructor.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
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.
void operator--()
Returns to the previous position.
A rectified grid is the spatial support for raster data.
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry *> &geoms)
It will get a GeometryCollection and distribute in a vector.
unsigned int getRow() const
Returns the current row in iterator.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
int m_column
The current column of the iterator (default -1).
std::complex< double > m_operatorParenthesis_value
static LineIterator end(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to after the end of the iterator.
unsigned int getRow() const
Returns the current row in iterator.
bool operator!=(const PolygonIterator< T > &rhs) const
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
std::vector< std::pair< unsigned int, unsigned int > > TileSegIndex
Definition: TileIndexer.h:58
#define TE_LOG_WARN(message)
Use this tag in order to log a message to the TerraLib default logger with the WARN level...
Definition: Logger.h:326
int m_totalRasterColumns
The number of columns in band (default: 0).
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.
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
bool initialize(const te::rst::Raster *r, const te::gm::Polygon *p, const IterationType iterationType)
Initialize this instance.
int m_polygonEndingRow
Polygon last raster row (default: -1).
virtual AbstractPositionIterator< T > & operator=(const AbstractPositionIterator< T > &rhs)
Assignment operator.
LineIterator & operator=(const LineIterator &rhs)
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
std::unique_ptr< te::rst::TileIndexer > m_tileIndexerPtr
Tile indexer used to optimize the geometric operations.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
static LineIterator begin(const te::rst::Raster *r, const te::gm::Line *l)
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.