27 #ifndef __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
28 #define __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
31 #include "../common/STLUtils.h"
32 #include "../common/MathUtils.h"
33 #include "../core/logger/Logger.h"
34 #include "../geometry.h"
73 virtual const std::vector<T>
operator*()
const = 0;
84 virtual T
operator[](
const unsigned int i)
const = 0;
95 virtual std::complex< T >
operator()(
const unsigned int i)
const = 0;
98 virtual unsigned int getRow()
const = 0;
101 virtual unsigned int getColumn()
const = 0;
132 virtual void setEnd() = 0;
186 std::complex< T >
operator()(
const unsigned int i)
const;
188 unsigned int getRow()
const;
275 std::complex< T >
operator()(
const unsigned int i)
const;
277 unsigned int getRow()
const;
348 std::complex< T >
operator()(
const unsigned int i)
const;
350 unsigned int getRow()
const;
419 m_actualintersection(-1),
434 m_maxcolumns(r->getNumberOfColumns()),
435 m_maxrows(r->getNumberOfRows()),
436 m_actualintersection(-1),
441 throw te::rst::Exception(
TE_TR(
"Invalid polygon SRID") );
453 ( ll.
x > rasterEnvelope.
m_urx )
455 ( ur.
x < rasterEnvelope.
m_llx )
457 ( ur.
y < rasterEnvelope.
m_lly )
459 ( ll.
y > rasterEnvelope.
m_ury )
507 : m_raster(rhs.m_raster),
518 m_actualintersection(-1),
531 if (m_actualintersection == -1 || m_actualintersection >= m_nintersections)
535 const double& rasterEnvelopeLLX =
536 m_raster->getGrid()->getExtent()->m_llx;
537 const double& rasterEnvelopeURX =
538 m_raster->getGrid()->getExtent()->m_urx;
544 double nexty = this->m_raster->getGrid()->gridToGeo(0, m_row).y;
546 m_currline->setX(0, std::min(
548 m_polygon->getMBR()->getLowerLeft().x) );
549 m_currline->setY(0, nexty);
551 m_currline->setX(1, std::max(
553 m_polygon->getMBR()->getUpperRight().x ) );
554 m_currline->setY(1, nexty);
564 std::vector<te::gm::Point*> intersectionPoints;
571 if (m_tileIndexer->getTile(m_currline->getY(0), ¤tTilePtr))
573 assert(currentTilePtr);
576 std::unique_ptr<te::gm::Line> tileSeg;
577 std::unique_ptr< te::gm::Geometry > interResultPtr;
578 std::vector< te::gm::Geometry * > singleGeomsPtrs;
579 std::size_t singleGeomsPtrsIdx = 0;
588 for (std::size_t i = 0; i < currentTilePtr->size(); i++)
590 assert((*currentTilePtr)[i].first < m_polygon->getNumRings());
593 assert(dynamic_cast<te::gm::LinearRing const*>((*m_polygon)[(*currentTilePtr)[i].first]));
596 assert((*currentTilePtr)[i].second < m_polygon->getNPoints());
600 ringTile->
getY((*currentTilePtr)[i].second),
603 ringTile->
getY((*currentTilePtr)[i].second + 1),
609 interResultPtr.reset( tileSeg->intersection( m_currline ) );
611 if( interResultPtr.get() != 0 )
613 singleGeomsPtrs.clear();
616 for( singleGeomsPtrsIdx = 0 ; singleGeomsPtrsIdx < singleGeomsPtrs.size() ;
617 ++singleGeomsPtrsIdx )
619 if( singleGeomsPtrs[ singleGeomsPtrsIdx ]->getGeomTypeId() ==
623 singleGeomsPtrs[ singleGeomsPtrsIdx ]->clone() ) );
625 else if( singleGeomsPtrs[ singleGeomsPtrsIdx ]->getGeomTypeId() ==
629 singleGeomsPtrsIdx ]);
631 intersectionPoints.push_back( lineStrPtr->
getEndPoint() );
637 catch(
const std::exception& e)
639 TE_LOG_WARN(
"Geometry intersection error:" + e.what() );
643 for( std::size_t intersectionPointsIdx = 0; intersectionPointsIdx <
644 intersectionPoints.size() ; ++intersectionPointsIdx )
646 delete intersectionPoints[intersectionPointsIdx];
649 intersectionPoints.clear();
657 for( std::size_t intersectionPointsIdx = 0; intersectionPointsIdx <
658 intersectionPoints.size() ; ++intersectionPointsIdx )
660 delete intersectionPoints[intersectionPointsIdx];
663 intersectionPoints.clear();
670 std::size_t positionBegin = 0;
671 std::size_t positionEnd = 0;
672 std::vector<te::gm::Point*> intersectionPointsAux = intersectionPoints;
674 intersectionPoints.clear();
676 while( positionBegin < intersectionPointsAux.size() )
678 if( intersectionPointsAux[positionBegin] )
680 positionEnd = positionBegin + 1;
682 while( positionEnd < intersectionPointsAux.size() )
685 ( intersectionPointsAux[positionEnd] != 0 )
687 intersectionPointsAux[positionBegin]->equals(
688 intersectionPointsAux[positionEnd],
true)
691 delete intersectionPointsAux[positionEnd];
692 intersectionPointsAux[positionEnd] = 0;
698 intersectionPoints.push_back( intersectionPointsAux[positionBegin] );
706 std::sort(intersectionPoints.begin(), intersectionPoints.end(),
712 std::size_t positionBegin = 0;
713 std::size_t positionEnd = 0;
716 double startingX = 0;
717 double startingY = 0;
721 while( ( positionBegin + 1 ) < intersectionPoints.size() )
723 positionEnd = positionBegin + 1;
725 startingX = intersectionPoints[positionBegin]->getX();
726 startingX = std::max( startingX, rasterEnvelopeLLX );
728 startingY = intersectionPoints[positionBegin]->getY();
730 endingX = intersectionPoints[positionEnd]->getX();
731 endingX = std::min( endingX, rasterEnvelopeURX );
733 endingY = intersectionPoints[positionEnd]->getY();
740 ( startingX != endingX )
744 (startingX + (endingX - startingX) / 2.0 ),
745 (startingY +(endingY - startingY) / 2.0 ),
746 m_polygon->getSRID()))
753 startingCol = (int)std::ceil( m_raster->getGrid()->geoToGrid(startingX, startingY).x );
754 startingCol = std::max( 0, startingCol );
755 startingCol = std::min( m_maxcolumns - 1, startingCol );
757 endingCol = (int)std::floor( m_raster->getGrid()->geoToGrid(endingX, endingY).x );
758 endingCol = std::max( 0, endingCol );
759 endingCol = std::min( m_maxcolumns - 1, endingCol );
761 if( endingCol >= startingCol )
763 m_columns.push_back(std::pair<int, int>(startingCol, endingCol));
767 positionBegin = positionEnd;
774 while ( positionBegin < intersectionPoints.size() )
776 delete intersectionPoints[positionBegin];
780 if (m_columns.empty())
783 if (m_row > m_endingrow)
795 m_actualintersection = 0;
797 m_nintersections =
static_cast<int>(m_columns.size());
800 m_startingcolumn = this->m_columns[m_actualintersection].first;
801 m_endingcolumn = this->m_columns[m_actualintersection].second;
804 if (m_startingcolumn > m_endingcolumn)
806 tmp = m_startingcolumn;
807 m_startingcolumn = m_endingcolumn;
808 m_endingcolumn = tmp;
821 std::vector<T> values(this->m_raster->getNumberOfBands());
824 for (
unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
826 this->m_raster->getValue(getColumn(), getRow(), value, b);
827 values[b] = ((T) value);
835 this->m_raster->getValue(m_column, m_row, m_operatorBrackets_value, i);
837 return (T) m_operatorBrackets_value;
842 this->m_raster->getValue(m_column, m_row, m_operatorParenthesis_value, i);
844 return (std::complex< T >) m_operatorParenthesis_value;
861 if (m_column > m_endingcolumn)
863 m_actualintersection++;
865 if (m_actualintersection >= m_nintersections)
868 if (m_row > m_endingrow)
877 m_column = m_startingcolumn;
884 throw te::rst::Exception(
TE_TR(
"This operation is not supported!"));
887 if (m_column < m_startingcolumn)
889 m_actualintersection--;
891 if (m_actualintersection < 0)
894 if (m_row < m_startingrow)
903 m_column = m_endingcolumn;
968 return ( (this->m_row != rhs.
m_row) && (this->m_column != rhs.
m_column));
990 m_startingcolumn = 0;
996 m_actualintersection = -1;
997 m_nintersections = 0;
1004 m_currentpixelindex(0),
1012 m_currentpixelindex(0),
1017 throw te::rst::Exception(
TE_TR(
"Invalid line SRID") );
1026 if (inter->isEmpty())
1037 double startingcolumn;
1041 startingcolumn, startingrow);
1043 double endingcolumn;
1047 endingcolumn, endingrow);
1052 double x1, x2, y1, y2, geoX, geoY;
1053 for(
int r = (
int)startingrow; r <= (int)endingrow; r++)
1054 for(
int c = (
int)startingcolumn; c <= (int)endingcolumn; c++)
1058 x1 = geoX - resXdiv2; y1 = geoY - resYdiv2;
1059 x2 = geoX + resXdiv2; y2 = geoY + resYdiv2;
1073 : m_raster(rhs.m_raster),
1074 m_currentpixelindex(rhs.m_currentpixelindex),
1075 m_pixelsinline(rhs.m_pixelsinline)
1081 m_pixelsinline.clear();
1086 std::vector<T> values(this->m_raster->getNumberOfBands());
1089 for (
unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
1091 this->m_raster->getValue(getColumn(), getRow(), value, b);
1092 values[b] = ((T) value);
1100 this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
1102 return (T) m_operatorBrackets_value;
1107 this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
1109 return (std::complex< T >) m_operatorParenthesis_value;
1114 return (
unsigned int)(m_pixelsinline[m_currentpixelindex]->getY());
1119 return (
unsigned int)(m_pixelsinline[m_currentpixelindex]->getX());
1124 m_currentpixelindex++;
1126 if (m_currentpixelindex >= (
int)(m_pixelsinline.size()))
1132 m_currentpixelindex--;
1134 if (m_currentpixelindex < 0)
1152 template<
typename T>
1161 this->m_currentpixelindex = -1;
1183 template<
typename T>
1193 m_pixelsinpointset(0),
1194 m_currentpixelindex(0)
1200 m_pixelsinpointset(p),
1201 m_currentpixelindex(0)
1209 std::vector<te::gm::Point*> inside_points;
1216 throw te::rst::Exception(
TE_TR(
"Invalid point SRID") );
1235 : m_raster(rhs.m_raster),
1236 m_pixelsinpointset(rhs.m_pixelsinpointset),
1237 m_currentpixelindex(rhs.m_currentpixelindex)
1243 m_pixelsinpointset.clear();
1248 std::vector<T> values(this->m_raster->getNumberOfBands());
1251 for (
unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
1253 this->m_raster->getValue(getColumn(), getRow(), value, b);
1254 values[b] = ((T) value);
1262 this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
1264 return (T) m_operatorBrackets_value;
1269 this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
1271 return (std::complex< T >) m_operatorParenthesis_value;
1276 return (
unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getY());
1281 return (
unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getX());
1286 m_currentpixelindex++;
1288 if (m_currentpixelindex >= (
int) m_pixelsinpointset.size())
1294 m_currentpixelindex--;
1296 if (m_currentpixelindex < 0)
1313 template<
typename T>
1322 this->m_currentpixelindex = -1;
1344 template<
typename T>
1354 #endif // __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H
virtual Geometry * intersection(const Geometry *const rhs) const
It returns a geometric object that represents the point set intersection with another geometry...
unsigned int getNumberOfRows() const
Returns the grid number of rows.
int m_row
The current row of the iterator.
void setEnd()
Sets the iterator position to the end of the current band.
virtual T operator[](const unsigned int i) const =0
Returns the real value in current position (column, row, band) from iterator.
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
Point * getStartPoint() const
The length of this Curve in its associated spatial reference.
int m_startingcolumn
The starting column (in current line) to initialize the iteration.
int m_startingrow
The starting row of the iteration.
Polygon tile indexing class for optmized geometrical relational tests.
A Line is LineString with 2 points.
It describes one band (or dimension) of a raster.
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
virtual bool operator!=(const AbstractPositionIterator< T > &rhs) const =0
Difference operator.
const te::gm::Line * m_line
The spatial restriction to be applied in the iterator.
int m_nintersections
The number number of intersected lines in current line of the iterator.
void setEnd()
Sets the iterator position to the end of the current band.
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
te::gm::Line * m_currline
The current line in the iterator.
double m_operatorBrackets_value
Used by the operator[] method.
virtual void operator--()=0
Returns to the previous position.
int m_maxcolumns
The number of columns in band.
bool operator!=(const PolygonIterator< T > &rhs) const
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
unsigned int getColumn() const
Returns the current column in iterator.
int m_currentpixelindex
The index of the current pixel location.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
double m_urx
Upper right corner x-coordinate.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
double m_operatorBrackets_value
Used by the operator[] method.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
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.
bool StdSortPointPointerComparison(te::gm::Point *p1, te::gm::Point *p2)
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.
bool operator!=(const PointSetIterator< T > &rhs) const
void operator++()
Advances to the next position.
TEDATAACCESSEXPORT te::da::Expression * operator!=(const te::da::Expression &e1, const te::da::Expression &e2)
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.
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
virtual std::complex< T > operator()(const unsigned int i) const =0
Returns the complex value in current position (column, row, band) from iterator.
PolygonIterator & operator=(const PolygonIterator &rhs)
PointSetIterator & operator=(const PointSetIterator &rhs)
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
int m_endingrow
The ending row of the iteration.
Grid * getGrid()
It returns the raster grid.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
int m_endingcolumn
The column (in current line) to finalize the iteration.
AbstractPositionIterator()
Constructor.
Utility functions for dealing with raster data blocks.
void setNextLine(bool updatecurrline=true)
virtual void operator++()=0
Advances to the next position.
void operator++()
Advances to the next position.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
A LinearRing is a LineString that is both closed and simple.
An exception class for the Raster module.
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
std::vector< te::gm::Point * > m_pixelsinline
A vector of pixel locations that intersects the line.
double m_llx
Lower left corner x-coordinate.
LineString is a curve with linear interpolation between points.
const te::rst::Raster * m_raster
The band from where to get the values.
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.
const double & getY() const
It returns the Point y-coordinate value.
A point with x and y coordinate values.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
const te::rst::Raster * m_raster
The band from where to get the values.
int m_currentpixelindex
The index of the current pixel location.
TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry *g1, const Geometry *g2, SpatialRelation relation)
It returns if two geometries satisfy a given spatial relation.
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
int m_maxrows
The number of rows in band.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
void setEnd()
Sets the iterator position to the end of the current band.
It gives access to values in one band (dimension) of a raster.
void clear()
Clear all internal allocated objects and reset back to the initial state.
virtual void setEnd()=0
Sets the iterator position to the end of the current band.
virtual const std::vector< T > operator*() const =0
Returns a vector of the values in current position (column, row) from iterator.
double m_operatorBrackets_value
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void operator--()
Returns to the previous position.
Point * getEndPoint() const
It returns the curve end point.
double m_lly
Lower left corner y-coordinate.
te::dt::AbstractData * clone() const
It clones the line.
int getSRID() const
Returns the raster spatial reference system identifier.
std::vector< te::gm::Point * > m_pixelsinpointset
The spatial restriction to be applied in the iterator.
virtual ~AbstractPositionIterator()
Destructor.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
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.
double m_ury
Upper right corner y-coordinate.
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.
unsigned int getColumn() const
Returns the current column in iterator.
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.
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
A rectified grid is the spatial support for raster data.
unsigned int getRow() const
Returns the current row in iterator.
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
std::vector< std::pair< int, int > > m_columns
Coordinates of the columns to be transversed.
TEGEOMEXPORT void Multi2Single(te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
virtual unsigned int getRow() const =0
Returns the current row in iterator.
std::vector< std::pair< unsigned int, unsigned int > > TileSegIndex
#define TE_LOG_WARN(message)
Use this tag in order to log a message to the TerraLib default logger with the WARN level...
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
void operator++()
Advances to the next position.
virtual unsigned int getColumn() const =0
Returns the current column in iterator.
void operator--()
Returns to the previous position.
unsigned int getColumn() const
Returns the current column in iterator.
A rectified grid is the spatial support for raster data.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
virtual AbstractPositionIterator< T > & operator=(const AbstractPositionIterator< T > &rhs)
Assignment operator.
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.
std::unique_ptr< te::rst::TileIndexer > m_tileIndexer
Tile indexer used to optimize the geometric operations.
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.