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 for(
auto p : singleGeomsPtrs)
615 singleGeomsPtrs.clear();
618 for( singleGeomsPtrsIdx = 0 ; singleGeomsPtrsIdx < singleGeomsPtrs.size() ;
619 ++singleGeomsPtrsIdx )
621 if( singleGeomsPtrs[ singleGeomsPtrsIdx ]->getGeomTypeId() ==
625 singleGeomsPtrs[ singleGeomsPtrsIdx ]->clone() ) );
627 else if( singleGeomsPtrs[ singleGeomsPtrsIdx ]->getGeomTypeId() ==
631 singleGeomsPtrsIdx ]);
632 intersectionPoints.push_back( lineStrPtr->
getStartPoint().release() );
633 intersectionPoints.push_back( lineStrPtr->
getEndPoint().release() );
639 catch(
const std::exception& e)
641 TE_LOG_WARN(
"Geometry intersection error:" + e.what() );
645 for( std::size_t intersectionPointsIdx = 0; intersectionPointsIdx <
646 intersectionPoints.size() ; ++intersectionPointsIdx )
648 delete intersectionPoints[intersectionPointsIdx];
651 intersectionPoints.clear();
659 for( std::size_t intersectionPointsIdx = 0; intersectionPointsIdx <
660 intersectionPoints.size() ; ++intersectionPointsIdx )
662 delete intersectionPoints[intersectionPointsIdx];
665 intersectionPoints.clear();
668 for(
auto p : singleGeomsPtrs)
670 singleGeomsPtrs.clear();
676 std::size_t positionBegin = 0;
677 std::size_t positionEnd = 0;
678 std::vector<te::gm::Point*> intersectionPointsAux = intersectionPoints;
680 intersectionPoints.clear();
682 while( positionBegin < intersectionPointsAux.size() )
684 if( intersectionPointsAux[positionBegin] )
686 positionEnd = positionBegin + 1;
688 while( positionEnd < intersectionPointsAux.size() )
691 ( intersectionPointsAux[positionEnd] != 0 )
693 intersectionPointsAux[positionBegin]->equals(
694 intersectionPointsAux[positionEnd],
true)
697 delete intersectionPointsAux[positionEnd];
698 intersectionPointsAux[positionEnd] = 0;
704 intersectionPoints.push_back( intersectionPointsAux[positionBegin] );
712 std::sort(intersectionPoints.begin(), intersectionPoints.end(),
718 std::size_t positionBegin = 0;
719 std::size_t positionEnd = 0;
722 double startingX = 0;
723 double startingY = 0;
727 while( ( positionBegin + 1 ) < intersectionPoints.size() )
729 positionEnd = positionBegin + 1;
731 startingX = intersectionPoints[positionBegin]->getX();
732 startingX = std::max( startingX, rasterEnvelopeLLX );
734 startingY = intersectionPoints[positionBegin]->getY();
736 endingX = intersectionPoints[positionEnd]->getX();
737 endingX = std::min( endingX, rasterEnvelopeURX );
739 endingY = intersectionPoints[positionEnd]->getY();
746 ( startingX != endingX )
750 (startingX + (endingX - startingX) / 2.0 ),
751 (startingY +(endingY - startingY) / 2.0 ),
752 m_polygon->getSRID()))
759 startingCol = (int)std::ceil( m_raster->getGrid()->geoToGrid(startingX, startingY).x );
760 startingCol = std::max( 0, startingCol );
761 startingCol = std::min( m_maxcolumns - 1, startingCol );
763 endingCol = (int)std::floor( m_raster->getGrid()->geoToGrid(endingX, endingY).x );
764 endingCol = std::max( 0, endingCol );
765 endingCol = std::min( m_maxcolumns - 1, endingCol );
767 if( endingCol >= startingCol )
769 m_columns.push_back(std::pair<int, int>(startingCol, endingCol));
773 positionBegin = positionEnd;
780 while ( positionBegin < intersectionPoints.size() )
782 delete intersectionPoints[positionBegin];
786 if (m_columns.empty())
789 if (m_row > m_endingrow)
801 m_actualintersection = 0;
803 m_nintersections =
static_cast<int>(m_columns.size());
806 m_startingcolumn = this->m_columns[m_actualintersection].first;
807 m_endingcolumn = this->m_columns[m_actualintersection].second;
810 if (m_startingcolumn > m_endingcolumn)
812 tmp = m_startingcolumn;
813 m_startingcolumn = m_endingcolumn;
814 m_endingcolumn = tmp;
827 std::vector<T> values(this->m_raster->getNumberOfBands());
830 for (
unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
832 this->m_raster->getValue(getColumn(), getRow(), value, b);
833 values[b] = ((T) value);
841 this->m_raster->getValue(m_column, m_row, m_operatorBrackets_value, i);
843 return (T) m_operatorBrackets_value;
848 this->m_raster->getValue(m_column, m_row, m_operatorParenthesis_value, i);
850 return (std::complex< T >) m_operatorParenthesis_value;
867 if (m_column > m_endingcolumn)
869 m_actualintersection++;
871 if (m_actualintersection >= m_nintersections)
874 if (m_row > m_endingrow)
883 m_column = m_startingcolumn;
890 throw te::rst::Exception(
TE_TR(
"This operation is not supported!"));
893 if (m_column < m_startingcolumn)
895 m_actualintersection--;
897 if (m_actualintersection < 0)
900 if (m_row < m_startingrow)
909 m_column = m_endingcolumn;
978 return ( (this->m_row != rhs.
m_row) && (this->m_column != rhs.
m_column));
1001 m_startingcolumn = 0;
1007 m_actualintersection = -1;
1008 m_nintersections = 0;
1009 m_tileIndexer.reset();
1017 m_currentpixelindex(0),
1025 m_currentpixelindex(0),
1030 throw te::rst::Exception(
TE_TR(
"Invalid line SRID") );
1039 if (inter->isEmpty())
1050 double startingcolumn;
1052 std::unique_ptr<te::gm::Point> startpoint = inrasterline->
getStartPoint();
1054 startingcolumn, startingrow);
1056 double endingcolumn;
1058 std::unique_ptr<te::gm::Point> endpoint = inrasterline->
getEndPoint();
1060 endingcolumn, endingrow);
1065 double x1, x2, y1, y2, geoX, geoY;
1066 for(
int r = (
int)startingrow; r <= (int)endingrow; r++)
1067 for(
int c = (
int)startingcolumn; c <= (int)endingcolumn; c++)
1071 x1 = geoX - resXdiv2; y1 = geoY - resYdiv2;
1072 x2 = geoX + resXdiv2; y2 = geoY + resYdiv2;
1086 : m_raster(rhs.m_raster),
1087 m_currentpixelindex(rhs.m_currentpixelindex),
1088 m_pixelsinline(rhs.m_pixelsinline)
1094 m_pixelsinline.clear();
1099 std::vector<T> values(this->m_raster->getNumberOfBands());
1102 for (
unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
1104 this->m_raster->getValue(getColumn(), getRow(), value, b);
1105 values[b] = ((T) value);
1113 this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
1115 return (T) m_operatorBrackets_value;
1120 this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
1122 return (std::complex< T >) m_operatorParenthesis_value;
1127 return (
unsigned int)(m_pixelsinline[m_currentpixelindex]->getY());
1132 return (
unsigned int)(m_pixelsinline[m_currentpixelindex]->getX());
1137 m_currentpixelindex++;
1139 if (m_currentpixelindex >= (
int)(m_pixelsinline.size()))
1145 m_currentpixelindex--;
1147 if (m_currentpixelindex < 0)
1165 template<
typename T>
1174 this->m_currentpixelindex = -1;
1196 template<
typename T>
1206 m_pixelsinpointset(0),
1207 m_currentpixelindex(0)
1213 m_pixelsinpointset(p),
1214 m_currentpixelindex(0)
1222 std::vector<te::gm::Point*> inside_points;
1229 throw te::rst::Exception(
TE_TR(
"Invalid point SRID") );
1248 : m_raster(rhs.m_raster),
1249 m_pixelsinpointset(rhs.m_pixelsinpointset),
1250 m_currentpixelindex(rhs.m_currentpixelindex)
1256 m_pixelsinpointset.clear();
1261 std::vector<T> values(this->m_raster->getNumberOfBands());
1264 for (
unsigned int b = 0; b < this->m_raster->getNumberOfBands(); b++)
1266 this->m_raster->getValue(getColumn(), getRow(), value, b);
1267 values[b] = ((T) value);
1275 this->m_raster->getValue(getColumn(), getRow(), m_operatorBrackets_value, i);
1277 return (T) m_operatorBrackets_value;
1282 this->m_raster->getValue(getColumn(), getRow(), m_operatorParenthesis_value, i);
1284 return (std::complex< T >) m_operatorParenthesis_value;
1289 return (
unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getY());
1294 return (
unsigned int)(m_pixelsinpointset[m_currentpixelindex]->getX());
1299 m_currentpixelindex++;
1301 if (m_currentpixelindex >= (
int) m_pixelsinpointset.size())
1307 m_currentpixelindex--;
1309 if (m_currentpixelindex < 0)
1326 template<
typename T>
1335 this->m_currentpixelindex = -1;
1357 template<
typename T>
1367 #endif // __TERRALIB_RASTER_INTERNAL_POSITIONITERATOR_H int m_row
The current row of the iterator.
void setEnd()
Sets the iterator position to the end of the current band.
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
static PointSetIterator begin(const te::rst::Raster *r, const std::vector< te::gm::Point *> p)
Returns an iterator referring to the first value of the band.
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.
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.
te::gm::Line * m_currline
The current line in the iterator.
double m_operatorBrackets_value
Used by the operator[] method.
unsigned int getNumberOfRows() const
Returns the grid number of rows.
virtual void operator--()=0
Returns to the previous position.
int m_maxcolumns
The number of columns in band.
bool operator!=(const PointSetIterator< T > &rhs) const
te::dt::AbstractData * clone() const
It clones the line.
int m_currentpixelindex
The index of the current pixel location.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
virtual unsigned int getRow() const =0
Returns the current row in iterator.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
double m_urx
Upper right corner x-coordinate.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
double m_operatorBrackets_value
Used by the operator[] method.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
unsigned int getColumn() const
Returns the current column in iterator.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
bool StdSortPointPointerComparison(te::gm::Point *p1, te::gm::Point *p2)
This class is the base for implementing ways to navigate over the band with spatial restriction...
An utility struct for representing 2D coordinates.
void operator++()
Advances to the next position.
virtual std::complex< T > operator()(const unsigned int i) const =0
Returns the complex value in current position (column, row, band) from iterator.
TEDATAACCESSEXPORT te::da::Expression * operator!=(const te::da::Expression &e1, const te::da::Expression &e2)
int m_actualintersection
The actual line of the iterator.
const te::gm::Polygon * m_polygon
The spatial restriction to be applied in the iterator.
#define TE_TR(message)
It marks a string in order to get translated.
PolygonIterator & operator=(const PolygonIterator &rhs)
PointSetIterator & operator=(const PointSetIterator &rhs)
virtual bool operator!=(const AbstractPositionIterator< T > &rhs) const =0
Difference operator.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
int m_endingrow
The ending row of the iteration.
Grid * getGrid()
It returns the raster grid.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
int m_endingcolumn
The column (in current line) to finalize the iteration.
unsigned int getRow() const
Returns the current row in iterator.
AbstractPositionIterator()
Constructor.
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
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.
A LinearRing is a LineString that is both closed and simple.
std::unique_ptr< Point > getStartPoint() const
The length of this Curve in its associated spatial reference.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
An exception class for the Raster module.
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
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.
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
A point with x and y coordinate values.
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
An Envelope defines a 2D rectangular region.
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
An abstract class for raster data strucutures.
virtual unsigned int getColumn() const =0
Returns the current column in iterator.
const te::rst::Raster * m_raster
The band from where to get the values.
unsigned int getColumn() const
Returns the current column in iterator.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
std::unique_ptr< Point > getEndPoint() const
It returns the curve end point.
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
int m_currentpixelindex
The index of the current pixel location.
TEGEOMEXPORT bool SatisfySpatialRelation(const Geometry *g1, const Geometry *g2, SpatialRelation relation)
It returns if two geometries satisfy a given spatial relation.
std::complex< double > m_operatorParenthesis_value
Used by the operator() method.
int m_maxrows
The number of rows in band.
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
void setEnd()
Sets the iterator position to the end of the current band.
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
It gives access to values in one band (dimension) of a raster.
void 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.
int getSRID() const
Returns the raster spatial reference system identifier.
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
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.
double m_lly
Lower left corner y-coordinate.
bool operator!=(const LineIterator< T > &rhs) const
virtual Geometry * intersection(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns a geometric object that represents the point set intersection with another geometry...
std::vector< te::gm::Point * > m_pixelsinpointset
The spatial restriction to be applied in the iterator.
unsigned int getColumn() const
Returns the current column in iterator.
virtual ~AbstractPositionIterator()
Destructor.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
static PointSetIterator end(const te::rst::Raster *r, const std::vector< te::gm::Point *> p)
Returns an iterator referring to after the end of the iterator.
void operator--()
Returns to the previous position.
A rectified grid is the spatial support for raster data.
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry *> &geoms)
It will get a GeometryCollection and distribute in a vector.
unsigned int getRow() const
Returns the current row in iterator.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.
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.
int m_column
The current column of the 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.
unsigned int getRow() const
Returns the current row in iterator.
bool operator!=(const PolygonIterator< T > &rhs) const
std::vector< std::pair< int, int > > m_columns
Coordinates of the columns to be transversed.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
std::vector< std::pair< unsigned int, unsigned int > > TileSegIndex
#define TE_LOG_WARN(message)
Use this tag in order to log a message to the TerraLib default logger with the WARN level...
void operator++()
Advances to the next position.
virtual T operator[](const unsigned int i) const =0
Returns the real value in current position (column, row, band) from iterator.
void operator--()
Returns to the previous position.
A rectified grid is the spatial support for raster data.
virtual AbstractPositionIterator< T > & operator=(const AbstractPositionIterator< T > &rhs)
Assignment operator.
LineIterator & operator=(const LineIterator &rhs)
T operator[](const unsigned int i) const
Returns the real value in current position (column, row, band) from iterator.
std::unique_ptr< te::rst::TileIndexer > m_tileIndexer
Tile indexer used to optimize the geometric operations.
std::complex< T > operator()(const unsigned int i) const
Returns the complex value in current position (column, row, band) from iterator.
static LineIterator begin(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to the first value of the band.
const std::vector< T > operator*() const
Returns a vector of the values in current position (column, row) from iterator.