Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetStatistics.java @ 11930

History | View | Annotate | Download (9.17 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.dataset.properties;
20

    
21
import java.io.File;
22
import java.util.Date;
23
import java.util.Hashtable;
24

    
25
import org.gvsig.raster.RasterLibrary;
26
import org.gvsig.raster.dataset.FileNotOpenException;
27
import org.gvsig.raster.dataset.IBuffer;
28
import org.gvsig.raster.dataset.IStatistics;
29
import org.gvsig.raster.dataset.InvalidSetViewException;
30
import org.gvsig.raster.dataset.RasterDataset;
31
import org.gvsig.raster.dataset.RasterDriverException;
32

    
33

    
34
/**
35
 * Estadisticas asociadas a un fichero raster.
36
 *  
37
 * @author Nacho Brodin (nachobrodin@gmail.com)
38
 */
39
public class DatasetStatistics implements IStatistics{
40
        public static final int                CANCEL_FULLSTAT = 1;
41
        private boolean[]                        cancel = new boolean[1];
42
        
43
        /*
44
         * Esta a false si las estadisticas no son del fichero completo. Esto es posible porque podemos
45
         * tener unas estad?sticas calculadas a partir de una petici?n con subsampleo. Hay que tener en
46
         * cuenta que el raster puede ser muy grande y este calculo muy costoso.
47
         */
48
        protected boolean                                complete = false;
49
        protected double[]                         max = null;
50
        protected double[]                         min = null;
51
        protected double[]                         secondMax = null;
52
        protected double[]                         secondMin = null;
53
        protected double[]                         mean = null;
54
        protected double[]                         variance = null;
55

    
56
        protected String                                 fName = null;
57
        protected RasterDataset                grf = null;
58
        protected boolean                                calculated = false;
59
        protected Hashtable                        tailTrim = new Hashtable();
60
        
61
        /**
62
         * Constructor. Asigna el fichero asociado.
63
         */
64
        public DatasetStatistics(RasterDataset grf){
65
                this.grf = grf;
66
        }
67
        
68
        /**
69
         * Asigna el valor m?ximo del grid
70
         * @return Valor m?ximo
71
         */
72
        public void setMax(double[] max) {
73
                this.max = max;
74
        }
75

    
76
        /**
77
         * Asigna el valor del segundo m?ximo
78
         * @return Valor del segundo m?ximo
79
         */
80
        public void setSecondMax(double[] smax) {
81
                this.secondMax = smax;
82
        }
83
        
84
        /**
85
         * Asigna el valor m?dio del grid
86
         * @return Valor medio
87
         */
88
        public void setMean(double[] mean) {
89
                this.mean = mean;
90
        }
91

    
92
        /**
93
         * Asigna el valor m?ximo del grid
94
         * @return Valor m?nimo
95
         */
96
        public void setMin(double[] min) {
97
                this.min = min;
98
        }
99

    
100
        /**
101
         * Asigna el valor del segundo m?nimo
102
         * @return Valor del segundo m?nimo
103
         */
104
        public void setSecondMin(double[] smin) {
105
                this.secondMin = smin;
106
        }
107
        
108
        /**
109
         * Asigna la varianza
110
         * @return Varianza
111
         */
112
        public void setVariance(double[] variance) {
113
                this.variance = variance;
114
        }
115
        
116
        /*
117
         *  (non-Javadoc)
118
         * @see org.gvsig.fmap.driver.IStatistics#getMax()
119
         */
120
        public double[] getMax() {
121
                return max;
122
        }
123
        
124
        /*
125
         *  (non-Javadoc)
126
         * @see org.gvsig.fmap.driver.IStatistics#getSecondMax()
127
         */
128
        public double[] getSecondMax() {
129
                return secondMax;
130
        }
131
        
132
        /*
133
         *  (non-Javadoc)
134
         * @see org.gvsig.fmap.driver.IStatistics#getSecondMin()
135
         */
136
        public double[] getSecondMin() {
137
                return secondMin;
138
        }
139
        
140
        /*
141
         *  (non-Javadoc)
142
         * @see org.gvsig.fmap.driver.IStatistics#getMaximun()
143
         */
144
        public double getMaximun(){
145
                double m = Double.NEGATIVE_INFINITY;
146
                for(int i = 0; i < max.length; i++)
147
                        m = Math.max(m, max[i]);
148
                return m;
149
        }
150
        
151
        /*
152
         *  (non-Javadoc)
153
         * @see org.gvsig.fmap.driver.IStatistics#getMinimun()
154
         */
155
        public double getMinimun(){
156
                double m = Double.MAX_VALUE;
157
                for(int i = 0; i < min.length; i++)
158
                        m = Math.min(m, min[i]);
159
                return m;
160
        }
161

    
162
        /*
163
         *  (non-Javadoc)
164
         * @see org.gvsig.fmap.driver.IStatistics#getMean()
165
         */
166
        public double[] getMean() {
167
                return mean;
168
        }
169

    
170
        /*
171
         *  (non-Javadoc)
172
         * @see org.gvsig.fmap.driver.IStatistics#getMin()
173
         */
174
        public double[] getMin() {
175
                return min;
176
        }
177

    
178
        /*
179
         *  (non-Javadoc)
180
         * @see org.gvsig.fmap.driver.IStatistics#getVariance()
181
         */
182
        public double[] getVariance() {
183
                return variance;
184
        }
185
        
186
        /*
187
         *  (non-Javadoc)
188
         * @see org.gvsig.fmap.driver.IStatistics#getBandCount()
189
         */
190
        public int getBandCount(){
191
                return grf.getBandCount();
192
        }
193
        
194
        /*
195
         *  (non-Javadoc)
196
         * @see org.gvsig.fmap.driver.IStatistics#calcFullStatistics()
197
         */
198
        public void calcFullStatistics() throws FileNotOpenException, RasterDriverException{
199
                long t2;
200
                long t1 = new Date().getTime();
201
                int type = grf.getDataType();
202
                max = new double[grf.getBandCount()];
203
                min = new double[grf.getBandCount()];
204
                secondMax = new double[grf.getBandCount()];
205
                secondMin = new double[grf.getBandCount()];
206
                mean = new double[grf.getBandCount()];
207
                variance = new double[grf.getBandCount()];
208
                long[] iValues = new long[grf.getBandCount()];
209
                boolean[] init = new boolean[grf.getBandCount()];
210

    
211
                byte[][][] b = null;
212
                short[][][] s = null;
213
                int[][][] i = null;
214
                float[][][] f = null;
215
                double[][][] d = null;
216
        
217
                for (int iBand = 0; iBand < grf.getBandCount(); iBand ++) {
218
                        max[iBand] = Double.NEGATIVE_INFINITY; 
219
                        min[iBand] = Double.POSITIVE_INFINITY;
220
                        secondMax[iBand] = Double.NEGATIVE_INFINITY; 
221
                        secondMin[iBand] = Double.POSITIVE_INFINITY;
222
                        init[iBand] = true;
223
                }
224
                
225
                int h = RasterLibrary.blockHeight;
226
                for (int block = 0; block < grf.getHeight(); block += h) {                        
227
                        Object buf = null;
228
                        try {
229
                                buf = grf.readBlock(block, RasterLibrary.blockHeight);
230
                        } catch (InvalidSetViewException e) {
231
                                //La vista se asigna autom?ticamente
232
                        }
233
                        switch(type){
234
                        case IBuffer.TYPE_BYTE:                b = (byte[][][])buf;break;
235
                        case IBuffer.TYPE_SHORT:         s = (short[][][])buf;break;
236
                        case IBuffer.TYPE_INT:                 i = (int[][][])buf;break;
237
                        case IBuffer.TYPE_FLOAT:         f = (float[][][])buf;break;
238
                        case IBuffer.TYPE_DOUBLE:         d = (double[][][])buf;break;
239
                        }
240
                        
241
                        int hB = RasterLibrary.blockHeight;
242
                        if((block + hB) > grf.getHeight())
243
                                hB = Math.abs(grf.getHeight() - block);
244
                        for (int iBand = 0; iBand < grf.getBandCount(); iBand ++) {
245
                                for (int col = 0; col < grf.getWidth(); col ++) {
246
                                        for (int row = 0; row < hB; row++) {        
247
                                                double z = (b != null) ? (b[iBand][row][col] & 0xff) : ((s != null) ? (s[iBand][row][col] & 0xffff) : ((i != null) ? (i[iBand][row][col] & 0xffffff)  : (f != null) ? f[iBand][row][col] : d[iBand][row][col]));
248
                                                
249
                                                if (init[iBand]) {
250
                                                        min[iBand] = z;
251
                                                        //secondMin[iBand] = z;
252
                                                        max[iBand] = z;
253
                                                        //secondMax[iBand] = z;
254
                                                        init[iBand] = false;
255
                                                } else {
256
                                                        if ( min[iBand] > z ) {
257
                                                                secondMin[iBand] = min[iBand];
258
                                                                min[iBand] = z;
259
                                                        }
260
                                                        
261
                                                        if ( max[iBand] < z ) {
262
                                                                secondMax[iBand] = max[iBand];
263
                                                                max[iBand] = z;
264
                                                        }
265
                                                        
266
                                                        if(z < max[iBand]  && z > secondMax[iBand])
267
                                                                secondMax[iBand] = z;
268

    
269
                                                        if(z > min[iBand]  && z < secondMin[iBand])
270
                                                                secondMin[iBand] = z;
271
                                                }
272
                                                mean[iBand] += z;
273
                                                variance[iBand] += z * z;
274
                                                iValues[iBand]++;
275
                                        }
276
                                        
277
                                        if (isCanceled(CANCEL_FULLSTAT))
278
                                                return;
279
                                }
280
                        }
281
                }
282
                
283
                for (int iBand = 0; iBand < grf.getBandCount(); iBand ++) {
284
                        if( iValues[iBand] > 0 ) {
285
                                mean[iBand] /= (double) iValues[iBand];
286
                                variance[iBand] = variance[iBand] / (double) iValues[iBand] - mean[iBand] * mean[iBand];
287
                        }
288
                }
289
                
290
                calculated = true;
291
                t2 = new Date().getTime();
292
            System.out.println("Estadisticas " + grf.getFName() + ": " + ((t2 - t1) / 1000D) + ", secs.");
293
        }
294
        
295
        /**
296
         * Carga estadisticas desde el fichero Rmf si las hay.
297
         * @param fName Nombre del fichero
298
         */
299
        public void loadFromRmf(){
300
                if(grf != null){
301
                        File f = new File(grf.getFName());
302
                        if(!f.exists())
303
                                return;
304
                        //calculated = true;
305
                        //TODO: FUNCIONALIDAD: Carga de estad?sticas rmf
306
                }
307
        }
308
        
309
        /**
310
         * Salva estad?sticas a fichero rmf.
311
         * @param fName
312
         */
313
        public void saveToRmf(){
314
                //TODO: FUNCIONALIDAD: Salvado de estad?sticas a rmf
315
        }
316

    
317
        /*
318
         *  (non-Javadoc)
319
         * @see org.gvsig.fmap.driver.IStatistics#isCalculated()
320
         */
321
        public boolean isCalculated() {
322
                return calculated;
323
        }
324
        
325
        /*
326
         *  (non-Javadoc)
327
         * @see org.gvsig.fmap.driver.IStatistics#setTailTrimValue(double, java.lang.Object)
328
         */
329
        public void setTailTrimValue(double percent, Object valueByBand){
330
                String s = new Double(percent).toString();
331
                tailTrim.put(s, valueByBand);
332
        }
333
        
334
        /*
335
         *  (non-Javadoc)
336
         * @see org.gvsig.fmap.driver.IStatistics#getTailTrimValue(double)
337
         */
338
        public Object getTailTrimValue(double percent){
339
                String s = new Double(percent).toString();
340
                return tailTrim.get(s);
341
        }
342

    
343
        /*
344
         * (non-Javadoc)
345
         * @see org.gvsig.raster.util.ICancellable#isCanceled()
346
         */
347
        public boolean isCanceled(int process) {
348
                if(process == CANCEL_FULLSTAT)
349
                        return cancel[0];
350
                return false;
351
        }
352

    
353
        /*
354
         * (non-Javadoc)
355
         * @see org.gvsig.raster.util.ICancellable#setCanceled(boolean)
356
         */
357
        public void setCanceled(boolean value, int process) {
358
                if(process == (CANCEL_FULLSTAT | 0))
359
                        cancel[0] = value;
360
        }
361
}