CreateIsolinesCore.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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/mnt/core/CreateIsolines.cpp
22 
23  \brief This file contains a class that represents the create isolines.
24 
25  */
26 
27 //TerraLib
28 
29 #include "../../../../src/terralib/core/translator/Translator.h"
30 #include "../../../../src/terralib/common/progress/TaskProgress.h"
31 #include "../../../../src/terralib/core/logger/Logger.h"
32 
33 #include "../../../../src/terralib/dataaccess/dataset/DataSetTypeConverter.h"
34 #include "../../../../src/terralib/dataaccess/dataset/DataSetTypeCapabilities.h"
35 
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"
41 
42 #include "../../../../src/terralib/datatype/Property.h"
43 #include "../../../../src/terralib/datatype/SimpleProperty.h"
44 #include "../../../../src/terralib/datatype/StringProperty.h"
45 
46 #include "../../../../src/terralib/geometry/Geometry.h"
47 #include "../../../../src/terralib/geometry/GeometryProperty.h"
48 
49 #include "../../../../src/terralib/memory/DataSet.h"
50 #include "../../../../src/terralib/memory/DataSetItem.h"
51 
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"
60 
61 #include "../../mnt/core/Utils.h"
62 #include "CreateIsolinesCore.h"
63 
64 //STL
65 #include <cassert>
66 
67 //Boost
68 #include <boost/lexical_cast.hpp>
69 
70 
75 
77 
79 
80 void te::mnt::CreateIsolines::setInput(te::da::DataSourcePtr inRasterDsrc, std::string inRasterName, std::unique_ptr<te::da::DataSetType> inDsetType)
81 {
82  m_inRasterDsrc = inRasterDsrc;
83  m_inRasterName = inRasterName;
84  m_inRasterDsType = std::move(inDsetType);
85 }
86 
87 void te::mnt::CreateIsolines::setParams(std::vector<double> &nval, std::vector<double> &gval, double vmax, double vmin, double dummy, bool hasDummy)
88 {
89  m_values = nval;
90  m_guidevalues = gval;
91  m_vmax = vmax;
92  m_vmin = vmin;
93  m_dummy = dummy;
94  m_hasDummy = hasDummy;
95 }
96 
98 {
99  m_outDsrc = outDsrc;
100  m_outDsetName = dsname;
101 }
102 
103 std::unique_ptr<te::rst::Raster> te::mnt::CreateIsolines::getPrepareRaster()
104 {
105  std::unique_ptr<te::da::DataSetType>dsTypeRaster = m_inRasterDsrc->getDataSetType(m_inRasterName);
106  //prepare raster
107  te::rst::RasterProperty* rasterProp = te::da::GetFirstRasterProperty(dsTypeRaster.get());
108  std::unique_ptr<te::da::DataSet> dsRaster = m_inRasterDsrc->getDataSet(m_inRasterName);
109  std::unique_ptr<te::rst::Raster> raster = dsRaster->getRaster(rasterProp->getName());
110  return raster;
111 }
112 
113 bool te::mnt::CreateIsolines::run(std::unique_ptr<te::rst::Raster> raster)
114 {
115  std::string timeResult = "Create Isolines Grid - Start.";
116 #ifdef TERRALIB_LOGGER_ENABLED
117  TE_CORE_LOG_DEBUG("mnt", timeResult);
118 #endif
119 
121  te::rst::Grid* grd = rstProp->getGrid();
122  m_srid = grd->getSRID();
123 
124  std::vector<std::vector<te::gm::LineString*> > vecSegments;
125 
126  for (unsigned int i = 0; i < m_values.size(); i++)
127  {
128  std::vector<te::gm::LineString*> vecLine;
129  vecSegments.push_back(vecLine);
130  }
131 
132  unsigned int numRows = raster->getNumberOfRows();
133  unsigned int numThreads = 8;
134  std::vector<RasterBlockSize> vecBlocks = calculateBlocks(numRows, numThreads);
135 
136  std::vector<GenerateSegmentsParams*> vecGenerateParams;
137 
138  for (unsigned int i = 0; i < numThreads; ++i)
139  {
140  vecGenerateParams.push_back(new GenerateSegmentsParams());
141  }
142 
143  rstMemoryBlock(std::move(raster), vecBlocks, vecGenerateParams);
144 
145 
146  for (unsigned int vg = 0; vg < vecGenerateParams.size(); ++vg)
147  {
148  GenerateSegmentsParams* currentBlock = vecGenerateParams[vg];
149  const std::vector<std::vector<te::gm::LineString*> >& allQuotas = currentBlock->m_vecSegments;
150 
151  for (size_t quota = 0; quota < allQuotas.size(); ++quota)
152  {
153  const std::vector<te::gm::LineString*>& allSegments = allQuotas[quota];
154  for (size_t seg = 0; seg < allSegments.size(); ++seg)
155  {
156  vecSegments[quota].push_back(allSegments[seg]);
157  }
158  }
159  }
160 
161  for (unsigned int i = 0; i < numThreads; ++i)
162  {
163  delete vecGenerateParams[i];
164  }
165  vecGenerateParams.clear();
166 
167  std::vector<ConnectLinesParams> vecParams;
168  vecParams.resize(m_values.size());
169  boost::thread_group thread;
170 
171  for (unsigned int idQuota = 0; idQuota < vecParams.size(); ++idQuota)
172  {
173  vecParams[idQuota].m_quota = m_values[idQuota];
174  vecParams[idQuota].m_vecSegments = vecSegments[idQuota];
175  vecParams[idQuota].m_srid = m_srid;
176  thread.add_thread(new boost::thread(connectLinesThreaded, &vecParams[idQuota]));
177  }
178  thread.join_all();
179 
180  std::vector< std::unique_ptr< te::gm::LineString> > lsOut;
181  for (unsigned int idQuota = 0; idQuota < vecParams.size(); ++idQuota)
182  {
183  for (unsigned int i = 0; i < vecParams[idQuota].m_lsOut.size(); ++i)
184  {
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));
187  }
188  }
189 
190  timeResult = "Create Isolines Grid - End.";
191 #ifdef TERRALIB_LOGGER_ENABLED
192  TE_CORE_LOG_DEBUG("mnt", timeResult);
193 #endif
194 
195  std::vector<double> guidevalues;
196  te::mnt::SaveIso(m_outDsetName, m_outDsrc, lsOut, guidevalues, m_srid);
197 
198  lsOut.clear();
199 
200  for (size_t i = 0; i < m_values.size(); ++i)
201  {
202  for (size_t ii = 0; ii < vecSegments[i].size(); ++ii)
203  {
204  te::gm::LineString* l = vecSegments[i][ii];
205  delete l;
206  }
207  vecSegments[i].clear();
208  }
209 
210  vecSegments.clear();
211 
212  return true;
213 }
214 
215 void te::mnt::CreateIsolines::rstMemoryBlock(std::unique_ptr<te::rst::Raster> raster, std::vector<RasterBlockSize> vecBlocks, std::vector<GenerateSegmentsParams*>& vecGenerateParams)
216 {
217  boost::thread_group threadGenerateSegments;
218 
219  int steps = (int)vecBlocks.size();
220 
221  te::common::TaskProgress task("Creating Isolines...", te::common::TaskProgress::UNDEFINED, steps);
222 
223  for (std::size_t vb = 0; vb < vecBlocks.size(); ++vb)
224  {
225  if (!task.isActive())
226  break;
227 
228  task.pulse();
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);
231 
232 
233  double xmin = coordLowerLeft.getX() - (raster->getResolutionX() / 2);
234  double ymin = coordLowerLeft.getY() - (raster->getResolutionY() / 2);
235 
236  double xmax = coordUpperRight.getX() + (raster->getResolutionX() / 2);
237  double ymax = coordUpperRight.getY() + (raster->getResolutionY() / 2);
238 
239  std::map<std::string, std::string> rinfo;
240 
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);
252  te::rst::Raster* myraster = te::rst::RasterFactory::make("MEM", nullptr, std::vector<te::rst::BandProperty*>(), rinfo);
253 
254  for (unsigned r = 0; r < myraster->getNumberOfRows(); r++)
255  {
256  for (unsigned c = 0; c < myraster->getNumberOfColumns(); c++)
257  {
258  double value = 0;
259  raster->getValue(c, r + vecBlocks[vb].m_initalRow, value);
260  myraster->setValue(c, r, value);
261  }
262  }
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);
267 
268  threadGenerateSegments.add_thread(new boost::thread(generateSegmentsThreaded, vecGenerateParams[vb]));
269  }
270  threadGenerateSegments.join_all();
271 }
272 
273 std::vector<RasterBlockSize> te::mnt::CreateIsolines::calculateBlocks(unsigned int numRows, unsigned int numThreads)
274 {
275  int calc = (int)(numRows / numThreads) - 1;
276  int aux = 0;
277  int var = calc;
278  struct RasterBlockSize rasterBlock;
279  std::vector<RasterBlockSize> rasterBlockSize;
280 
281  for (unsigned int i = 1; i < numThreads; i++)
282  {
283  rasterBlock.m_idThread = (int)i;
284  rasterBlock.m_numRows = var + 1;
285  rasterBlock.m_initalRow = aux;
286  rasterBlock.m_finalRow = calc;
287  rasterBlockSize.push_back(rasterBlock);
288  aux = calc;
289  calc += var;
290  }
291  int dif = (int)numThreads - 1;
292  int sum = dif + 1;
293  int allRasters = var * dif;
294  int lastRaster = abs(allRasters - (int)numRows);
295  calc = 0;
296  calc = aux + lastRaster;
297  rasterBlock.m_idThread = sum;
298  rasterBlock.m_numRows = lastRaster;
299  rasterBlock.m_initalRow = aux;
300  rasterBlock.m_finalRow = calc - 1;
301  rasterBlockSize.push_back(rasterBlock);
302 
303  return rasterBlockSize;
304 }
305 
307 
309 {
310  m_rasterPtr.release();
311  m_vecSegments.clear();
312  m_nvals.clear();
313 }
314 
316 {
317  generateSegments(std::move(params->m_rasterPtr), params->m_nvals, params->m_vecSegments);
318  return true;
319 }
320 
321 bool te::mnt::CreateIsolines::generateSegments(std::unique_ptr<te::rst::Raster> raster, std::vector<double> nvals, std::vector< std::vector<te::gm::LineString*> >& vecSegments)
322 {
323  double quota;
324 
325  double resX = raster->getResolutionX();
326  double resY = raster->getResolutionY();
327 
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;
332 
333  std::unique_ptr<te::gm::LineString> line(new te::gm::LineString(0, te::gm::LineStringZType));
334  double lineSupLeft = 0, lineSupRigth = 0, lineInfLeft = 0, lineInfRigth = 0;
335  unsigned int numberColumns = raster->getNumberOfColumns();
336  unsigned int numberRows = raster->getNumberOfRows();
337 
338  for (unsigned int nr = 1; nr < numberRows; ++nr)
339  {
340  double ylg_sup = ylg_inf;
341  ylg_inf = ymax - nr * resY;
342  xlg_pos = xmin;
343 
344  for (unsigned int nc = 0; nc < numberColumns - 1; ++nc)
345  {
346  double xlg_ant = xlg_pos;
347  xlg_pos = xmin + (nc + 1) * resX;
348 
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);
353  if ((m_hasDummy == true &&
354  (lineSupRigth == m_dummy ||
355  lineSupLeft == m_dummy ||
356  lineInfRigth == m_dummy ||
357  lineInfLeft == m_dummy)) ||
358  ((lineSupRigth > m_vmax) ||
359  (lineSupLeft > m_vmax) ||
360  (lineInfRigth > m_vmax) ||
361  (lineInfLeft > m_vmax) ||
362  (lineSupRigth < m_vmin) ||
363  (lineSupLeft < m_vmin) ||
364  (lineInfRigth < m_vmin) ||
365  (lineInfLeft < m_vmin)))
366  {
367  continue;
368  }
369 
370  for (unsigned int idQuota = 0; idQuota < nvals.size(); ++idQuota)
371  {
372  quota = nvals[idQuota];
373  double delta = 0.0001;
374 
375  bool changed = true;
376 
377  while (changed)
378  {
379  changed = false;
380  if (fabs(quota - lineSupRigth) < delta)
381  {
382  lineSupRigth += delta;
383  changed = true;
384  }
385  if (fabs(quota - lineInfRigth) < delta)
386  {
387  lineInfRigth += delta;
388  changed = true;
389  }
390  if (fabs(quota - lineSupLeft) < delta)
391  {
392  lineSupLeft += delta;
393  changed = true;
394  }
395  if (fabs(quota - lineInfLeft) < delta)
396  {
397  lineInfLeft += delta;
398  changed = true;
399  }
400  } //while (changed)
401 
402  if (((quota > lineSupLeft) && (quota > lineSupRigth) &&
403  (quota > lineInfLeft) && (quota > lineInfRigth)) ||
404  ((quota < lineSupLeft) && (quota < lineSupRigth) &&
405  (quota < lineInfLeft) && (quota < lineInfRigth)))
406  {
407  }
408  else
409  {
410  if (((quota < lineSupLeft) && (quota > lineSupRigth) &&
411  (quota > lineInfLeft) && (quota < lineInfRigth)) ||
412  ((quota > lineSupLeft) && (quota < lineSupRigth) &&
413  (quota < lineInfLeft) && (quota > lineInfRigth)))
414  {
415  double zmeio = (lineSupLeft + lineSupRigth + lineInfLeft + lineInfRigth) / 4;
416 
417  while (quota == zmeio)
418  {
419  if (zmeio == 0)
420  zmeio += 0.0001;
421  else
422  zmeio *= 1.001;
423  }
424 
425  if (((quota > zmeio) && (quota > lineSupLeft)) ||
426  ((quota < zmeio) && (quota < lineSupLeft)))
427  {
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);
432  }
433  else
434  {
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);
439  }
440  }
441  else
442  {
443  if (((quota < lineSupLeft) && (quota > lineSupRigth)) ||
444  ((quota > lineSupLeft) && (quota < lineSupRigth)))
445  {
446  interpolacao(1, line.get(), quota, ylg_sup, xlg_ant, xlg_pos, lineSupLeft, lineSupRigth);
447  }
448  if (((quota < lineSupRigth) && (quota > lineInfRigth)) ||
449  ((quota > lineSupRigth) && (quota < lineInfRigth)))
450  {
451  interpolacao(0, line.get(), quota, xlg_pos, ylg_inf, ylg_sup, lineInfRigth, lineSupRigth);
452  }
453  if (((quota < lineInfLeft) && (quota > lineInfRigth)) ||
454  ((quota > lineInfLeft) && (quota < lineInfRigth)))
455  {
456  interpolacao(1, line.get(), quota, ylg_inf, xlg_ant, xlg_pos, lineInfLeft, lineInfRigth);
457  }
458  if (((quota < lineSupLeft) && (quota > lineInfLeft)) ||
459  ((quota > lineSupLeft) && (quota < lineInfLeft)))
460  {
461  interpolacao(0, line.get(), quota, xlg_ant, ylg_inf, ylg_sup, lineInfLeft, lineSupLeft);
462  }
463  }
464  if (line->size() > 1)
465  {
466  if (line->size() == 4)
467  {
468  std::unique_ptr<te::gm::LineString> line1(new te::gm::LineString(2, te::gm::LineStringZType));
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());
473 
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());
477 
478  std::unique_ptr<te::gm::LineString> line2(new te::gm::LineString(2, te::gm::LineStringZType));
479 
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());
483 
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());
487 
488  vecSegments[idQuota].push_back(line1.release());
489  vecSegments[idQuota].push_back(line2.release());
490  }
491  else if (line->size())
492  {
493  vecSegments[idQuota].push_back(line.release());
494  }
495  line.reset(new te::gm::LineString(0, te::gm::LineStringZType));
496  } // if (line->size() > 1) ...
497  }
498  }
499  }
500  }
501  return true;
502 }
503 
505 {
506 }
507 
509 {
510  for (size_t i = 0; i < m_lsOut.size(); ++i)
511  {
512  te::gm::LineString *l = m_lsOut[i];
513  delete l;
514  }
515 }
516 
518 {
519  std::vector<te::gm::LineString*> lsOut;
520  connectLines(params->m_vecSegments, params->m_srid, lsOut);
521  params->m_lsOut = lsOut;
522  return true;
523 }
524 
525 bool te::mnt::CreateIsolines::connectLines(std::vector<te::gm::LineString*> vecSegments, int srid, std::vector< te::gm::LineString* >& lsOut)
526 {
527  std::vector<te::gm::Point> vecPoints;
528  std::vector<size_t> candidate;
529  std::set<size_t> lineRemoved;
530  te::sam::rtree::Index<size_t> segmentsTree;
531  bool borda;
532  bool newiso = false;
533 
534  for (size_t i = 0; i < vecSegments.size(); ++i)
535  segmentsTree.insert(*(vecSegments[i]->getMBR()), i);
536 
538  double tol = 0.000001;
539  if (unitin.get())
540  {
541  if (unitin->getId() == te::common::UOM_Degree)
542  {
543  tol = 0.000001 / 1000;
544  }
545  }
546 
547  std::vector<te::gm::Envelope> vecEnvelope;
548 
549  for (size_t l = 0; l < vecSegments.size(); ++l)
550  {
551  std::set<size_t>::iterator id = lineRemoved.find(l);
552  if (id != lineRemoved.end())
553  continue;
554 
555  if (std::find(vecEnvelope.begin(), vecEnvelope.end(), *vecSegments[l]->getMBR()) != vecEnvelope.end())
556  {
557  continue;
558  }
559 
560  borda = false;
562  currentSegment.setNumCoordinates(vecSegments[l]->size());
563  currentSegment = *vecSegments[l];
564 
565  segmentsTree.remove(*(currentSegment.getMBR()), (int)l);
566  lineRemoved.insert(l);
567 
568  vecPoints.clear();
569 
571  ps.setX(currentSegment.getStartPoint()->getX());
572  ps.setY(currentSegment.getStartPoint()->getY());
573  ps.setZ(currentSegment.getEndPoint()->getZ());
574  vecPoints.push_back(ps);
575 
577  pe.setX(currentSegment.getEndPoint()->getX());
578  pe.setY(currentSegment.getEndPoint()->getY());
579  pe.setZ(currentSegment.getEndPoint()->getZ());
580  vecPoints.push_back(pe);
581 
582  size_t npts = vecPoints.size();
583 
584  while (!segmentsTree.isEmpty())
585  {
586  bool hasSegment = false;
587 
588  te::gm::Envelope currentEnvelope = *currentSegment.getMBR();
589  te::gm::Envelope searchEnvelope(currentEnvelope.getLowerLeftX() - tol, currentEnvelope.getLowerLeftY() - tol, currentEnvelope.getUpperRightX() + tol, currentEnvelope.getUpperRightY() + tol);
590 
591  segmentsTree.search(searchEnvelope, candidate);
592 
593  std::sort(candidate.begin(), candidate.end());
594 
595  for (size_t i = 0; i < candidate.size(); ++i)
596  {
597  if (std::find(vecEnvelope.begin(), vecEnvelope.end(), *vecSegments[(unsigned int)candidate[i]]->getMBR()) != vecEnvelope.end())
598  {
599  continue;
600  }
601 
602  te::gm::Point ptStartC(0, te::gm::PointZType);
603  ptStartC.setX(vecSegments[candidate[i]]->getX(0));
604  ptStartC.setY(vecSegments[candidate[i]]->getY(0));
605  ptStartC.setZ(vecSegments[candidate[i]]->getZ(1));
606 
608  ptEndC.setX(vecSegments[candidate[i]]->getX(1));
609  ptEndC.setY(vecSegments[candidate[i]]->getY(1));
610  ptEndC.setZ(vecSegments[candidate[i]]->getZ(1));
611 
613  ptEnd.setX(vecPoints[npts - 1].getX());
614  ptEnd.setY(vecPoints[npts - 1].getY());
615  ptEnd.setZ(vecPoints[npts - 1].getZ());
616 
617  if (te::mnt::Equal(ptStartC, ptEnd, tol))
618  {
620  pt.setX(vecSegments[candidate[i]]->getEndPoint()->getX());
621  pt.setY(vecSegments[candidate[i]]->getEndPoint()->getY());
622  pt.setZ(vecSegments[candidate[i]]->getEndPoint()->getZ());
623 
624  vecPoints.push_back(pt);
625  ++npts;
626 
627  segmentsTree.remove(*(vecSegments[candidate[i]]->getMBR()), candidate[i]);
628  lineRemoved.insert(candidate[i]);
629 
630  currentSegment.makeEmpty();
631  currentSegment.setNumCoordinates(currentSegment.size() + 1);
632  currentSegment.setPointZ(currentSegment.size() - 1, vecPoints[npts - 2].getX(), vecPoints[npts - 2].getY(), vecPoints[npts - 2].getZ());
633  currentSegment.setNumCoordinates(currentSegment.size() + 1);
634  currentSegment.setPointZ(currentSegment.size() - 1, vecPoints[npts - 1].getX(), vecPoints[npts - 1].getY(), vecPoints[npts - 1].getZ());
635  hasSegment = true;
636  break;
637  }
638  else if (te::mnt::Equal(ptEndC, ptEnd, tol))
639  {
641  pt.setX(vecSegments[candidate[i]]->getStartPoint()->getX());
642  pt.setY(vecSegments[candidate[i]]->getStartPoint()->getY());
643  pt.setZ(vecSegments[candidate[i]]->getEndPoint()->getZ());
644 
645  vecPoints.push_back(pt);
646  ++npts;
647 
648  segmentsTree.remove(*(vecSegments[candidate[i]]->getMBR()), candidate[i]);
649  lineRemoved.insert(candidate[i]);
650 
651  currentSegment.makeEmpty();
652  currentSegment.setNumCoordinates(currentSegment.size() + 1);
653  currentSegment.setPointZ(currentSegment.size() - 1, vecPoints[npts - 2].getX(), vecPoints[npts - 2].getY(), vecPoints[npts - 2].getZ());
654  currentSegment.setNumCoordinates(currentSegment.size() + 1);
655  currentSegment.setPointZ(currentSegment.size() - 1, vecPoints[npts - 1].getX(), vecPoints[npts - 1].getY(), vecPoints[npts - 1].getZ());
656 
657  hasSegment = true;
658  break;
659  }
660  }
661  if (!hasSegment)
662  {
663  if (borda){
664  if (vecPoints.size()>2)
665  newiso = true;
666  break;
667  }
668  else
669  {
670  std::reverse(vecPoints.begin(), vecPoints.end());
671  borda = true;
672 
673  currentSegment.makeEmpty();
674  currentSegment.setNumCoordinates(currentSegment.size() + 1);
675  currentSegment.setPointZ(currentSegment.size() - 1, vecPoints[npts - 2].getX(), vecPoints[npts - 2].getY(), vecPoints[npts - 1].getZ());
676  currentSegment.setNumCoordinates(currentSegment.size() + 1);
677  currentSegment.setPointZ(currentSegment.size() - 1, vecPoints[npts - 1].getX(), vecPoints[npts - 1].getY(), vecPoints[npts - 1].getZ());
678  hasSegment = true;
679  }
680  }
681  te::gm::Point ptStart(0, te::gm::PointZType);
682  ptStart.setX(vecPoints[0].getX());
683  ptStart.setY(vecPoints[0].getY());
684  ptStart.setZ(vecPoints[npts - 1].getZ());
685 
687  ptEnd.setX(vecPoints[npts - 1].getX());
688  ptEnd.setY(vecPoints[npts - 1].getY());
689  ptEnd.setZ(vecPoints[npts - 1].getZ());
690 
691  if (npts > 2 && te::mnt::Equal(ptStart, ptEnd, tol))
692  {
693  newiso = true;
694  break;
695  }
696  candidate.clear();
697  }
698  if (newiso || segmentsTree.isEmpty())
699  {
700  te::gm::LineString* lineOut = new te::gm::LineString(vecPoints.size(), te::gm::LineStringZType);
701  for (size_t ll = 0; ll < vecPoints.size(); ++ll)
702  {
703  lineOut->setPointZ(ll, vecPoints[ll].getX(), vecPoints[ll].getY(), vecPoints[ll].getZ());
704  }
705  lsOut.push_back(lineOut);
706  newiso = false;
707  }
708  }
709  return true;
710 }
711 
712 void te::mnt::CreateIsolines::interpolacao(int direction, te::gm::LineString* line, double quota, double coord, double c_inf, double c_sup, double z_inf, double z_sup)
713 {
714  assert(z_sup != z_inf);
715 
716  double aux = c_inf + ((quota - z_inf) * (c_sup - c_inf) / (z_sup - z_inf));
717 
718  line->setNumCoordinates(line->size() + 1);
719 
720  if (direction == 0)
721  {
722  line->setPointZ(line->size()-1, coord, aux, quota);
723  }
724  else
725  {
726  line->setPointZ(line->size()-1, aux, coord, quota);
727  }
728 }
bool remove(const te::gm::Envelope &mbr, const DATATYPE &data)
It removes an item from the tree.
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.
Definition: Logger.h:225
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.
Definition: TaskProgress.h:53
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.
Definition: Coord2D.h:40
double getY() const
It returns the y-coordinate.
Definition: Coord2D.h:108
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.
Raster property.
static bool generateSegmentsThreaded(GenerateSegmentsParams *params)
unsigned int line
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.
Definition: LineString.h:62
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
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
double getX() const
It returns the x-coordinate.
Definition: Coord2D.h:102
GenerateSegmentsParams()
Default constructor.
void setX(const double &x)
It sets the Point x-coordinate value.
Definition: Point.h:145
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.
Definition: Point.h:159
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.
Definition: raster/Grid.h:68
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.
Definition: Point.h:173
std::size_t size() const
It returns the number of points (vertexes) in the geometry.
Definition: LineString.h:262
const std::string & getName() const
It returns the property name.
Definition: Property.h:127