00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef __TERRALIB_INTERNAL_GROUPINGALGORITHMS_H
00028 #define __TERRALIB_INTERNAL_GROUPINGALGORITHMS_H
00029
00030 #include "TeDefines.h"
00031 #include "TeSlice.h"
00032 #include "TeUtils.h"
00033 #include "TeDataTypes.h"
00034
00035 #include <math.h>
00036 #include <time.h>
00037 #include <algorithm>
00038
00039
00040
00041 template<typename It>
00042 void TeMinimumValue(It begin, It end, vector<double>& minValue, double dummy=-9999.99, bool usesDummy=false)
00043 {
00044 for (int i=0; i<minValue.size(); i++)
00045 minValue[i] = TeMAXFLOAT;
00046
00047 It it = begin;
00048 double val;
00049 while ( it != end)
00050 {
00051 if (!(usesDummy && dummy == val))
00052 for (int i=0; i<minValue.size(); i++)
00053 {
00054 val= (*it)[i];
00055 if (val < minValue[i])
00056 minValue[i] = val;
00057 }
00058 it++;
00059 }
00060 }
00061
00062
00063 template<typename It>
00064 void TeMaximumValue(It begin, It end, vector<double>& maxValue, double dummy=-9999.99, bool usesDummy=false)
00065 {
00066 for (int i=0; i<maxValue.size(); i++)
00067 maxValue[i] = -TeMAXFLOAT;
00068
00069 It it = begin;
00070 double val;
00071 while ( it != end)
00072 {
00073 if (!(usesDummy && dummy == val))
00074 for (int i=0; i<maxValue.size(); i++)
00075 {
00076 val= (*it)[i];
00077 if (val > maxValue[i])
00078 maxValue[i] = val;
00079 }
00080 it++;
00081 }
00082 }
00083
00084
00085 TL_DLL void TeGroupByEqualStep(double min, double max, int nstep, vector<TeSlice>& result, int precision=0);
00086
00087
00088
00089
00090
00091
00092
00093 template<class iterator>
00094 void TeGroupByEqualStep(iterator begin, iterator end, int nstep, vector<TeSlice>& result,
00095 int precision=0, bool countElements = true)
00096 {
00097 double min = TeMAXFLOAT;
00098 double max = -TeMAXFLOAT;
00099
00100 iterator it=begin;
00101 while(it < end)
00102 {
00103 min = MIN(min, *it);
00104 max = MAX(max, *it);
00105 it++;
00106 }
00107 double slice = (max - min)/double(nstep);
00108 int ns;
00109 for (ns=0;ns<nstep;ns++)
00110 {
00111 TeSlice ps;
00112 ps.count_ = 0;
00113 ps.from_ = Te2String(min+double(ns)*slice, precision);
00114 ps.to_ = Te2String(min+double(ns+1)*slice, precision);
00115 result.push_back(ps);
00116 }
00117 min = TeAdjustToPrecision(min, precision, true);
00118 result[0].from_ = Te2String(min, precision);
00119 max = TeAdjustToPrecision(max, precision);
00120 result[result.size()-1].to_ = Te2String(max, precision);
00121
00122
00123 if (countElements == true)
00124 TeElemCountingBySlice(begin, end, result);
00125 }
00126
00127
00128 template<class iterator>
00129 void TeGroupByQuantil(iterator begin, iterator end, int nstep, vector<TeSlice>& result,
00130 int precision = 0, bool countElements = true)
00131 {
00132 sort(begin, end);
00133
00134 int size = end - begin;
00135 double step = (double)size / (double)nstep;
00136
00137 int n = 0;
00138 iterator it = begin;
00139 while(it < end)
00140 {
00141 TeSlice ps;
00142 ps.from_ = Te2String((*it), precision);
00143 int p = (int)(step * (double)++n + .5);
00144 it = begin + p;
00145 if(it < end)
00146 ps.to_ = Te2String((*it), precision);
00147 else
00148 ps.to_ = Te2String(*(it-1), precision);
00149 result.push_back(ps);
00150 }
00151 if(end-begin > 1)
00152 {
00153 double min = (*begin);
00154 double max = (*(end-1));
00155 min = TeAdjustToPrecision(min, precision, true);
00156 result[0].from_ = Te2String(min, precision);
00157 max = TeAdjustToPrecision(max, precision);
00158 result[result.size()-1].to_ = Te2String(max, precision);
00159 }
00160
00161
00162 if (countElements == true)
00163 TeElemCountingBySlice(begin, end, result);
00164 }
00165
00166
00167 template<class iterator>
00168 void TeGroupByStdDev(iterator begin, iterator end, double ndev, vector<TeSlice>& result, string& rmean,
00169 int precision = 0, bool countElements = true)
00170 {
00171
00172 double min = TeMAXFLOAT;
00173 double max = -TeMAXFLOAT;
00174 long double sum=0.;
00175 long double sm2=0.;
00176 iterator it=begin;
00177 while(it < end)
00178 {
00179 min = MIN(min, *it);
00180 max = MAX(max, *it);
00181 sum += (*it);
00182 sm2 += ((*it) * (*it));
00183 it++;
00184 }
00185 double cont = (double)(end - begin);
00186 double mean = (double)(sum/cont);
00187 long double var = (sm2 / cont) - (mean * mean);
00188 double sdev = sqrt(var);
00189
00190 double slice = sdev * ndev;
00191
00192 vector<TeSlice> aux;
00193 rmean = Te2String(mean, precision);
00194 double val = mean;
00195 while(val-slice > min-slice)
00196 {
00197 TeSlice ps;
00198 double v = val - slice;
00199 ps.from_ = Te2String(v, precision);
00200 ps.to_ = Te2String(val, precision);
00201 aux.push_back(ps);
00202 val = v;
00203 }
00204
00205 std::vector<TeSlice>::reverse_iterator sit;
00206
00207 for(sit = aux.rbegin(); sit != aux.rend(); ++sit)
00208 {
00209 result.push_back(*sit);
00210 }
00211
00212 string media = "mean = " + rmean;
00213 TeSlice ps;
00214 ps.from_ = media;
00215 ps.to_.clear();
00216 result.push_back(ps);
00217 val = mean;
00218 while(val+slice < max+slice)
00219 {
00220 TeSlice ps;
00221 double v = val + slice;
00222 ps.from_ = Te2String(val, precision);
00223 ps.to_ = Te2String(v, precision);
00224 result.push_back(ps);
00225 val = v;
00226 }
00227 if(result.size() > 2)
00228 {
00229 if (result[0].from_.find("mean") == string::npos)
00230 {
00231 min = TeAdjustToPrecision(min, precision, true);
00232 result[0].from_ = Te2String(min, precision);
00233 }
00234 if (result[result.size()-1].from_.find("mean") == string::npos)
00235 {
00236 max = TeAdjustToPrecision(max, precision);
00237 result[result.size()-1].to_ = Te2String(max, precision);
00238 }
00239 }
00240
00241
00242 if (countElements == true)
00243 TeElemCountingBySlice(begin, end, result);
00244 }
00245
00246
00247
00248 template<class iterator>
00249 void TeElemCountingBySlice(iterator begin, iterator end, vector<TeSlice>& result)
00250 {
00251 iterator it;
00252 double from, to;
00253
00254 for (unsigned int i = 0; i < result.size(); ++i)
00255 {
00256 TeSlice& sl = result[i];
00257 sl.count_ = 0;
00258 from = atof(sl.from_.c_str());
00259 to = atof(sl.to_.c_str());
00260 for (it = begin; it != end; ++it)
00261 {
00262 if (*it >= from && *it < to)
00263 ++sl.count_;
00264 }
00265 }
00266 }
00267
00268
00269
00270 TL_DLL void TeGroupByUniqueValue(vector<string>& vec, TeAttrDataType tipo, vector<TeSlice>& result, int precision);
00271
00272
00273 #endif
00274
00275
00276
00277
00278
00279
00280
00281