/usr/include/terralib/kernel/TeGroupingAlgorithms.h is in libterralib-dev 4.0.0-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
You should have received a copy of the GNU Lesser General Public
License along with this library.
The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/
/*! \file TeGroupingAlgorithms.h
\brief This file contains some generic grouping algorithms (based on iterators)
*/
#ifndef __TERRALIB_INTERNAL_GROUPINGALGORITHMS_H
#define __TERRALIB_INTERNAL_GROUPINGALGORITHMS_H
#include "TeDefines.h"
#include "TeSlice.h"
#include "TeUtils.h"
#include "TeDataTypes.h"
#include <math.h>
#include <time.h>
#include <algorithm>
//! Finds the element with minimum value among the elements contained in a range of iterators
template<typename It>
void TeMinimumValue(It begin, It end, vector<double>& minValue, double dummy=-9999.99, bool usesDummy=false)
{
for (int i=0; i<minValue.size(); i++)
minValue[i] = TeMAXFLOAT;
It it = begin;
double val;
while ( it != end)
{
if (!(usesDummy && dummy == val))
for (int i=0; i<minValue.size(); i++)
{
val= (*it)[i];
if (val < minValue[i])
minValue[i] = val;
}
it++;
}
}
//! Finds the element with maximum value among the elements contained in a range of iterators
template<typename It>
void TeMaximumValue(It begin, It end, vector<double>& maxValue, double dummy=-9999.99, bool usesDummy=false)
{
for (int i=0; i<maxValue.size(); i++)
maxValue[i] = -TeMAXFLOAT;
It it = begin;
double val;
while ( it != end)
{
if (!(usesDummy && dummy == val))
for (int i=0; i<maxValue.size(); i++)
{
val= (*it)[i];
if (val > maxValue[i])
maxValue[i] = val;
}
it++;
}
}
//! Defines the classes (slices) of a equal step grouping
TL_DLL void TeGroupByEqualStep(double min, double max, int nstep, vector<TeSlice>& result, int precision=0);
/** @defgroup GenGroupAlg Generic Algorithms to do grouping
* A set of of generic functions to do grouping
* @{
*/
//! Groups a set of elements defined by a range of iterators in nstep groups, using Equal Step algorithm
template<class iterator>
void TeGroupByEqualStep(iterator begin, iterator end, int nstep, vector<TeSlice>& result,
int precision=0, bool countElements = true)
{
double min = TeMAXFLOAT;
double max = -TeMAXFLOAT;
iterator it=begin;
while(it < end)
{
min = MIN(min, *it);
max = MAX(max, *it);
it++;
}
double slice = (max - min)/double(nstep);
int ns;
for (ns=0;ns<nstep;ns++)
{
TeSlice ps;
ps.count_ = 0;
ps.from_ = Te2String(min+double(ns)*slice, precision);
ps.to_ = Te2String(min+double(ns+1)*slice, precision);
result.push_back(ps);
}
min = TeAdjustToPrecision(min, precision, true);
result[0].from_ = Te2String(min, precision);
max = TeAdjustToPrecision(max, precision);
result[result.size()-1].to_ = Te2String(max, precision);
// Set the number of elements for each slice
if (countElements == true)
TeElemCountingBySlice(begin, end, result);
}
//! Groups a set of elements defined by a range of iterators in nstep groups, using Quantil algorithm
template<class iterator>
void TeGroupByQuantil(iterator begin, iterator end, int nstep, vector<TeSlice>& result,
int precision = 0, bool countElements = true)
{
sort(begin, end);
int size = end - begin;
double step = (double)size / (double)nstep;
int n = 0;
iterator it = begin;
while(it < end)
{
TeSlice ps;
ps.from_ = Te2String((*it), precision);
int p = (int)(step * (double)++n + .5);
it = begin + p;
if(it < end)
ps.to_ = Te2String((*it), precision);
else
ps.to_ = Te2String(*(it-1), precision);
result.push_back(ps);
}
if(end-begin > 1)
{
double min = (*begin);
double max = (*(end-1));
min = TeAdjustToPrecision(min, precision, true);
result[0].from_ = Te2String(min, precision);
max = TeAdjustToPrecision(max, precision);
result[result.size()-1].to_ = Te2String(max, precision);
}
// Set the number of elements for each slice
if (countElements == true)
TeElemCountingBySlice(begin, end, result);
}
//! Groups a set of elements defined by a range of iterators in ndev groups, using Standanrd deviation algorithm
template<class iterator>
void TeGroupByStdDev(iterator begin, iterator end, double ndev, vector<TeSlice>& result, string& rmean,
int precision = 0, bool countElements = true)
{
// Compute mim, max and mean
double min = TeMAXFLOAT;
double max = -TeMAXFLOAT;
long double sum=0.;
long double sm2=0.;
iterator it=begin;
while(it < end)
{
min = MIN(min, *it);
max = MAX(max, *it);
sum += (*it);
sm2 += ((*it) * (*it));
it++;
}
double cont = (double)(end - begin);
double mean = (double)(sum/cont);
long double var = (sm2 / cont) - (mean * mean);
double sdev = sqrt(var);
double slice = sdev * ndev;
vector<TeSlice> aux;
rmean = Te2String(mean, precision);
double val = mean;
while(val-slice > min-slice)
{
TeSlice ps;
double v = val - slice;
ps.from_ = Te2String(v, precision);
ps.to_ = Te2String(val, precision);
aux.push_back(ps);
val = v;
}
std::vector<TeSlice>::reverse_iterator sit;
for(sit = aux.rbegin(); sit != aux.rend(); ++sit)
{
result.push_back(*sit);
}
string media = "mean = " + rmean;
TeSlice ps;
ps.from_ = media;
ps.to_.clear();
result.push_back(ps);
val = mean;
while(val+slice < max+slice)
{
TeSlice ps;
double v = val + slice;
ps.from_ = Te2String(val, precision);
ps.to_ = Te2String(v, precision);
result.push_back(ps);
val = v;
}
if(result.size() > 2)
{
if (result[0].from_.find("mean") == string::npos)
{
min = TeAdjustToPrecision(min, precision, true);
result[0].from_ = Te2String(min, precision);
}
if (result[result.size()-1].from_.find("mean") == string::npos)
{
max = TeAdjustToPrecision(max, precision);
result[result.size()-1].to_ = Te2String(max, precision);
}
}
// Set the number of elements for each slice
if (countElements == true)
TeElemCountingBySlice(begin, end, result);
}
//! Counts the number of elements, from a general container, per slice of a vector of slices
template<class iterator>
void TeElemCountingBySlice(iterator begin, iterator end, vector<TeSlice>& result)
{
iterator it;
double from, to;
for (unsigned int i = 0; i < result.size(); ++i)
{
TeSlice& sl = result[i];
sl.count_ = 0;
from = atof(sl.from_.c_str());
to = atof(sl.to_.c_str());
for (it = begin; it != end; ++it)
{
if (*it >= from && *it < to)
++sl.count_;
}
}
}
//! Groups a set of elements in a vector of string using Unique Value algorithm
TL_DLL void TeGroupByUniqueValue(vector<string>& vec, TeAttrDataType tipo, vector<TeSlice>& result, int precision);
/** @} */
#endif
|