VectorToVectorMemory.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 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 VectorToVectorMemory.h
22 
23  \brief Vector to Vector processing.
24 
25  \ingroup attributefill
26  */
27 
28 //Terralib
29 #include "../BuildConfig.h"
30 #include "../common/Logger.h"
31 #include "../common/progress/TaskProgress.h"
32 #include "../common/STLUtils.h"
33 #include "../common/StringUtils.h"
34 #include "../common/Translator.h"
35 #include "../dataaccess/dataset/DataSetAdapter.h"
36 #include "../dataaccess/dataset/DataSetTypeConverter.h"
37 #include "../dataaccess/utils/Utils.h"
38 #include "../datatype/StringProperty.h"
39 #include "../datatype/SimpleData.h"
40 #include "../geometry.h"
41 #include "../memory/DataSet.h"
42 #include "../memory/DataSetItem.h"
43 #include "../statistics/core/NumericStatisticalSummary.h"
44 #include "../statistics/core/StringStatisticalSummary.h"
45 #include "../statistics/core/SummaryFunctions.h"
46 #include "../statistics/core/Enums.h"
47 
48 #include "Config.h"
49 #include "VectorToVectorMemory.h"
50 
51 // STL
52 #include <memory>
53 
54 // Boost
55 #include <boost/algorithm/string/replace.hpp>
56 #include <boost/lexical_cast.hpp>
57 
59 {
60  std::string invalidChar;
61  bool changed;
62  if(!te::da::IsValidName(name, invalidChar))
63  {
64  name = te::common::ReplaceSpecialChars(name, changed);
65 
66  boost::replace_all(name, "/", "_");
67  boost::replace_all(name, " ", "_");
68  boost::replace_all(name, ".", "");
69  boost::replace_all(name, "'", "");
70  boost::replace_all(name, "-", "_");
71  boost::replace_all(name, "&", "e");
72  }
73 }
74 
76 {
77 
78 }
79 
81 {
82  te::common::FreeContents(m_mapGeom);
83  m_mapGeom.clear();
84 }
85 
87 {
88  int fromSrid = m_fromLayer->getSRID();
89  int toSrid = m_toLayer->getSRID();
90 
91  std::auto_ptr<te::da::DataSetType> fromSchemaOrigin = m_fromLayer->getSchema();
92 
93  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(fromSchemaOrigin.get(), m_outDsrc->getCapabilities(), m_outDsrc->getEncoding());
94 
95  te::da::AssociateDataSetTypeConverterSRID(converter, fromSrid, toSrid);
96 
97  std::auto_ptr<te::da::DataSetType> fromSchema(converter->getResult());
98 
99  std::auto_ptr<te::da::DataSet> fromDsOrigin = m_fromLayer->getData();
100 
101  std::auto_ptr<te::da::DataSetAdapter> fromDs(te::da::CreateAdapter(fromDsOrigin.get(), converter));
102 
103  te::gm::Envelope fromEnv = m_fromLayer->getExtent();
104 
105  te::gm::Envelope toEnv = m_toLayer->getExtent();
106  std::auto_ptr<te::da::DataSet> toDs = m_toLayer->getData();
107  std::auto_ptr<te::da::DataSetType> toSchema = m_toLayer->getSchema();
108 
109  if (fromSrid != toSrid)
110  fromEnv.transform(fromSrid, toSrid);
111 
112  if(!fromEnv.intersects(toEnv))
113  {
114  throw te::common::Exception(TE_TR("The two layers do not intersect."));
115  }
116 
117  std::auto_ptr<te::da::DataSetType> outDst(getOutputDataSetType());
118 
119  std::auto_ptr<te::mem::DataSet> outDs(new te::mem::DataSet(outDst.get()));
120 
121  te::sam::rtree::Index<size_t, 8>* rtree = getRtree(fromDs.get());
122  KD_ADAPTATIVE_TREE* kdtree = 0;
123 
124  toDs->moveBeforeFirst();
125 
126  te::common::TaskProgress task("Processing Vector to Vector...");
127  task.setTotalSteps((int)toDs->size());
128  task.useTimer(true);
129 
130  std::string logInfo1 = "";
131  std::string logInfo2 = "";
132 
133  std::vector<te::dt::Property*> outProps = outDst->getProperties();
134  std::vector<te::dt::Property*> toProps = toSchema->getProperties();
135 
136  while(toDs->moveNext())
137  {
138  try
139  {
140  te::mem::DataSetItem* item = new te::mem::DataSetItem(outDs.get());
141 
142  for(std::size_t i = 0; i < outProps.size(); ++i)
143  {
144  std::string outPropName = outProps[i]->getName();
145 
146  for(std::size_t j = 0; j < toProps.size(); ++j)
147  {
148  if(toProps[j]->getName() == outPropName)
149  {
150  item->setValue(outPropName, toDs->getValue(outPropName).release());
151  break;
152  }
153  }
154  }
155 
156  bool hasInvalid = false;
157  std::vector<std::size_t> intersections = getIntersections(toDs.get(), fromDs.get(), rtree, hasInvalid);
158 
159  if (hasInvalid)
160  {
161 #ifdef TERRALIB_LOGGER_ENABLED
162  te::da::PrimaryKey* toPk = toSchema->getPrimaryKey();
163  te::dt::Property* pkProp = toPk->getProperties()[0];
164 
165  std::string ex = "The \"To\" layer geometry (" + pkProp->getName() + ": " + toDs->getValue(pkProp->getName())->toString() + ") has intersection candidate invalid!";
166  te::common::Logger::logDebug("attributefill", ex.c_str());
167 #endif //TERRALIB_LOGGER_ENABLED
168 
169  m_hasErrors = true;
170  outDs->add(item);
171  continue;
172  }
173 
174 
175  if(intersections.empty() && !hasNoIntersectionOperations())
176  {
177  outDs->add(item);
178  continue;
179  }
180 
181  std::map<std::string, std::vector<te::attributefill::OperationType> >::iterator it = m_options.begin();
182 
183  std::vector< std::vector<te::dt::AbstractData*> > dataValues;
184 
185  dataValues = getDataValues(fromDs.get(), intersections);
186 
187  while(it != m_options.end())
188  {
189  logInfo1 = it->first;
190 
191  te::dt::Property* prop = fromSchema->getProperty(it->first);
192  std::size_t propPos = fromSchema->getPropertyPosition(it->first);
193 
196 
197  std::vector<double> numValues;
198  std::vector<std::string> strValues;
199 
200  if(prop->getType() == te::dt::STRING_TYPE)
201  {
202  strValues = getStrValues(dataValues, propPos);
203  te::stat::GetStringStatisticalSummary(strValues, ssStr, "");
204  }
205  else
206  {
207  numValues = getNumValues(dataValues, propPos);
208  te::stat::GetNumericStatisticalSummary(numValues, ssNum);
209  }
210 
211  std::vector<te::attributefill::OperationType> funcs = it->second;
212 
213  for(std::size_t i = 0; i < funcs.size(); ++i)
214  {
215  std::string outPropName = getPropertyName(prop, funcs[i]);
216 
217  if(funcs[i] == te::attributefill::VALUE)
218  {
219  if(prop->getType() == te::dt::STRING_TYPE)
220  item->setString(outPropName, ssStr.m_minVal);
221  else
222  item->setDouble(outPropName, ssNum.m_minVal);
223  }
224  else if(funcs[i] == te::attributefill::HIGHEST_INTERSECTION)
225  {
226  te::dt::AbstractData* value = getClassWithHighestIntersectionArea(toDs.get(), toSrid, fromDs.get(), fromSrid, intersections, prop->getName(), dataValues);
227 
228  item->setValue(outPropName, value);
229  }
230  else if(funcs[i] == te::attributefill::HIGHEST_OCCURRENCE)
231  {
232  te::dt::AbstractData* value = getClassWithHighestOccurrence(fromDs.get(), intersections, prop->getName(), dataValues);
233  if (value)
234  item->setValue(outPropName, value);
235  }
236  else if(funcs[i] == te::attributefill::PERCENT_CLASS)
237  {
238  std::map<std::string, double> result = getPercentagePerClass(fromDs.get(), intersections, prop->getName(), dataValues);
239 
240  std::map<std::string, double>::iterator itAux = result.begin();
241  while(itAux != result.end())
242  {
243  std::string className = itAux->first;
244  normalizeClassName(className);
245  std::string newPropName = prop->getName() + "_" + className;
246 
247  item->setDouble(newPropName, itAux->second);
248 
249  ++itAux;
250  }
251  }
252  else if(funcs[i] == te::attributefill::PRESENCE)
253  {
254  if(intersections.size() > 0)
255  {
256  item->setInt32(outPropName, 1);
257  }
258  else
259  {
260  item->setInt32(outPropName, 0);
261  }
262  }
263  else if(funcs[i] == te::attributefill::PERCENT_TOTAL_AREA)
264  {
265  double area = getPercentageOfTotalArea(toDs.get(), toSrid, fromDs.get(), fromSrid, intersections, prop->getName(), dataValues);
266 
267  item->setDouble(outPropName, area);
268  }
269  else if(funcs[i] == te::attributefill::PERCENT_EACH_CLASS)
270  {
271  std::map<std::string, double> result = getPercentageOfEachClassByArea(toDs.get(), toSrid, fromDs.get(), fromSrid, intersections, prop->getName(), dataValues);
272 
273  std::map<std::string, double>::iterator itAux = result.begin();
274  while(itAux != result.end())
275  {
276  std::string className = itAux->first;
277  normalizeClassName(className);
278  std::string newPropName = prop->getName() + "_" + className;
279 
280  item->setDouble(newPropName, itAux->second);
281 
282  ++itAux;
283  }
284  }
285  else if(funcs[i] == te::attributefill::WEIGHTED)
286  {
287  double weigh = getWeightedByArea(toDs.get(), toSrid, fromDs.get(), fromSrid, intersections, prop->getName(), dataValues);
288 
289  item->setDouble(outPropName, weigh);
290  }
291  else if(funcs[i] == te::attributefill::WEIGHTED_SUM)
292  {
293  double weigh = getWeightedSumByArea(toDs.get(), toSrid, fromDs.get(), fromSrid, intersections, prop->getName(), dataValues);
294 
295  item->setDouble(outPropName, weigh);
296  }
297  else if(funcs[i] == te::attributefill::MIN_DISTANCE)
298  {
299  double result = 0;
300 
301  if(intersections.empty())
302  {
303  if(!kdtree)
304  kdtree = getKDtree(fromDs.get(), toSrid);
305 
306  result = getMinimumDistance(toDs.get(), toSrid, fromDs.get(), fromSrid, kdtree);
307  }
308 
309  item->setDouble(outPropName, result);
310  }
311  else if(isStatistical(funcs[i]))
312  {
313  if(prop->getType() == te::dt::STRING_TYPE)
314  {
315  std::string value = getValue(ssStr, funcs[i]);
316  item->setString(outPropName, value);
317  }
318  else
319  {
320  if(funcs[i] == te::attributefill::MODE)
321  {
322  std::string value = getModeValue(ssNum);
323  item->setString(outPropName, value);
324  }
325  else
326  {
327  double value = getValue(ssNum, funcs[i]);
328  item->setDouble(outPropName, value);
329  }
330  }
331  }
332  }
333 
334  ++it;
335  }
336 
337  for (std::size_t a = 0; a < dataValues.size(); ++a)
338  te::common::FreeContents(dataValues[a]);
339 
340  dataValues.clear();
341 
342  outDs->add(item);
343 
344  if (task.isActive() == false)
345  throw te::common::Exception(TE_TR("Operation canceled!"), 1);
346 
347  task.pulse();
348  }
349  catch(te::common::Exception& e)
350  {
351  if(e.code() == 1)
352  {
353  throw e;
354  }
355 
356 #ifdef TERRALIB_LOGGER_ENABLED
357  std::string ex = e.what();
358  ex += " | Ref: " + logInfo1 + " : " + logInfo2;
359  te::common::Logger::logDebug("attributefill", ex.c_str());
360 #endif //TERRALIB_LOGGER_ENABLED
361 
362  m_hasErrors = true;
363  }
364  catch(std::exception& e)
365  {
366 #ifdef TERRALIB_LOGGER_ENABLED
367  std::string ex = e.what();
368  ex += " | Ref: " + logInfo1 + " : " + logInfo2;
369  te::common::Logger::logDebug("attributefill", ex.c_str());
370 #endif //TERRALIB_LOGGER_ENABLED
371  m_hasErrors = true;
372  }
373  }
374 
375  if (!outDs->isEmpty())
376  save(outDs, outDst);
377 
378  if(rtree)
379  delete rtree;
380 
381  if(kdtree)
382  delete kdtree;
383 
384  return true;
385 }
386 
388 {
389  std::auto_ptr<te::da::DataSet> fromDs = m_fromLayer->getData();
390  std::auto_ptr<te::da::DataSetType> fromSchema = m_fromLayer->getSchema();
391 
392  std::auto_ptr<te::da::DataSetType> toScheme = m_toLayer->getSchema();
393 
394  te::da::DataSetType* dst = new te::da::DataSetType(*toScheme.get());
395  dst->setName(m_outDset);
396  dst->setTitle(m_outDset);
397 
398  std::vector<te::dt::Property*> outProps = dst->getProperties();
399  std::vector<te::dt::Property*> pkProps = dst->getPrimaryKey()->getProperties();
400 
401  // Keep only the properties that the user selected
402  for(std::size_t i = 0; i < outProps.size(); ++i)
403  {
404  bool isPk = false;
405  for(std::size_t j = 0; j < pkProps.size(); ++j)
406  {
407  if(outProps[i]->getName() == pkProps[j]->getName())
408  {
409  isPk = true;
410  break;
411  }
412  }
413 
414  if(outProps[i]->getType() != te::dt::GEOMETRY_TYPE && !isPk)
415  {
416  std::string name = outProps[i]->getName();
417  if(std::find(m_toLayerProps.begin(), m_toLayerProps.end(), name) == m_toLayerProps.end())
418  dst->remove(outProps[i]);
419  }
420  }
421 
422  std::map<std::string, std::vector<te::attributefill::OperationType> >::iterator it = m_options.begin();
423 
424  while(it != m_options.end())
425  {
426  te::dt::Property* currentProperty = fromSchema->getProperty(it->first);
427 
428  std::vector<te::attributefill::OperationType> funcs = it->second;
429 
430  for(std::size_t i = 0; i < funcs.size(); ++i)
431  {
432  te::dt::SimpleProperty* newProp = 0;
433 
434  std::string newName = getPropertyName(currentProperty, (te::attributefill::OperationType)funcs[i]);
435 
436  if(funcs[i] == te::attributefill::VALUE)
437  {
438  if(currentProperty->getType() == te::dt::STRING_TYPE)
439  newProp = new te::dt::StringProperty(newName);
440  else
441  newProp = new te::dt::SimpleProperty(newName, te::dt::DOUBLE_TYPE);
442  }
443  else if(funcs[i] == te::attributefill::HIGHEST_OCCURRENCE ||
445  {
446  newProp = dynamic_cast<te::dt::SimpleProperty*>(currentProperty->clone());
447  newProp->setRequired(false);
448  newProp->setAutoNumber(false);
449  newProp->setDefaultValue(0);
450  newProp->setName(newName);
451  }
453  {
454  continue;//Sera feito fora do for
455  }
456  else if(funcs[i] == te::attributefill::PRESENCE)
457  {
458  newProp = new te::dt::SimpleProperty(newName, te::dt::INT32_TYPE);
459  }
460  else if(funcs[i] == te::attributefill::PERCENT_TOTAL_AREA ||
462  {
463  newProp = new te::dt::SimpleProperty(newName, te::dt::DOUBLE_TYPE);
464  }
465  else if(currentProperty->getType() == te::dt::STRING_TYPE || funcs[i] == te::attributefill::MODE)
466  {
467  newProp = new te::dt::StringProperty(newName);
468  }
469  else
470  {
471  newProp = new te::dt::SimpleProperty(newName, te::dt::DOUBLE_TYPE);
472  }
473 
474  dst->add(newProp);
475  }
476 
477  if(std::find(funcs.begin(), funcs.end(), te::attributefill::PERCENT_CLASS) != funcs.end() ||
478  std::find(funcs.begin(), funcs.end(), te::attributefill::PERCENT_EACH_CLASS) != funcs.end())
479  {
480  std::vector<std::string> strClasses = getDistinctClasses(fromDs.get(), currentProperty->getName());
481 
482  for(std::size_t i = 0; i < strClasses.size(); ++i)
483  {
484  std::string className = strClasses[i];
485 
486  normalizeClassName(className);
487 
488  std::string newPropName = currentProperty->getName() + "_" + className;
489 
491 
492  dst->add(newProp);
493  }
494  }
495 
496  ++it;
497  }
498 
499  return dst;
500 }
501 
503  const std::string& propertyName)
504 {
505  std::vector<std::string> result;
506 
507  fromDs->moveBeforeFirst();
508 
509  while(fromDs->moveNext())
510  {
511  std::string strClass = fromDs->getAsString(propertyName, 9);
512 
513  if(std::find(result.begin(), result.end(), strClass) == result.end())
514  {
515  result.push_back(strClass);
516  }
517  }
518 
519  return result;
520 }
521 
523 {
525 
526  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(data);
527 
528  data->moveBeforeFirst();
529 
530  int count = 0;
531 
532  te::common::FreeContents(m_mapGeom);
533  m_mapGeom.clear();
534 
535  while(data->moveNext())
536  {
537  std::auto_ptr<te::gm::Geometry> geom = data->getGeometry(geomPos);
538 
539  rtree->insert(*geom->getMBR(), count);
540 
541  m_mapGeom.insert(std::map<int, te::gm::Geometry*>::value_type(count, geom.release()));
542 
543  ++count;
544  }
545 
546  return rtree;
547 }
548 
550 {
551  std::string name = te::common::Convert2LCase(prop->getName());
552 
553  std::string newName = name + "_";
554 
555  if(func == te::attributefill::VALUE)
556  newName += "value";
557  else if(func == te::attributefill::MIN_VALUE)
558  newName += "min_val";
559  else if(func == te::attributefill::MAX_VALUE)
560  newName += "max_val";
561  else if(func == te::attributefill::MEAN)
562  newName += "mean";
563  else if(func == te::attributefill::SUM)
564  newName += "sum_values";
565  else if(func == te::attributefill::COUNT)
566  newName += "total_values";
567  else if(func == te::attributefill::VALID_COUNT)
568  newName += "total_notnull_values";
570  newName += "stand_dev";
571  else if(func == te::attributefill::VARIANCE)
572  newName += "variance";
573  else if(func == te::attributefill::SKEWNESS)
574  newName += "skewness";
575  else if(func == te::attributefill::KURTOSIS)
576  newName += "kurtosis";
577  else if(func == te::attributefill::AMPLITUDE)
578  newName += "amplitude";
579  else if(func == te::attributefill::MEDIAN)
580  newName += "median";
581  else if(func == te::attributefill::VAR_COEFF)
582  newName += "coeff_variation";
583  else if(func == te::attributefill::MODE)
584  newName += "mode";
586  newName += "class_high_occurrence";
588  newName += "class_high_area";
589  else if(func == te::attributefill::MIN_DISTANCE)
590  newName += "min_distance";
591  else if(func == te::attributefill::PRESENCE)
592  newName += "presence";
594  newName += "percent_of_total_area";
596  newName += "percent_area_class";
597  else if(func == te::attributefill::WEIGHTED)
598  newName += "weigh_area";
599  else if(func == te::attributefill::WEIGHTED_SUM)
600  newName += "weigh_sum_area";
601  else if(func == te::attributefill::PERCENT_CLASS)
602  newName += "percent_class";
603 
604  return newName;
605 }
606 
610  bool& hasInvalid)
611 {
612  std::size_t toSpatialPos = te::da::GetFirstSpatialPropertyPos(toDs);
613 
614  std::auto_ptr<te::gm::Geometry> geom = toDs->getGeometry(toSpatialPos);
615 
616  std::vector<size_t> report;
617  rtree->search(*geom->getMBR(), report);
618 
619  std::vector<std::size_t> interVec;
620  for(std::size_t i = 0; i < report.size(); ++i)
621  {
622  //fromDs->move(report[i]);
623 
624  te::gm::Geometry* g = m_mapGeom[report[i]];//fromDs->getGeometry(fromSpatialPos);
625 
626  if (!g->isValid())
627  hasInvalid = true;
628 
629  if(geom->intersects(g))
630  {
631  interVec.push_back(report[i]);
632  }
633  }
634  return interVec;
635 }
636 
637 std::vector<double> te::attributefill::VectorToVectorMemory::getNumValues(std::vector< std::vector<te::dt::AbstractData*> > dataValues, std::size_t pos)
638 {
639  std::vector<double> result;
640 
641  for (std::size_t t = 0; t < dataValues.size(); ++t)
642  {
643  std::vector<te::dt::AbstractData*> data = dataValues[t];
644 
645  if (!data[pos])
646  {
647  result.push_back(0.0f);
648  continue;
649  }
650 
651  if (data[pos]->getTypeCode() == te::dt::INT16_TYPE ||
652  data[pos]->getTypeCode() == te::dt::UINT16_TYPE ||
653  data[pos]->getTypeCode() == te::dt::INT32_TYPE ||
654  data[pos]->getTypeCode() == te::dt::UINT32_TYPE ||
655  data[pos]->getTypeCode() == te::dt::INT64_TYPE ||
656  data[pos]->getTypeCode() == te::dt::UINT64_TYPE ||
657  data[pos]->getTypeCode() == te::dt::FLOAT_TYPE ||
658  data[pos]->getTypeCode() == te::dt::DOUBLE_TYPE ||
659  data[pos]->getTypeCode() == te::dt::CINT16_TYPE ||
660  data[pos]->getTypeCode() == te::dt::CINT32_TYPE ||
661  data[pos]->getTypeCode() == te::dt::CFLOAT_TYPE ||
662  data[pos]->getTypeCode() == te::dt::CDOUBLE_TYPE)
663  {
664  std::string strValue = data[pos]->toString();
665 
666  result.push_back(boost::lexical_cast<double>(strValue));
667  }
668  }
669 
670  return result;
671 }
672 
673 std::vector<std::string> te::attributefill::VectorToVectorMemory::getStrValues(std::vector< std::vector<te::dt::AbstractData*> > dataValues, std::size_t pos)
674 {
675  std::vector<std::string> result;
676 
677  for (std::size_t t = 0; t < dataValues.size(); ++t)
678  {
679  std::vector<te::dt::AbstractData*> data = dataValues[t];
680 
681  if (!data[pos])
682  {
683  result.push_back("");
684  continue;
685  }
686 
687  if (data[pos]->getTypeCode() == te::dt::STRING_TYPE)
688  result.push_back(data[pos]->toString());
689 
690  }
691 
692  return result;
693 }
694 
696 {
697  if(type == te::attributefill::MIN_VALUE ||
699  type == te::attributefill::MEAN ||
700  type == te::attributefill::SUM ||
701  type == te::attributefill::COUNT ||
704  type == te::attributefill::VARIANCE ||
705  type == te::attributefill::SKEWNESS ||
706  type == te::attributefill::KURTOSIS ||
708  type == te::attributefill::MEDIAN ||
710  type == te::attributefill::MODE)
711  {
712  return true;
713  }
714 
715  return false;
716 }
717 
719 {
720  if(type == te::attributefill::AMPLITUDE)
721  return ss.m_amplitude;
722  else if(type == te::attributefill::COUNT)
723  return ss.m_count;
724  else if(type == te::attributefill::KURTOSIS)
725  return ss.m_kurtosis;
726  else if(type == te::attributefill::MAX_VALUE)
727  return ss.m_maxVal;
728  else if(type == te::attributefill::MEAN)
729  return ss.m_mean;
730  else if(type == te::attributefill::MEDIAN)
731  return ss.m_median;
732  else if(type == te::attributefill::MIN_VALUE)
733  return ss.m_minVal;
734  else if(type == te::attributefill::SKEWNESS)
735  return ss.m_skewness;
737  return ss.m_stdDeviation;
738  else if(type == te::attributefill::SUM)
739  return ss.m_sum;
740  else if(type == te::attributefill::VALID_COUNT)
741  return ss.m_validCount;
742  else if(type == te::attributefill::VAR_COEFF)
743  return ss.m_varCoeff;
744  else if(type == te::attributefill::VARIANCE)
745  return ss.m_variance;
746  else
747  return -1;
748 }
749 
751 {
752  if(type == te::attributefill::MAX_VALUE)
753  return ss.m_maxVal;
754  else if(type == te::attributefill::MIN_VALUE)
755  return ss.m_minVal;
756  else if(type == te::attributefill::MODE)
757  return ss.m_mode;
758  else if(type == te::attributefill::COUNT)
759  return boost::lexical_cast<std::string>(ss.m_count);
760  else if(type == te::attributefill::VALID_COUNT)
761  return boost::lexical_cast<std::string>(ss.m_validCount);
762  else
763  return "null";
764 }
765 
767 {
768  std::string result = "";
769  std::vector<double> values = ss.m_mode;
770  for(std::size_t i = 0; i < values.size(); ++i)
771  {
772  if(i == 0)
773  result += boost::lexical_cast<std::string>(values[i]);
774  else
775  result += ", " + boost::lexical_cast<std::string>(values[i]);
776  }
777 
778  return result;
779 }
780 
781 std::vector<std::vector<te::dt::AbstractData*> > te::attributefill::VectorToVectorMemory::getDataValues(te::da::DataSet* fromDs,
782  std::vector<std::size_t> dsPos)
783 {
784  std::vector<std::vector<te::dt::AbstractData*> > result;
785 
786  for(std::size_t i = 0; i < dsPos.size(); ++i)
787  {
788  std::vector<te::dt::AbstractData*> resultItem;
789 
790  fromDs->move(dsPos[i]);
791 
792  for (std::size_t t = 0; t < fromDs->getNumProperties(); ++t)
793  {
794  if (fromDs->isNull(t))
795  resultItem.push_back(0);
796  else
797  resultItem.push_back(fromDs->getValue(t).release());
798  }
799  result.push_back(resultItem);
800  }
801 
802  return result;
803 }
804 
806  std::vector<std::size_t> dsPos,
807  const std::string& propertyName,
808  std::vector< std::vector<te::dt::AbstractData*> >& dataValues)
809 {
810  int propIndex = te::da::GetPropertyIndex(fromDs, propertyName);
811  int propType = fromDs->getPropertyDataType(propIndex);
812 
813  std::size_t highOccur = 0;
814  std::vector<std::string> highValues;
815 
816  std::map<std::string, std::size_t> counter;
817  for (std::size_t i = 0; i < dsPos.size(); ++i)
818  {
819  //dataValues[i][propIndex];
820 
821  if (!dataValues[i][propIndex])
822  continue;
823 
824  std::string value = dataValues[i][propIndex]->toString();
825 
826  if (counter.find(value) == counter.end())
827  {
828  counter[value] = 1;
829  highOccur = 1;
830  }
831  else
832  {
833  std::size_t aux = counter[value] + 1;
834  counter[value] = aux;
835 
836  if (aux > highOccur)
837  highOccur = aux;
838  }
839  }
840 
841  std::map<std::string, std::size_t>::iterator it = counter.begin();
842  while (it != counter.end())
843  {
844  if (it->second == highOccur)
845  highValues.push_back(it->first);
846  ++it;
847  }
848 
849  if (highValues.size() > 1)
850  {
851  std::vector<double> intVec;
852 
853  if (propType == te::dt::STRING_TYPE)
854  {
856  te::stat::GetStringStatisticalSummary(highValues, ssStr);
857 
858  return getDataBasedOnType(ssStr.m_minVal, propType);
859  }
860  else
861  {
862  for (std::size_t i = 0; i < highValues.size(); ++i)
863  {
864  intVec.push_back(boost::lexical_cast<double>(highValues[i]));
865  }
866 
869  std::string strVal = boost::lexical_cast<std::string>(ssNum.m_minVal);
870  return getDataBasedOnType(strVal, propType);
871  }
872  }
873  else if (!highValues.empty())
874  return getDataBasedOnType(highValues[0], propType);
875  else
876  return 0;
877 }
878 
880  std::size_t toSrid,
881  te::da::DataSet* fromDs,
882  std::size_t,
883  std::vector<std::size_t> dsPos,
884  const std::string& propertyName,
885  std::vector< std::vector<te::dt::AbstractData*> >& dataValues)
886 {
887  std::size_t toGeomPos = te::da::GetFirstSpatialPropertyPos(toDs);
888 
889  int propIndex = te::da::GetPropertyIndex(fromDs, propertyName);
890  int propType = fromDs->getPropertyDataType(propIndex);
891 
892  std::auto_ptr<te::gm::Geometry> toGeom = toDs->getGeometry(toGeomPos);
893  if(toGeom->getSRID() <= 0)
894  toGeom->setSRID((int)toSrid);
895 
896  std::map<std::string, double> classAreaMap;
897  for(std::size_t i = 0; i < dsPos.size(); ++i)
898  {
899  //dataValues[i][propIndex];
900 
901  if (!dataValues[i][propIndex])
902  continue;
903 
904  te::gm::Geometry* fromGeom = m_mapGeom[dsPos[i]]; //fromDs->getGeometry(fromGeomPos);
905 
906  std::auto_ptr<te::gm::Geometry> interGeom;
907 
908  if(!checkGeometries(fromGeom, dsPos[i], toGeom.get()))
909  {
910  m_hasErrors = true;
911  continue;
912  }
913 
914  try
915  {
916  interGeom.reset(toGeom->intersection(fromGeom));
917  }
918  catch(const std::exception &e)
919  {
920 #ifdef TERRALIB_LOGGER_ENABLED
921  std::string ex = e.what();
922  te::common::Logger::logDebug("attributefill", ex.c_str());
923 #endif //TERRALIB_LOGGER_ENABLED
924  m_hasErrors = true;
925  continue;
926  }
927 
928  std::string value = dataValues[i][propIndex]->toString();// fromDs->getAsString(propertyName);
929 
930  double area = getArea(interGeom.get());
931 
932  if(classAreaMap.find(value) == classAreaMap.end())
933  {
934  classAreaMap[value] = area;
935  }
936  else
937  {
938  double aux = classAreaMap[value];
939  classAreaMap[value] = aux + area;
940  }
941  }
942 
943  std::map<std::string, double>::iterator it = classAreaMap.begin();
944  std::string value;
945  double aux = 0;
946  while(it != classAreaMap.end())
947  {
948  if(aux < it->second)
949  {
950  aux = it->second;
951  value = it->first;
952  }
953 
954  ++it;
955  }
956 
957  te::dt::AbstractData* data = getDataBasedOnType(value, propType);
958 
959  return data;
960 }
961 
963  std::vector<std::size_t> dsPos,
964  const std::string& propertyName,
965  std::vector< std::vector<te::dt::AbstractData*> >& dataValues)
966 {
967  int propIndex = te::da::GetPropertyIndex(fromDs, propertyName);
968 
969  std::map<std::string, double> result;
970 
971  std::map<std::string, std::size_t> aux;
972  for(std::size_t i = 0; i < dsPos.size(); ++i)
973  {
974  //dataValues[i][propIndex];//fromDs->move(dsPos[i]);
975 
976  std::string value = dataValues[i][propIndex]->toString();//fromDs->getAsString(propertyName);
977 
978  if(aux.find(value) == aux.end())
979  {
980  aux[value] = 1;
981  }
982  else
983  {
984  aux[value] += 1;
985  }
986  }
987 
988  std::size_t total = 0;
989  std::map<std::string, std::size_t>::iterator it = aux.begin();
990  while(it != aux.end())
991  {
992  total += it->second;
993 
994  ++it;
995  }
996 
997  it = aux.begin();
998  while(it != aux.end())
999  {
1000  std::string normName = it->first;
1001  normalizeClassName(normName);
1002  result[normName] = ((double)it->second / total);
1003 
1004  ++it;
1005  }
1006 
1007  return result;
1008 }
1009 
1011  std::size_t toSrid,
1012  te::da::DataSet*,
1013  std::size_t,
1014  std::vector<std::size_t> dsPos,
1015  const std::string&,
1016  std::vector< std::vector<te::dt::AbstractData*> >&)
1017 {
1018  std::size_t toGeomPos = te::da::GetFirstSpatialPropertyPos(toDs);
1019 
1020  std::auto_ptr<te::gm::Geometry> toGeom = toDs->getGeometry(toGeomPos);
1021  if(toGeom->getSRID() <= 0)
1022  toGeom->setSRID((int)toSrid);
1023 
1024  double classArea = 0;
1025  for(std::size_t i = 0; i < dsPos.size(); ++i)
1026  {
1027  //dataValues[i][propIndex];//fromDs->move(dsPos[i]);
1028 
1029  te::gm::Geometry* fromGeom = m_mapGeom[dsPos[i]];//fromDs->getGeometry(fromGeomPos);
1030 
1031  if(!checkGeometries(fromGeom, dsPos[i], toGeom.get()))
1032  {
1033  m_hasErrors = true;
1034  continue;
1035  }
1036 
1037  std::auto_ptr<te::gm::Geometry> interGeom(toGeom->intersection(fromGeom));
1038 
1039  classArea += getArea(interGeom.get());
1040  }
1041 
1042  double polArea = getArea(toGeom.get());
1043 
1044  return classArea/polArea;
1045 }
1046 
1048  std::size_t toSrid,
1049  te::da::DataSet* fromDs,
1050  std::size_t,
1051  std::vector<std::size_t> dsPos,
1052  const std::string& propertyName,
1053  std::vector< std::vector<te::dt::AbstractData*> >& dataValues)
1054 {
1055  std::map<std::string, double> result;
1056 
1057  std::size_t toGeomPos = te::da::GetFirstSpatialPropertyPos(toDs);
1058 
1059  int propIndex = te::da::GetPropertyIndex(fromDs, propertyName);
1060 
1061  std::auto_ptr<te::gm::Geometry> toGeom = toDs->getGeometry(toGeomPos);
1062  if(toGeom->getSRID() <= 0)
1063  toGeom->setSRID((int)toSrid);
1064 
1065  double toGeomArea = getArea(toGeom.get());
1066 
1067  for(std::size_t i = 0; i < dsPos.size(); ++i)
1068  {
1069  //dataValues[i][propIndex];//fromDs->move(dsPos[i]);
1070 
1071  te::gm::Geometry* fromGeom = m_mapGeom[dsPos[i]];//fromDs->getGeometry(fromGeomPos);
1072 
1073  if(!checkGeometries(fromGeom, dsPos[i], toGeom.get()))
1074  {
1075  m_hasErrors = true;
1076  continue;
1077  }
1078 
1079  std::auto_ptr<te::gm::Geometry> interGeom(toGeom->intersection(fromGeom));
1080 
1081  std::string value = dataValues[i][propIndex]->toString();//fromDs->getAsString(propertyName);
1082 
1083  double area = getArea(interGeom.get());
1084 
1085  if(result.find(value) == result.end())
1086  {
1087  result[value] = area/toGeomArea;
1088  }
1089  else
1090  {
1091  result[value] += area/toGeomArea;
1092  }
1093  }
1094 
1095  return result;
1096 }
1097 
1099  std::size_t toSrid,
1100  te::da::DataSet* fromDs,
1101  std::size_t,
1102  std::vector<std::size_t> dsPos,
1103  const std::string& propertyName,
1104  std::vector< std::vector<te::dt::AbstractData*> >& dataValues)
1105 {
1106  std::size_t toGeomPos = te::da::GetFirstSpatialPropertyPos(toDs);
1107 
1108  int propIndex = te::da::GetPropertyIndex(fromDs, propertyName);
1109 
1110  std::auto_ptr<te::gm::Geometry> toGeom = toDs->getGeometry(toGeomPos);
1111  if(toGeom->getSRID() <= 0)
1112  toGeom->setSRID((int)toSrid);
1113 
1114  double toGeomArea = getArea(toGeom.get());
1115 
1116  double weigh = 0;
1117 
1118  for(std::size_t i = 0; i < dsPos.size(); ++i)
1119  {
1120  //fromDs->move(dsPos[i]);
1121 
1122  te::gm::Geometry* fromGeom = m_mapGeom[dsPos[i]];//fromDs->getGeometry(fromGeomPos);
1123 
1124  if(!checkGeometries(fromGeom, dsPos[i], toGeom.get()))
1125  {
1126  m_hasErrors = true;
1127  continue;
1128  }
1129 
1130  std::auto_ptr<te::gm::Geometry> interGeom(toGeom->intersection(fromGeom));
1131 
1132  double value_num = 0;
1133 
1134  if(!fromDs->isNull(propertyName))
1135  {
1136  std::string value = dataValues[i][propIndex]->toString();//fromDs->getAsString(propertyName);
1137  value_num = boost::lexical_cast<double>(value);
1138  }
1139 
1140  double intersectionArea = getArea(interGeom.get());
1141 
1142  weigh += value_num*(intersectionArea/toGeomArea);
1143  }
1144 
1145  return weigh;
1146 }
1147 
1149  std::size_t toSrid,
1150  te::da::DataSet* fromDs,
1151  std::size_t,
1152  std::vector<std::size_t> dsPos,
1153  const std::string& propertyName,
1154  std::vector< std::vector<te::dt::AbstractData*> >& dataValues)
1155 {
1156  std::size_t toGeomPos = te::da::GetFirstSpatialPropertyPos(toDs);
1157 
1158  int propIndex = te::da::GetPropertyIndex(fromDs, propertyName);
1159 
1160  std::auto_ptr<te::gm::Geometry> toGeom = toDs->getGeometry(toGeomPos);
1161  if(toGeom->getSRID() <= 0)
1162  toGeom->setSRID((int)toSrid);
1163 
1164  double weigh = 0;
1165 
1166  for(std::size_t i = 0; i < dsPos.size(); ++i)
1167  {
1168  //fromDs->move(dsPos[i]);
1169 
1170  te::gm::Geometry* fromGeom = m_mapGeom[dsPos[i]];//fromDs->getGeometry(fromGeomPos);
1171 
1172  double fromGeomArea = getArea(fromGeom);
1173 
1174  if(!checkGeometries(fromGeom, dsPos[i], toGeom.get()))
1175  {
1176  m_hasErrors = true;
1177  continue;
1178  }
1179 
1180  std::auto_ptr<te::gm::Geometry> interGeom(toGeom->intersection(fromGeom));
1181 
1182  double value_num = 0;
1183 
1184  if (!dataValues[i][propIndex])
1185  {
1186  std::string value = dataValues[i][propIndex]->toString();//fromDs->getAsString(propertyName);
1187  value_num = boost::lexical_cast<double>(value);
1188  }
1189 
1190  double intersectionArea = getArea(interGeom.get());
1191 
1192  weigh += value_num*(intersectionArea/fromGeomArea);
1193  }
1194 
1195  return weigh;
1196 }
1197 
1199 {
1200  if(type == te::gm::PolygonType ||
1201  type == te::gm::PolygonZType ||
1202  type == te::gm::PolygonMType ||
1203  type == te::gm::PolygonZMType)
1204  return true;
1205 
1206  return false;
1207 }
1208 
1210 {
1211  if(type == te::gm::LineStringType ||
1212  type == te::gm::LineStringMType ||
1213  type == te::gm::LineStringZMType ||
1214  type == te::gm::LineStringZType)
1215  return true;
1216 
1217  return false;
1218 }
1219 
1221 {
1222  if(type == te::gm::PointType ||
1223  type == te::gm::PointZType ||
1224  type == te::gm::PointMType ||
1225  type == te::gm::PointZMType)
1226  return true;
1227 
1228  return false;
1229 }
1230 
1232 {
1233  if(type == te::gm::MultiPolygonType ||
1234  type == te::gm::MultiPolygonZType ||
1235  type == te::gm::MultiPolygonMType ||
1237  return true;
1238 
1239  return false;
1240 }
1241 
1243 {
1244  if(type == te::gm::MultiLineStringMType ||
1245  type == te::gm::MultiLineStringType ||
1248  return true;
1249 
1250  return false;
1251 }
1252 
1254 {
1255  if(type == te::gm::MultiPointType ||
1256  type == te::gm::MultiPointZType ||
1257  type == te::gm::MultiPointMType ||
1258  type == te::gm::MultiPointZMType)
1259  return true;
1260 
1261  return false;
1262 }
1263 
1265 {
1266  te::gm::GeomType geomType = geom->getGeomTypeId();
1267 
1268  double area = 0;
1269 
1270  switch(geomType)
1271  {
1272  case te::gm::PolygonType:
1273  {
1274  te::gm::Polygon* g = dynamic_cast<te::gm::Polygon*>(geom);
1275  area = g->getArea();
1276  break;
1277  }
1279  {
1280  te::gm::MultiPolygon* g = dynamic_cast<te::gm::MultiPolygon*>(geom);
1281  area = g->getArea();
1282  break;
1283  }
1285  {
1286  te::gm::GeometryCollection* col = dynamic_cast<te::gm::GeometryCollection*>(geom);
1287  for(std::size_t j = 0; j < col->getNumGeometries(); ++j)
1288  {
1289  te::gm::Geometry* auxGeom = col->getGeometryN(j);
1290  if(isPolygon(auxGeom->getGeomTypeId()))
1291  {
1292  area += dynamic_cast<te::gm::Polygon*>(auxGeom)->getArea();
1293  }
1294  else if(isMultiPolygon(auxGeom->getGeomTypeId()))
1295  {
1296  area += dynamic_cast<te::gm::MultiPolygon*>(auxGeom)->getArea();
1297  }
1298  }
1299 
1300  break;
1301  }
1302  default:
1303  {
1304  throw te::common::Exception(TE_TR("Unexpected geometry type!"));
1305  }
1306  }
1307 
1308  return area;
1309 }
1310 
1312 {
1313  te::dt::AbstractData* data = 0;
1314 
1315  switch(type)
1316  {
1317  case te::dt::STRING_TYPE:
1318  {
1320  break;
1321  }
1322  case te::dt::INT16_TYPE:
1323  {
1324  int16_t v = boost::lexical_cast<int16_t>(strValue);
1326  break;
1327  }
1328  case te::dt::INT32_TYPE:
1329  {
1330  int32_t v = boost::lexical_cast<int32_t>(strValue);
1332  break;
1333  }
1334  case te::dt::INT64_TYPE:
1335  {
1336  int64_t v = boost::lexical_cast<int64_t>(strValue);
1338  break;
1339  }
1340  case te::dt::UINT16_TYPE:
1341  {
1342  uint16_t v = boost::lexical_cast<uint16_t>(strValue);
1344  break;
1345  }
1346  case te::dt::UINT32_TYPE:
1347  {
1348  uint32_t v = boost::lexical_cast<uint32_t>(strValue);
1350  break;
1351  }
1352  case te::dt::UINT64_TYPE:
1353  {
1354  uint64_t v = boost::lexical_cast<uint64_t>(strValue);
1356  break;
1357  }
1358  default:
1359  {
1360  throw te::common::Exception(TE_TR("Unexpected data type!"));
1361  }
1362  }
1363 
1364  return data;
1365 }
1366 
1368 {
1369  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(data);
1370 
1371  KD_ADAPTATIVE_TREE* kdtree = new KD_ADAPTATIVE_TREE(*data->getExtent(geomPos).release(), 5);
1372 
1373  std::vector<std::pair<te::gm::Coord2D, te::gm::Point> > kdset;
1374 
1375  data->moveBeforeFirst();
1376 
1377  while(data->moveNext())
1378  {
1379  std::auto_ptr<te::gm::Geometry> geom = data->getGeometry(geomPos);
1380 
1381  std::vector<te::gm::Point*> allPoints = getAllPointsOfGeometry(geom.get());
1382 
1383  for(std::size_t i = 0; i < allPoints.size(); ++i)
1384  {
1385  te::gm::Point* p = allPoints[i];
1386 
1387  if(p->getSRID() != toSrid)
1388  {
1389  p->transform((int)toSrid);
1390  }
1391 
1392  te::gm::Coord2D coord(p->getX(), p->getY());
1393  kdset.push_back(std::pair<te::gm::Coord2D, te::gm::Point>(coord, *p));
1394  }
1395  }
1396 
1397  kdtree->build(kdset);
1398 
1399  return kdtree;
1400 }
1401 
1403  std::size_t,
1404  te::da::DataSet*,
1405  std::size_t,
1406  KD_ADAPTATIVE_TREE* kdtree)
1407 {
1408  std::size_t toGeomPos = te::da::GetFirstSpatialPropertyPos(toDs);
1409  std::auto_ptr<te::gm::Geometry> toGeom = toDs->getGeometry(toGeomPos);
1410 
1411  std::vector<te::gm::Point*> allPoints = getAllPointsOfGeometry(toGeom.get());
1412 
1413  std::vector<double> distances;
1414 
1415  for(std::size_t i = 0; i < allPoints.size(); ++i)
1416  {
1417  te::gm::Point* p = allPoints[i];
1418  te::gm::Coord2D key = te::gm::Coord2D(p->getX(), p->getY());
1419  std::vector<te::gm::Point> points;
1420  points.push_back(te::gm::Point(std::numeric_limits<double>::max(), std::numeric_limits<double>::max()));
1421  std::vector<double> sqrDists;
1422 
1423  kdtree->nearestNeighborSearch(key, points, sqrDists, 1);
1424 
1425  distances.push_back(sqrDists[0]);
1426  }
1427 
1428  double finalResult = std::numeric_limits<double>::max();
1429  for(std::size_t i = 0; i < distances.size(); ++i)
1430  {
1431  if(distances[i] < finalResult)
1432  finalResult = distances[i];
1433  }
1434 
1435  return sqrt(finalResult);
1436 }
1437 
1439 {
1440  std::size_t geomType = geom->getGeomTypeId();
1441 
1442  std::vector<te::gm::Point*> result;
1443 
1444  switch(geomType)
1445  {
1446  case te::gm::PointType:
1447  {
1448  te::gm::Point* g = dynamic_cast<te::gm::Point*>(geom);
1449  result.push_back(g);
1450  break;
1451  }
1453  {
1454  te::gm::MultiPoint* g = dynamic_cast<te::gm::MultiPoint*>(geom);
1455  for(std::size_t i = 0; i < g->getNumGeometries(); ++i)
1456  {
1457  te::gm::Geometry* gAux = g->getGeometryN(i);
1458 
1459  std::vector<te::gm::Point*> vec = getAllPointsOfGeometry(gAux);
1460  result.insert(result.end(), vec.begin(), vec.end());
1461  }
1462  break;
1463  }
1464  case te::gm::PolygonType:
1465  {
1466  te::gm::Polygon* g = dynamic_cast<te::gm::Polygon*>(geom);
1467 
1468  for(std::size_t i = 0; i < g->getNumRings(); ++i)
1469  {
1470 
1471  te::gm::Curve* c = g->getRingN(i);
1472 
1473  te::gm::LinearRing* lr = dynamic_cast<te::gm::LinearRing*>(c);
1474 
1475  for(std::size_t j = 0; j < lr->getNPoints(); ++j)
1476  {
1477  result.push_back(lr->getPointN(j));
1478  }
1479  }
1480 
1481  break;
1482  }
1484  {
1485  te::gm::MultiPolygon* g = dynamic_cast<te::gm::MultiPolygon*>(geom);
1486 
1487  for(std::size_t i = 0; i < g->getNumGeometries(); ++i)
1488  {
1489  te::gm::Geometry* gAux = g->getGeometryN(i);
1490 
1491  std::vector<te::gm::Point*> vec = getAllPointsOfGeometry(gAux);
1492  result.insert(result.end(), vec.begin(), vec.end());
1493  }
1494  break;
1495  }
1497  {
1498  te::gm::LineString* g = dynamic_cast<te::gm::LineString*>(geom);
1499 
1500  for(std::size_t i = 0; i < g->getNPoints(); ++i)
1501  {
1502  result.push_back(g->getPointN(i));
1503  }
1504 
1505  break;
1506  }
1508  {
1509  te::gm::MultiLineString* g = dynamic_cast<te::gm::MultiLineString*>(geom);
1510 
1511  for(std::size_t i = 0; i < g->getNumGeometries(); ++i)
1512  {
1513  te::gm::Geometry* gAux = g->getGeometryN(i);
1514 
1515  std::vector<te::gm::Point*> vec = getAllPointsOfGeometry(gAux);
1516  result.insert(result.end(), vec.begin(), vec.end());
1517  }
1518 
1519  break;
1520  }
1521  default:
1522  {
1523  return std::vector<te::gm::Point*>();
1524  }
1525  }
1526 
1527  return result;
1528 }
1529 
1531 {
1532  std::map<std::string, std::vector<te::attributefill::OperationType> >::iterator it = m_options.begin();
1533 
1534  while(it != m_options.end())
1535  {
1536  std::vector<te::attributefill::OperationType> ops = it->second;
1537 
1538  for(std::size_t i = 0; i < ops.size(); ++i)
1539  {
1540  if(ops[i] == te::attributefill::MIN_DISTANCE ||
1541  ops[i] == te::attributefill::PRESENCE)
1542  return true;
1543  }
1544  ++it;
1545  }
1546 
1547  return false;
1548 }
1549 
1551 {
1552  if(!fromGeom->isValid())
1553  {
1554 #ifdef TERRALIB_LOGGER_ENABLED
1555  std::string ex = TE_TR("\"From\" layer geometry at position ");
1556  ex += boost::lexical_cast<std::string>(fromPos);
1557  ex += TE_TR(" is invalid.");
1558  te::common::Logger::logDebug("attributefill", ex.c_str());
1559 #endif //TERRALIB_LOGGER_ENABLED
1560 
1561  return false;
1562  }
1563  else if(!toGeom->isValid())
1564  {
1565 #ifdef TERRALIB_LOGGER_ENABLED
1566  std::string ex = TE_TR("\"To\" layer geometry is invalid.");
1567  te::common::Logger::logDebug("attributefill", ex.c_str());
1568 #endif //TERRALIB_LOGGER_ENABLED
1569 
1570  return false;
1571  }
1572 
1573  return true;
1574 }
TESTATEXPORT void GetNumericStatisticalSummary(std::vector< double > &values, te::stat::NumericStatisticalSummary &ss, double nullVal)
std::vector< te::gm::Point * > getAllPointsOfGeometry(te::gm::Geometry *geom)
It get all points of a geometry.
double getValue(te::stat::NumericStatisticalSummary ss, te::attributefill::OperationType type)
It get the value of required operation type from numeric statistical summary.
std::size_t getNumRings() const
It returns the number of rings in this CurvePolygon.
Definition: CurvePolygon.h:153
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
Definition: DataSetType.h:137
void transform(int srid)
It converts the coordinate values of the point to the new spatial reference system.
bool isStatistical(te::attributefill::OperationType type)
It verify if the operation is a statistical operation.
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
A structure to hold the set of statistics from a set of numerical values.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
Configuration flags for the Attribute Fill module of TerraLib.
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
Definition: Envelope.h:493
An atomic property like an integer or double.
std::vector< double > getNumValues(std::vector< std::vector< te::dt::AbstractData * > > dataValues, std::size_t pos)
It get the numeric values of a vector of abstract data.
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
Definition: StringUtils.h:200
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
Definition: Utils.cpp:670
A class that represents an R-tree.
Definition: Index.h:56
A class that models the description of a dataset.
Definition: DataSetType.h:72
Curve is an abstract class that represents 1-dimensional geometric objects stored as a sequence of co...
Definition: Curve.h:58
virtual const char * what() const
It outputs the exception message.
Definition: Exception.cpp:58
void useTimer(bool flag)
Used to define if task use progress timer information.
te::dt::AbstractData * getDataBasedOnType(const std::string &strValue, const int type)
It get a abstract data with the value based on the type.
Point * getPointN(std::size_t i) const
It returns the specified point in this LineString.
Definition: LineString.cpp:323
A class that represents a two dimensional K-d Tree (2-d Tree) that store data-elements into the leafs...
Definition: Index.h:352
virtual Property * clone() const =0
It returns a clone of the object.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
std::vector< std::size_t > getIntersections(te::da::DataSet *toDs, te::da::DataSet *fromDs, te::sam::rtree::Index< size_t, 8 > *rtree, bool &hasInvalid)
It verify all intersection between the "From" and "To" data sets.
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
std::map< std::string, double > getPercentageOfEachClassByArea(te::da::DataSet *toDs, std::size_t toSrid, te::da::DataSet *fromDs, std::size_t fromSrid, std::vector< std::size_t > dsPos, const std::string &propertyName, std::vector< std::vector< te::dt::AbstractData * > > &dataValues)
It get the percentage of each class intersection area in the total area.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
bool checkGeometries(te::gm::Geometry *fromGeom, std::size_t fromPos, te::gm::Geometry *toGeom)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
TECOMMONEXPORT std::string ReplaceSpecialChars(const std::string &str, bool &changed)
It replace special characters of a string.
It models a property definition.
Definition: Property.h:59
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
Definition: Utils.cpp:957
bool isActive() const
Verify if the task is active.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
void setDefaultValue(std::string *d)
It sets the default value associated to the property, or NULL if none is associated.
virtual bool move(std::size_t i)=0
It moves the dataset internal pointer to a given position.
void setTotalSteps(int value)
Set the task total stepes.
std::string getPropertyName(te::dt::Property *prop, te::attributefill::OperationType func)
It return a name based on original property name and the selected operation.
An converter for DataSetType.
Vector to Vector processing.
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
virtual int code() const
It gets the exception code.
Definition: Exception.cpp:53
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
double getPercentageOfTotalArea(te::da::DataSet *toDs, std::size_t toSrid, te::da::DataSet *fromDs, std::size_t fromSrid, std::vector< std::size_t > dsPos, const std::string &propertyName, std::vector< std::vector< te::dt::AbstractData * > > &dataValues)
It get the percentage of intersection area in the total area.
void remove(Constraint *c)
It removes the constraint.
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
bool isMultiPoint(te::gm::GeomType type)
It verify if the geometry is a multi point.
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void build(std::vector< std::pair< kdKey, kdDataItem > > &dataSet)
It inserts the data set into the tree.
Definition: Index.h:431
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
te::sam::kdtree::AdaptativeIndex< KD_ADAPTATIVE_NODE > KD_ADAPTATIVE_TREE
bool hasNoIntersectionOperations()
Verify if has operations that don't need the intersections.
KD_ADAPTATIVE_TREE * getKDtree(te::da::DataSet *data, std::size_t toSrid)
It return a KDTree.
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
Definition: LineString.h:193
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
te::dt::AbstractData * getClassWithHighestOccurrence(te::da::DataSet *fromDs, std::vector< std::size_t > dsPos, const std::string &propertyName, std::vector< std::vector< te::dt::AbstractData * > > &dataValues)
It get the class with highest occurrence from "From" data set in "To" data set.
virtual bool isValid() const
It tells if the geometry is well formed.
Definition: Geometry.cpp:168
double getWeightedSumByArea(te::da::DataSet *toDs, std::size_t toSrid, te::da::DataSet *fromDs, std::size_t fromSrid, std::vector< std::size_t > dsPos, const std::string &propertyName, std::vector< std::vector< te::dt::AbstractData * > > &dataValues)
It get the sum of weighted average if intersections values.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
std::string getModeValue(te::stat::NumericStatisticalSummary ss)
It get a string with the mode operation format.
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
Definition: Index.h:326
te::dt::AbstractData * getClassWithHighestIntersectionArea(te::da::DataSet *toDs, std::size_t toSrid, te::da::DataSet *fromDs, std::size_t fromSrid, std::vector< std::size_t > dsPos, const std::string &propertyName, std::vector< std::vector< te::dt::AbstractData * > > &dataValues)
It get the class with highest intersection area from "From" data set in "To" data set...
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
std::vector< std::string > getDistinctClasses(te::da::DataSet *fromDs, const std::string &propertyName)
It return distincts values of a property from the "From" data set.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
TESTATEXPORT void GetStringStatisticalSummary(std::vector< std::string > &values, te::stat::StringStatisticalSummary &ss)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
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.
int getType() const
It returns the property data type.
Definition: Property.h:161
void add(Constraint *c)
It adds a new constraint.
MultiLineString is a MultiCurve whose elements are LineStrings.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
bool run()
It execute the operations.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
double getArea() const
It returns the area of this MultiSurface, as measured in the spatial reference system of this multisu...
bool isLine(te::gm::GeomType type)
It verify if the geometry is a line.
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
Definition: Index.h:313
A structure to hold the set of statistics from a set of categorical (sample) values.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
virtual std::auto_ptr< te::gm::Envelope > getExtent(std::size_t i)=0
It computes the bounding rectangle for a spatial property of the dataset.
std::vector< std::vector< te::dt::AbstractData * > > getDataValues(te::da::DataSet *fromDs, std::vector< std::size_t > dsPos)
It get the value of all positions in "To" data set that has intersection with a position of "From" da...
TEDATAACCESSEXPORT bool IsValidName(const std::string &name, std::string &invalidChar)
It checks if the name is not valid as the existence of invalid characters, reserved words...
Definition: Utils.cpp:972
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
void setRequired(bool r)
It tells if the property is required or not.
std::string toString() const
It returns the data value in a WKT representation.
Definition: Geometry.h:858
bool isPolygon(te::gm::GeomType type)
It verify if the geometry is a polygon.
te::da::DataSetType * getOutputDataSetType()
It create a data set type based on selecteds properties and operations.
bool isMultiLine(te::gm::GeomType type)
It verify if the geometry is a multi line.
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
bool isPoint(te::gm::GeomType type)
It verify if the geometry is a point.
It is a collection of other geometric objects.
double getMinimumDistance(te::da::DataSet *toDs, std::size_t toSrid, te::da::DataSet *fromDs, std::size_t fromSrid, KD_ADAPTATIVE_TREE *kdtree)
It get the minimum distance of objects that not intersect.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
void nearestNeighborSearch(const kdKey &key, std::vector< kdDataItem > &report, std::vector< double > &sqrDists, const std::size_t &k) const
It searches the nearest data in nodes: you must pass an array of kdDataItem of size "k" with coordina...
Definition: Index.h:441
double getArea(te::gm::Geometry *geom)
It get the area of a geometry.
OperationType
Define grouping operations type.
Definition: Enums.h:39
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
Definition: Envelope.cpp:92
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
std::vector< std::string > getStrValues(std::vector< std::vector< te::dt::AbstractData * > > dataValues, std::size_t pos)
It get the string values of a vector of abstract data.
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
std::map< std::string, double > getPercentagePerClass(te::da::DataSet *fromDs, std::vector< std::size_t > dsPos, const std::string &propertyName, std::vector< std::vector< te::dt::AbstractData * > > &dataValues)
It get the percent per class from "From" data set in "To" data set.
bool isMultiPolygon(te::gm::GeomType type)
It verify if the geometry is a multi polygon.
double getWeightedByArea(te::da::DataSet *toDs, std::size_t toSrid, te::da::DataSet *fromDs, std::size_t fromSrid, std::vector< std::size_t > dsPos, const std::string &propertyName, std::vector< std::vector< te::dt::AbstractData * > > &dataValues)
It get the weighted average if intersections values.
virtual std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
Definition: DataSet.cpp:151
Curve * getRingN(std::size_t i) const
It returns the n-th ring for this curve polygon as a curve.
Definition: CurvePolygon.h:193
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
te::sam::rtree::Index< size_t, 8 > * getRtree(te::da::DataSet *data)
It return a RTree with data set iterator position information.