Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridTools / histogram / HistogramAlgorithm.java @ 59

History | View | Annotate | Download (4.03 KB)

1
package es.unex.sextante.gridTools.histogram;
2

    
3
import java.util.ArrayList;
4

    
5
import org.jfree.chart.ChartFactory;
6
import org.jfree.chart.ChartPanel;
7
import org.jfree.chart.JFreeChart;
8
import org.jfree.chart.plot.PlotOrientation;
9
import org.jfree.data.statistics.HistogramDataset;
10

    
11
import es.unex.sextante.core.GeoAlgorithm;
12
import es.unex.sextante.core.Sextante;
13
import es.unex.sextante.dataObjects.IRasterLayer;
14
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
15
import es.unex.sextante.exceptions.RepeatedParameterNameException;
16

    
17
public class HistogramAlgorithm
18
         extends
19
            GeoAlgorithm {
20

    
21
   private static final int   CLASS_COUNT = 100;
22

    
23
   public static final String GRID        = "GRID";
24
   public static final String GRAPH       = "GRAPH";
25

    
26

    
27
   @Override
28
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
29

    
30
      int x, y;
31
      int i;
32
      int A;
33
      int iNX, iNY;
34
      double z;
35
      double dMin = 0, dMax = 0;
36
      double Count[];
37

    
38
      Count = new double[CLASS_COUNT + 1];
39

    
40
      final IRasterLayer input = m_Parameters.getParameterValueAsRasterLayer(GRID);
41
      input.setFullExtent();
42

    
43
      iNX = input.getNX();
44
      iNY = input.getNY();
45

    
46
      A = 0;
47

    
48
      for (y = 0; (y < iNY) && setProgress(y, iNY); y++) {
49
         for (x = 0; x < iNX; x++) {
50
            z = input.getCellValueAsDouble(x, y);
51
            if (!input.isNoDataValue(z)) {
52
               if (A <= 0) {
53
                  dMin = dMax = z;
54
               }
55
               else {
56
                  if (dMin > z) {
57
                     dMin = z;
58
                  }
59
                  else if (dMax < z) {
60
                     dMax = z;
61
                  }
62
               }
63
               A++;
64
            }
65
         }
66
      }
67

    
68
      final double dInterval = (dMax - dMin) / CLASS_COUNT;
69
      if ((A > 0) && (dMin < dMax)) {
70
         for (y = 0; y < iNY; y++) {
71
            for (x = 0; x < iNX; x++) {
72
               z = input.getCellValueAsDouble(x, y);
73
               if (!input.isNoDataValue(z)) {
74
                  i = (int) ((z - dMin) / dInterval);
75
                  Count[Math.min(i, CLASS_COUNT)]++;
76
               }
77
            }
78
         }
79

    
80
         final ArrayList list = new ArrayList();
81
         for (i = 0; i < Count.length; i++) {
82
            final int iCount = (int) (10000 * Count[i] / A);
83
            for (int j = 0; j < iCount; j++) {
84
               list.add(new Double(dMin + dInterval * i));
85
            }
86
         }
87

    
88
         final double countForHistogram[] = new double[list.size()];
89
         for (int j = 0; j < list.size(); j++) {
90
            countForHistogram[j] = ((Double) list.get(j)).doubleValue();
91
         }
92

    
93
         final HistogramDataset dataset = new HistogramDataset();
94
         dataset.addSeries(input.getName(), countForHistogram, 100);
95

    
96
         final JFreeChart chart = ChartFactory.createHistogram("", null, null, dataset, PlotOrientation.VERTICAL, true, false,
97
                  false);
98

    
99
         final ChartPanel jPanelChart = new ChartPanel(chart);
100
         jPanelChart.setMouseZoomable(true, true);
101
         jPanelChart.setPreferredSize(new java.awt.Dimension(500, 300));
102
         jPanelChart.setPreferredSize(new java.awt.Dimension(500, 300));
103
         jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray, 1));
104

    
105
         addOutputChart(GRAPH, Sextante.getText("Histogram") + "[" + input.getName() + "]", jPanelChart);
106
      }
107

    
108
      return !m_Task.isCanceled();
109

    
110
   }
111

    
112

    
113
   @Override
114
   public void defineCharacteristics() {
115

    
116
      setName(Sextante.getText("Histogram"));
117
      setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
118
      setUserCanDefineAnalysisExtent(false);
119
      try {
120
         m_Parameters.addInputRasterLayer(GRID, Sextante.getText("Raster_layer"), true);
121
         addOutputChart(GRAPH, Sextante.getText("Histogram"), null);
122
      }
123
      catch (final RepeatedParameterNameException e) {
124
         Sextante.addErrorToLog(e);
125
      }
126

    
127
   }
128

    
129
}