gvsig-raster / org.gvsig.raster / tags / 2.0.0 / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridStatistic.java @ 1708
History | View | Annotate | Download (5.8 KB)
1 | 21 | nbrodin | /* 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; |
||
23 | |||
24 | 55 | nbrodin | import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
25 | 28 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.BandAccessException; |
26 | import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
||
27 | 21 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.GridException; |
28 | import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidAccessException; |
||
29 | 28 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
30 | 55 | nbrodin | import org.gvsig.fmap.dal.coverage.store.props.BasicStats; |
31 | 21 | nbrodin | |
32 | |||
33 | /**
|
||
34 | * Estadisticas de un grid
|
||
35 | * @author Victor Olaya (volaya@ya.com)
|
||
36 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
37 | */
|
||
38 | 28 | nbrodin | public class GridStatistic implements BasicStats { |
39 | 21 | nbrodin | private double[] max = null; |
40 | private double[] min = null; |
||
41 | private double[] mean = null; |
||
42 | private double[] variance = null; |
||
43 | private boolean statisticsCalculated = false; |
||
44 | 28 | nbrodin | private GridImpl grid = null; |
45 | 21 | nbrodin | protected int bandToOperate = 0; |
46 | |||
47 | public GridStatistic(GridImpl grid){
|
||
48 | this.grid = grid;
|
||
49 | } |
||
50 | |||
51 | 28 | nbrodin | private void calculateStatistics() throws GridException { |
52 | 21 | nbrodin | int x, y;
|
53 | double z;
|
||
54 | int[] iValues = new int[grid.getBandCount()]; |
||
55 | mean = new double[grid.getBandCount()]; |
||
56 | min = new double[grid.getBandCount()]; |
||
57 | max = new double[grid.getBandCount()]; |
||
58 | variance = new double[grid.getBandCount()]; |
||
59 | |||
60 | for (int iBand = 0; iBand < grid.getBandCount(); iBand++) { |
||
61 | mean[iBand] = 0.0;
|
||
62 | variance[iBand] = 0.0;
|
||
63 | iValues[iBand] = 0;
|
||
64 | } |
||
65 | int oldBandToOperate = bandToOperate;
|
||
66 | statisticsCalculated = true;
|
||
67 | |||
68 | for (int iBand = 0; iBand < grid.getBandCount(); iBand++) { |
||
69 | grid.setBandToOperate(iBand); |
||
70 | for (y = 0; y < grid.getNY(); y++){ |
||
71 | for (x = 0; x < grid.getNX(); x++){ |
||
72 | z = getValue(x, y); |
||
73 | if( !grid.isNoDataValue(z)) {
|
||
74 | if( iValues[iBand] == 0 ) |
||
75 | min[iBand] = max[iBand] = z; |
||
76 | else if( min[iBand] > z ) |
||
77 | min[iBand] = z; |
||
78 | else if( max[iBand] < z ) |
||
79 | max[iBand] = z; |
||
80 | |||
81 | mean[iBand] += z; |
||
82 | variance[iBand] += z * z; |
||
83 | iValues[iBand]++; |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | if( iValues[iBand] > 0 ){ |
||
89 | mean[iBand] /= (double) iValues[iBand];
|
||
90 | variance[iBand] = variance[iBand] / (double) iValues[iBand] - mean[iBand] * mean[iBand];
|
||
91 | } |
||
92 | } |
||
93 | bandToOperate = oldBandToOperate; |
||
94 | statisticsCalculated = true;
|
||
95 | } |
||
96 | |||
97 | /**
|
||
98 | * Obtiene un valor de una celda del grid independientemente del tipo de dato.
|
||
99 | * @param x Posici?n en X
|
||
100 | * @param y Posici?n en Y
|
||
101 | * @return Valor de la celda en formato double
|
||
102 | * @throws RasterBufferInvalidAccessException
|
||
103 | */
|
||
104 | private double getValue(int x, int y) throws GridException { |
||
105 | switch(grid.getDataType()) {
|
||
106 | case Buffer.TYPE_BYTE:return (double)(grid.getCellValueAsByte(x, y) & 0xff); |
||
107 | case Buffer.TYPE_SHORT:return (double)(grid.getCellValueAsShort(x, y) & 0xffff); |
||
108 | case Buffer.TYPE_INT:return (double)(grid.getCellValueAsInt(x, y) & 0xffffffff); |
||
109 | case Buffer.TYPE_FLOAT:return (double)grid.getCellValueAsFloat(x, y); |
||
110 | case Buffer.TYPE_DOUBLE:return (double)grid.getCellValueAsDouble(x, y); |
||
111 | } |
||
112 | return 0; |
||
113 | } |
||
114 | |||
115 | /**
|
||
116 | 28 | nbrodin | * Asigna el flag que informa si las estadisticas est?n calculadas para un grid concreto
|
117 | * @param calc true si est?n calculadas y false si no lo est?n
|
||
118 | 21 | nbrodin | */
|
119 | 28 | nbrodin | public void setStatisticsCalculated(boolean calc){ |
120 | this.statisticsCalculated = calc;
|
||
121 | 21 | nbrodin | } |
122 | 28 | nbrodin | |
123 | 21 | nbrodin | /**
|
124 | 28 | nbrodin | * Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
|
125 | * con lo que para el uso de MDTs no habr? que modificar este valor.
|
||
126 | * @param band Banda sobre la que se realizan las operaciones.
|
||
127 | 21 | nbrodin | */
|
128 | 28 | nbrodin | public void setBandToOperate(int band){ |
129 | this.bandToOperate = band;
|
||
130 | 21 | nbrodin | } |
131 | |||
132 | 28 | nbrodin | public void calcFullStatistics() throws FileNotOpenException, |
133 | RasterDriverException, InterruptedException {
|
||
134 | try {
|
||
135 | calculateStatistics(); |
||
136 | } catch (GridException e) {
|
||
137 | throw new RasterDriverException(""); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /*
|
||
142 | * (non-Javadoc)
|
||
143 | * @see org.gvsig.fmap.dal.coverage.dataset.BasicStats#getMaxValue()
|
||
144 | 21 | nbrodin | */
|
145 | 28 | nbrodin | public double getMaxValue() throws BandAccessException { |
146 | return max[bandToOperate];
|
||
147 | 21 | nbrodin | } |
148 | |||
149 | 28 | nbrodin | /*
|
150 | * (non-Javadoc)
|
||
151 | * @see org.gvsig.fmap.dal.coverage.dataset.BasicStats#getMeanValue()
|
||
152 | 21 | nbrodin | */
|
153 | 28 | nbrodin | public double getMeanValue() throws BandAccessException { |
154 | return mean[bandToOperate];
|
||
155 | 21 | nbrodin | } |
156 | 28 | nbrodin | |
157 | /*
|
||
158 | * (non-Javadoc)
|
||
159 | * @see org.gvsig.fmap.dal.coverage.dataset.BasicStats#getMinValue()
|
||
160 | 21 | nbrodin | */
|
161 | 28 | nbrodin | public double getMinValue() throws BandAccessException { |
162 | return min[bandToOperate];
|
||
163 | 21 | nbrodin | } |
164 | |||
165 | 28 | nbrodin | /*
|
166 | * (non-Javadoc)
|
||
167 | * @see org.gvsig.fmap.dal.coverage.dataset.BasicStats#getVarianceValue()
|
||
168 | 21 | nbrodin | */
|
169 | 28 | nbrodin | public double getVarianceValue() throws BandAccessException { |
170 | 21 | nbrodin | return variance[bandToOperate];
|
171 | } |
||
172 | 28 | nbrodin | |
173 | /*
|
||
174 | * (non-Javadoc)
|
||
175 | * @see org.gvsig.fmap.dal.coverage.dataset.BasicStats#isCalculated()
|
||
176 | 21 | nbrodin | */
|
177 | 28 | nbrodin | public boolean isCalculated() { |
178 | return statisticsCalculated;
|
||
179 | 21 | nbrodin | } |
180 | |||
181 | 1055 | nbrodin | public void dispose() { |
182 | try {
|
||
183 | finalize(); |
||
184 | } catch (Throwable e) { |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /*
|
||
189 | * (non-Javadoc)
|
||
190 | * @see java.lang.Object#finalize()
|
||
191 | */
|
||
192 | protected void finalize() throws Throwable { |
||
193 | max = null;
|
||
194 | min = null;
|
||
195 | mean = null;
|
||
196 | variance = null;
|
||
197 | grid = null;
|
||
198 | super.finalize();
|
||
199 | } |
||
200 | 21 | nbrodin | } |