~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/terralib/kernel/TeGroupingAlgorithms.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
Import upstream version 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
5
This code is part of the TerraLib library.
 
6
This library is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU Lesser General Public
 
8
License as published by the Free Software Foundation; either
 
9
version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
14
The authors reassure the license terms regarding the warranties.
 
15
They specifically disclaim any warranties, including, but not limited to,
 
16
the implied warranties of merchantability and fitness for a particular purpose.
 
17
The library provided hereunder is on an "as is" basis, and the authors have no
 
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
 
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
 
20
indirect, special, incidental, or consequential damages arising out of the use
 
21
of this library and its documentation.
 
22
*************************************************************************************/
 
23
 
 
24
#ifdef WIN32
 
25
#pragma warning ( disable: 4786 )
 
26
#endif
 
27
 
 
28
#include "TeGroupingAlgorithms.h"
 
29
 
 
30
void
 
31
TeGroupByUniqueValue(vector<string>& vec, TeAttrDataType tipo, vector<TeSlice>& result, int precision)
 
32
{
 
33
        unsigned int i, j;
 
34
                
 
35
        if (tipo == TeINT)
 
36
        {
 
37
                vector<int> v;
 
38
                for (i = 0; i < vec.size(); ++i)
 
39
                        v.push_back(atoi(vec[i].c_str()));
 
40
                sort(v.begin(), v.end());
 
41
 
 
42
                for (i = 0; i < v.size(); ++i)
 
43
                        vec[i] = Te2String(v[i]);
 
44
        }
 
45
        else if (tipo == TeREAL)
 
46
        {
 
47
                vector<double> v;
 
48
                for (i = 0; i < vec.size(); ++i)
 
49
                {
 
50
                        double a = atof(vec[i].c_str());
 
51
                        v.push_back(a);
 
52
                }
 
53
                stable_sort(v.begin(), v.end());
 
54
 
 
55
                for (i = 0; i < v.size(); ++i)
 
56
                        vec[i] = Te2String(v[i], precision);
 
57
        }
 
58
        else
 
59
        {
 
60
                sort(vec.begin(), vec.end());
 
61
        }
 
62
 
 
63
        // Check the elements that are equal, incrementing
 
64
        // the variable count associated to each one
 
65
        int count = 1;
 
66
        TeSlice slice;
 
67
        unsigned int sz = vec.size();
 
68
        for (i = 0, j = 1; i < sz - 1 && j < sz; ++i, ++j)
 
69
        {
 
70
                if (vec[i] == vec[j])
 
71
                        ++count;
 
72
                else
 
73
                {
 
74
                        slice.from_ = vec[i];
 
75
                        slice.count_ = count;
 
76
                        result.push_back(slice);
 
77
                        count = 1;
 
78
                }
 
79
        }
 
80
 
 
81
        if (vec[i] == vec[i - 1])
 
82
        {
 
83
                slice.from_ = vec[i];
 
84
                slice.count_ = count;
 
85
                result.push_back(slice);
 
86
        }
 
87
        else
 
88
        {
 
89
                slice.from_ = vec[i];
 
90
                slice.count_ = 1;
 
91
                result.push_back(slice);
 
92
        }
 
93
}
 
94
 
 
95
void
 
96
TeGroupByEqualStep(double min, double max, int nstep, vector<TeSlice>& result, int precision)
 
97
{
 
98
        double slice = (max - min)/double(nstep);
 
99
        int ns;
 
100
        for (ns=0;ns<nstep;ns++)
 
101
        {
 
102
                TeSlice ps;
 
103
                ps.count_ = 0;
 
104
                ps.from_ = Te2String(min+double(ns)*slice, precision);
 
105
                ps.to_ = Te2String(min+double(ns+1)*slice, precision);
 
106
                result.push_back(ps);
 
107
        }
 
108
        min = TeAdjustToPrecision(min, precision, true);
 
109
        result[0].from_ = Te2String(min, precision);
 
110
        max = TeAdjustToPrecision(max, precision);
 
111
        result[result.size()-1].to_ = Te2String(max, precision);
 
112
}