gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / roi / ROIStatistic.java @ 2125
History | View | Annotate | Download (10.7 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 |
*/
|
22 |
package org.gvsig.raster.impl.grid.roi; |
23 |
|
24 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
25 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
26 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
27 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
28 |
import org.gvsig.fmap.dal.coverage.exception.GridException; |
29 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
30 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
31 |
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidAccessException; |
32 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
33 |
import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
34 |
import org.gvsig.fmap.dal.coverage.store.props.Statistics; |
35 |
import org.gvsig.raster.roi.AbstractROI; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
/**
|
40 |
* Estad?sticas asociadas a una ROI.
|
41 |
*
|
42 |
* Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
43 |
*
|
44 |
*/
|
45 |
public class ROIStatistic implements Statistics { |
46 |
private static final Logger logger = LoggerFactory.getLogger(ROIStatistic.class); |
47 |
private AbstractROI roi = null; |
48 |
private long[] values = null; |
49 |
private double[] max = null; |
50 |
private double[] min = null; |
51 |
private double[] mean = null; |
52 |
private double[] variance = null; |
53 |
private boolean statisticsCalculated = false; |
54 |
private boolean advancedStatisticsCalculated = false; |
55 |
private Buffer fullBuffer = null; |
56 |
//protected int bandToOperate = 0;
|
57 |
|
58 |
/**
|
59 |
* Matriz de varianza covarianza
|
60 |
*/
|
61 |
private double [][] varCov = null; |
62 |
|
63 |
public ROIStatistic(AbstractROI roi) {
|
64 |
this.roi = roi;
|
65 |
} |
66 |
|
67 |
private Buffer getFullBuffer() { |
68 |
if(fullBuffer == null) { |
69 |
RasterManager rManager = RasterLocator.getManager(); |
70 |
RasterQuery query = rManager.createQuery(); |
71 |
query.setAllDrawableBands(); |
72 |
query.setReadOnly(true);
|
73 |
try {
|
74 |
query.setAreaOfInterest(); |
75 |
fullBuffer = roi.getStore().query(query); |
76 |
} catch (RasterDriverException e) {
|
77 |
logger.debug("Error reading buffer", e);
|
78 |
} catch (ProcessInterruptedException e) {
|
79 |
logger.debug("Carga interrumpida", e);
|
80 |
} catch (InvalidSetViewException e) {
|
81 |
logger.debug("Error reading buffer", e);
|
82 |
} |
83 |
} |
84 |
return fullBuffer;
|
85 |
} |
86 |
|
87 |
/**
|
88 |
* Calcula las estad?sticas b?sicas (Max, Min, Media, Varianza).
|
89 |
*
|
90 |
* @throws RasterBufferInvalidAccessException
|
91 |
*/
|
92 |
public void calculate(double scale) throws RasterDriverException { |
93 |
int x, y;
|
94 |
double z;
|
95 |
int bandCount = roi.getStore().getBandCount();
|
96 |
values = new long[bandCount]; |
97 |
mean = new double[bandCount]; |
98 |
min = new double[bandCount]; |
99 |
max = new double[bandCount]; |
100 |
variance = new double[bandCount]; |
101 |
|
102 |
for (int iBand = 0; iBand < bandCount; iBand++) { |
103 |
mean[iBand] = 0.0;
|
104 |
variance[iBand] = 0.0;
|
105 |
values[iBand] = 0;
|
106 |
} |
107 |
//int oldBandToOperate = bandToOperate;
|
108 |
statisticsCalculated = true;
|
109 |
|
110 |
for (int iBand = 0; iBand < bandCount; iBand++) { |
111 |
for (y = 0; y < getFullBuffer().getHeight(); y++){ |
112 |
for (x = 0; x < getFullBuffer().getWidth(); x++){ |
113 |
z = getValue(x, y, iBand); |
114 |
if(roi.getStore().getNoDataValue().getValue().doubleValue() != z) {
|
115 |
if( values[iBand] == 0 ) |
116 |
min[iBand] = max[iBand] = z; |
117 |
else if( min[iBand] > z ) |
118 |
min[iBand] = z; |
119 |
else if( max[iBand] < z ) |
120 |
max[iBand] = z; |
121 |
|
122 |
mean[iBand] += z; |
123 |
variance[iBand] += z * z; |
124 |
values[iBand]++; |
125 |
} |
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
for (int iBand = 0; iBand < bandCount; iBand++) { |
131 |
if( values[iBand] > 0 ){ |
132 |
mean[iBand] /= (double) values[iBand];
|
133 |
variance[iBand] = variance[iBand] / (double) values[iBand] - mean[iBand] * mean[iBand];
|
134 |
} |
135 |
} |
136 |
//bandToOperate = oldBandToOperate;
|
137 |
statisticsCalculated = true;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Calculo de estadisticas avanzadas (Matriz varianza- covarianza).
|
142 |
*/
|
143 |
public void calculateAdvanced() throws GridException { |
144 |
if (!isCalculated())
|
145 |
try {
|
146 |
calculate(1);
|
147 |
} catch (RasterDriverException e) {
|
148 |
throw new GridException("", e); |
149 |
} |
150 |
|
151 |
int bandCount = roi.getBandCount();
|
152 |
double dSum[][] = new double[bandCount][bandCount]; |
153 |
double iValues[][] = new double[bandCount][bandCount]; |
154 |
varCov = new double[bandCount][bandCount]; |
155 |
double valorBandai = 0, valorBandaj = 0; |
156 |
|
157 |
for (int iBand = 0; iBand < bandCount; iBand++) |
158 |
for (int jBand = 0; jBand < bandCount; jBand++){ |
159 |
dSum[iBand][jBand] = 0;
|
160 |
iValues[iBand][jBand] = 0;
|
161 |
} |
162 |
|
163 |
for (int k = 0; k < getFullBuffer().getHeight(); k++) { |
164 |
for (int l = 0; l < getFullBuffer().getWidth(); l++) { |
165 |
for (int i = 0; i < bandCount; i++) { |
166 |
for (int j = i; j < bandCount; j++) { |
167 |
valorBandai = getValue(l, k, i); |
168 |
valorBandaj = getValue(l, k, j); |
169 |
if (roi.getStore().getNoDataValue().getValue().doubleValue() != valorBandai &&
|
170 |
roi.getStore().getNoDataValue().getValue().doubleValue() != valorBandaj) { |
171 |
valorBandai = valorBandai - mean[i]; |
172 |
valorBandaj = valorBandaj - mean[j]; |
173 |
dSum[i][j] += valorBandai*valorBandaj; |
174 |
iValues[i][j]++; |
175 |
} |
176 |
} |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
// Asigno el valor a la matriz
|
182 |
for (int iBand = 0; iBand < bandCount; iBand++) |
183 |
for (int jBand = 0; jBand < bandCount; jBand++) |
184 |
if (iValues[iBand][jBand] > 1) |
185 |
varCov[iBand][jBand] = dSum[iBand][jBand] / (double)(iValues[iBand][jBand]);
|
186 |
else
|
187 |
varCov[iBand][jBand] = roi.getStore().getNoDataValue().getValue().doubleValue(); |
188 |
|
189 |
// Completar parte simetrica de la matriz
|
190 |
for (int i = 0; i < bandCount; i++) { |
191 |
for (int j = 0; j < bandCount; j++) { |
192 |
if(j < i)
|
193 |
varCov[i][j] = varCov[j][i]; |
194 |
} |
195 |
} |
196 |
advancedStatisticsCalculated = true;
|
197 |
} |
198 |
|
199 |
private double getValue(int j, int i, int band) { |
200 |
double val = 0; |
201 |
Buffer b = getFullBuffer();
|
202 |
if(b.getDataType() == Buffer.TYPE_BYTE) { |
203 |
val = (double)b.getElemByte(j, i, band);
|
204 |
return val;
|
205 |
} |
206 |
if(b.getDataType() == Buffer.TYPE_DOUBLE) { |
207 |
val = (double)b.getElemDouble(j, i, band);
|
208 |
return val;
|
209 |
} |
210 |
if(b.getDataType() == Buffer.TYPE_FLOAT) { |
211 |
val = (double)b.getElemFloat(j, i, band);
|
212 |
return val;
|
213 |
} |
214 |
if(b.getDataType() == Buffer.TYPE_INT) { |
215 |
val = (double)b.getElemInt(j, i, band);
|
216 |
return val;
|
217 |
} |
218 |
if(b.getDataType() == Buffer.TYPE_SHORT) { |
219 |
val = (double)b.getElemShort(j, i, band);
|
220 |
return val;
|
221 |
} |
222 |
NoData nd = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(1, Buffer.TYPE_DOUBLE); |
223 |
return nd.getValue().doubleValue();
|
224 |
} |
225 |
|
226 |
/*
|
227 |
* (non-Javadoc)
|
228 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#getMaximun()
|
229 |
*/
|
230 |
public double getMaximun() { |
231 |
double m = Double.NEGATIVE_INFINITY; |
232 |
for (int i = 0; i < max.length; i++) |
233 |
m = Math.max(m, max[i]);
|
234 |
return m;
|
235 |
} |
236 |
|
237 |
/*
|
238 |
* (non-Javadoc)
|
239 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#getMean()
|
240 |
*/
|
241 |
public double[] getMean() { |
242 |
return mean;
|
243 |
} |
244 |
|
245 |
/*
|
246 |
* (non-Javadoc)
|
247 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#getMinimun()
|
248 |
*/
|
249 |
public double getMinimun() { |
250 |
double m = Double.MAX_VALUE; |
251 |
for (int i = 0; i < min.length; i++) |
252 |
m = Math.min(m, min[i]);
|
253 |
return m;
|
254 |
} |
255 |
|
256 |
/*
|
257 |
* (non-Javadoc)
|
258 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#getMin()
|
259 |
*/
|
260 |
public double[] getMin() { |
261 |
return min;
|
262 |
} |
263 |
|
264 |
/*
|
265 |
* (non-Javadoc)
|
266 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#getMax()
|
267 |
*/
|
268 |
public double[] getMax() { |
269 |
return max;
|
270 |
} |
271 |
|
272 |
/*
|
273 |
* (non-Javadoc)
|
274 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#isCalculated()
|
275 |
*/
|
276 |
public boolean isCalculated() { |
277 |
return statisticsCalculated;
|
278 |
} |
279 |
|
280 |
/*
|
281 |
* (non-Javadoc)
|
282 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#setCalculated(boolean)
|
283 |
*/
|
284 |
public void setCalculated(boolean calc) { |
285 |
statisticsCalculated = calc; |
286 |
advancedStatisticsCalculated = calc; |
287 |
} |
288 |
|
289 |
/**
|
290 |
* Obtiene la varianza
|
291 |
* @return Varianza
|
292 |
*/
|
293 |
public double[] getVariance() { |
294 |
return variance;
|
295 |
} |
296 |
|
297 |
/**
|
298 |
* Devuelve la matriz de varianza-covarianza, si no se encuentra calculada se calcula
|
299 |
* @retrun Matriz de varianza-covarianza
|
300 |
*/
|
301 |
public double[][] getVarianceCovarianceMatrix() { |
302 |
return varCov;
|
303 |
} |
304 |
|
305 |
public boolean isAdvancedStatisticsCalculated() { |
306 |
return advancedStatisticsCalculated;
|
307 |
|
308 |
} |
309 |
|
310 |
public void setAdvancedStatisticCalculated( |
311 |
boolean advancedStatisticCalculated) {
|
312 |
this.advancedStatisticsCalculated = advancedStatisticCalculated;
|
313 |
} |
314 |
|
315 |
/**
|
316 |
*
|
317 |
* @return n?mero de celdas del ROI.
|
318 |
*/
|
319 |
public long[] getNumberOfCells() { |
320 |
return values;
|
321 |
} |
322 |
|
323 |
public void forceToRecalc() { |
324 |
|
325 |
} |
326 |
|
327 |
public int getBandCount() { |
328 |
return 0; |
329 |
} |
330 |
|
331 |
public void setBandCount(int bandCount) { |
332 |
} |
333 |
|
334 |
public double[] getMaxByteUnsigned() { |
335 |
return null; |
336 |
} |
337 |
|
338 |
public double getMaximunByteUnsigned() { |
339 |
return 0; |
340 |
} |
341 |
|
342 |
public double[] getMinByteUnsigned() { |
343 |
return null; |
344 |
} |
345 |
|
346 |
public double getMinimunByteUnsigned() { |
347 |
return 0; |
348 |
} |
349 |
|
350 |
public int getPercent() { |
351 |
return 0; |
352 |
} |
353 |
|
354 |
public double[] getSecondMax() { |
355 |
return null; |
356 |
} |
357 |
|
358 |
public double[] getSecondMaxByteUnsigned() { |
359 |
return null; |
360 |
} |
361 |
|
362 |
public double[] getSecondMin() { |
363 |
return null; |
364 |
} |
365 |
|
366 |
public double[] getSecondMinByteUnsigned() { |
367 |
return null; |
368 |
} |
369 |
|
370 |
public int getTailTrimCount() { |
371 |
return 0; |
372 |
} |
373 |
|
374 |
public Object getTailTrimValue(double percent) { |
375 |
return null; |
376 |
} |
377 |
|
378 |
public void setTailTrimValue(double percent, Object valueByBand) { |
379 |
|
380 |
} |
381 |
|
382 |
public Object[] getTailTrimValue(int pos) { |
383 |
return null; |
384 |
} |
385 |
|
386 |
public void resetPercent() { |
387 |
|
388 |
} |
389 |
|
390 |
public Statistics cloneStatistics() {
|
391 |
return null; |
392 |
} |
393 |
|
394 |
/*
|
395 |
* (non-Javadoc)
|
396 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#getNumberOfValues()
|
397 |
*/
|
398 |
public long[] getNumberOfValues() { |
399 |
return values;
|
400 |
} |
401 |
|
402 |
/*
|
403 |
* (non-Javadoc)
|
404 |
* @see org.gvsig.fmap.dal.coverage.store.props.Statistics#setNumberOfValues(long[])
|
405 |
*/
|
406 |
public void setNumberOfValues(long[] values) { |
407 |
this.values = values;
|
408 |
} |
409 |
|
410 |
} |