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