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 / DefaultStatisticsBand.java @ 5515
History | View | Annotate | Download (6.99 KB)
1 |
package org.gvsig.raster.lib.buffer.impl.statistics; |
---|---|
2 |
|
3 |
import org.gvsig.raster.lib.buffer.api.Band; |
4 |
import org.gvsig.raster.lib.buffer.api.statistics.HistogramBand; |
5 |
import org.gvsig.raster.lib.buffer.api.statistics.StatisticsBand; |
6 |
import org.gvsig.tools.ToolsLocator; |
7 |
import org.gvsig.tools.task.SimpleTaskStatus; |
8 |
|
9 |
/**
|
10 |
* @author fdiaz
|
11 |
*
|
12 |
*/
|
13 |
public class DefaultStatisticsBand implements StatisticsBand { |
14 |
|
15 |
private Band band;
|
16 |
|
17 |
private boolean calculated; |
18 |
|
19 |
private double mean; |
20 |
private double max; |
21 |
private double secondMax; |
22 |
private double min; |
23 |
private double secondMin; |
24 |
private double variance; |
25 |
private HistogramBand histogramBand;
|
26 |
|
27 |
/**
|
28 |
* @param band
|
29 |
*/
|
30 |
public DefaultStatisticsBand(Band band) {
|
31 |
this.band = band;
|
32 |
this.calculated = false; |
33 |
} |
34 |
|
35 |
@Override
|
36 |
public boolean isCalculated() { |
37 |
return calculated;
|
38 |
} |
39 |
|
40 |
@Override
|
41 |
public void calculate(SimpleTaskStatus status) { |
42 |
boolean isMyStatus = false; |
43 |
if (status == null) { |
44 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Statistics");
|
45 |
status.add(); |
46 |
isMyStatus = true;
|
47 |
} else {
|
48 |
status.push(); |
49 |
} |
50 |
try {
|
51 |
calculated = false;
|
52 |
status.setRangeOfValues(0, band.getRows() * 2); |
53 |
status.message("Calculating statistics band");
|
54 |
long cont = 0; |
55 |
|
56 |
MeanOperation meanOperation = new MeanOperation();
|
57 |
MaxOperation maxOperation = new MaxOperation();
|
58 |
MinOperation minOperation = new MinOperation();
|
59 |
SecondMaxOperation secondMaxOperation = new SecondMaxOperation();
|
60 |
SecondMinOperation secondMinOperation = new SecondMinOperation();
|
61 |
VarianceOperation varianceOperation = new VarianceOperation();
|
62 |
// TODO: CovarianceOperation
|
63 |
|
64 |
meanOperation.pre(); |
65 |
maxOperation.pre(); |
66 |
minOperation.pre(); |
67 |
secondMaxOperation.pre(); |
68 |
secondMinOperation.pre(); |
69 |
varianceOperation.pre(); |
70 |
|
71 |
for (int row = 0; row < band.getRows(); row++) { |
72 |
status.setCurValue(cont++); |
73 |
if (status.isCancelled()) {
|
74 |
status.abort(); |
75 |
return;
|
76 |
} |
77 |
for (int column = 0; column < band.getColumns(); column++) { |
78 |
Object obj = band.get(row, column);
|
79 |
if (obj instanceof Number) { |
80 |
double value = ((Number) obj).doubleValue(); |
81 |
meanOperation.addValue(value); |
82 |
maxOperation.addValue(value); |
83 |
minOperation.addValue(value); |
84 |
secondMaxOperation.addValue(value); |
85 |
secondMinOperation.addValue(value); |
86 |
varianceOperation.addValue(value); |
87 |
} |
88 |
} |
89 |
} |
90 |
meanOperation.post(); |
91 |
maxOperation.post(); |
92 |
minOperation.post(); |
93 |
secondMaxOperation.post(); |
94 |
secondMinOperation.post(); |
95 |
varianceOperation.post(); |
96 |
|
97 |
this.mean = ((Number) meanOperation.getResult()).doubleValue(); |
98 |
this.max = ((Number) maxOperation.getResult()).doubleValue(); |
99 |
this.min = ((Number) minOperation.getResult()).doubleValue(); |
100 |
this.secondMax = ((Number) secondMaxOperation.getResult()).doubleValue(); |
101 |
this.secondMin = ((Number) secondMinOperation.getResult()).doubleValue(); |
102 |
this.variance = ((Number) varianceOperation.getResult()).doubleValue(); |
103 |
HistogramOperation histogramOperation = new HistogramOperation(this, this.band); |
104 |
histogramOperation.pre(); |
105 |
for (int row = 0; row < band.getRows(); row++) { |
106 |
status.setCurValue(cont++); |
107 |
if (status.isCancelled()) {
|
108 |
status.abort(); |
109 |
return;
|
110 |
} |
111 |
for (int column = 0; column < band.getColumns(); column++) { |
112 |
Object obj = band.get(row, column);
|
113 |
if (obj instanceof Number) { |
114 |
double value = ((Number) obj).doubleValue(); |
115 |
histogramOperation.addValue(value); |
116 |
} |
117 |
} |
118 |
} |
119 |
histogramOperation.post(); |
120 |
this.histogramBand = ((HistogramBand) histogramOperation.getResult());
|
121 |
if(isMyStatus){
|
122 |
status.terminate(); |
123 |
} else {
|
124 |
status.pop(); |
125 |
} |
126 |
calculated = true;
|
127 |
} catch (Exception e) { |
128 |
status.abort(); |
129 |
throw e;
|
130 |
} |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public int getDataType() { |
135 |
return this.band.getDataType(); |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public long getBandLenght() { |
140 |
return band.getRows() * band.getColumns();
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public double getSecondMax() { |
145 |
// TODO ??? y si no est? calculado???
|
146 |
return this.secondMax; |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public double getSecondMin() { |
151 |
return this.secondMin; |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public double getMaximum() { |
156 |
return this.max; |
157 |
} |
158 |
|
159 |
@Override
|
160 |
public double getMinimum() { |
161 |
return this.min; |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public double getMean() { |
166 |
return this.mean; |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public double getVariance() { |
171 |
return this.variance; |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public Object getTailTrimValue(double percent) { |
176 |
return getHistogramBand().getTailTrimValue(percent);
|
177 |
} |
178 |
|
179 |
@Override
|
180 |
public Object getTailTrimValue(int pos) { |
181 |
return getHistogramBand().getTailTrimValue(pos);
|
182 |
} |
183 |
|
184 |
@Override
|
185 |
public HistogramBand getHistogramBand() {
|
186 |
return this.histogramBand; |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public double getCovariance(StatisticsBand statisticBand) { |
191 |
Band otherBand = ((DefaultStatisticsBand) statisticBand).getBand(); |
192 |
long otherBandLenght = statisticBand.getBandLenght();
|
193 |
|
194 |
if (getBandLenght() != otherBandLenght) {
|
195 |
return 0; |
196 |
} |
197 |
|
198 |
MeanOperation productMeanOperation = new MeanOperation();
|
199 |
|
200 |
productMeanOperation.pre(); |
201 |
|
202 |
for (int row = 0; row < band.getRows(); row++) { |
203 |
for (int column = 0; column < band.getColumns(); column++) { |
204 |
Object obj = band.get(row, column);
|
205 |
Object obj2 = otherBand.get(row, column);
|
206 |
double value = ((Number) obj).doubleValue() * ((Number) obj2).doubleValue(); |
207 |
productMeanOperation.addValue(value); |
208 |
} |
209 |
} |
210 |
productMeanOperation.post(); |
211 |
|
212 |
return ((Number) productMeanOperation.getResult()).doubleValue() - (this.getMean() * statisticBand.getMean()); |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public double getMedian() { |
217 |
return getHistogramBand().getMedian();
|
218 |
} |
219 |
|
220 |
protected Band getBand() {
|
221 |
return this.band; |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public long getNoDataValuesCount() { |
226 |
// TODO Auto-generated method stub
|
227 |
return 0; |
228 |
} |
229 |
|
230 |
} |