Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / statistics / DefaultStatistics.java @ 5518

History | View | Annotate | Download (6.87 KB)

1
package org.gvsig.raster.lib.buffer.impl.statistics;
2

    
3
import java.util.Arrays;
4
import java.util.Iterator;
5
import java.util.List;
6

    
7
import org.gvsig.raster.lib.buffer.api.Band;
8
import org.gvsig.raster.lib.buffer.api.statistics.HistogramBand;
9
import org.gvsig.raster.lib.buffer.api.statistics.Statistics;
10
import org.gvsig.raster.lib.buffer.api.statistics.StatisticsBand;
11
import org.gvsig.tools.ToolsLocator;
12
import org.gvsig.tools.task.SimpleTaskStatus;
13

    
14

    
15
/**
16
 * @author fdiaz
17
 *
18
 */
19
public class DefaultStatistics implements Statistics {
20

    
21
    StatisticsBand[] statisticsBands;
22
    boolean calculated;
23
    double[][] varCov;
24

    
25
    /**
26
     * @param bands
27
     *
28
     */
29
    public DefaultStatistics( List<Band> bands) {
30
        statisticsBands = new StatisticsBand[bands.size()];
31
        for (int i = 0; i < statisticsBands.length; i++) {
32
            statisticsBands[i] = new DefaultStatisticsBand(bands.get(i));
33
        }
34
        varCov = new double[statisticsBands.length][statisticsBands.length];
35
        calculated = false;
36
    }
37

    
38
    @Override
39
    public long[] getNumberOfValues() {
40
        long[] result = new long[statisticsBands.length];
41
        for (int i = 0; i < statisticsBands.length; i++) {
42
            result[i]= statisticsBands[i].getBandLenght();
43
        }
44
        return result;
45
    }
46

    
47
    @Override
48
    public double[] getMax() {
49
        double[] result = new double[statisticsBands.length];
50
        for (int i = 0; i < statisticsBands.length; i++) {
51
            result[i]= statisticsBands[i].getMaximum();
52
        }
53
        return result;
54
    }
55

    
56
    @Override
57
    public double[] getSecondMax() {
58
        double[] result = new double[statisticsBands.length];
59
        for (int i = 0; i < statisticsBands.length; i++) {
60
            result[i]= statisticsBands[i].getSecondMax();
61
        }
62
        return result;
63
    }
64

    
65
    @Override
66
    public double[] getSecondMin() {
67
        double[] result = new double[statisticsBands.length];
68
        for (int i = 0; i < statisticsBands.length; i++) {
69
            result[i]= statisticsBands[i].getSecondMin();
70
        }
71
        return result;
72
    }
73

    
74
    @Override
75
    public double getMaximun() {
76
        double result = Double.NEGATIVE_INFINITY;
77
        for (int i = 0; i < statisticsBands.length; i++) {
78
            double max = statisticsBands[i].getMaximum();
79
            if(result<max){
80
                result = max;
81
            }
82
        }
83
        return result;
84
    }
85

    
86
    @Override
87
    public double getMinimun() {
88
        double result = Double.POSITIVE_INFINITY;
89
        for (int i = 0; i < statisticsBands.length; i++) {
90
            double min = statisticsBands[i].getMinimum();
91
            if(result>min){
92
                result = min;
93
            }
94
        }
95
        return result;
96
    }
97

    
98
    @Override
99
    public double[] getMean() {
100
        double[] result = new double[statisticsBands.length];
101
        for (int i = 0; i < statisticsBands.length; i++) {
102
            result[i]= statisticsBands[i].getMean();
103
        }
104
        return result;
105
    }
106

    
107
    @Override
108
    public double[] getMedian() {
109
        double[] result = new double[statisticsBands.length];
110
        for (int i = 0; i < statisticsBands.length; i++) {
111
            result[i]= statisticsBands[i].getMedian();
112
        }
113
        return result;
114
    }
115

    
116
    @Override
117
    public double[] getMin() {
118
        double[] result = new double[statisticsBands.length];
119
        for (int i = 0; i < statisticsBands.length; i++) {
120
            result[i]= statisticsBands[i].getMinimum();
121
        }
122
        return result;
123
    }
124

    
125
    @Override
126
    public double[] getVariance() {
127
        double[] result = new double[statisticsBands.length];
128
        for (int i = 0; i < statisticsBands.length; i++) {
129
            result[i]= statisticsBands[i].getVariance();
130
        }
131
        return result;
132
    }
133

    
134
    @Override
135
    public int getBandCount() {
136
        return statisticsBands.length;
137
    }
138

    
139
    @Override
140
    public boolean isCalculated() {
141
        return calculated;
142
    }
143

    
144
    @Override
145
    public void calculate(SimpleTaskStatus status) {
146
        calculated = false;
147
        boolean isMyStatus = false;
148
        if (status == null) {
149
            status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Statistics");
150
            status.add();
151
            isMyStatus = true;
152
        } else {
153
            status.push();
154
        }
155
        try {
156
            status.setRangeOfValues(0, statisticsBands.length * 3);
157
            status.message("Calculating statistics");
158
            long cont = 0;
159

    
160
            for (int i = 0; i < statisticsBands.length; i++) {
161
                status.setCurValue(cont++);
162
                if (status.isCancelled()) {
163
                    status.abort();
164
                    return;
165
                }
166
                statisticsBands[i].calculate(status);
167
            }
168
            for (int i = 0; i < statisticsBands.length; i++) {
169
                status.setCurValue(cont++);
170
                if (status.isCancelled()) {
171
                    status.abort();
172
                    return;
173
                }
174
                for (int j = i; j < statisticsBands.length; j++) {
175
                    this.varCov[i][j] = statisticsBands[i].getCovariance(statisticsBands[j]);
176
                }
177
            }
178

    
179
            for (int i = 0; i < statisticsBands.length; i++) {
180
                status.setCurValue(cont++);
181
                if (status.isCancelled()) {
182
                    status.abort();
183
                    return;
184
                }
185
                for (int j = 0; j < i; j++) {
186
                    varCov[i][j] = varCov[j][i];
187
                }
188
            }
189
            calculated = true;
190
            if (isMyStatus) {
191
                status.terminate();
192
            } else {
193
                status.pop();
194
            }
195
            calculated = true;
196
        } catch (Exception e) {
197
            status.abort();
198
            throw e;
199
        }
200
    }
201

    
202
    @Override
203
    public double[][] getVarianceCovarianceMatrix() {
204
        return this.varCov;
205
    }
206

    
207
    @Override
208
    public HistogramBand[] getHistogram() {
209
        HistogramBand[] histogramBands = new HistogramBand[statisticsBands.length];
210
        for (int i = 0; i < statisticsBands.length; i++) {
211
            histogramBands[i]= statisticsBands[i].getHistogramBand();
212
        }
213
        return histogramBands;
214
    }
215

    
216
    @Override
217
    public Object getTailTrimValue(double percent) {
218
        Object[] result = new Object[statisticsBands.length];
219
        for (int i = 0; i < statisticsBands.length; i++) {
220
            result[i]=statisticsBands[i].getTailTrimValue(percent);
221
        }
222
        return result;
223
    }
224

    
225
    @Override
226
    public Object[] getTailTrimValue(int pos) {
227
        Object[] result = new Object[statisticsBands.length];
228
        for (int i = 0; i < statisticsBands.length; i++) {
229
            result[i]=statisticsBands[i].getTailTrimValue(pos);
230
        }
231
        return result;
232
    }
233

    
234
    @Override
235
    public Iterator<StatisticsBand> iterator() {
236
        return Arrays.asList(statisticsBands).iterator();
237
    }
238

    
239
}