All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/StringUtils.h"
28 #include "../datatype/Enums.h"
29 #include "GroupingAlgorithms.h"
30 
31 // STL
32 #include <algorithm>
33 #include <stdlib.h>
34 
35 // Boost
36 #include <boost/cstdint.hpp>
37 
38 
39 void te::map::GroupingByUniqueValues(std::vector<std::string>& inputValues, int dataType,
40  std::vector<te::map::GroupingItem*>& legend, int precision)
41 {
42  size_t i, j;
43 
44  size_t valSize = inputValues.size();
45 
46  // Sort the input values
47  if (dataType == te::dt::INT16_TYPE || dataType == te::dt::UINT16_TYPE ||
48  dataType == te::dt::INT32_TYPE || dataType == te::dt::UINT32_TYPE ||
49  dataType == te::dt::INT64_TYPE || dataType == te::dt::UINT64_TYPE)
50  {
51  std::vector<boost::int64_t> v;
52 
53  for(i = 0; i < valSize; ++i)
54  v.push_back(atoi(inputValues[i].c_str()));
55 
56  sort(v.begin(), v.end());
57 
58  for (i = 0; i < v.size(); ++i)
59  inputValues[i] = te::common::Convert2String(v[i]);
60  }
61  else if(dataType == te::dt::FLOAT_TYPE || dataType == te::dt::DOUBLE_TYPE)
62  {
63  std::vector<double> v;
64 
65  for (i = 0; i < valSize; ++i)
66  {
67  double a = atof(inputValues[i].c_str());
68  v.push_back(a);
69  }
70 
71  stable_sort(v.begin(), v.end());
72 
73  for (i = 0; i < v.size(); ++i)
74  inputValues[i] = te::common::Convert2String(v[i], precision);
75  }
76  else
77  {
78  sort(inputValues.begin(), inputValues.end());
79  }
80 
81  // Check the elements that are equal, incrementing
82  // the count variable associated to each one
83  int count = 1;
84  te::map::GroupingItem* legendItem;
85 
86  for (i = 0, j = 1; i < valSize - 1 && j < valSize; ++i, ++j)
87  {
88  if (inputValues[i] == inputValues[j])
89  ++count;
90  else
91  {
92  legendItem = new te::map::GroupingItem;
93  legendItem->setValue(inputValues[i]);
94  legendItem->setCount(count);
95  legend.push_back(legendItem);
96  count = 1;
97  }
98  }
99 
100  if((i > 1) && (inputValues[i] == inputValues[i-1]))
101  {
102  legendItem = new te::map::GroupingItem;
103  legendItem->setValue(inputValues[i]);
104  legendItem->setCount(count);
105  legend.push_back(legendItem);
106  }
107  else
108  {
109  legendItem = new te::map::GroupingItem;
110  legendItem->setValue(inputValues[i]);
111  legendItem->setCount(1);
112  legend.push_back(legendItem);
113  }
114 }
115 
116 double te::map::AdjustToPrecision(double val, int precision, bool reduce)
117 {
118  double p = pow(10.0, (double)-precision);
119 
120  if (reduce)
121  return (val - p);
122 
123  return (val + p);
124 }
void setValue(const std::string &value)
It sets value of the legend item.
TEMAPEXPORT double AdjustToPrecision(double val, int precision, bool reduce=false)
It adjusts a value to the precision specified.
A GroupingItem contains information about a grouping item associated to a layer.
Definition: GroupingItem.h:48
This file contains functions containing the algorithms for grouping values.
TEMAPEXPORT void GroupingByUniqueValues(std::vector< std::string > &inputValues, int dataType, std::vector< te::map::GroupingItem * > &legend, int precision)
It groups the values using the unique value algorithm.
void setCount(std::size_t count)
It It sets the number of objects whose values are between the lower and upper limits.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:51