GroupingAlgorithms.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 GroupingAlgorithms.cpp
22 
23  \brief This file contains functions containing the algorithms for grouping values.
24  */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/StringUtils.h"
29 #include "../core/translator/Translator.h"
30 #include "../datatype/Enums.h"
31 #include "../fe.h"
32 #include "GroupingAlgorithms.h"
33 
34 // STL
35 #include <algorithm>
36 #include <stdlib.h>
37 
38 // Boost
39 #include <boost/cstdint.hpp>
40 
41 void te::map::GroupingByUniqueValues(std::string attrName, std::vector<std::string>& inputValues, int dataType,
42  std::vector<te::se::Rule*>& rules, int precision)
43 {
44  size_t i, j;
45 
46  size_t valSize = inputValues.size();
47 
48  if (valSize == 0)
49  {
50  throw te::common::Exception(TE_TR("There are no values to be grouped"));
51  }
52 
53  // Sort the input values
54  if (dataType == te::dt::INT16_TYPE || dataType == te::dt::UINT16_TYPE ||
55  dataType == te::dt::INT32_TYPE || dataType == te::dt::UINT32_TYPE ||
56  dataType == te::dt::INT64_TYPE || dataType == te::dt::UINT64_TYPE)
57  {
58  std::vector<boost::int64_t> v;
59 
60  for(i = 0; i < valSize; ++i)
61  v.push_back(atoi(inputValues[i].c_str()));
62 
63  sort(v.begin(), v.end());
64 
65  for (i = 0; i < v.size(); ++i)
66  inputValues[i] = te::common::Convert2String(v[i]);
67  }
68  else if(dataType == te::dt::FLOAT_TYPE || dataType == te::dt::DOUBLE_TYPE)
69  {
70  std::vector<double> v;
71 
72  for (i = 0; i < valSize; ++i)
73  {
74  double a = atof(inputValues[i].c_str());
75  v.push_back(a);
76  }
77 
78  stable_sort(v.begin(), v.end());
79 
80  for (i = 0; i < v.size(); ++i)
81  inputValues[i] = te::common::Convert2String(v[i], precision);
82  }
83  else
84  {
85  sort(inputValues.begin(), inputValues.end());
86  }
87 
88  // Check the elements that are equal, incrementing
89  // the count variable associated to each one
90  int count = 1;
91  te::se::Rule* ruleItem;
92 
93  for (i = 0, j = 1; i < valSize - 1 && j < valSize; ++i, ++j)
94  {
95  if (inputValues[i] == inputValues[j])
96  ++count;
97  else
98  {
99  ruleItem = new te::se::Rule;
100  std::string* ruleName = new std::string(inputValues[i]);
101  ruleItem->setName(ruleName);
102  ruleItem->setFilter(te::fe::CreateFilterByUniqueValue(attrName, inputValues[i]));
103 
104  rules.push_back(ruleItem);
105 
106  count = 1;
107  }
108  }
109 
110  if((i > 1) && (inputValues[i] == inputValues[i-1]))
111  {
112  ruleItem = new te::se::Rule;
113  std::string* ruleName = new std::string(inputValues[i]);
114  ruleItem->setName(ruleName);
115  ruleItem->setFilter(te::fe::CreateFilterByUniqueValue(attrName, inputValues[i]));
116 
117  rules.push_back(ruleItem);
118  }
119  else
120  {
121  ruleItem = new te::se::Rule;
122  std::string* ruleName = new std::string(inputValues[i]);
123  ruleItem->setName(ruleName);
124  ruleItem->setFilter(te::fe::CreateFilterByUniqueValue(attrName, inputValues[i]));
125 
126  rules.push_back(ruleItem);
127  }
128 }
129 
130 double te::map::AdjustToPrecision(double val, int precision, bool reduce)
131 {
132  double p = pow(10.0, (double)-precision);
133 
134  if (reduce)
135  return (val - p);
136 
137  return (val + p);
138 }
TEMAPEXPORT void GroupingByUniqueValues(std::string attrName, std::vector< std::string > &inputValues, int dataType, std::vector< te::se::Rule * > &rules, int precision)
It groups the values using the unique value algorithm.
void setName(std::string *name)
Definition: Rule.cpp:58
TEMAPEXPORT double AdjustToPrecision(double val, int precision, bool reduce=false)
It adjusts a value to the precision specified.
void setFilter(te::fe::Filter *f)
Definition: Rule.cpp:91
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
TEFEEXPORT te::fe::Filter * CreateFilterByUniqueValue(const std::string &attrName, const std::string &value)
te::gm::Polygon * p
This file contains functions containing the algorithms for grouping values.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56