Revision 11207 trunk/libraries/libRaster/src/org/gvsig/raster/dataset/properties/DatasetStatistics.java
DatasetStatistics.java | ||
---|---|---|
22 | 22 |
import java.util.Date; |
23 | 23 |
import java.util.Hashtable; |
24 | 24 |
|
25 |
import org.gvsig.raster.RasterLibrary; |
|
25 | 26 |
import org.gvsig.raster.dataset.FileNotOpenException; |
26 | 27 |
import org.gvsig.raster.dataset.IBuffer; |
27 | 28 |
import org.gvsig.raster.dataset.IStatistics; |
... | ... | |
202 | 203 |
secondMin = new double[grf.getBandCount()]; |
203 | 204 |
mean = new double[grf.getBandCount()]; |
204 | 205 |
variance = new double[grf.getBandCount()]; |
205 |
int iValues = 0; |
|
206 |
long[] iValues = new long[grf.getBandCount()]; |
|
207 |
boolean[] init = new boolean[grf.getBandCount()]; |
|
206 | 208 |
|
207 |
byte[] b = null;short[] s = null;int[] i = null;float[] f = null;double[] d = null; |
|
209 |
byte[][][] b = null; |
|
210 |
short[][][] s = null; |
|
211 |
int[][][] i = null; |
|
212 |
float[][][] f = null; |
|
213 |
double[][][] d = null; |
|
208 | 214 |
|
209 |
for (int iBand = 0; iBand < grf.getBandCount(); iBand ++){ |
|
210 |
iValues = 0; |
|
215 |
for (int iBand = 0; iBand < grf.getBandCount(); iBand ++) { |
|
211 | 216 |
max[iBand] = Double.MIN_VALUE; |
212 | 217 |
min[iBand] = Double.MAX_VALUE; |
213 | 218 |
secondMax[iBand] = Double.MIN_VALUE; |
214 | 219 |
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; |
|
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]++; |
|
239 | 273 |
} |
240 |
if ( min[iBand] > z ){ |
|
241 |
secondMin[iBand] = min[iBand]; |
|
242 |
min[iBand] = z; |
|
243 |
} |
|
244 | 274 |
|
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++; |
|
275 |
if (isCanceled()) |
|
276 |
return; |
|
259 | 277 |
} |
260 |
|
|
261 |
if (isCanceled()) |
|
262 |
return; |
|
263 | 278 |
} |
264 |
if( iValues > 0 ){ |
|
265 |
mean[iBand] /= (double) iValues; |
|
266 |
variance[iBand] = variance[iBand] / (double) iValues - mean[iBand] * mean[iBand]; |
|
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]; |
|
267 | 285 |
} |
268 | 286 |
} |
269 | 287 |
|
270 |
|
|
271 | 288 |
calculated = true; |
272 | 289 |
t2 = new Date().getTime(); |
273 | 290 |
System.out.println("Estadisticas " + grf.getFName() + ": " + ((t2 - t1) / 1000D) + ", secs."); |
Also available in: Unified diff