src/terralib/attributefill/RasterToVector.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled
4  applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file RasterToVector.cpp
23  */
24 
25 #include "../common/progress/TaskProgress.h"
26 #include "../common/StringUtils.h"
27 
28 #include "../core/translator/Translator.h"
29 
30 #include "../dataaccess/dataset/DataSet.h"
31 #include "../dataaccess/dataset/DataSetAdapter.h"
32 
33 #include "../dataaccess/dataset/DataSetType.h"
34 #include "../dataaccess/dataset/DataSetTypeConverter.h"
35 #include "../dataaccess/dataset/ObjectIdSet.h"
36 #include "../dataaccess/datasource/DataSource.h"
37 #include "../dataaccess/datasource/DataSourceCapabilities.h"
38 #include "../dataaccess/datasource/DataSourceTransactor.h"
39 #include "../dataaccess/utils/Utils.h"
40 
41 #include "../datatype/Property.h"
42 #include "../datatype/StringProperty.h"
43 
44 #include "../geometry.h"
45 
46 #include "../memory/DataSetItem.h"
47 
48 #include "../raster/RasterProperty.h"
49 #include "../raster/RasterSummaryManager.h"
50 #include "../raster/Utils.h"
51 
52 #include "../rp/RasterAttributes.h"
53 #include "../rp/Texture.h"
54 
55 #include "../statistics.h"
56 
57 #include "Exception.h"
58 #include "RasterToVector.h"
59 #include "Utils.h"
60 
61 
62 // STL
63 #include <map>
64 
65 // Boost
66 #include <boost/algorithm/string.hpp>
67 #include <boost/lexical_cast.hpp>
68 
70 
72  std::string inVectorName,
73  te::da::DataSetTypeConverter* inVectorDsType,
74  const te::da::ObjectIdSet* oidSet)
75 {
76  m_inRaster = inRaster;
77  m_inVectorDsrc = inVectorDsrc;
78  m_inVectorName = inVectorName;
79  m_inVectorDsType.reset(inVectorDsType);
80  m_oidSet = oidSet;
81 }
82 
84  std::vector<unsigned int> bands,
85  std::vector<te::stat::StatisticalSummary> statSum,
86  bool iteratorByBox,
87  bool texture)
88 {
89  m_bands = bands;
90  m_statSum = statSum;
91  m_iteratorByBox = iteratorByBox;
92  m_texture = texture;
93 }
94 
96  std::string dsName)
97 {
98  m_outDsrc = outDsrc;
99  m_outDset = dsName;
100 }
101 
103 {
104  if(!m_inVectorDsType.get())
105  return false;
106 
107  if(!m_inVectorDsType->getResult()->hasGeom())
108  return false;
109 
110  if(m_outDset.empty() || !m_outDsrc.get())
111  return false;
112 
113  return true;
114 }
115 
117 {
118  // Prepare raster
119  double resX = m_inRaster->getResolutionX();
120  double resY = m_inRaster->getResolutionY();
121 
123 
124  // Raster Attributes
125  te::rp::RasterAttributes* rasterAtt = 0;
126 
127  // Prepare vector
128  te::gm::GeometryProperty* vectorProp =
130 
131  std::size_t geomIdx = boost::lexical_cast<std::size_t>(
132  m_inVectorDsType->getResult()->getPropertyPosition(
133  vectorProp->getName()));
134 
135  std::unique_ptr<te::da::DataSet> dataSetVector;
136 
137  if(m_oidSet == 0)
138  dataSetVector = m_inVectorDsrc->getDataSet(m_inVectorName);
139  else
140  dataSetVector = m_inVectorDsrc->getDataSet(m_inVectorName, m_oidSet);
141 
142  std::unique_ptr<te::da::DataSetAdapter> dsVector(
143  te::da::CreateAdapter(dataSetVector.get(), m_inVectorDsType.get()));
144 
145  // Parameters to get the percentage of classes by area
146  bool percentByArea = false;
147  std::vector<std::vector<double> > pixelDistinct;
148  std::vector<te::stat::StatisticalSummary>::iterator it = std::find(
150 
151  if(it != m_statSum.end())
152  {
153  std::vector<double> pixelDistinctCurrentBand;
154 
155  for(std::size_t b = 0; b < m_bands.size(); ++b)
156  {
157  getPixelDistinct(*m_inRaster, m_bands[b], pixelDistinctCurrentBand);
158  pixelDistinct.push_back(pixelDistinctCurrentBand);
159  }
160  percentByArea = true;
161  }
162 
163  // Get output DataSetType.
164  std::unique_ptr<te::da::DataSetType> outDsType;
165  if(percentByArea)
166  outDsType = std::move(getDataSetType(pixelDistinct));
167  else
168  outDsType = std::move(getDataSetType());
169 
170  // Create the output dataset in memory
171  std::unique_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(outDsType.get()));
172 
173  // Task progress
174  te::common::TaskProgress task("Processing Operation...");
175  task.setTotalSteps((int)dsVector->size() * (int)m_statSum.size() *
176  (int)m_bands.size());
177  task.useTimer(true);
178 
179  // Verify if is necessary to remap
180  bool remap = false;
181 
182  if(m_inRaster->getSRID() != vectorProp->getSRID())
183  remap = true;
184 
185  // Start to read the dataSet.
186  dsVector->moveBeforeFirst();
187 
188  while(dsVector->moveNext())
189  {
190  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
191 
192  std::vector<te::dt::Property*> vecProp = m_inVectorDsType->getResult()->getProperties();
193 
194  for(std::size_t i = 0; i < vecProp.size(); ++i)
195  {
196  if(!dsVector->isNull(i))
197  outDSetItem->setValue(i, dsVector->getValue(i).release());
198  }
199 
200  // Geometry
201  std::unique_ptr<te::gm::Geometry> geom = dsVector->getGeometry(geomIdx);
202 
203  if(!geom->isValid())
204  continue;
205 
206  if(remap)
207  geom->transform(m_inRaster->getSRID());
208 
209  // Add Item in DataSet if geom does not intersects the raster envelope and
210  // continue to the next dataSet item.
211  if(!env->intersects(*geom->getMBR()))
212  {
213  outDataset->add(outDSetItem);
214 
215  if(outDataset->size() == 2000)
216  {
217  save(outDataset.get(), outDsType.get());
218  outDataset->clear();
219  }
220 
221  continue;
222  }
223 
224  double area = 0;
225 
226  // Values from raster for each band
227  std::vector<std::map<double, int > > valuesFromRasterByBand;
228  std::vector<std::map<double, double > > percentOfEachClassByArea;
229  for(std::size_t band = 0; band < m_bands.size(); ++band)
230  {
231  std::map<double, int > values1;
232  valuesFromRasterByBand.push_back(values1);
233 
234  std::map<double, double > values2;
235  percentOfEachClassByArea.push_back(values2);
236  }
237 
238  // Flags
239  bool isPoint = false;
240  bool isNoDataValue = false;
241  std::vector< te::gm::Geometry* > singleGeomsPtrs;
242 
243  switch(geom->getGeomTypeId())
244  {
246  {
247  if(percentByArea)
248  area = ((te::gm::MultiPolygon*)geom.get())->getArea();
249 
250  singleGeomsPtrs.clear();
251  te::gm::Multi2Single( geom.get(), singleGeomsPtrs );
252 
253  const std::size_t n_geom = singleGeomsPtrs.size();
254 
255  for(std::size_t n = 0; n < n_geom; ++n)
256  {
257  assert( singleGeomsPtrs[ n ]->getGeomTypeId() == te::gm::PolygonType );
258  te::gm::Polygon* polygon = (te::gm::Polygon*)singleGeomsPtrs[ n ];
259 
260  for(std::size_t band = 0; band < m_bands.size(); ++band)
261  {
262  std::map<double, int> values;
263 
264  if(m_iteratorByBox == false)
265  {
266  rasterAtt->getValuesFromBand(*m_inRaster, m_bands[band], *polygon, values);
267  }
268  else
269  {
270  uint32_t minimumRow, minimumColumn, maximumRow, maximumColumn;
271  GetMinMaxLineAndColumn(*m_inRaster, *polygon, minimumRow, minimumColumn, maximumRow, maximumColumn);
272 
273  GetValuesFromBand(*m_inRaster, m_bands[band], *polygon, minimumRow, minimumColumn, maximumRow, maximumColumn, values);
274 
275  if(std::find(m_statSum.begin(), m_statSum.end(), te::stat::PERCENT_EACH_CLASS_BY_AREA) != m_statSum.end())
276  {
277  std::map<double, double> percents;
278  GetPercentOfEachClassByArea(*m_inRaster, m_bands[band], *polygon, minimumRow, minimumColumn, maximumRow, maximumColumn, percents);
279  joinMaps(percentOfEachClassByArea[band], percents);
280  }
281  }
282 
283  joinMaps(valuesFromRasterByBand[band], values);
284  }
285  }
286 
287  isPoint = false;
288 
289  break;
290  }
291  case te::gm::PolygonType:
292  {
293  te::gm::Polygon* polygon = dynamic_cast<te::gm::Polygon*>(geom.get());
294 
295  area = polygon->getArea();
296 
297  for(std::size_t band = 0; band < m_bands.size(); ++band)
298  {
299  std::map<double, int> values;
300 
301  if(m_iteratorByBox == false)
302  {
303  rasterAtt->getValuesFromBand(*m_inRaster, m_bands[band], *polygon, values);
304  }
305  else
306  {
307  uint32_t minimumRow, minimumColumn, maximumRow, maximumColumn;
308  GetMinMaxLineAndColumn(*m_inRaster, *polygon, minimumRow, minimumColumn, maximumRow, maximumColumn);
309 
310  GetValuesFromBand(*m_inRaster, m_bands[band], *polygon, minimumRow, minimumColumn, maximumRow, maximumColumn, values);
311 
312  if(std::find(m_statSum.begin(), m_statSum.end(), te::stat::PERCENT_EACH_CLASS_BY_AREA) != m_statSum.end())
313  {
314  std::map<double, double> percents;
315  GetPercentOfEachClassByArea(*m_inRaster, m_bands[band], *polygon, minimumRow, minimumColumn, maximumRow, maximumColumn, percents);
316  joinMaps(percentOfEachClassByArea[band], percents);
317  }
318  }
319 
320  joinMaps(valuesFromRasterByBand[band], values);
321  }
322 
323  isPoint = false;
324 
325  break;
326  }
328  {
329  te::gm::MultiPoint* mPoint = dynamic_cast<te::gm::MultiPoint*>(geom.get());
330 
331  std::size_t n_geom = mPoint->getNumGeometries();
332 
333  for(std::size_t n = 0; n < n_geom; ++n)
334  {
335  te::gm::Point* point = dynamic_cast<te::gm::Point*>(mPoint->getGeometryN(n));
336 
337  const double coordX = point->getX();
338  const double coordY = point->getY();
339 
340  te::gm::Coord2D coord2d =
341  m_inRaster->getGrid()->geoToGrid(coordX, coordY);
342 
343  int x = te::rst::Round(coord2d.x);
344  int y = te::rst::Round(coord2d.y);
345 
346  for(std::size_t band = 0; band < m_bands.size(); ++band)
347  {
348  double noDataValue =
350 
351  double value = noDataValue;
352  if((x >= 0 && x < (int)m_inRaster->getNumberOfColumns()) &&
353  (y >= 0 && y < (int)m_inRaster->getNumberOfRows()))
354  {
355  m_inRaster->getValue(x, y, value, m_bands[band]);
356  }
357 
358  if(value == noDataValue)
359  {
360  isNoDataValue = true;
361  continue;
362  }
363 
364  std::map<double, int> values;
365 
366  ++values[value];
367 
368  joinMaps(valuesFromRasterByBand[band], values);
369  }
370 
371  isPoint = true;
372  }
373  break;
374  }
375  case te::gm::PointType:
376  {
377  te::gm::Point* point = dynamic_cast<te::gm::Point*>(geom.get());
378 
379  const double coordX = point->getX();
380  const double coordY = point->getY();
381 
382  te::gm::Coord2D coord2d =
383  m_inRaster->getGrid()->geoToGrid(coordX, coordY);
384 
385  int x = te::rst::Round(coord2d.x);
386  int y = te::rst::Round(coord2d.y);
387 
388  for(std::size_t band = 0; band < m_bands.size(); ++band)
389  {
390  double noDataValue = m_inRaster->getBand(m_bands[band])->getProperty()->m_noDataValue;
391 
392  double value = noDataValue;
393  if((x >= 0 && x < (int)m_inRaster->getNumberOfColumns()) &&
394  (y >= 0 && y < (int)m_inRaster->getNumberOfRows()))
395  {
396  m_inRaster->getValue(x, y, value, m_bands[band]);
397  }
398 
399  if(value == noDataValue)
400  {
401  isNoDataValue = true;
402  continue;
403  }
404 
405  std::map<double, int> values;
406 
407  ++values[value];
408 
409  joinMaps(valuesFromRasterByBand[band], values);
410  }
411 
412  isPoint = true;
413 
414  break;
415  }
416  default:
417  continue;
418  }
419 
420  if(isNoDataValue)
421  {
422  outDataset->add(outDSetItem);
423 
424  if(outDataset->size() == 2000)
425  {
426  save(outDataset.get(), outDsType.get());
427  outDataset->clear();
428  }
429 
430  continue;
431  }
432 
433  std::size_t init_index =
434  m_inVectorDsType->getResult()->getProperties().size();
435 
436  // Statistics set value
437  if(!isPoint)
438  {
439  for(std::size_t band = 0; band < valuesFromRasterByBand.size(); ++band)
440  {
442 
443  te::stat::GetNumericStatisticalSummary(valuesFromRasterByBand[band], summary);
444 
445  if(percentOfEachClassByArea[band].empty())
446  te::stat::GetPercentOfEachClassByArea(valuesFromRasterByBand[band], resX, resY, area, summary);
447  else
448  summary.m_percentEachClass = percentOfEachClassByArea[band];
449 
450  std::size_t current_index = init_index + m_statSum.size();
451 
452  for(std::size_t it = 0, i = init_index; i < current_index; ++it, ++i)
453  {
455 
456  switch(ss)
457  {
458  case te::stat::MIN_VALUE:
459  outDSetItem->setDouble(i, summary.m_minVal);
460  break;
461  case te::stat::MAX_VALUE:
462  outDSetItem->setDouble(i, summary.m_maxVal);
463  break;
464  case te::stat::COUNT:
465  outDSetItem->setDouble(i, summary.m_count);
466  break;
468  outDSetItem->setDouble(i, summary.m_validCount);
469  break;
470  case te::stat::MEAN:
471  outDSetItem->setDouble(i, summary.m_mean);
472  break;
473  case te::stat::SUM:
474  outDSetItem->setDouble(i, summary.m_sum);
475  break;
477  outDSetItem->setDouble(i, summary.m_stdDeviation);
478  break;
479  case te::stat::VARIANCE:
480  outDSetItem->setDouble(i, summary.m_variance);
481  break;
482  case te::stat::SKEWNESS:
483  outDSetItem->setDouble(i, summary.m_skewness);
484  break;
485  case te::stat::KURTOSIS:
486  outDSetItem->setDouble(i, summary.m_kurtosis);
487  break;
488  case te::stat::AMPLITUDE:
489  outDSetItem->setDouble(i, summary.m_amplitude);
490  break;
491  case te::stat::MEDIAN:
492  outDSetItem->setDouble(i, summary.m_median);
493  break;
494  case te::stat::VAR_COEFF:
495  outDSetItem->setDouble(i, summary.m_varCoeff);
496  break;
497  case te::stat::MODE:
498  {
499  std::string mode;
500 
501  if(!summary.m_mode.empty())
502  {
503  mode = boost::lexical_cast<std::string>(summary.m_mode[0]);
504  for(std::size_t m = 1; m < summary.m_mode.size(); ++m)
505  {
506  mode += ",";
507  mode += boost::lexical_cast<std::string>(summary.m_mode[m]);
508  }
509  outDSetItem->setString(i, mode);
510  }
511  else
512  {
513  outDSetItem->setString(i, "");
514  }
515  break;
516  }
518  {
519  std::vector<double>::iterator itPixelDistinct =
520  pixelDistinct[band].begin();
521 
522  std::map<double, double>::iterator itPercent =
523  summary.m_percentEachClass.begin();
524 
525  while(itPixelDistinct != pixelDistinct[band].end())
526  {
527  if(itPercent != summary.m_percentEachClass.end())
528  {
529  std::string name = outDSetItem->getPropertyName(i);
530  std::vector<std::string> splitString;
531  boost::split(splitString, name, boost::is_any_of("_"));
532  if(splitString[1] ==
533  boost::lexical_cast<std::string>(itPercent->first))
534  {
535  outDSetItem->setDouble(i, itPercent->second);
536  ++itPercent;
537  }
538  else
539  {
540  outDSetItem->setDouble(i, 0);
541  }
542  }
543  else
544  {
545  outDSetItem->setDouble(i, 0);
546  }
547  ++itPixelDistinct;
548  ++i;
549  }
550  current_index += pixelDistinct[band].size() - 1;
551  break;
552  }
553  default:
554  continue;
555  }
556 
557  if (task.isActive() == false)
558  throw te::attributefill::Exception(TE_TR("Operation canceled!"));
559 
560  task.pulse();
561  }
562 
563  // texture
564  std::vector<te::rp::Texture> metrics;
565  init_index = current_index;
566 
567  if(m_texture == true)
568  {
569  metrics = getTexture(m_inRaster, geom.get(), m_bands[band]);
570  current_index += 5;
571  for(std::size_t t = 0, i = init_index; i < current_index; ++t, ++i)
572  {
573  switch(t)
574  {
575  case 0:
576  {
577  outDSetItem->setDouble(i, metrics[band].m_contrast);
578  break;
579  }
580  case 1:
581  {
582  outDSetItem->setDouble(i, metrics[band].m_dissimilarity);
583  break;
584  }
585  case 2:
586  {
587  outDSetItem->setDouble(i, metrics[band].m_energy);
588  break;
589  }
590  case 3:
591  {
592  outDSetItem->setDouble(i, metrics[band].m_entropy);
593  break;
594  }
595  case 4:
596  {
597  outDSetItem->setDouble(i, metrics[band].m_homogeneity);
598  break;
599  }
600  }
601  }
602  }
603 
604  init_index = current_index;
605  }
606  }
607  else
608  {
609  for(std::size_t i = 0; i < valuesFromRasterByBand.size(); ++i)
610  {
611  std::map<double, int>::iterator it = valuesFromRasterByBand[i].begin();
612 
613  while(it != valuesFromRasterByBand[i].end())
614  {
615  outDSetItem->setDouble(init_index, it->first);
616 
617  ++it;
618  }
619  }
620  }
621 
622  outDataset->add(outDSetItem);
623 
624  if(outDataset->size() == 2000)
625  {
626  save(outDataset.get(), outDsType.get());
627  outDataset->clear();
628  }
629 
630  if(task.isActive() == false)
631  throw te::attributefill::Exception(TE_TR("Operation canceled!"));
632  }
633 
634  if(!outDataset->isEmpty())
635  save(outDataset.get(), outDsType.get());
636 
637  return true;
638 }
639 
641  te::rst::Raster& inputRaster, unsigned int inputRasterBand,
642  std::vector<double>& values)
643 {
644  values.clear();
645 
646  const unsigned int nCols = inputRaster.getNumberOfColumns();
647  const unsigned int nRows = inputRaster.getNumberOfRows();
648  unsigned int col = 0;
649  unsigned int row = 0;
650  std::set<double> valuesSet;
651  double centerValue = 0;
652 
653  const te::rst::Band& inputBand = *inputRaster.getBand(inputRasterBand);
654 
655  const double inputBandDataValue = inputBand.getProperty()->m_noDataValue;
656 
657  for (row = 0; row < nRows; ++row)
658  {
659  for (col = 0; col < nCols; ++col)
660  {
661  inputBand.getValue(col, row, centerValue);
662 
663  if (centerValue != inputBandDataValue)
664  {
665  if (valuesSet.find(centerValue) == valuesSet.end())
666  valuesSet.insert(centerValue);
667  }
668  }
669  }
670 
671  std::set<double>::iterator valuesIt = valuesSet.begin();
672  while (valuesIt != valuesSet.end())
673  {
674  values.push_back(*valuesIt);
675  ++valuesIt;
676  }
677 }
678 
679 std::unique_ptr<te::da::DataSetType> te::attributefill::RasterToVector::getDataSetType(
680  std::vector<std::vector<double> > pixelDistinct)
681 {
682  std::unique_ptr<te::da::DataSetType> outdsType(new te::da::DataSetType(*m_inVectorDsType->getResult()));
683  outdsType->setCompositeName(m_outDset);
684  outdsType->setName(m_outDset);
685  outdsType->setTitle(m_outDset);
686 
687  te::da::PrimaryKey* pk = outdsType->getPrimaryKey();
688  pk->setName(outdsType->getName() + "_pkey");
689 
690  for(std::size_t b = 0; b < m_bands.size(); ++b)
691  {
692  if(!m_statSum.empty() || m_texture == true)
693  {
694  for(std::size_t i = 0; i < m_statSum.size(); ++i)
695  {
697  switch(m_statSum[i])
698  {
699  case 0:
700  prop = new te::dt::SimpleProperty(
701  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
702  "_Min_Value",
704  outdsType->add(prop);
705  break;
706  case 1:
707  prop = new te::dt::SimpleProperty(
708  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
709  "_Max_Value",
711  outdsType->add(prop);
712  break;
713  case 2:
714  prop = new te::dt::SimpleProperty(
715  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Mean",
717  outdsType->add(prop);
718  break;
719  case 3:
720  prop = new te::dt::SimpleProperty(
721  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Sum",
723  outdsType->add(prop);
724  break;
725  case 4:
726  prop = new te::dt::SimpleProperty(
727  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Count",
729  outdsType->add(prop);
730  break;
731  case 5:
732  prop = new te::dt::SimpleProperty(
733  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
734  "_Valid_Count",
736  outdsType->add(prop);
737  break;
738  case 6:
739  prop = new te::dt::SimpleProperty(
740  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
741  "_Standard_Deviation",
743  outdsType->add(prop);
744  break;
745  case 7:
746  prop = new te::dt::SimpleProperty(
747  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
748  "_Variance",
750  outdsType->add(prop);
751  break;
752  case 8:
753  prop = new te::dt::SimpleProperty(
754  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
755  "_Skewness",
757  outdsType->add(prop);
758  break;
759  case 9:
760  prop = new te::dt::SimpleProperty(
761  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
762  "_Kurtosis",
764  outdsType->add(prop);
765  break;
766  case 10:
767  prop = new te::dt::SimpleProperty(
768  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
769  "_Amplitude",
771  outdsType->add(prop);
772  break;
773  case 11:
774  prop = new te::dt::SimpleProperty(
775  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Median",
777  outdsType->add(prop);
778  break;
779  case 12:
780  prop = new te::dt::SimpleProperty(
781  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
782  "_Var_Coeff",
784  outdsType->add(prop);
785  break;
786  case 13:
787  prop = new te::dt::StringProperty(
788  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Mode");
789  outdsType->add(prop);
790  break;
791  case 14:
792  {
793  std::vector<double>::iterator it = pixelDistinct[b].begin();
794  while(it != pixelDistinct[b].end())
795  {
796  prop = new te::dt::SimpleProperty(
797  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_" +
798  boost::lexical_cast<std::string>(*it),
800  outdsType->add(prop);
801  ++it;
802  }
803  }
804  break;
805  default:
806  continue;
807  }
808  }
809  if(m_texture)
810  {
812  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Contrast",
814  outdsType->add(propContrast);
815 
816  te::dt::SimpleProperty* propDissimilarity = new te::dt::SimpleProperty(
817  "B" + boost::lexical_cast<std::string>(m_bands[b]) +
818  "_Dissimilarity",
820  outdsType->add(propDissimilarity);
821 
823  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Energy",
825  outdsType->add(propEnergy);
826 
828  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Entropy",
830  outdsType->add(propEntropy);
831 
832  te::dt::SimpleProperty* propHomogeneity = new te::dt::SimpleProperty(
833  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Homogeneity",
835  outdsType->add(propHomogeneity);
836  }
837  }
838  else
839  {
841  "B" + boost::lexical_cast<std::string>(m_bands[b]) + "_Value",
843  outdsType->add(propValue);
844  }
845  }
846 
847  return outdsType;
848 }
849 
852 {
853  te::rp::RasterAttributes rattributes;
854  std::vector<te::rp::Texture> textureVec;
855 
856  te::gm::Polygon* polygon;
857 
859  {
860  te::gm::MultiPolygon* mPolygon = dynamic_cast<te::gm::MultiPolygon*>(geom);
861  polygon = dynamic_cast<te::gm::Polygon*>(mPolygon->getGeometryN(0));
862  }
863  else
864  {
865  polygon = dynamic_cast<te::gm::Polygon*>(geom);
866  }
867 
868  // Retrieving the maximum and minimum GL values of the band.
869  double minPixel, maxPixel;
870  te::rst::GetDataTypeRanges(rst->getBandDataType(band), minPixel, maxPixel);
871 
872  if((maxPixel - minPixel) > 255)
873  {
876  const std::complex<double>* cmin = rsMin->at(band).m_minVal;
877  const std::complex<double>* cmax = rsMax->at(band).m_maxVal;
878 
879  minPixel = cmin->real();
880  maxPixel = cmax->real();
881  }
882 
883  boost::numeric::ublas::matrix<double> glcm =
884  rattributes.getGLCM(*rst, band, 1, 1, *polygon, minPixel, maxPixel);
885  te::rp::Texture metrics = rattributes.getGLCMMetrics(glcm);
886  textureVec.push_back(metrics);
887 
888  return textureVec;
889 }
890 
891 void te::attributefill::RasterToVector::joinMaps(std::map<double, int>& mainMap, std::map<double, int>& newMap)
892 {
893  std::map<double, int>::iterator it_newMap = newMap.begin();
894 
895  while(it_newMap != newMap.end())
896  {
897  mainMap[it_newMap->first] += it_newMap->second;
898 
899  ++it_newMap;
900  }
901 }
902 
903 void te::attributefill::RasterToVector::joinMaps(std::map<double, double>& mainMap, std::map<double, double>& newMap)
904 {
905  std::map<double, double>::iterator it_newMap = newMap.begin();
906 
907  while(it_newMap != newMap.end())
908  {
909  mainMap[it_newMap->first] += it_newMap->second;
910 
911  ++it_newMap;
912  }
913 }
914 
916 {
917  // do any adaptation necessary to persist the output dataset
918  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(outDsType, m_outDsrc->getCapabilities());
919  te::da::DataSetType* dsTypeResult = converter->getResult();
920 
921  std::unique_ptr<te::da::DataSourceTransactor> t = m_outDsrc->getTransactor();
922 
923  std::map<std::string, std::string> options;
924 
925  try
926  {
927  t->begin();
928 
929  std::string dsTypeName = outDsType->getName();
930  std::vector<std::string> dataSetNames = t->getDataSetNames();
931  bool create = true;
932 
933  for (std::size_t i = 0; i < dataSetNames.size(); ++i)
934  {
935  std::vector<std::string> tok;
936  te::common::Tokenize(dataSetNames[i], tok, ".");
937 
938  std::string temp_dsName;
939 
940  if (tok.size() > 1)
941  temp_dsName = tok[1];
942  else
943  temp_dsName = dataSetNames[i];
944 
945  if (dsTypeName == temp_dsName)
946  {
947  create = false;
948  }
949  }
950 
951  if (create)
952  {
953  // create the dataset
954  t->createDataSet(dsTypeResult, options);
955  }
956 
957  // copy from memory to output datasource
958  result->moveBeforeFirst();
959  t->add(dsTypeName, result, options);
960 
961  t->commit();
962  }
963  catch(te::common::Exception& e)
964  {
965  t->rollBack();
966  throw e;
967  }
968  catch(std::exception& e)
969  {
970  t->rollBack();
971  throw e;
972  }
973 }
virtual void setName(const std::string &name)
It sets the constraint name.
Definition: Constraint.h:126
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
void getPixelDistinct(rst::Raster &inputRaster, unsigned int inputRasterBand, std::vector< double > &values)
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
void setInput(te::rst::Raster *inRaster, te::da::DataSourcePtr inVectorDsrc, std::string inVectorName, te::da::DataSetTypeConverter *inVectorDsType, const te::da::ObjectIdSet *oidSet=0)
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
unsigned int band
TESTATEXPORT void GetPercentOfEachClassByArea(std::map< double, int > &values, double &resolutionX, double &resolutionY, double &area, te::stat::NumericStatisticalSummary &ss)
Geometric property.
A structure to hold the set of statistics from a set of numerical values.
TEATTRIBUTEFILLEXPORT void GetValuesFromBand(const te::rst::Raster &raster, const unsigned int &band, const gm::Polygon &polygon, const uint32_t &minimumRow, const uint32_t &minimumColumn, const uint32_t &maximumRow, const uint32_t &maximumColumn, std::map< double, int > &values)
Gets the pixel values for a specific band that intersects the polygon.
double y
y-coordinate.
Definition: Coord2D.h:114
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
An atomic property like an integer or double.
std::string getPropertyName(std::size_t pos) const
It returns the name of the pos-th property.
boost::shared_ptr< DataSource > DataSourcePtr
double x
x-coordinate.
Definition: Coord2D.h:113
te::da::DataSourcePtr m_outDsrc
Total number of values.
A class that models the description of a dataset.
Definition: DataSetType.h:72
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
void useTimer(bool flag)
Used to define if task use progress timer information.
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
std::vector< te::rp::Texture > getTexture(te::rst::Raster *rst, te::gm::Geometry *geom, int band)
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
std::vector< te::stat::StatisticalSummary > m_statSum
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
Definition: BandProperty.h:136
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
bool isActive() const
Verify if the task is active.
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
std::map< double, double > m_percentEachClass
Extraction of attributes from Raster, Bands, and Polygons.
TEATTRIBUTEFILLEXPORT void GetMinMaxLineAndColumn(const te::rst::Raster &raster, const te::gm::Polygon &polygon, uint32_t &minimumRow, uint32_t &minimumColumn, uint32_t &maximumRow, uint32_t &maximumColumn)
Gets the minimum and maximum row and column values of the raster based on polygon.
void setTotalSteps(int value)
Set the task total stepes.
An converter for DataSetType.
unsigned int unsigned int nCols
int b
Definition: TsRtree.cpp:32
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:221
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
int getSRID() const
It returns the spatial reference system identifier associated to this property.
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:152
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
boost::numeric::ublas::matrix< double > getGLCM(const te::rst::Raster &rin, unsigned int band, int dx, int dy, double minPixel, double maxPixel, double gLevels=256)
Computes the Gray-Level Co-occurrence Matrix (GLCM) from a raster band.
An Envelope defines a 2D rectangular region.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
An abstract class for raster data strucutures.
const te::da::ObjectIdSet * m_oidSet
unsigned int getNumberOfRows() const
Returns the raster number of rows.
BandProperty * getProperty()
Returns the band property.
Raster to Vector processing.
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
std::unique_ptr< te::da::DataSetTypeConverter > m_inVectorDsType
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
list bands
Definition: compose.py:2
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
te::da::DataSourcePtr m_inVectorDsrc
TESTATEXPORT void GetNumericStatisticalSummary(std::vector< double > &values, te::stat::NumericStatisticalSummary &ss, double nullValue)
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
void save(te::da::DataSet *result, te::da::DataSetType *outDsType)
std::vector< unsigned int > m_bands
A raster band description.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void setParams(std::vector< unsigned int > bands, std::vector< te::stat::StatisticalSummary > statSum, bool iteratorByBox, bool texture)
te::rp::Texture getGLCMMetrics(boost::numeric::ublas::matrix< double > glcm, double noDataValue=0.0)
Compute texture metrics from GLCM matrix.
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
int getSRID() const
Returns the raster spatial reference system identifier.
std::vector< double > getValuesFromBand(const te::rst::Raster &raster, unsigned int band, const te::gm::Polygon &polygon)
Returns the pixel values for the band, inside the polygon.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
void joinMaps(std::map< double, int > &mainMap, std::map< double, int > &newMap)
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
TEATTRIBUTEFILLEXPORT void GetPercentOfEachClassByArea(const te::rst::Raster &raster, const unsigned int &band, const gm::Polygon &polygon, const uint32_t &minimumRow, const uint32_t &minimumColumn, const uint32_t &maximumRow, const uint32_t &maximumColumn, std::map< double, double > &percentOfEachClassByArea)
Gets the pixel percentage for a specific band that intersects the polygon.
std::unique_ptr< da::DataSetType > getDataSetType(std::vector< std::vector< double > > pixelDistinct=std::vector< std::vector< double > >())
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
A structure to hold the set of GLCM metrics.
Definition: Texture.h:44
StatisticalSummary
Define grouping functions type.
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
Calculate the min value.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Calculate the max value.
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:138
unsigned int col
An exception class for the Attribute Fill module.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127