29 #include "../../../../src/terralib/core/translator/Translator.h" 30 #include "../../../../src/terralib/common/progress/TaskProgress.h" 31 #include "../../../../src/terralib/core/logger/Logger.h" 33 #include "../../../../src/terralib/dataaccess/dataset/DataSetTypeConverter.h" 34 #include "../../../../src/terralib/dataaccess/dataset/DataSetTypeCapabilities.h" 36 #include "../../../../src/terralib/dataaccess/datasource/DataSource.h" 37 #include "../../../../src/terralib/dataaccess/datasource/DataSourceFactory.h" 38 #include "../../../../src/terralib/dataaccess/datasource/DataSourceInfo.h" 39 #include "../../../../src/terralib/dataaccess/datasource/DataSourceManager.h" 40 #include "../../../../src/terralib/dataaccess/query/EqualTo.h" 42 #include "../../../../src/terralib/datatype/Property.h" 43 #include "../../../../src/terralib/datatype/SimpleProperty.h" 44 #include "../../../../src/terralib/datatype/StringProperty.h" 46 #include "../../../../src/terralib/geometry/Geometry.h" 47 #include "../../../../src/terralib/geometry/GeometryProperty.h" 49 #include "../../../../src/terralib/memory/DataSet.h" 50 #include "../../../../src/terralib/memory/DataSetItem.h" 52 #include "../../../../src/terralib/raster/RasterProperty.h" 53 #include "../../../../src/terralib/raster/RasterFactory.h" 54 #include "../../../../src/terralib/raster/Utils.h" 55 #include "../../../../src/terralib/raster/BandProperty.h" 56 #include "../../../../src/terralib/geometry/Point.h" 57 #include "../../../../src/terralib/common/UnitsOfMeasureManager.h" 58 #include "../../../../src/terralib/srs/SpatialReferenceSystemManager.h" 59 #include "../../../../src/terralib/statistics/core/Enums.h" 61 #include "../../mnt/core/Utils.h" 68 #include <boost/lexical_cast.hpp> 109 std::unique_ptr<te::rst::Raster> raster = dsRaster->getRaster(rasterProp->
getName());
115 std::string timeResult =
"Create Isolines Grid - Start.";
116 #ifdef TERRALIB_LOGGER_ENABLED 124 std::vector<std::vector<te::gm::LineString*> > vecSegments;
126 for (
unsigned int i = 0; i <
m_values.size(); i++)
128 std::vector<te::gm::LineString*> vecLine;
129 vecSegments.push_back(vecLine);
132 unsigned int numRows = raster->getNumberOfRows();
133 unsigned int numThreads = 8;
134 std::vector<RasterBlockSize> vecBlocks =
calculateBlocks(numRows, numThreads);
136 std::vector<GenerateSegmentsParams*> vecGenerateParams;
138 for (
unsigned int i = 0; i < numThreads; ++i)
146 for (
unsigned int vg = 0; vg < vecGenerateParams.size(); ++vg)
149 const std::vector<std::vector<te::gm::LineString*> >& allQuotas = currentBlock->
m_vecSegments;
151 for (
size_t quota = 0; quota < allQuotas.size(); ++quota)
153 const std::vector<te::gm::LineString*>& allSegments = allQuotas[quota];
154 for (
size_t seg = 0; seg < allSegments.size(); ++seg)
156 vecSegments[quota].push_back(allSegments[seg]);
161 for (
unsigned int i = 0; i < numThreads; ++i)
163 delete vecGenerateParams[i];
165 vecGenerateParams.clear();
167 std::vector<ConnectLinesParams> vecParams;
169 boost::thread_group thread;
171 for (
unsigned int idQuota = 0; idQuota < vecParams.size(); ++idQuota)
173 vecParams[idQuota].m_quota =
m_values[idQuota];
174 vecParams[idQuota].m_vecSegments = vecSegments[idQuota];
175 vecParams[idQuota].m_srid =
m_srid;
180 std::vector< std::unique_ptr< te::gm::LineString> > lsOut;
181 for (
unsigned int idQuota = 0; idQuota < vecParams.size(); ++idQuota)
183 for (
unsigned int i = 0; i < vecParams[idQuota].m_lsOut.size(); ++i)
185 std::unique_ptr< te::gm::LineString> out_iso(
new te::gm::LineString(*vecParams[idQuota].m_lsOut[i]));
186 lsOut.push_back(std::move(out_iso));
190 timeResult =
"Create Isolines Grid - End.";
191 #ifdef TERRALIB_LOGGER_ENABLED 195 std::vector<double> guidevalues;
200 for (
size_t i = 0; i <
m_values.size(); ++i)
202 for (
size_t ii = 0; ii < vecSegments[i].size(); ++ii)
207 vecSegments[i].clear();
217 boost::thread_group threadGenerateSegments;
219 int steps = (
int)vecBlocks.size();
223 for (std::size_t vb = 0; vb < vecBlocks.size(); ++vb)
229 te::gm::Coord2D coordLowerLeft = raster->getGrid()->gridToGeo(0, vecBlocks[vb].m_finalRow);
230 te::gm::Coord2D coordUpperRight = raster->getGrid()->gridToGeo(raster->getNumberOfColumns() - 1, vecBlocks[vb].m_initalRow);
233 double xmin = coordLowerLeft.
getX() - (raster->getResolutionX() / 2);
234 double ymin = coordLowerLeft.
getY() - (raster->getResolutionY() / 2);
236 double xmax = coordUpperRight.
getX() + (raster->getResolutionX() / 2);
237 double ymax = coordUpperRight.
getY() + (raster->getResolutionY() / 2);
239 std::map<std::string, std::string> rinfo;
241 rinfo[
"MEM_RASTER_NROWS"] = boost::lexical_cast<std::string>(vecBlocks[vb].m_numRows);
242 rinfo[
"MEM_RASTER_NCOLS"] = boost::lexical_cast<std::string>(raster->getNumberOfColumns());
243 rinfo[
"MEM_RASTER_DATATYPE"] = boost::lexical_cast<std::string>(raster->getBandDataType(0));
244 rinfo[
"MEM_RASTER_NBANDS"] = boost::lexical_cast<std::string>(raster->getNumberOfBands());
245 rinfo[
"MEM_RASTER_SRID"] = boost::lexical_cast<std::string>(raster->getSRID());
246 rinfo[
"MEM_RASTER_RES_X"] = boost::lexical_cast<std::string>(raster->getResolutionX());
247 rinfo[
"MEM_RASTER_RES_Y"] = boost::lexical_cast<std::string>(raster->getResolutionY());
248 rinfo[
"MEM_RASTER_MIN_X"] = boost::lexical_cast<std::string>(xmin);
249 rinfo[
"MEM_RASTER_MIN_Y"] = boost::lexical_cast<std::string>(ymin);
250 rinfo[
"MEM_RASTER_MAX_X"] = boost::lexical_cast<std::string>(xmax);
251 rinfo[
"MEM_RASTER_MAX_Y"] = boost::lexical_cast<std::string>(ymax);
254 for (
unsigned r = 0; r < myraster->getNumberOfRows(); r++)
256 for (
unsigned c = 0; c < myraster->getNumberOfColumns(); c++)
259 raster->getValue(c, r + vecBlocks[vb].m_initalRow, value);
260 myraster->setValue(c, r, value);
263 std::unique_ptr<te::rst::Raster> rasterPtr(myraster);
264 vecGenerateParams[vb]->m_nvals =
m_values;
265 vecGenerateParams[vb]->m_vecSegments.resize(
m_values.size());
266 vecGenerateParams[vb]->m_rasterPtr = std::move(rasterPtr);
270 threadGenerateSegments.join_all();
275 int calc = (
int)(numRows / numThreads) - 1;
279 std::vector<RasterBlockSize> rasterBlockSize;
281 for (
unsigned int i = 1; i < numThreads; i++)
287 rasterBlockSize.push_back(rasterBlock);
291 int dif = (
int)numThreads - 1;
293 int allRasters = var * dif;
294 int lastRaster = abs(allRasters - (
int)numRows);
296 calc = aux + lastRaster;
301 rasterBlockSize.push_back(rasterBlock);
303 return rasterBlockSize;
310 m_rasterPtr.release();
311 m_vecSegments.clear();
325 double resX = raster->getResolutionX();
326 double resY = raster->getResolutionY();
328 double xmin = raster->getExtent()->getLowerLeftX() + (resX / 2);
329 double ymax = raster->getExtent()->getUpperRightY() - (resY / 2);
330 double xlg_pos = xmin;
331 double ylg_inf = ymax;
334 double lineSupLeft = 0, lineSupRigth = 0, lineInfLeft = 0, lineInfRigth = 0;
335 unsigned int numberColumns = raster->getNumberOfColumns();
336 unsigned int numberRows = raster->getNumberOfRows();
338 for (
unsigned int nr = 1; nr < numberRows; ++nr)
340 double ylg_sup = ylg_inf;
341 ylg_inf = ymax - nr * resY;
344 for (
unsigned int nc = 0; nc < numberColumns - 1; ++nc)
346 double xlg_ant = xlg_pos;
347 xlg_pos = xmin + (nc + 1) * resX;
349 raster->getValue(nc, nr - 1, lineSupLeft);
350 raster->getValue(nc + 1, nr - 1, lineSupRigth);
351 raster->getValue(nc, nr, lineInfLeft);
352 raster->getValue(nc + 1, nr, lineInfRigth);
358 ((lineSupRigth >
m_vmax) ||
360 (lineInfRigth >
m_vmax) ||
362 (lineSupRigth <
m_vmin) ||
364 (lineInfRigth <
m_vmin) ||
370 for (
unsigned int idQuota = 0; idQuota < nvals.size(); ++idQuota)
372 quota = nvals[idQuota];
373 double delta = 0.0001;
380 if (fabs(quota - lineSupRigth) < delta)
382 lineSupRigth += delta;
385 if (fabs(quota - lineInfRigth) < delta)
387 lineInfRigth += delta;
390 if (fabs(quota - lineSupLeft) < delta)
392 lineSupLeft += delta;
395 if (fabs(quota - lineInfLeft) < delta)
397 lineInfLeft += delta;
402 if (((quota > lineSupLeft) && (quota > lineSupRigth) &&
403 (quota > lineInfLeft) && (quota > lineInfRigth)) ||
404 ((quota < lineSupLeft) && (quota < lineSupRigth) &&
405 (quota < lineInfLeft) && (quota < lineInfRigth)))
410 if (((quota < lineSupLeft) && (quota > lineSupRigth) &&
411 (quota > lineInfLeft) && (quota < lineInfRigth)) ||
412 ((quota > lineSupLeft) && (quota < lineSupRigth) &&
413 (quota < lineInfLeft) && (quota > lineInfRigth)))
415 double zmeio = (lineSupLeft + lineSupRigth + lineInfLeft + lineInfRigth) / 4;
417 while (quota == zmeio)
425 if (((quota > zmeio) && (quota > lineSupLeft)) ||
426 ((quota < zmeio) && (quota < lineSupLeft)))
428 interpolacao(1, line.get(), quota, ylg_sup, xlg_ant, xlg_pos, lineSupLeft, lineSupRigth);
429 interpolacao(0, line.get(), quota, xlg_pos, ylg_inf, ylg_sup, lineInfRigth, lineSupRigth);
430 interpolacao(0, line.get(), quota, xlg_ant, ylg_inf, ylg_sup, lineInfLeft, lineSupLeft);
431 interpolacao(1, line.get(), quota, ylg_inf, xlg_ant, xlg_pos, lineInfLeft, lineInfRigth);
435 interpolacao(0, line.get(), quota, xlg_ant, ylg_inf, ylg_sup, lineInfLeft, lineSupLeft);
436 interpolacao(1, line.get(), quota, ylg_sup, xlg_ant, xlg_pos, lineSupLeft, lineSupRigth);
437 interpolacao(1, line.get(), quota, ylg_inf, xlg_ant, xlg_pos, lineInfLeft, lineInfRigth);
438 interpolacao(0, line.get(), quota, xlg_pos, ylg_inf, ylg_sup, lineInfRigth, lineSupRigth);
443 if (((quota < lineSupLeft) && (quota > lineSupRigth)) ||
444 ((quota > lineSupLeft) && (quota < lineSupRigth)))
446 interpolacao(1, line.get(), quota, ylg_sup, xlg_ant, xlg_pos, lineSupLeft, lineSupRigth);
448 if (((quota < lineSupRigth) && (quota > lineInfRigth)) ||
449 ((quota > lineSupRigth) && (quota < lineInfRigth)))
451 interpolacao(0, line.get(), quota, xlg_pos, ylg_inf, ylg_sup, lineInfRigth, lineSupRigth);
453 if (((quota < lineInfLeft) && (quota > lineInfRigth)) ||
454 ((quota > lineInfLeft) && (quota < lineInfRigth)))
456 interpolacao(1, line.get(), quota, ylg_inf, xlg_ant, xlg_pos, lineInfLeft, lineInfRigth);
458 if (((quota < lineSupLeft) && (quota > lineInfLeft)) ||
459 ((quota > lineSupLeft) && (quota < lineInfLeft)))
461 interpolacao(0, line.get(), quota, xlg_ant, ylg_inf, ylg_sup, lineInfLeft, lineSupLeft);
464 if (line->size() > 1)
466 if (line->size() == 4)
469 size_t n = line->size();
470 line1->setX(0, line->getPointN(0)->getX());
471 line1->setY(0, line->getPointN(0)->getY());
472 line1->setZ(0, line->getPointN(n - 1)->getZ());
474 line1->setX(1, line->getPointN(1)->getX());
475 line1->setY(1, line->getPointN(1)->getY());
476 line1->setZ(1, line->getPointN(n - 1)->getZ());
480 line2->setX(0, line->getPointN(2)->getX());
481 line2->setY(0, line->getPointN(2)->getY());
482 line2->setZ(0, line->getPointN(n - 1)->getZ());
484 line2->setX(1, line->getPointN(3)->getX());
485 line2->setY(1, line->getPointN(3)->getY());
486 line2->setZ(1, line->getPointN(n - 1)->getZ());
488 vecSegments[idQuota].push_back(line1.release());
489 vecSegments[idQuota].push_back(line2.release());
491 else if (line->size())
493 vecSegments[idQuota].push_back(line.release());
510 for (
size_t i = 0; i <
m_lsOut.size(); ++i)
519 std::vector<te::gm::LineString*> lsOut;
527 std::vector<te::gm::Point> vecPoints;
528 std::vector<size_t> candidate;
529 std::set<size_t> lineRemoved;
534 for (
size_t i = 0; i < vecSegments.size(); ++i)
535 segmentsTree.
insert(*(vecSegments[i]->getMBR()), i);
538 double tol = 0.000001;
543 tol = 0.000001 / 1000;
547 std::vector<te::gm::Envelope> vecEnvelope;
549 for (
size_t l = 0; l < vecSegments.size(); ++l)
551 std::set<size_t>::iterator
id = lineRemoved.find(l);
552 if (
id != lineRemoved.end())
555 if (std::find(vecEnvelope.begin(), vecEnvelope.end(), *vecSegments[l]->getMBR()) != vecEnvelope.end())
563 currentSegment = *vecSegments[l];
565 segmentsTree.
remove(*(currentSegment.
getMBR()), (
int)l);
566 lineRemoved.insert(l);
574 vecPoints.push_back(ps);
580 vecPoints.push_back(pe);
582 size_t npts = vecPoints.size();
584 while (!segmentsTree.
isEmpty())
586 bool hasSegment =
false;
591 segmentsTree.
search(searchEnvelope, candidate);
593 std::sort(candidate.begin(), candidate.end());
595 for (
size_t i = 0; i < candidate.size(); ++i)
597 if (std::find(vecEnvelope.begin(), vecEnvelope.end(), *vecSegments[(
unsigned int)candidate[i]]->getMBR()) != vecEnvelope.end())
603 ptStartC.
setX(vecSegments[candidate[i]]->getX(0));
604 ptStartC.
setY(vecSegments[candidate[i]]->getY(0));
605 ptStartC.
setZ(vecSegments[candidate[i]]->getZ(1));
608 ptEndC.
setX(vecSegments[candidate[i]]->getX(1));
609 ptEndC.
setY(vecSegments[candidate[i]]->getY(1));
610 ptEndC.
setZ(vecSegments[candidate[i]]->getZ(1));
613 ptEnd.
setX(vecPoints[npts - 1].getX());
614 ptEnd.
setY(vecPoints[npts - 1].getY());
615 ptEnd.
setZ(vecPoints[npts - 1].getZ());
620 pt.
setX(vecSegments[candidate[i]]->getEndPoint()->getX());
621 pt.
setY(vecSegments[candidate[i]]->getEndPoint()->getY());
622 pt.
setZ(vecSegments[candidate[i]]->getEndPoint()->getZ());
624 vecPoints.push_back(pt);
627 segmentsTree.
remove(*(vecSegments[candidate[i]]->getMBR()), candidate[i]);
628 lineRemoved.insert(candidate[i]);
632 currentSegment.
setPointZ(currentSegment.
size() - 1, vecPoints[npts - 2].getX(), vecPoints[npts - 2].getY(), vecPoints[npts - 2].getZ());
634 currentSegment.
setPointZ(currentSegment.
size() - 1, vecPoints[npts - 1].getX(), vecPoints[npts - 1].getY(), vecPoints[npts - 1].getZ());
641 pt.
setX(vecSegments[candidate[i]]->getStartPoint()->getX());
642 pt.
setY(vecSegments[candidate[i]]->getStartPoint()->getY());
643 pt.
setZ(vecSegments[candidate[i]]->getEndPoint()->getZ());
645 vecPoints.push_back(pt);
648 segmentsTree.
remove(*(vecSegments[candidate[i]]->getMBR()), candidate[i]);
649 lineRemoved.insert(candidate[i]);
653 currentSegment.
setPointZ(currentSegment.
size() - 1, vecPoints[npts - 2].getX(), vecPoints[npts - 2].getY(), vecPoints[npts - 2].getZ());
655 currentSegment.
setPointZ(currentSegment.
size() - 1, vecPoints[npts - 1].getX(), vecPoints[npts - 1].getY(), vecPoints[npts - 1].getZ());
664 if (vecPoints.size()>2)
670 std::reverse(vecPoints.begin(), vecPoints.end());
675 currentSegment.
setPointZ(currentSegment.
size() - 1, vecPoints[npts - 2].getX(), vecPoints[npts - 2].getY(), vecPoints[npts - 1].getZ());
677 currentSegment.
setPointZ(currentSegment.
size() - 1, vecPoints[npts - 1].getX(), vecPoints[npts - 1].getY(), vecPoints[npts - 1].getZ());
682 ptStart.
setX(vecPoints[0].getX());
683 ptStart.
setY(vecPoints[0].getY());
684 ptStart.
setZ(vecPoints[npts - 1].getZ());
687 ptEnd.
setX(vecPoints[npts - 1].getX());
688 ptEnd.
setY(vecPoints[npts - 1].getY());
689 ptEnd.
setZ(vecPoints[npts - 1].getZ());
698 if (newiso || segmentsTree.
isEmpty())
701 for (
size_t ll = 0; ll < vecPoints.size(); ++ll)
703 lineOut->
setPointZ(ll, vecPoints[ll].getX(), vecPoints[ll].getY(), vecPoints[ll].getZ());
705 lsOut.push_back(lineOut);
714 assert(z_sup != z_inf);
716 double aux = c_inf + ((quota - z_inf) * (c_sup - c_inf) / (z_sup - z_inf));
bool remove(const te::gm::Envelope &mbr, const DATATYPE &data)
It removes an item from the tree.
std::string m_inRasterName
std::unique_ptr< Point > getEndPoint() const
It returns the curve end point.
~CreateIsolines()
Virtual destructor.
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
std::vector< RasterBlockSize > calculateBlocks(unsigned int numRows, unsigned int numThreads)
std::vector< double > m_guidevalues
~GenerateSegmentsParams()
Virtual destructor.
ConnectLinesParams()
Default constructor.
std::unique_ptr< te::rst::Raster > m_rasterPtr
boost::shared_ptr< DataSource > DataSourcePtr
A class that represents an R-tree.
TEMNTEXPORT bool Equal(te::gm::Point &p1, te::gm::Point &p2, double &tol)
void makeEmpty()
It clears all the coordinates.
#define TE_CORE_LOG_DEBUG(channel, message)
Use this tag in order to log a message to a specified logger with the DEBUG level.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
This class can be used to inform the progress of a task.
static bool connectLinesThreaded(ConnectLinesParams *params)
std::vector< std::vector< te::gm::LineString * > > m_vecSegments
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
std::vector< double > m_values
An utility struct for representing 2D coordinates.
double getY() const
It returns the y-coordinate.
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
te::da::DataSourcePtr m_inRasterDsrc
bool isActive() const
Verify if the task is active.
static bool generateSegmentsThreaded(GenerateSegmentsParams *params)
bool isEmpty(void) const
It returns true if the tree is empty.
void setParams(std::vector< double > &nval, std::vector< double > &gval, double vmax, double vmin, double dummy, bool hasDummy)
LineString is a curve with linear interpolation between points.
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
std::vector< te::gm::LineString * > m_lsOut
static bool connectLines(std::vector< te::gm::LineString * > vec, int srid, std::vector< te::gm::LineString * > &lsOut)
void setNumCoordinates(std::size_t size)
It reserves room for the number of coordinates in this LineString.
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
void setPointZ(std::size_t i, const double &x, const double &y, const double &z)
It sets the value of the specified point.
static bool generateSegments(std::unique_ptr< te::rst::Raster > raster, std::vector< double > nvals, std::vector< std::vector< te::gm::LineString * > > &vecSegments)
te::rst::Grid * getGrid()
Returns the definition of the raster grid support.
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
std::string m_outDsetName
double getX() const
It returns the x-coordinate.
GenerateSegmentsParams()
Default constructor.
void setX(const double &x)
It sets the Point x-coordinate value.
bool run(std::unique_ptr< te::rst::Raster > raster)
static void interpolacao(int direction, te::gm::LineString *line, double quota, double coord, double c_inf, double c_sup, double z_inf, double z_sup)
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
static Raster * make()
It creates and returns an empty raster with default raster driver.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
std::vector< te::gm::LineString * > m_vecSegments
te::da::DataSourcePtr m_outDsrc
std::unique_ptr< te::rst::Raster > getPrepareRaster()
void rstMemoryBlock(std::unique_ptr< te::rst::Raster > raster, std::vector< RasterBlockSize > vecBlocks, std::vector< GenerateSegmentsParams * > &vecGenerateParams)
std::vector< double > m_nvals
~ConnectLinesParams()
Virtual destructor.
void setY(const double &y)
It sets the Point y-coordinate value.
void setInput(te::da::DataSourcePtr inRasterDsrc, std::string inRasterName, std::unique_ptr< te::da::DataSetType > inDsetType)
A rectified grid is the spatial support for raster data.
TEMNTEXPORT bool SaveIso(std::string &outDsetName, te::da::DataSourcePtr &outDsrc, std::vector< std::unique_ptr< te::gm::LineString > > &isolist, std::vector< double > &guidevalues, int srid)
CreateIsolines()
Default constructor.
std::unique_ptr< Point > getStartPoint() const
The length of this Curve in its associated spatial reference.
std::unique_ptr< te::da::DataSetType > m_inRasterDsType
void setZ(const double &z)
It sets the Point z-coordinate value.
std::size_t size() const
It returns the number of points (vertexes) in the geometry.
const std::string & getName() const
It returns the property name.