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