Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.dissolve / src / main / java / org / gvsig / sextante / app / algorithm / dissolve / Summary.java @ 172

History | View | Annotate | Download (6.1 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 */
21
package org.gvsig.sextante.app.algorithm.dissolve;
22

    
23
import es.unex.sextante.core.Sextante;
24

    
25
import org.gvsig.fmap.dal.feature.EditableFeature;
26
import org.gvsig.fmap.dal.feature.Feature;
27
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
28
import org.gvsig.fmap.dal.feature.FeatureType;
29

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

    
202
        public void setMax(double max) {
203
                this.max = max;
204
        }
205

    
206
        public double getMin() {
207
                return min;
208
        }
209

    
210
        public void setMin(double min) {
211
                this.min = min;
212
        }
213

    
214
        public double getAvg() {
215
                return avg;
216
        }
217

    
218
        public void setAvg(double avg) {
219
                this.avg = avg;
220
        }
221

    
222
        public double getSum() {
223
                return sum;
224
        }
225

    
226
        public void setSum(double sum) {
227
                this.sum = sum;
228
        }
229
}