Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.dissolve / src / main / java / org / gvsig / geoprocess / algorithm / dissolve / Summary.java @ 1259

History | View | Annotate | Download (5.87 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.dissolve;
25

    
26
import es.unex.sextante.core.Sextante;
27

    
28
import org.gvsig.fmap.dal.feature.EditableFeature;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32

    
33
/**
34
 * <p>
35
 * Computes summary functions. There are four summary functions, maximum, minimum,
36
 * average and sum. 
37
 * </p>
38
 * <p>
39
 * At the beginning you have to call loadDefaultSummarizes with the
40
 * first analyzed feature. Next, the function updateValues will update the statistics 
41
 * values and in the end you have to load the statistics value in the output feature 
42
 * calling the function loadEditableFeature.
43
 * </p>
44
 * 
45
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
46
 */
47
public class Summary {
48
        private IDissolveRule                    rule             = null;
49
        private double                           max              = Double.NEGATIVE_INFINITY;
50
        private double                           min              = Double.POSITIVE_INFINITY;
51
        private double                           avg              = 0;
52
        private double                           sum              = 0;
53
        private double                           sumAvg           = 0;
54
        private double                           nfeat            = 0;
55
        private FeatureType                      featType         = null;
56
        
57
        public Summary(IDissolveRule criteria, FeatureType featType) {
58
                this.rule = criteria;
59
                this.featType = featType;
60
        }
61
        
62
        /**
63
         * This function updates statistics values reading the field 
64
         * values of the selected feature .
65
         * @param feature
66
         */
67
        public void updateValues(Feature feature) {
68
                try {
69
                        FeatureAttributeDescriptor[] desc = featType.getAttributeDescriptors();
70
                        Object obj = null;
71
                        String fName = null;
72
                        
73
                        for (int i = 0; i < desc.length; i++) {
74
                                String fieldName = desc[i].getName();
75
                                
76
                                if(fieldName.endsWith("_Max")) {
77
                                        fName = rule.getFieldName("Max");
78
                                        if(fName != null) {
79
                                                obj = feature.get(fName);
80
                                                max = Math.max(max, ((Number)obj).doubleValue());
81
                                        }
82
                                }
83
                                
84
                                if(fieldName.endsWith("_Min")) {
85
                                        fName = rule.getFieldName("Min");
86
                                        if(fName != null) {
87
                                                obj = feature.get(fName);
88
                                                min = Math.min(min, ((Number)obj).doubleValue());
89
                                        }
90
                                }
91
                                
92
                                if(fieldName.endsWith("_Sum")) {
93
                                        fName = rule.getFieldName("Sum");
94
                                        if(fName != null) {
95
                                                obj = feature.get(fName);
96
                                                sum = sum + ((Number)obj).doubleValue();
97
                                        }
98
                                }
99
                                
100
                                if(fieldName.endsWith("_Avg")) {
101
                                        fName = rule.getFieldName("Avg");
102
                                        if(fName != null) {
103
                                                obj = feature.get(fName);
104
                                                sumAvg = sumAvg + ((Number)obj).doubleValue();
105
                                                nfeat ++;
106
                                                avg = sumAvg / nfeat;
107
                                        }
108
                                }
109
                                fName = null;
110
                        }
111
                        
112
                } catch (NumberFormatException e) {
113
                        Sextante.addErrorToLog(e);
114
                }
115
        }
116
        
117
        /**
118
         * Loads an EditableFeature with the statistics
119
         * @param feat
120
         */
121
        public void loadEditableFeature(EditableFeature feat) {
122
                FeatureAttributeDescriptor[] desc = featType.getAttributeDescriptors();
123
                for (int i = 0; i < desc.length; i++) {
124
                        String fieldName = desc[i].getName();
125
                        if(fieldName.endsWith("_Max")) {
126
                                if(feat.getType().getAttributeDescriptor(fieldName) != null)
127
                                        feat.setDouble(fieldName, getMax());
128
                        }
129
                        
130
                        if(fieldName.endsWith("_Min")) {
131
                                if(feat.getType().getAttributeDescriptor(fieldName) != null)
132
                                        feat.setDouble(fieldName, getMin());
133
                        }
134
                        
135
                        if(fieldName.endsWith("_Sum")) {
136
                                if(feat.getType().getAttributeDescriptor(fieldName) != null)
137
                                        feat.setDouble(fieldName, getSum());
138
                        }
139
                        
140
                        if(fieldName.endsWith("_Avg")) {
141
                                if(feat.getType().getAttributeDescriptor(fieldName) != null)
142
                                        feat.setDouble(fieldName, getAvg());
143
                        }
144
                }
145
        }
146
        
147
        /**
148
         * Loads the default statistics values.
149
         * @param feature
150
         */
151
        public void loadDefaultSummarizes(Feature feature) {
152
                try {
153
                        FeatureAttributeDescriptor[] desc = featType.getAttributeDescriptors();
154
                        Object obj = null;
155
                        String fName = null;
156
                        sum = avg = sumAvg = nfeat = 0;
157
                        
158
                        for (int i = 0; i < desc.length; i++) {
159
                                String fieldName = desc[i].getName();
160
                                if(fieldName.endsWith("_Max")) {
161
                                        fName = rule.getFieldName("Max");
162
                                        if(fName != null) {
163
                                                obj = feature.get(fName);
164
                                                max = ((Number)obj).doubleValue();
165
                                        }
166
                                }
167
                                
168
                                if(fieldName.endsWith("_Min")) {
169
                                        fName = rule.getFieldName("Min");
170
                                        if(fName != null) {
171
                                                obj = feature.get(fName);
172
                                                min = ((Number)obj).doubleValue();
173
                                        }
174
                                }
175
                                
176
                                fName = null;
177
                        }
178
                        
179
                } catch (NumberFormatException e) {
180
                        Sextante.addErrorToLog(e);
181
                }
182
        }
183
        
184
        public double getMax() {
185
                return max;
186
        }
187

    
188
        public void setMax(double max) {
189
                this.max = max;
190
        }
191

    
192
        public double getMin() {
193
                return min;
194
        }
195

    
196
        public void setMin(double min) {
197
                this.min = min;
198
        }
199

    
200
        public double getAvg() {
201
                return avg;
202
        }
203

    
204
        public void setAvg(double avg) {
205
                this.avg = avg;
206
        }
207

    
208
        public double getSum() {
209
                return sum;
210
        }
211

    
212
        public void setSum(double sum) {
213
                this.sum = sum;
214
        }
215
}