27 #include "../common/progress/TaskProgress.h"
28 #include "../common/Translator.h"
29 #include "../geometry/Coord2D.h"
30 #include "../geometry/Envelope.h"
31 #include "../geometry/Line.h"
32 #include "../geometry/LinearRing.h"
33 #include "../geometry/Point.h"
34 #include "../geometry/Polygon.h"
35 #include "../geometry/Utils.h"
52 assert(b < m_raster->getNumberOfBands());
166 int countObjects = 0;
167 std::vector<unsigned int> indexVec;
176 std::cout <<
"raster bbox: " <<
te::gm::GetGeomFromEnvelope(m_raster->getGrid()->getExtent(), m_raster->getSRID())->toString() << std::endl;
177 for (
unsigned int j = 0; j < m_nLines ; j++)
180 std::cout <<
"analyzing line " << j <<
"/" << m_nLines << std::endl;
183 last_line_ll_point_indexed.
x = 0.0;
184 last_line_ll_point_indexed.
y = ((double) j) - 1.0;
186 last_line_lr_point_indexed.
x = ((double) m_nColumns) - 1.0;
187 last_line_lr_point_indexed.
y = ((double) j) - 1.0;
189 last_line_ll_point = m_raster->getGrid()->gridToGeo(last_line_ll_point_indexed.
x, last_line_ll_point_indexed.
y);
190 last_line_lr_point = m_raster->getGrid()->gridToGeo(last_line_lr_point_indexed.
x, last_line_lr_point_indexed.
y);
193 last_line_lr_point.
x, last_line_lr_point.
y);
196 m_rTreePolygons->search(last_line_bbox, indexVec);
198 for(
unsigned int indexVec_index = 0; indexVec_index < indexVec.size(); indexVec_index++)
201 m_containerPolygons[indexVec[indexVec_index]];
205 if(m_raster->getGrid()->geoToGrid(curr_pol_ll_x, curr_pol_ll_y).y < j)
211 for (
unsigned int i = 0; i < m_nColumns; i++)
213 std::cout <<
" column " << i <<
"/" << m_nColumns << std::endl;
217 te::gm::Coord2D coordWorld = m_raster->getGrid()->gridToGeo(coordMatrix.
x, coordMatrix.
y);
225 m_rTreePolygons->search(boxPoint, indexVec);
227 m_raster->getValue(i, j, val, m_rasterBand);
230 if (!indexVec.empty())
232 unsigned int indexVec_index = 0;
234 while(indexVec_index < indexVec.size())
236 assert(indexVec[indexVec_index] < m_containerPolygons.size());
240 if (val == pol_str_ref.m_value)
243 if (pointWorld.
intersects(&pol_str_ref.m_indexer->getPolygon()))
260 if(detectEdge(i, j, line))
268 unsigned int indexVec_index = 0;
269 while(indexVec_index < indexVec.size())
272 m_containerPolygons[indexVec[indexVec_index]];
298 if (val != m_noDataValue)
303 std::cout <<
"countObjects: " << countObjects << std::endl;
304 std::cout << poly->
toString() << std::endl;
308 m_containerPolygons.push_back(dummy_ps);
309 m_containerPolygons[m_containerPolygons.size() - 1].
reset(*poly, (
int)val,
317 ( (double) poly->operator[](0)->
getNPoints() )
321 m_raster->getResolutionY()
325 m_rTreePolygons->insert(*poly->
getMBR(), m_containerPolygons.size() - 1);
329 std::cout <<
"Error generating polygon - not enough memory" << std::endl;
344 if(countObjects >= ((
int) m_maxPolygons))
356 delete m_rTreePolygons;
360 std::map<double, std::vector<te::gm::Polygon*> > output_polsets;
361 std::map<double, std::vector<te::gm::Polygon*> >::iterator psm_it;
363 std::vector<VectorizerPolygonStructure>::iterator cpit = m_containerPolygons.begin();
364 std::vector<VectorizerPolygonStructure>::iterator cpit_end = m_containerPolygons.end();
369 while(cpit != cpit_end)
371 value = (double) cpit->m_value;
373 if ((!m_useNoData ) || (value != m_noDataValue))
375 psm_it = output_polsets.find(value);
377 if(psm_it == output_polsets.end())
379 std::vector<te::gm::Polygon*> dummy_ps;
380 dummy_ps.push_back(&cpit->m_polygon);
381 std::cout << cpit->m_polygon.toString() << std::endl;
384 output_polsets[value] = dummy_ps;
388 psm_it->second.push_back(&cpit->m_polygon);
398 throw(
"Memory error - error generating output polygons sets map");
403 for (psm_it = output_polsets.begin(); psm_it != output_polsets.end(); ++psm_it)
405 std::cout << psm_it->second[0]->toString() << std::endl;
406 polygons.push_back(psm_it->second[0]);
414 std::cout <<
"detectEdge(" << i <<
"," << j <<
")" << std::endl;
415 assert(startingEdgeTest(i, j));
422 double pol_pixels_value = 0;
424 m_raster->getValue(i, j, pol_pixels_value, m_rasterBand);
428 short curr_dir =
EAST;
429 short new_dir =
EAST;
430 short new_test_start_dir =
EAST;
433 short pinit_leaving_dir =
WEST;
434 bool pinit_leaving_dir_set =
false;
438 int curr_x_index = i;
439 int curr_y_index = j;
440 int next_x_index = i;
441 int next_y_index = j;
443 double curr_pixel_value = 0;
445 bool look_for_next_pixel =
true;
446 bool next_pixel_found =
false;
447 bool must_add_curr_chain_p =
false;
452 while(look_for_next_pixel)
455 new_test_start_dir = (curr_dir + 6) % 8;
456 new_dir = new_test_start_dir;
457 next_pixel_found =
false;
460 next_x_index = curr_x_index + (int) m_directions[new_dir].x;
461 next_y_index = curr_y_index + (int) m_directions[new_dir].y;
463 if ((next_x_index > -1) && (next_y_index > -1) &&
464 (next_x_index < (
long) m_nColumns ) && (next_y_index < (
long) m_nLines))
466 m_raster->getValue(next_x_index, next_y_index, curr_pixel_value, m_rasterBand);
469 if (curr_pixel_value == pol_pixels_value)
471 next_pixel_found =
true;
476 new_dir = (new_dir + 2) % 8;
477 }
while (new_dir != new_test_start_dir);
480 if (!next_pixel_found)
484 curr_chain_p.
x += m_directions[
EAST].x;
485 curr_chain_p.
y += m_directions[
EAST].y;
490 curr_chain_p.
x += m_directions[
SOUTH].x;
491 curr_chain_p.
y += m_directions[
SOUTH].y;
495 curr_chain_p.
x += m_directions[
WEST].x;
496 curr_chain_p.
y += m_directions[
WEST].y;
500 curr_chain_p.
x += m_directions[
NORTH].x;
501 curr_chain_p.
y += m_directions[
NORTH].y;
505 look_for_next_pixel =
false;
507 else if ((curr_x_index == i) && (curr_y_index == j) &&
508 (pinit_leaving_dir == new_dir))
511 switch (curr_pixel_corner)
515 curr_chain_p.
x += m_directions[
SOUTH].x;
516 curr_chain_p.
y += m_directions[
SOUTH].y;
521 curr_chain_p.
x += m_directions[
WEST].x;
522 curr_chain_p.
y += m_directions[
WEST].y;
526 curr_chain_p.
x += m_directions[
NORTH].x;
527 curr_chain_p.
y += m_directions[
NORTH].y;
535 curr_chain_p.
x += m_directions[
WEST].x;
536 curr_chain_p.
y += m_directions[
WEST].y;
540 curr_chain_p.
x += m_directions[
NORTH].x;
541 curr_chain_p.
y += m_directions[
NORTH].y;
549 curr_chain_p.
x += m_directions[
NORTH].x;
550 curr_chain_p.
y += m_directions[
NORTH].y;
563 look_for_next_pixel =
false;
568 if (curr_dir == new_dir)
570 curr_chain_p.
x += m_directions[curr_dir].x;
571 curr_chain_p.
y += m_directions[curr_dir].y;
573 must_add_curr_chain_p =
true;
577 if (must_add_curr_chain_p)
582 must_add_curr_chain_p =
false;
589 switch (curr_pixel_corner)
593 curr_chain_p.
x += m_directions[
EAST].x;
594 curr_chain_p.
y += m_directions[
EAST].y;
607 curr_chain_p.
x += m_directions[
WEST].x;
608 curr_chain_p.
y += m_directions[
WEST].y;
612 curr_chain_p.
x += m_directions[
NORTH].x;
613 curr_chain_p.
y += m_directions[
NORTH].y;
617 curr_chain_p.
x += m_directions[
EAST].x;
618 curr_chain_p.
y += m_directions[
EAST].y;
627 curr_chain_p.
x += m_directions[
NORTH].x;
628 curr_chain_p.
y += m_directions[
NORTH].y;
632 curr_chain_p.
x += m_directions[
EAST].x;
633 curr_chain_p.
x += m_directions[
EAST].x;
642 throw(
"Invalid pixel corner");
651 switch (curr_pixel_corner)
655 curr_chain_p.
x += m_directions[
EAST].x;
656 curr_chain_p.
y += m_directions[
EAST].y;
660 curr_chain_p.
x += m_directions[
SOUTH].x;
661 curr_chain_p.
y += m_directions[
SOUTH].y;
670 curr_chain_p.
x += m_directions[
SOUTH].x;
671 curr_chain_p.
y += m_directions[
SOUTH].y;
684 curr_chain_p.
x += m_directions[
NORTH].x;
685 curr_chain_p.
y += m_directions[
NORTH].y;
689 curr_chain_p.
x += m_directions[
EAST].x;
690 curr_chain_p.
y += m_directions[
EAST].y;
694 curr_chain_p.
x += m_directions[
SOUTH].x;
695 curr_chain_p.
y += m_directions[
SOUTH].y;
704 throw(
"Invalid pixel corner");
713 switch (curr_pixel_corner)
717 curr_chain_p.
x += m_directions[
EAST].x;
718 curr_chain_p.
y += m_directions[
EAST].y;
722 curr_chain_p.
x += m_directions[
SOUTH].x;
723 curr_chain_p.
y += m_directions[
SOUTH].y;
727 curr_chain_p.
x += m_directions[
WEST].x;
728 curr_chain_p.
y += m_directions[
WEST].y;
737 curr_chain_p.
x += m_directions[
SOUTH].x;
738 curr_chain_p.
y += m_directions[
SOUTH].y;
742 curr_chain_p.
x += m_directions[
WEST].x;
743 curr_chain_p.
y += m_directions[
WEST].y;
752 curr_chain_p.
x += m_directions[
WEST].x;
753 curr_chain_p.
y += m_directions[
WEST].y;
766 throw(
"Invalid pixel corner");
775 switch (curr_pixel_corner)
784 curr_chain_p.
x += m_directions[
SOUTH].x;
785 curr_chain_p.
y += m_directions[
SOUTH].y;
789 curr_chain_p.
x += m_directions[
WEST].x;
790 curr_chain_p.
y += m_directions[
WEST].y;
794 curr_chain_p.
x += m_directions[
NORTH].x;
795 curr_chain_p.
y += m_directions[
NORTH].y;
804 curr_chain_p.
x += m_directions[
WEST].x;
805 curr_chain_p.
y += m_directions[
WEST].y;
809 curr_chain_p.
x += m_directions[
NORTH].x;
810 curr_chain_p.
y += m_directions[
NORTH].y;
819 curr_chain_p.
x += m_directions[
NORTH].x;
820 curr_chain_p.
y += m_directions[
NORTH].y;
828 throw(
"Invalid pixel corner");
837 throw(
"Invalid new_dir");
844 if ((!pinit_leaving_dir_set) && (curr_x_index == i) &&
847 pinit_leaving_dir_set =
true;
848 pinit_leaving_dir = new_dir;
852 curr_x_index = next_x_index;
853 curr_y_index = next_y_index;
860 const te::gm::Coord2D chain_init_p = m_raster->getGrid()->gridToGeo(((
double) i) - 0.5, ((
double) j) - 0.5);
862 std::cout <<
"linha antes: ";
863 for (
unsigned int i = 0; i < line.
getNPoints(); i++)
864 std::cout << line.
getX(i) <<
" " << line.
getY(i) <<
", ";
865 std::cout << std::endl;
867 double min_x = std::numeric_limits<double>::max();
868 double min_y = std::numeric_limits<double>::max();
869 double max_x = ( -1.0 ) * std::numeric_limits<double>::max();
870 double max_y = ( -1.0 ) * std::numeric_limits<double>::max();
874 for (
unsigned int i = 0; i < line.
getNPoints(); i++)
876 tmp_x = chain_init_p.
x + (line.
getX(i) * m_resX);
877 tmp_y = chain_init_p.
y - (line.
getY(i) * m_resY);
879 if (tmp_x < min_x ) min_x = tmp_x;
880 if (tmp_y < min_y ) min_y = tmp_y;
882 if (tmp_x > max_x ) max_x = tmp_x;
883 if (tmp_y > max_y ) max_y = tmp_y;
889 std::cout <<
"linha depois: ";
890 for (
unsigned int i = 0; i < line.
getNPoints(); i++)
891 std::cout << line.
getX(i) <<
" " << line.
getY(i) <<
", ";
892 std::cout << std::endl;
922 int nlines = m_raster->getNumberOfRows();
923 int ncols = m_raster->getNumberOfColumns();
928 if ((x == 0) || (y == 0) ||
929 (x == (ncols - 1)) || (y == (nlines - 1)))
934 double current_val = 0;
936 m_raster->getValue(x, y, current_val, m_rasterBand);
937 m_raster->getValue(x - 1, y, test_val, m_rasterBand);
949 delete m_rTreePolygons;
953 m_containerPolygons.clear();
std::size_t getNumRings() const
It returns the number of rings in this CurvePolygon.
void add(Curve *ring)
It adds the ring to the curve polygon.
Polygon tile indexing class for optmized geometrical relational tests.
bool detectEdge(long i, long j, te::gm::LineString &line)
Detects an edge of a cell in Raster.
Vectorizer(Raster *r, std::size_t b, unsigned int mp=0)
Constructor.
void computeMBR(bool cascade) const
It computes the minimum bounding rectangle for the linestring.
It describes one band (or dimension) of a raster.
void setX(std::size_t i, const double &x)
It sets the n-th x coordinate value.
unsigned long m_nColumns
The number of columns.
Point * getEndPoint() const
It returns the curve end point.
virtual bool intersects(const Geometry *const rhs) const
It returns true if the geometry object spatially intersects rhs geometry.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
void makeEmpty()
It clears all the coordinates.
Point * getStartPoint() const
The length of this Curve in its associated spatial reference.
This class can be used to inform the progress of a task.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
double getWidth() const
It returns the envelope width.
te::gm::Polygon m_polygon
The stored polygon instance.
An utility struct for representing 2D coordinates.
bool startingEdgeTest(const int &x, const int &y)
Tests if the current point is a edge start.
virtual bool contains(const Geometry *const rhs) const
It returns true if this geometry object spatially contains rhs geometry.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
#define TE_TR(message)
It marks a string in order to get translated.
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
unsigned long m_nLines
The number of lines.
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
It implements the vectorizer, based on TerraLib 4 algorithm.
te::sam::rtree::Index< unsigned int, 8, 4 > * m_rTreePolygons
A RTree instance pointer to optimize the searching of points inside already created polygons...
void clear()
Clear all internally allocated resources.
te::gm::Coord2D m_directions[8]
Directions vector.
TileIndexer * m_indexer
The polygon tile indexer pointer.
A LinearRing is a LineString that is both closed and simple.
A polygon container node class.
double m_resX
Resolution X.
std::size_t getNPoints() const
it returns the number of points (vertexes) in the geometry.
LineString is a curve with linear interpolation between points.
bool run(std::vector< te::gm::Geometry * > &polygons)
Returns true if current algorithm implementation runs ok, false otherwise.
std::vector< VectorizerPolygonStructure > m_containerPolygons
Vector of all polygons.
A point with x and y coordinate values.
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
unsigned int m_rasterBand
The raster band to be used.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
BandProperty * getProperty()
Returns the band property.
void setNumCoordinates(std::size_t size)
It reserves room for the number of coordinates in this LineString.
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
double m_resY
Resolution Y.
It implements the vectorizer, based on TerraLib 4 algorithm.
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
It gives access to values in one band (dimension) of a raster.
Grid * getGrid()
It returns the raster grid.
void addRing(const unsigned int &ri)
Update the tile index with the information of the supplied ring.
void reset(const te::gm::Polygon &p, const int &v, const double &tidy)
Reset the current instance.
void clear()
Clear all internal resources.
double m_noDataValue
The used dummy value.
bool m_useNoData
Flag indication for dummy value use (rotulated image).
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
A polygon container node class.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
std::string toString() const
It returns the data value in a WKT representation.
A rectified grid is the spatial support for raster data.
Vectorizer & operator=(const Vectorizer &rhs)
Assignment operator.
Configuration flags for the Raster module of TerraLib.
void setY(std::size_t i, const double &y)
It sets the n-th y coordinate value.
const te::gm::Polygon & getPolygon() const
Returns the polygon.
double getHeight() const
It returns the envelope height.
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Curve * getRingN(std::size_t i) const
It returns the n-th ring for this curve polygon as a curve.
Raster * m_raster
The input image.