Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridCategorical / lacunarity / LacunarityAlgorithm.java @ 59

History | View | Annotate | Download (5.11 KB)

1
package es.unex.sextante.gridCategorical.lacunarity;
2

    
3
import org.jfree.chart.ChartFactory;
4
import org.jfree.chart.ChartPanel;
5
import org.jfree.chart.JFreeChart;
6
import org.jfree.chart.plot.PlotOrientation;
7
import org.jfree.data.xy.XYSeries;
8
import org.jfree.data.xy.XYSeriesCollection;
9

    
10
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
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.dataObjects.ITable;
15
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
16
import es.unex.sextante.exceptions.RepeatedParameterNameException;
17
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
18
import es.unex.sextante.math.simpleStats.SimpleStats;
19

    
20
public class LacunarityAlgorithm
21
         extends
22
            GeoAlgorithm {
23

    
24
   public static final String RESULT_CHART = "RESULT_CHART";
25
   public static final String RESULT_TABLE = "RESULT_TABLE";
26
   public static final String INPUT        = "INPUT";
27
   public static final String MAX_SIZE     = "MAX_SIZE";
28

    
29
   private int                m_iNX, m_iNY;
30
   private IRasterLayer       m_Window;
31
   private int                m_iMaxSize;
32
   private double[]           m_dLacunarity;
33

    
34

    
35
   @Override
36
   public void defineCharacteristics() {
37

    
38
      this.setName(Sextante.getText("Lacunarity"));
39
      setGroup(Sextante.getText("Raster_categories_analysis"));
40
      setUserCanDefineAnalysisExtent(false);
41

    
42
      try {
43
         m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Input_Layer"), true);
44
         m_Parameters.addNumericalValue(MAX_SIZE, Sextante.getText("Largest_window_size"),
45
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 10, 4, Integer.MAX_VALUE);
46
         addOutputTable(RESULT_TABLE, Sextante.getText("Lacunarity"));
47
         addOutputChart(RESULT_CHART, Sextante.getText("Lacunarity"));
48
      }
49
      catch (final RepeatedParameterNameException e) {
50
         Sextante.addErrorToLog(e);
51
      }
52

    
53
   }
54

    
55

    
56
   @Override
57
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
58

    
59
      m_Window = m_Parameters.getParameterValueAsRasterLayer(INPUT);
60
      m_iMaxSize = m_Parameters.getParameterValueAsInt(MAX_SIZE);
61
      m_Window.setFullExtent();
62

    
63
      m_iNX = m_Window.getNX();
64
      m_iNY = m_Window.getNY();
65

    
66
      if (calculateLacunarity()) {
67
         createTableAndGraph();
68
         return true;
69
      }
70
      else {
71
         return false;
72
      }
73

    
74
   }
75

    
76

    
77
   private boolean calculateLacunarity() {
78

    
79
      int x, y;
80

    
81
      m_dLacunarity = new double[m_iMaxSize + 1];
82

    
83
      for (int iSize = 2; iSize < m_dLacunarity.length; iSize++) {
84
         setProgressText("Box size: " + Integer.toString(iSize));
85
         final SimpleStats stats = new SimpleStats();
86
         for (y = 0; (y < m_iNY - iSize) && setProgress(y, m_iNY); y++) {
87
            for (x = 0; x < m_iNX - iSize; x++) {
88
               final int iBoxMass = getBoxMass(x, y, iSize);
89
               stats.addValue(iBoxMass);
90
            }
91
         }
92
         m_dLacunarity[iSize] = 1 + (stats.getVariance() / Math.pow(stats.getMean(), 2.));
93
      }
94

    
95
      return !m_Task.isCanceled();
96

    
97
   }
98

    
99

    
100
   private int getBoxMass(final int x,
101
                          final int y,
102
                          final int iSize) {
103

    
104
      int iBoxMass = 0;
105

    
106
      for (int i = 0; i < iSize; i++) {
107
         for (int j = 0; j < iSize; j++) {
108
            final double dValue = m_Window.getCellValueAsDouble(x + i, y + j);
109
            if (!m_Window.isNoDataValue(dValue)) {
110
               iBoxMass++;
111
            }
112
         }
113
      }
114

    
115
      return iBoxMass;
116

    
117
   }
118

    
119

    
120
   private void createTableAndGraph() throws UnsupportedOutputChannelException {
121

    
122
      Object[] values;
123

    
124

    
125
      final String sFields[] = { Sextante.getText("Window_size"), Sextante.getText("Lacunaridad") };
126
      final Class types[] = { Integer.class, Double.class };
127
      final String sTableName = Sextante.getText("Lacunaridad") + "[" + m_Window.getName() + "]";
128

    
129
      final ITable table = getNewTable(RESULT_TABLE, sTableName, types, sFields);
130
      values = new Object[2];
131

    
132
      final XYSeriesCollection dataset = new XYSeriesCollection();
133
      final XYSeries serie = new XYSeries(Sextante.getText("Profile"));
134
      dataset.addSeries(serie);
135

    
136
      for (int i = 2; i < m_dLacunarity.length; i++) {
137
         values[0] = new Integer(i);
138
         values[1] = new Double(m_dLacunarity[i]);
139
         table.addRecord(values);
140
         serie.add(i, m_dLacunarity[i]);
141
      }
142

    
143
      final JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.VERTICAL, false, true,
144
               true);
145

    
146
      final ChartPanel jPanelChart = new ChartPanel(chart);
147
      jPanelChart.setPreferredSize(new java.awt.Dimension(500, 300));
148
      jPanelChart.setPreferredSize(new java.awt.Dimension(500, 300));
149
      jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray, 1));
150

    
151
      addOutputChart(RESULT_CHART, Sextante.getText("Profile"), jPanelChart);
152

    
153
   }
154

    
155
}