All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PositionIterator.h
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 "../raster/Band.h"
34 #include "../raster/BandProperty.h"
35 #include "../raster/BlockUtils.h"
36 #include "../raster/Grid.h"
37 
38 // STL
39 #include <iostream>
40 
41 namespace te
42 {
43  namespace rst
44  {
45 // Forward declaration.
46  class Band;
47 
48  /*!
49  \class AbstractPositionIterator
50 
51  \brief This class is the base for implementing ways to navigate over the band with spatial restriction,
52  e.g. through a line, inside a bounding box or polygon, etc.
53 
54  \ingroup rst
55  */
56  template<class T> class AbstractPositionIterator
57  {
58  public:
59 
60  /*! \brief Constructor. */
62 
63  /*!
64  \brief Constructor.
65 
66  \param b The band to iterate.
67  */
69 
70  /*!
71  \brief Copy constructor.
72 
73  \param rhs The right-hand-side copy used to copy from.
74  */
76 
77  /*! \brief Destructor. */
79 
80  /*! \brief Returns a vector of the values in current position (column, row) from iterator. */
81  virtual const std::vector<T> operator*() const = 0;
82 
83  /*!
84  \brief Returns the value in current position (column, row, band) from iterator.
85 
86  \param i The band index.
87 
88  \return The pixel value in current position.
89  */
90  virtual T operator[](const unsigned int i) const = 0;
91 
92  /*! \brief Returns the current row in iterator. */
93  virtual unsigned int getRow() const = 0;
94 
95  /*! \brief Returns the current column in iterator. */
96  virtual unsigned int getColumn() const = 0;
97 
98  /*! \brief Advances to the next position. */
99  virtual void operator++() = 0;
100 
101  /*! \brief Returns to the previous position. */
102  virtual void operator--() = 0;
103 
104  /*!
105  \brief Assignment operator.
106 
107  \param rhs The right-hand-side copy used to copy from.
108 
109  \return A reference to this object.
110  */
112 
113  /*!
114  \brief Difference operator.
115 
116  \param rhs The right-hand side to compare.
117 
118  \return Returns true if the iterators are at different positions, or false otherwise.
119  */
120  virtual bool operator!=(const AbstractPositionIterator& rhs) const;
121 
122  /*!
123  \brief Sets the iterator position to the end of the current band.
124 
125  \param b The band to retrieve the end information.
126  */
127  virtual void setEnd() = 0;
128 
129  protected:
130 
131  const te::rst::Raster* m_raster; //!< The band from where to get the values.
132 
133  };
134 
135  /*!
136  \class PolygonIterator
137 
138  \brief This class implements the strategy to iterate with spatial restriction,
139  the iteration occurs inside a polygon.
140 
141  \ingroup rst
142  */
143  template<class T> class PolygonIterator: public AbstractPositionIterator<T>
144  {
145  public:
146 
147  PolygonIterator();
148 
149  /*!
150  \brief Constructor.
151 
152  \param b The band to iterate.
153  \param p The polygon from where the iteration will navigate.
154  */
155  PolygonIterator(const te::rst::Raster* r, const te::gm::Polygon* p);
156 
157  /*!
158  \brief Copy constructor.
159 
160  \param rhs The right-hand-side copy used to copy from.
161  */
162  PolygonIterator(const PolygonIterator& rhs);
163 
165 
166  /*!
167  \brief Decomponse one geometry collection in a vector of basic components (line, point).
168 
169  \param g The input geometry collection.
170 
171  \return A vector of geometries.
172  */
173  std::vector<te::gm::LineString*> decompose(te::gm::Geometry* g);
174 
175  void setNextLine(bool updatecurrline = true);
176 
177  const std::vector<T> operator*() const;
178 
179  T operator[](const unsigned int i) const;
180 
181  unsigned int getRow() const;
182 
183  unsigned int getColumn() const;
184 
185  void operator++();
186 
187  void operator--();
188 
190 
191  void setEnd();
192 
193  /*! \brief Returns an iterator referring to the first value of the band.*/
194  static PolygonIterator begin(const te::rst::Raster* r, const te::gm::Polygon* p);
195 
196  /*! \brief Returns an iterator referring to after the end of the iterator. */
197  static PolygonIterator end(const te::rst::Raster* r, const te::gm::Polygon* p);
198 
199  bool operator!=(const PolygonIterator<T>& rhs) const;
200 
201  protected:
202 
203  const te::gm::Polygon* m_polygon; //!< The spatial restriction to be applied in the iterator.
204  std::vector<te::gm::LineString*> m_intersections; //!< The points or lines of the intersection between the geometry and the current line.
205  te::gm::Line* m_currline; //!< The current line in the iterator.
206  int m_column; //!< The current column of the iterator.
207  int m_row; //!< The current row of the iterator.
208  int m_startingcolumn; //!< The starting column (in current line) to initialize the iteration.
209  int m_endingcolumn; //!< The column (in current line) to finalize the iteration.
210  int m_startingrow; //!< The starting row of the iteration.
211  int m_endingrow; //!< The ending row of the iteration.
212  int m_maxcolumns; //!< The number of columns in band.
213  int m_maxrows; //!< The number of rows in band.
214  int m_actualintersection; //!< The actual line of the iterator.
215  int m_nintersections; //!< The number number of intersected lines in current line of the iterator.
216 
217  };
218 
219  /*!
220  \class LineIterator
221 
222  \brief This class implements the strategy to iterate with spatial restriction,
223  the iteration occurs inside a line.
224 
225  \ingroup rst
226  */
227  template<class T> class LineIterator: public AbstractPositionIterator<T>
228  {
229  public:
230 
231  LineIterator();
232 
233  /*!
234  \brief Constructor.
235 
236  \param b The band to iterate.
237  \param l The line from where the iteration will navigate.
238  */
239  LineIterator(const te::rst::Raster* r, const te::gm::Line* l);
240 
241  /*!
242  \brief Copy constructor.
243 
244  \param rhs The right-hand-side copy used to copy from.
245  */
246  LineIterator(const LineIterator& rhs);
247 
248  ~LineIterator();
249 
250  const std::vector<T> operator*() const;
251 
252  T operator[](const unsigned int i) const;
253 
254  unsigned int getRow() const;
255 
256  unsigned int getColumn() const;
257 
258  void operator++();
259 
260  void operator--();
261 
262  LineIterator& operator=(const LineIterator& rhs);
263 
264  void setEnd();
265 
266  /*! \brief Returns an iterator referring to the first value of the band.*/
267  static LineIterator begin(const te::rst::Raster* r, const te::gm::Line* l);
268 
269  /*! \brief Returns an iterator referring to after the end of the iterator. */
270  static LineIterator end(const te::rst::Raster* r, const te::gm::Line* l);
271 
272  bool operator!=(const LineIterator<T>& rhs) const;
273 
274  protected:
275 
276  const te::gm::Line* m_line; //!< The spatial restriction to be applied in the iterator.
277  int m_currentpixelindex; //!< The index of the current pixel location.
278  std::vector<te::gm::Point*> m_pixelsinline; //!< A vector of pixel locations that intersects the line.
279 
280  };
281 
282  /*!
283  \class PointSetIterator
284 
285  \brief This class implements the strategy to iterate with spatial restriction,
286  the iteration occurs inside a vector of points.
287 
288  \ingroup rst
289  */
290  template<class T> class PointSetIterator: public AbstractPositionIterator<T>
291  {
292  public:
293 
295 
296  /*!
297  \brief Constructor.
298 
299  \param b The band to iterate.
300  \param p The vector of points where the iteration will navigate.
301  */
302  PointSetIterator(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
303 
304  /*!
305  \brief Copy constructor.
306 
307  \param rhs The right-hand-side copy used to copy from.
308  */
310 
312 
313  const std::vector<T> operator*() const;
314 
315  T operator[](const unsigned int i) const;
316 
317  unsigned int getRow() const;
318 
319  unsigned int getColumn() const;
320 
321  void operator++();
322 
323  void operator--();
324 
326 
327  void setEnd();
328 
329  /*! \brief Returns an iterator referring to the first value of the band.*/
330  static PointSetIterator begin(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
331 
332  /*! \brief Returns an iterator referring to after the end of the iterator. */
333  static PointSetIterator end(const te::rst::Raster* r, const std::vector<te::gm::Point*> p);
334 
335  bool operator!=(const PointSetIterator<T>& rhs) const;
336 
337  protected:
338 
339  std::vector<te::gm::Point*> m_pixelsinpointset; //!< The spatial restriction to be applied in the iterator.
340  int m_currentpixelindex; //!< The index of the current pixel location.
341 
342  };
343 // implementation of abstract position iterator
345  : m_raster(0)
346  {
347  }
348 
350  : m_raster(r)
351  {
352  }
353 
355  : m_raster(rhs.m_raster)
356  {
357  }
358 
360  {
361  }
362 
364  {
365  if (this != &rhs)
366  m_raster = rhs.m_raster;
367 
368  return *this;
369  }
370 
372  {
373  return (m_raster != rhs.m_raster);
374  }
375 
376 // implementation of iteration strategy bounded by a polygon
379  m_polygon(0),
380  m_intersections(0),
381  m_currline(0),
382  m_column(-1),
383  m_row(-1),
384  m_startingcolumn(0),
385  m_endingcolumn(0),
386  m_startingrow(0),
387  m_endingrow(0),
388  m_maxcolumns(0),
389  m_maxrows(0),
390  m_actualintersection(-1),
391  m_nintersections(0)
392  {
393  }
394 
396  : AbstractPositionIterator<T>(r),
397  m_polygon(p),
398  m_intersections(0),
399  m_column(0),
400  m_maxcolumns(r->getNumberOfColumns()),
401  m_maxrows(r->getNumberOfRows()),
402  m_actualintersection(-1),
403  m_nintersections(0)
404  {
407 
408 // defining starting/ending rows
409  m_startingrow = (int) r->getGrid()->geoToGrid(ll.x, ur.y).y;
410  m_endingrow = (int) r->getGrid()->geoToGrid(ll.x, ll.y).y;
411 
412  int tmp;
414  {
415  tmp = m_startingrow;
417  m_endingrow = tmp;
418  }
419 
420 // avoiding bad access
423 
426 
428 
430  te::gm::Point(ur.x, ur.y, m_polygon->getSRID()),
432 
433 // in case of problems, we initialize the first line here
434  m_startingcolumn = 0;
436 
437 // defining starting/ending columns
438  setNextLine(false);
439 
441  }
442 
444  : AbstractPositionIterator<T>(rhs),
445  m_polygon(rhs.m_polygon),
446  m_intersections(rhs.m_intersections),
447  m_currline(rhs.m_currline),
448  m_column(rhs.m_column),
449  m_row(rhs.m_row),
450  m_startingcolumn(rhs.m_startingcolumn),
451  m_endingcolumn(rhs.m_endingcolumn),
452  m_startingrow(rhs.m_startingrow),
453  m_endingrow(rhs.m_endingrow),
454  m_maxcolumns(rhs.m_maxcolumns),
455  m_maxrows(rhs.m_maxrows),
456  m_actualintersection(rhs.m_actualintersection),
457  m_nintersections(rhs.m_nintersections)
458  {
459  }
460 
462  {
463  m_intersections.clear();
464  }
465 
466  template<class T> std::vector<te::gm::LineString*> te::rst::PolygonIterator<T>::decompose(te::gm::Geometry* g)
467  {
468  std::vector<te::gm::LineString*> lines;
469 
470  te::gm::Geometry* ing = g;
472  if (gc->getNumGeometries() == 1)
473  ing = gc->getGeometryN(0);
474 
475 // check if the geometry is a multi line string
477  {
478  te::gm::MultiLineString* mls = static_cast<te::gm::MultiLineString*> (ing);
479 
480  for (unsigned int i = 0; i < mls->getNumGeometries(); i++)
481  {
482  te::gm::LineString* ls = static_cast<te::gm::LineString*> (mls->getGeometryN(i));
483 
484  lines.push_back(ls);
485  }
486  }
487 // check if the geometry is a line
488  else if (ing->getGeomTypeId() == te::gm::LineStringType)
489  {
490  te::gm::LineString* ls = static_cast<te::gm::LineString*> (ing);
491 
492  lines.push_back(ls);
493  }
494 // check if the geometry is a multi point
495  else if (ing->getGeomTypeId() == te::gm::MultiPointType)
496  {
497  te::gm::MultiPoint* mp = static_cast<te::gm::MultiPoint*> (ing);
498 
499  for (unsigned int i = 0; i < mp->getNumGeometries(); i++)
500  {
502  te::gm::Point* pointinter = static_cast<te::gm::Point*> (mp->getGeometryN(i));
503 
504  lineinter->setX(0, pointinter->getX());
505  lineinter->setY(0, pointinter->getY());
506  lineinter->setX(1, pointinter->getX());
507  lineinter->setY(1, pointinter->getY());
508 
509  lines.push_back(lineinter);
510  }
511  }
512 // check if the geometry is a point
513  else if (ing->getGeomTypeId() == te::gm::PointType)
514  {
515  te::gm::Point* p = static_cast<te::gm::Point*> (ing);
516 
518 
519  lineinter->setX(0, p->getX());
520  lineinter->setY(0, p->getY());
521  lineinter->setX(1, p->getX());
522  lineinter->setY(1, p->getY());
523 
524  lines.push_back(lineinter);
525  }
526 // check if the geometry is a geometry collection
528  {
529  for (unsigned int i = 0; i < gc->getNumGeometries(); i++)
530  {
531  std::vector<te::gm::LineString*> vg = decompose(gc->getGeometryN(i));
532 
533  for (unsigned int j = 0; j < vg.size(); j++)
534  lines.push_back(vg[j]);
535  }
536  }
537 // throw exception when other types?
538  else
539  {
540  std::string message = "An exception has occurried in Polygon Iterator, with geometry " + g->toString();
541 
542  throw(message.c_str());
543  }
544 
545 // clean up (?)
546 // delete g;
547 
548  return lines;
549  }
550 
551  template<class T> void te::rst::PolygonIterator<T>::setNextLine(bool updatecurrline)
552  {
553  if (m_actualintersection == -1 || m_actualintersection >= m_nintersections)
554  {
555  if (updatecurrline)
556  {
557  double nexty = this->m_raster->getGrid()->gridToGeo(0, m_row).y;
558 
559  m_currline->setX(0, m_polygon->getMBR()->getLowerLeft().x);
560  m_currline->setX(1, m_polygon->getMBR()->getUpperRight().x);
561  m_currline->setY(0, nexty);
562  m_currline->setY(1, nexty);
563  }
564 
565  te::gm::Geometry* inter;
566 // in some cases the intersection presents an unhandled exception, in this case we do not paint the current line
567  try
568  {
569  inter = m_polygon->intersection(m_currline);
570 
571  if (inter->isEmpty())
572  {
573  delete inter;
574 
575  m_row++;
576  if (m_row > m_endingrow)
577  {
578  setEnd();
579 
580  return;
581  }
582 
583  setNextLine();
584 
585  return;
586  }
587  }
588  catch(const std::exception& e)
589  {
590  std::cout << "Unhandled exception, status:" << std::endl;
591  std::cout << " m_startingcolumn: " << m_startingcolumn << " m_endingcolumn: " << m_endingcolumn << std::endl;
592  std::cout << " m_startingrow: " << m_startingrow << " m_endingrow: " << m_endingrow<< std::endl;
593  std::cout << " m_column: " << m_column << " m_row: " << m_row << std::endl;
594  std::cout << " intersection line: " << m_currline->toString() << std::endl << std::endl;
595  std::cout << " current polygon: " << m_polygon->toString() << std::endl << std::endl;
596  std::cout << " exception message: " << e.what() << std::endl;
597  std::cout << std::endl;
598 
599  operator++();
600 
601  return;
602  }
603 
604  te::gm::GeometryCollection* intersections = new te::gm::GeometryCollection(0, inter->getGeomTypeId(), inter->getSRID());
605 
606  intersections->add(inter);
607 
608  m_actualintersection = 0;
609 
610  m_intersections.clear();
611 
612  m_intersections = decompose(intersections);
613 
614  m_nintersections = m_intersections.size();
615  }
616 
617  te::gm::LineString* lineinter = m_intersections[m_actualintersection];
618 
619  m_startingcolumn = (int) this->m_raster->getGrid()->geoToGrid(lineinter->getStartPoint()->getX(), lineinter->getStartPoint()->getY()).x;
620 
621  m_endingcolumn = (int) this->m_raster->getGrid()->geoToGrid(lineinter->getEndPoint()->getX(), lineinter->getEndPoint()->getY()).x;
622 
623  int tmp;
624  if (m_startingcolumn > m_endingcolumn)
625  {
626  tmp = m_startingcolumn;
627  m_startingcolumn = m_endingcolumn;
628  m_endingcolumn = tmp;
629  }
630 
631 // avoiding bad access
632  m_startingcolumn = m_startingcolumn < 0? 0: m_startingcolumn;
633  m_startingcolumn = m_startingcolumn >= m_maxcolumns? m_maxcolumns - 1: m_startingcolumn;
634 
635  m_endingcolumn = m_endingcolumn < 0? 0: m_endingcolumn;
636  m_endingcolumn = m_endingcolumn >= m_maxcolumns? m_maxcolumns - 1: m_endingcolumn;
637  }
638 
639  template<class T> const std::vector<T> te::rst::PolygonIterator<T>::operator*() const
640  {
641  std::vector<T> values(this->m_raster->getNumberOfBands());
642  double value;
643 
644  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
645  {
646  this->m_raster->getValue(getColumn(), getRow(), value, b);
647  values[b] = ((T) value);
648  }
649 
650  return values;
651  }
652 
653  template<class T> T te::rst::PolygonIterator<T>::operator[](const unsigned int i) const
654  {
655  double value;
656 
657  this->m_raster->getValue(m_column, m_row, value, i);
658 
659  return (T) value;
660  }
661 
662  template<class T> unsigned int te::rst::PolygonIterator<T>::getRow() const
663  {
664  return m_row;
665  }
666 
667  template<class T> unsigned int te::rst::PolygonIterator<T>::getColumn() const
668  {
669  return m_column;
670  }
671 
673  {
674  m_column++;
675 
676  if (m_column > m_endingcolumn)
677  {
678  m_actualintersection++;
679 
680  if (m_actualintersection >= m_nintersections)
681  m_row++;
682 
683  if (m_row > m_endingrow)
684  {
685  setEnd();
686 
687  return;
688  }
689 
690  setNextLine();
691 
692  m_column = m_startingcolumn;
693  }
694  }
695 
697  {
698  m_column--;
699 
700  if (m_column < m_startingcolumn)
701  {
702  m_actualintersection--;
703 
704  if (m_actualintersection < 0)
705  m_row--;
706 
707  if (m_row < m_startingrow)
708  {
709  setEnd();
710 
711  return;
712  }
713 
714  setNextLine();
715 
716  m_column = m_endingcolumn;
717  }
718  }
719 
721  {
722  if (this != &rhs)
723  {
725 
726  m_polygon = rhs.m_polygon;
727  }
728 
729  return *this;
730  }
731 
732  template<class T> void te::rst::PolygonIterator<T>::setEnd()
733  {
734  this->m_column = -1;
735 
736  this->m_row = -1;
737  }
738 
740  {
741  return te::rst::PolygonIterator<T>(r, p);
742  }
743 
745  {
747 
748  it.setEnd();
749 
750  return it;
751  }
752 
754  {
755  return ( (this->m_row != rhs.m_row) && (this->m_column != rhs.m_column));
756  }
757 
758 // implementation of iteration strategy bounded by a line
761  m_line(0),
762  m_currentpixelindex(0),
763  m_pixelsinline(0)
764  {
765  }
766 
768  : AbstractPositionIterator<T>(r),
769  m_line(l),
770  m_currentpixelindex(0),
771  m_pixelsinline(0)
772  {
773  int srid = this->m_raster->getSRID();
774 
775 // make intersection between line and band's envelope
776  te::gm::Geometry* bandEnvelope = te::gm::GetGeomFromEnvelope(this->m_raster->getExtent(), srid);
777  te::gm::Geometry* inter = bandEnvelope->intersection(m_line);
778 
779  if (inter->isEmpty())
780  {
781  setEnd();
782 
783  return;
784  }
785 
786 // create line that intersects only band's envelope
787  te::gm::Line* inrasterline = (te::gm::Line*) inter;
788 
789 // find starting and ending points
790  double startingcolumn;
791  double startingrow;
792  te::gm::Point* startpoint = inrasterline->getStartPoint();
793  this->m_raster->getGrid()->geoToGrid(startpoint->getX(), startpoint->getY(),
794  startingcolumn, startingrow);
795 
796  double endingcolumn;
797  double endingrow;
798  te::gm::Point* endpoint = inrasterline->getEndPoint();
799  this->m_raster->getGrid()->geoToGrid(endpoint->getX(), endpoint->getY(),
800  endingcolumn, endingrow);
801 
802 // creating one envelope per pixel, and intersects with line
803  const double resXdiv2 = this->m_raster->getResolutionX() / 2;
804  const double resYdiv2 = this->m_raster->getResolutionY() / 2;
805  double x1, x2, y1, y2, geoX, geoY;
806  for(int r = (int)startingrow; r <= (int)endingrow; r++)
807  for(int c = (int)startingcolumn; c <= (int)endingcolumn; c++)
808  {
809 // define envelope of pixel
810  this->m_raster->getGrid()->gridToGeo(c, r, geoX, geoY);
811  x1 = geoX - resXdiv2; y1 = geoY - resYdiv2;
812  x2 = geoX + resXdiv2; y2 = geoY + resYdiv2;
813 
814  te::gm::Envelope* pixelbox = new te::gm::Envelope(x1, y1, x2, y2);
815  te::gm::Geometry* pixelboxgeometry = GetGeomFromEnvelope(pixelbox, srid);
816 
817  if (te::gm::SatisfySpatialRelation(inrasterline, pixelboxgeometry, te::gm::INTERSECTS))
818  m_pixelsinline.push_back(new te::gm::Point(c, r, srid));
819  }
820  }
821 
823  : AbstractPositionIterator<T>(rhs),
824  m_currentpixelindex(rhs.m_currentpixelindex),
825  m_pixelsinline(rhs.m_pixelsinline)
826  {
827  }
828 
830  {
831  m_pixelsinline.clear();
832  }
833 
834  template<class T> const std::vector<T> te::rst::LineIterator<T>::operator*() const
835  {
836  std::vector<T> values(this->m_raster->getNumberOfBands());
837  double value;
838 
839  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
840  {
841  this->m_raster->getValue(getColumn(), getRow(), value, b);
842  values[b] = ((T) value);
843  }
844 
845  return values;
846  }
847 
848  template<class T> T te::rst::LineIterator<T>::operator[](const unsigned int i) const
849  {
850  double value;
851 
852  this->m_raster->getValue(getColumn(), getRow(), value, i);
853 
854  return (T) value;
855  }
856  template<class T> unsigned int te::rst::LineIterator<T>::getRow() const
857  {
858  return (unsigned int)(m_pixelsinline[m_currentpixelindex]->getY());
859  }
860 
861  template<class T> unsigned int te::rst::LineIterator<T>::getColumn() const
862  {
863  return (unsigned int)(m_pixelsinline[m_currentpixelindex]->getX());
864  }
865 
866  template<class T> void te::rst::LineIterator<T>::operator++()
867  {
868  m_currentpixelindex++;
869 
870  if (m_currentpixelindex >= (int)(m_pixelsinline.size()))
871  setEnd();
872  }
873 
874  template<class T> void te::rst::LineIterator<T>::operator--()
875  {
876  m_currentpixelindex--;
877 
878  if (m_currentpixelindex < 0)
879  setEnd();
880  }
881 
883  {
884  if (this != &rhs)
885  {
887 
888  m_line = rhs.m_line;
889  m_currentpixelindex = rhs.m_currentpixelindex;
890  m_pixelsinline = rhs.m_pixelsinline;
891  }
892 
893  return *this;
894  }
895 
896  template<class T> void te::rst::LineIterator<T>::setEnd()
897  {
898  this->m_currentpixelindex = -1;
899  }
900 
902  {
903  return te::rst::LineIterator<T>(r, l);
904  }
905 
907  {
908  te::rst::LineIterator<T> it(r, l);
909 
910  it.setEnd();
911 
912  return it;
913  }
914 
915  template<class T> bool te::rst::LineIterator<T>::operator!=(const te::rst::LineIterator<T>& rhs) const
916  {
917  return ( (this->m_currentpixelindex != rhs.m_currentpixelindex) );
918  }
919 
920 // implementation of iteration strategy bounded by a vector of points
923  m_pixelsinpointset(0),
924  m_currentpixelindex(0)
925  {
926  }
927 
928  template<class T> te::rst::PointSetIterator<T>::PointSetIterator(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
929  : AbstractPositionIterator<T>(r),
930  m_pixelsinpointset(p),
931  m_currentpixelindex(0)
932  {
933  int srid = this->m_raster->getSRID();
934 
935  const te::gm::Envelope* rasterbox = r->getExtent();
936  te::gm::Geometry* rasterboxgeometry = GetGeomFromEnvelope(rasterbox, srid);
937 
938 // remove points that are not inside the band's envelope
939  std::vector<te::gm::Point*> inside_points;
940  double column;
941  double row;
942  for (unsigned int i = 0; i < m_pixelsinpointset.size(); i++)
944  {
945  this->m_raster->getGrid()->geoToGrid(m_pixelsinpointset[i]->getX(), m_pixelsinpointset[i]->getY(), column, row);
946 
947  inside_points.push_back(new te::gm::Point(column, row));
948  }
949 
950  m_pixelsinpointset.clear();
951  m_pixelsinpointset = inside_points;
952 
953  if (m_pixelsinpointset.empty())
954  setEnd();
955  }
956 
958  : AbstractPositionIterator<T>(rhs),
959  m_pixelsinpointset(rhs.m_pixelsinpointset),
960  m_currentpixelindex(rhs.m_currentpixelindex)
961  {
962  }
963 
965  {
966  m_pixelsinpointset.clear();
967  }
968 
969  template<class T> const std::vector<T> te::rst::PointSetIterator<T>::operator*() const
970  {
971  std::vector<T> values(this->m_raster->getNumberOfBands());
972  double value;
973 
974  for (unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
975  {
976  this->m_raster->getValue(getColumn(), getRow(), value, b);
977  values[b] = ((T) value);
978  }
979 
980  return values;
981  }
982 
983  template<class T> T te::rst::PointSetIterator<T>::operator[](const unsigned int i) const
984  {
985  double value;
986 
987  this->m_raster->getValue(getColumn(), getRow(), value, i);
988 
989  return (T) value;
990  }
991 
992  template<class T> unsigned int te::rst::PointSetIterator<T>::getRow() const
993  {
994  return (unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getY());
995  }
996 
997  template<class T> unsigned int te::rst::PointSetIterator<T>::getColumn() const
998  {
999  return (unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getX());
1000  }
1001 
1003  {
1004  m_currentpixelindex++;
1005 
1006  if (m_currentpixelindex >= (int) m_pixelsinpointset.size())
1007  setEnd();
1008  }
1009 
1011  {
1012  m_currentpixelindex--;
1013 
1014  if (m_currentpixelindex < 0)
1015  setEnd();
1016  }
1017 
1019  {
1020  if (this != &rhs)
1021  {
1023 
1024  m_pixelsinpointset = rhs.m_pixelsinpointset;
1025  m_currentpixelindex = rhs.m_currentpixelindex;
1026  }
1027 
1028  return *this;
1029  }
1030 
1031  template<class T> void te::rst::PointSetIterator<T>::setEnd()
1032  {
1033  this->m_currentpixelindex = -1;
1034  }
1035 
1036  template<class T> te::rst::PointSetIterator<T> te::rst::PointSetIterator<T>::begin(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1037  {
1038  return te::rst::PointSetIterator<T>(r, p);
1039  }
1040 
1041  template<class T> te::rst::PointSetIterator<T> te::rst::PointSetIterator<T>::end(const te::rst::Raster* r, const std::vector<te::gm::Point*> p)
1042  {
1044 
1045  it.setEnd();
1046 
1047  return it;
1048  }
1049 
1051  {
1052  return ( (this->m_currentpixelindex != rhs.m_currentpixelindex) );
1053  }
1054  } // end namespace rst
1055 } // end namespace te
1056 
1057 #endif // __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
MultiLineString is a MultiCurve whose elements are LineStrings.
void setY(std::size_t i, const double &y)
It sets the n-th y coordinate value.
Definition: LineString.cpp:376
AbstractPositionIterator & operator=(const AbstractPositionIterator &rhs)
Assignment operator.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:35
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
virtual void setEnd()=0
Sets the iterator position to the end of the current band.
unsigned int getRow() const
Returns the current row in iterator.
int m_nintersections
The number number of intersected lines in current line of the iterator.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
te::gm::Line * m_currline
The current line in the iterator.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
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.
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
Definition: Raster.cpp:218
unsigned int getRow() const
Returns the current row in iterator.
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.
double y
y-coordinate.
Definition: Coord2D.h:87
unsigned int getColumn() const
Returns the current column in iterator.
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
T operator[](const unsigned int i) const
Returns the value in current position (column, row, band) from iterator.
void setEnd()
Sets the iterator position to the end of the current band.
std::vector< te::gm::LineString * > decompose(te::gm::Geometry *g)
Decomponse one geometry collection in a vector of basic components (line, point). ...
virtual T operator[](const unsigned int i) const =0
Returns the value in current position (column, row, band) from iterator.
LineIterator & operator=(const LineIterator &rhs)
bool operator!=(const LineIterator< T > &rhs) const
This class is the base for implementing ways to navigate over the band with spatial restriction...
void operator--()
Returns to the previous position.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
bool operator!=(const PointSetIterator< T > &rhs) const
std::vector< te::gm::LineString * > m_intersections
The points or lines of the intersection between the geometry and the current line.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
static LineIterator end(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to after the end of the iterator.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
virtual unsigned int getRow() const =0
Returns the current row in iterator.
int m_currentpixelindex
The index of the current pixel location.
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to the first value of the band.
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
Definition: Envelope.cpp:43
void add(Geometry *g)
It adds the geometry into the collection.
void setEnd()
Sets the iterator position to the end of the current band.
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
virtual bool isEmpty() const
It returns true if this geometric object is the empty Geometry.
Definition: Geometry.cpp:143
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
Definition: Raster.cpp:104
PointSetIterator & operator=(const PointSetIterator &rhs)
TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry *g1, const Geometry *g2, SpatialRelation relation)
It returns if two geometries satisfy a given spatial relation.
Definition: Utils.cpp:53
T operator[](const unsigned int i) const
Returns the value in current position (column, row, band) from iterator.
void setEnd()
Sets the iterator position to the end of the current band.
virtual bool operator!=(const AbstractPositionIterator &rhs) const
Difference operator.
int getSRID() const
Returns the raster spatial reference system identifier.
Definition: Raster.cpp:203
virtual void operator--()=0
Returns to the previous position.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
void operator++()
Advances to the next position.
int m_endingcolumn
The column (in current line) to finalize the iteration.
int m_row
The current row of the iterator.
virtual Geometry * intersection(const Geometry *const rhs) const
It returns a geometric object that represents the point set intersection with another geometry...
Definition: Geometry.cpp:455
A point with x and y coordinate values.
Definition: Point.h:50
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
int m_endingrow
The ending row of the iteration.
unsigned int getRow() const
Returns the current row in iterator.
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
unsigned int getColumn() const
Returns the current column in iterator.
int m_maxcolumns
The number of columns in band.
std::string toString() const
It returns the data value in a WKT representation.
Definition: Geometry.h:858
static LineIterator begin(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to the first value of the band.
void operator++()
Advances to the next position.
int m_startingrow
The starting row of the iteration.
Point * getEndPoint() const
It returns the curve end point.
Definition: LineString.cpp:213
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
bool operator!=(const PolygonIterator< T > &rhs) const
It is a collection of other geometric objects.
int m_startingcolumn
The starting column (in current line) to initialize the iteration.
int m_column
The current column of the iterator.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
Definition: Grid.cpp:302
void setNextLine(bool updatecurrline=true)
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.
virtual void operator++()=0
Advances to the next position.
An abstract class for raster data strucutures.
Definition: Raster.h:70
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:103
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
Definition: Raster.cpp:223
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
int m_currentpixelindex
The index of the current pixel location.
void operator--()
Returns to the previous position.
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
PolygonIterator & operator=(const PolygonIterator &rhs)
const te::rst::Raster * m_raster
The band from where to get the values.
double x
x-coordinate.
Definition: Coord2D.h:86
int m_maxrows
The number of rows in band.
void setX(std::size_t i, const double &x)
It sets the n-th x coordinate value.
Definition: LineString.cpp:370
void operator++()
Advances to the next position.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
int m_actualintersection
The actual line of the iterator.
const te::gm::Line * m_line
The spatial restriction to be applied in the iterator.
Point * getStartPoint() const
It returns the curve start point.
Definition: LineString.cpp:207
std::vector< te::gm::Point * > m_pixelsinpointset
The spatial restriction to be applied in the iterator.
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
Definition: Grid.cpp:308
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
void operator--()
Returns to the previous position.
A Line is LineString with 2 points.
Definition: Line.h:50
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
Definition: Envelope.cpp:38
const te::gm::Polygon * m_polygon
The spatial restriction to be applied in the iterator.
unsigned int getColumn() const
Returns the current column in iterator.
T operator[](const unsigned int i) const
Returns the value in current position (column, row, band) from iterator.
virtual unsigned int getColumn() const =0
Returns the current column in iterator.
std::vector< te::gm::Point * > m_pixelsinline
A vector of pixel locations that intersects the line.