root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetStatistics.java @ 11207
History | View | Annotate | Download (8.95 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 |
/*
|
41 |
* Esta a false si las estadisticas no son del fichero completo. Esto es posible porque podemos
|
42 |
* tener unas estad?sticas calculadas a partir de una petici?n con subsampleo. Hay que tener en
|
43 |
* cuenta que el raster puede ser muy grande y este calculo muy costoso.
|
44 |
*/
|
45 |
protected boolean complete = false; |
46 |
protected double[] max = null; |
47 |
protected double[] min = null; |
48 |
protected double[] secondMax = null; |
49 |
protected double[] secondMin = null; |
50 |
protected double[] mean = null; |
51 |
protected double[] variance = null; |
52 |
|
53 |
protected String fName = null; |
54 |
protected RasterDataset grf = null; |
55 |
protected boolean calculated = false; |
56 |
protected Hashtable tailTrim = new Hashtable(); |
57 |
private boolean canceled = false; |
58 |
|
59 |
/**
|
60 |
* Constructor. Asigna el fichero asociado.
|
61 |
*/
|
62 |
public DatasetStatistics(RasterDataset grf){
|
63 |
this.grf = grf;
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* Asigna el valor m?ximo del grid
|
68 |
* @return Valor m?ximo
|
69 |
*/
|
70 |
public void setMax(double[] max) { |
71 |
this.max = max;
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* Asigna el valor del segundo m?ximo
|
76 |
* @return Valor del segundo m?ximo
|
77 |
*/
|
78 |
public void setSecondMax(double[] smax) { |
79 |
this.secondMax = smax;
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* Asigna el valor m?dio del grid
|
84 |
* @return Valor medio
|
85 |
*/
|
86 |
public void setMean(double[] mean) { |
87 |
this.mean = mean;
|
88 |
} |
89 |
|
90 |
/**
|
91 |
* Asigna el valor m?ximo del grid
|
92 |
* @return Valor m?nimo
|
93 |
*/
|
94 |
public void setMin(double[] min) { |
95 |
this.min = min;
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* Asigna el valor del segundo m?nimo
|
100 |
* @return Valor del segundo m?nimo
|
101 |
*/
|
102 |
public void setSecondMin(double[] smin) { |
103 |
this.secondMin = smin;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Asigna la varianza
|
108 |
* @return Varianza
|
109 |
*/
|
110 |
public void setVariance(double[] variance) { |
111 |
this.variance = variance;
|
112 |
} |
113 |
|
114 |
/*
|
115 |
* (non-Javadoc)
|
116 |
* @see org.gvsig.fmap.driver.IStatistics#getMax()
|
117 |
*/
|
118 |
public double[] getMax() { |
119 |
return max;
|
120 |
} |
121 |
|
122 |
/*
|
123 |
* (non-Javadoc)
|
124 |
* @see org.gvsig.fmap.driver.IStatistics#getSecondMax()
|
125 |
*/
|
126 |
public double[] getSecondMax() { |
127 |
return secondMax;
|
128 |
} |
129 |
|
130 |
/*
|
131 |
* (non-Javadoc)
|
132 |
* @see org.gvsig.fmap.driver.IStatistics#getSecondMin()
|
133 |
*/
|
134 |
public double[] getSecondMin() { |
135 |
return secondMin;
|
136 |
} |
137 |
|
138 |
/*
|
139 |
* (non-Javadoc)
|
140 |
* @see org.gvsig.fmap.driver.IStatistics#getMaximun()
|
141 |
*/
|
142 |
public double getMaximun(){ |
143 |
double m = Double.MIN_VALUE; |
144 |
for(int i = 0; i < max.length; i++) |
145 |
m = Math.max(m, max[i]);
|
146 |
return m;
|
147 |
} |
148 |
|
149 |
/*
|
150 |
* (non-Javadoc)
|
151 |
* @see org.gvsig.fmap.driver.IStatistics#getMinimun()
|
152 |
*/
|
153 |
public double getMinimun(){ |
154 |
double m = Double.MAX_VALUE; |
155 |
for(int i = 0; i < min.length; i++) |
156 |
m = Math.min(m, min[i]);
|
157 |
return m;
|
158 |
} |
159 |
|
160 |
/*
|
161 |
* (non-Javadoc)
|
162 |
* @see org.gvsig.fmap.driver.IStatistics#getMean()
|
163 |
*/
|
164 |
public double[] getMean() { |
165 |
return mean;
|
166 |
} |
167 |
|
168 |
/*
|
169 |
* (non-Javadoc)
|
170 |
* @see org.gvsig.fmap.driver.IStatistics#getMin()
|
171 |
*/
|
172 |
public double[] getMin() { |
173 |
return min;
|
174 |
} |
175 |
|
176 |
/*
|
177 |
* (non-Javadoc)
|
178 |
* @see org.gvsig.fmap.driver.IStatistics#getVariance()
|
179 |
*/
|
180 |
public double[] getVariance() { |
181 |
return variance;
|
182 |
} |
183 |
|
184 |
/*
|
185 |
* (non-Javadoc)
|
186 |
* @see org.gvsig.fmap.driver.IStatistics#getBandCount()
|
187 |
*/
|
188 |
public int getBandCount(){ |
189 |
return grf.getBandCount();
|
190 |
} |
191 |
|
192 |
/*
|
193 |
* (non-Javadoc)
|
194 |
* @see org.gvsig.fmap.driver.IStatistics#calcFullStatistics()
|
195 |
*/
|
196 |
public void calcFullStatistics() throws FileNotOpenException, RasterDriverException{ |
197 |
long t2;
|
198 |
long t1 = new Date().getTime(); |
199 |
int type = grf.getDataType();
|
200 |
max = new double[grf.getBandCount()]; |
201 |
min = new double[grf.getBandCount()]; |
202 |
secondMax = new double[grf.getBandCount()]; |
203 |
secondMin = new double[grf.getBandCount()]; |
204 |
mean = new double[grf.getBandCount()]; |
205 |
variance = new double[grf.getBandCount()]; |
206 |
long[] iValues = new long[grf.getBandCount()]; |
207 |
boolean[] init = new boolean[grf.getBandCount()]; |
208 |
|
209 |
byte[][][] b = null; |
210 |
short[][][] s = null; |
211 |
int[][][] i = null; |
212 |
float[][][] f = null; |
213 |
double[][][] d = null; |
214 |
|
215 |
for (int iBand = 0; iBand < grf.getBandCount(); iBand ++) { |
216 |
max[iBand] = Double.MIN_VALUE;
|
217 |
min[iBand] = Double.MAX_VALUE;
|
218 |
secondMax[iBand] = Double.MIN_VALUE;
|
219 |
secondMin[iBand] = Double.MAX_VALUE;
|
220 |
init[iBand] = true;
|
221 |
} |
222 |
|
223 |
int h = RasterLibrary.blockHeight;
|
224 |
for (int block = 0; block < grf.getHeight(); block += h) { |
225 |
Object buf = null; |
226 |
try {
|
227 |
buf = grf.readBlock(block, RasterLibrary.blockHeight); |
228 |
} catch (InvalidSetViewException e) {
|
229 |
//La vista se asigna autom?ticamente
|
230 |
} |
231 |
switch(type){
|
232 |
case IBuffer.TYPE_BYTE: b = (byte[][][])buf;break; |
233 |
case IBuffer.TYPE_SHORT: s = (short[][][])buf;break; |
234 |
case IBuffer.TYPE_INT: i = (int[][][])buf;break; |
235 |
case IBuffer.TYPE_FLOAT: f = (float[][][])buf;break; |
236 |
case IBuffer.TYPE_DOUBLE: d = (double[][][])buf;break; |
237 |
} |
238 |
|
239 |
int hB = RasterLibrary.blockHeight;
|
240 |
if((block + hB) > grf.getHeight())
|
241 |
hB = Math.abs(grf.getHeight() - block);
|
242 |
for (int iBand = 0; iBand < grf.getBandCount(); iBand ++) { |
243 |
for (int row = 0; row < hB; row++) { |
244 |
for (int col = 0; col < grf.getWidth(); col ++) { |
245 |
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])); |
246 |
if (init[iBand]){
|
247 |
min[iBand] = z; |
248 |
secondMin[iBand] = z; |
249 |
max[iBand] = z; |
250 |
secondMax[iBand] = z; |
251 |
init[iBand] = false;
|
252 |
continue;
|
253 |
} |
254 |
if ( min[iBand] > z ){
|
255 |
secondMin[iBand] = min[iBand]; |
256 |
min[iBand] = z; |
257 |
} |
258 |
|
259 |
if ( secondMin[iBand] == min[iBand] && min[iBand] < z)
|
260 |
secondMin[iBand] = z; |
261 |
|
262 |
if ( max[iBand] < z ){
|
263 |
secondMax[iBand] = max[iBand]; |
264 |
max[iBand] = z; |
265 |
} |
266 |
|
267 |
if ( secondMax[iBand] == max[iBand] && max[iBand] > z)
|
268 |
secondMax[iBand] = z; |
269 |
|
270 |
mean[iBand] += z; |
271 |
variance[iBand] += z * z; |
272 |
iValues[iBand]++; |
273 |
} |
274 |
|
275 |
if (isCanceled())
|
276 |
return;
|
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
for (int iBand = 0; iBand < grf.getBandCount(); iBand ++) { |
282 |
if( iValues[iBand] > 0 ) { |
283 |
mean[iBand] /= (double) iValues[iBand];
|
284 |
variance[iBand] = variance[iBand] / (double) iValues[iBand] - mean[iBand] * mean[iBand];
|
285 |
} |
286 |
} |
287 |
|
288 |
calculated = true;
|
289 |
t2 = new Date().getTime(); |
290 |
System.out.println("Estadisticas " + grf.getFName() + ": " + ((t2 - t1) / 1000D) + ", secs."); |
291 |
} |
292 |
|
293 |
/**
|
294 |
* Carga estadisticas desde el fichero Rmf si las hay.
|
295 |
* @param fName Nombre del fichero
|
296 |
*/
|
297 |
public void loadFromRmf(){ |
298 |
if(grf != null){ |
299 |
File f = new File(grf.getFName()); |
300 |
if(!f.exists())
|
301 |
return;
|
302 |
//calculated = true;
|
303 |
//TODO: FUNCIONALIDAD: Carga de estad?sticas rmf
|
304 |
} |
305 |
} |
306 |
|
307 |
/**
|
308 |
* Salva estad?sticas a fichero rmf.
|
309 |
* @param fName
|
310 |
*/
|
311 |
public void saveToRmf(){ |
312 |
//TODO: FUNCIONALIDAD: Salvado de estad?sticas a rmf
|
313 |
} |
314 |
|
315 |
/*
|
316 |
* (non-Javadoc)
|
317 |
* @see org.gvsig.fmap.driver.IStatistics#isCalculated()
|
318 |
*/
|
319 |
public boolean isCalculated() { |
320 |
return calculated;
|
321 |
} |
322 |
|
323 |
/*
|
324 |
* (non-Javadoc)
|
325 |
* @see org.gvsig.fmap.driver.IStatistics#setTailTrimValue(double, java.lang.Object)
|
326 |
*/
|
327 |
public void setTailTrimValue(double percent, Object valueByBand){ |
328 |
String s = new Double(percent).toString(); |
329 |
tailTrim.put(s, valueByBand); |
330 |
} |
331 |
|
332 |
/*
|
333 |
* (non-Javadoc)
|
334 |
* @see org.gvsig.fmap.driver.IStatistics#getTailTrimValue(double)
|
335 |
*/
|
336 |
public Object getTailTrimValue(double percent){ |
337 |
String s = new Double(percent).toString(); |
338 |
return tailTrim.get(s);
|
339 |
} |
340 |
|
341 |
/*
|
342 |
* (non-Javadoc)
|
343 |
* @see org.gvsig.raster.util.ICancellable#isCanceled()
|
344 |
*/
|
345 |
public boolean isCanceled() { |
346 |
return canceled;
|
347 |
} |
348 |
|
349 |
/*
|
350 |
* (non-Javadoc)
|
351 |
* @see org.gvsig.raster.util.ICancellable#setCanceled(boolean)
|
352 |
*/
|
353 |
public void setCanceled(boolean value) { |
354 |
canceled = value; |
355 |
} |
356 |
} |