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