svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / statistics / HistogramOperation.java @ 44831
History | View | Annotate | Download (2.57 KB)
1 | 44831 | jjdelcerro | package org.gvsig.raster.lib.buffer.impl.statistics; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | import java.util.List; |
||
5 | |||
6 | import org.gvsig.raster.lib.buffer.api.Band; |
||
7 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
8 | import org.gvsig.raster.lib.buffer.api.statistics.HistogramBand; |
||
9 | import org.gvsig.raster.lib.buffer.api.statistics.HistogramClass; |
||
10 | import org.gvsig.raster.lib.buffer.api.statistics.StatisticsBand; |
||
11 | |||
12 | /**
|
||
13 | * @author fdiaz
|
||
14 | *
|
||
15 | */
|
||
16 | public class HistogramOperation implements StatisticalOperation { |
||
17 | |||
18 | private StatisticsBand statisticsBand;
|
||
19 | private List<HistogramClass> histogramClasses; |
||
20 | private HistogramBand histogramBand;
|
||
21 | private Band band;
|
||
22 | |||
23 | /**
|
||
24 | * @param statisticsBand
|
||
25 | * @param band
|
||
26 | */
|
||
27 | public HistogramOperation(StatisticsBand statisticsBand, Band band) {
|
||
28 | this.statisticsBand = statisticsBand;
|
||
29 | this.band = band;
|
||
30 | } |
||
31 | |||
32 | @Override
|
||
33 | public void pre() { |
||
34 | int dataType = this.statisticsBand.getDataType(); |
||
35 | if(dataType == BufferManager.TYPE_DOUBLE || dataType == BufferManager.TYPE_FLOAT){
|
||
36 | calculateHistogramClasses(); |
||
37 | histogramBand = new DefaultHistogramBand(band, this.histogramClasses); |
||
38 | |||
39 | } else {
|
||
40 | histogramBand = new DefaultHistogramBand(band);
|
||
41 | } |
||
42 | } |
||
43 | |||
44 | private void calculateHistogramClasses() { |
||
45 | int classesNumber = new Double(Math.ceil(Math.sqrt(this.statisticsBand.getBandLength()-this.statisticsBand.getNoDataValuesCount()))).intValue(); |
||
46 | histogramClasses = new ArrayList<HistogramClass>(classesNumber); |
||
47 | |||
48 | double range = this.statisticsBand.getMaximum()-this.statisticsBand.getMinimum(); |
||
49 | double widthInterval = range/classesNumber;
|
||
50 | double minInterval = this.statisticsBand.getMinimum(); |
||
51 | double maxInterval; // = this.statisticsBand.getMaximum(); |
||
52 | for(int i=0; i<classesNumber-1; i++){ |
||
53 | maxInterval = minInterval+widthInterval; |
||
54 | DefaultHistogramClass histogramClass = new DefaultHistogramClass(minInterval, maxInterval);
|
||
55 | histogramClass.setValue(0);
|
||
56 | histogramClasses.add(histogramClass); |
||
57 | minInterval = maxInterval; |
||
58 | } |
||
59 | DefaultHistogramClass histogramClass = new DefaultHistogramClass(minInterval, this.statisticsBand.getMaximum()); |
||
60 | histogramClasses.add(histogramClass); |
||
61 | |||
62 | } |
||
63 | |||
64 | @Override
|
||
65 | public void addValue(Number value) { |
||
66 | ((DefaultHistogramBand)histogramBand).addValue(value.doubleValue()); |
||
67 | } |
||
68 | |||
69 | @Override
|
||
70 | public void post() { |
||
71 | // do nothing
|
||
72 | } |
||
73 | |||
74 | @Override
|
||
75 | public Object getResult() { |
||
76 | return histogramBand;
|
||
77 | } |
||
78 | |||
79 | } |