Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / GridStatistic.java @ 11200

History | View | Annotate | Download (2.93 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.grid;
20

    
21
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
22

    
23

    
24
/**
25
 * Estadisticas de un grid
26
 * @author Victor Olaya (volaya@ya.com)
27
 * @author Nacho Brodin (nachobrodin@gmail.com)
28
 */
29
public class GridStatistic {
30
        private double                                 max;
31
        private double                                 min;
32
        private double                                 mean;
33
        private double                                 variance;
34
        private boolean                         statisticsCalculated = false;
35
        private Grid                                grid = null;
36
        
37
        public GridStatistic(Grid grid){
38
                this.grid = grid;
39
        }
40
        
41
        public void calculateStatistics() throws RasterBufferInvalidAccessException{
42
                int x, y;
43
                double z;
44
                int iValues;
45
                
46
                mean        = 0.0;
47
                variance        = 0.0;
48
                iValues        = 0;
49

    
50
                statisticsCalculated = true;
51
                
52
                for (y = 0; y < grid.getNY(); y++){
53
                        for (x = 0; x < grid.getNX(); x++){
54
                                z = grid.getCellValueAsDouble(x,y);
55
                                if( !grid.isNoDataValue(z))        {
56
                                        if( iValues == 0 )
57
                                                min = max = z;
58
                                        else if( min > z )
59
                                                min        = z;
60
                                        else if( max < z )
61
                                                max        = z;
62

    
63
                                        mean        += z;
64
                                        variance += z * z;
65
                                        iValues++;
66
                                }
67
                        }
68
                }
69

    
70
                if( iValues > 0 ){
71
                        mean        /= (double) iValues;
72
                        variance = variance / (double) iValues - mean * mean;
73
                }
74
                statisticsCalculated = true;
75
        }
76

    
77
        /**
78
         * Obtiene el valor m?ximo del grid
79
         * @return Valor m?ximo
80
         */
81
        public double getMax() {
82
                return max;
83
        }
84

    
85
        /**
86
         * Obtiene el valor m?dio del grid
87
         * @return Valor medio
88
         */
89
        public double getMean() {
90
                return mean;
91
        }
92

    
93
        /**
94
         * Obtiene el valor m?ximo del grid
95
         * @return Valor m?nimo
96
         */
97
        public double getMin() {
98
                return min;
99
        }
100

    
101
        /**
102
         * Consulta si las estadisticas han sido calculadas o no.
103
         * @return true si las estadisticas del grid est?n calculadas y false si no lo est?n 
104
         */
105
        public boolean isStatisticsCalculated() {
106
                return statisticsCalculated;
107
        }
108
        
109
        /**
110
         * Asigna el flag que informa si las estadisticas est?n calculadas para un grid concreto
111
         * @param calc true si est?n calculadas y false si no lo est?n
112
         */
113
        public void setStatisticsCalculated(boolean calc){
114
                this.statisticsCalculated = calc;
115
        }
116

    
117
        /**
118
         * Obtiene la varianza
119
         * @return Varianza
120
         */
121
        public double getVariance() {
122
                return variance;
123
        }
124
}