Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridAnalysis / fuzzify / FuzzifyAlgorithm.java @ 59

History | View | Annotate | Download (5.26 KB)

1
package es.unex.sextante.gridAnalysis.fuzzify;
2

    
3
import java.util.Arrays;
4

    
5
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
6
import es.unex.sextante.core.GeoAlgorithm;
7
import es.unex.sextante.core.AnalysisExtent;
8
import es.unex.sextante.core.Sextante;
9
import es.unex.sextante.dataObjects.IRasterLayer;
10
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
11
import es.unex.sextante.exceptions.RepeatedParameterNameException;
12
import es.unex.sextante.rasterWrappers.GridWrapper;
13

    
14
public class FuzzifyAlgorithm
15
         extends
16
            GeoAlgorithm {
17

    
18
   public static final String INPUT                     = "INPUT";
19
   public static final String FUNCTIONTYPE              = "FUNCTIONTYPE";
20
   public static final String RESULT                    = "RESULT";
21
   public static final String A                         = "A";
22
   public static final String B                         = "B";
23
   public static final String C                         = "C";
24
   public static final String D                         = "D";
25

    
26
   public static final int    MEMBER_FUNCTION_LINEAL    = 0;
27
   public static final int    MEMBER_FUNCTION_SIGMOIDAL = 1;
28
   public static final int    MEMBER_FUNCTION_J_SHAPED  = 2;
29

    
30

    
31
   int                        m_iNX, m_iNY;
32
   IRasterLayer               m_Grid;
33
   IRasterLayer               m_Result;
34

    
35

    
36
   @Override
37
   public void defineCharacteristics() {
38

    
39
      final String[] sOptions = { Sextante.getText("Linear"), Sextante.getText("Sigmoidal"), Sextante.getText("J-shaped") };
40

    
41
      setName(Sextante.getText("Fuzzify"));
42
      setGroup(Sextante.getText("Fuzzy_logic"));
43
      setUserCanDefineAnalysisExtent(true);
44

    
45
      try {
46
         m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Input_Layer"), true);
47
         m_Parameters.addSelection(FUNCTIONTYPE, Sextante.getText("Member_function"), sOptions);
48
         m_Parameters.addNumericalValue(A, Sextante.getText("Control_point_A"), 10,
49
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
50
         m_Parameters.addNumericalValue(B, Sextante.getText("Control_point_B"), 10,
51
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
52
         m_Parameters.addNumericalValue(C, Sextante.getText("Control_point_C"), 10,
53
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
54
         m_Parameters.addNumericalValue(D, Sextante.getText("Control_point_D"), 10,
55
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
56
         addOutputRasterLayer(RESULT, Sextante.getText("Result"));
57
      }
58
      catch (final RepeatedParameterNameException e) {
59
         Sextante.addErrorToLog(e);
60
      }
61

    
62
   }
63

    
64

    
65
   @Override
66
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
67

    
68
      int x, y;
69
      double dX;
70
      double dW;
71
      double dValue;
72

    
73
      m_Grid = m_Parameters.getParameterValueAsRasterLayer(INPUT);
74
      final int iType = m_Parameters.getParameterValueAsInt(FUNCTIONTYPE);
75
      double dA = m_Parameters.getParameterValueAsDouble(A);
76
      double dB = m_Parameters.getParameterValueAsDouble(B);
77
      double dC = m_Parameters.getParameterValueAsDouble(C);
78
      double dD = m_Parameters.getParameterValueAsDouble(D);
79

    
80
      final double dPts[] = new double[4];
81
      dPts[0] = dA;
82
      dPts[1] = dB;
83
      dPts[2] = dC;
84
      dPts[3] = dD;
85
      Arrays.sort(dPts);
86
      dA = dPts[0];
87
      dB = dPts[1];
88
      dC = dPts[2];
89
      dD = dPts[3];
90

    
91
      m_Result = getNewRasterLayer(RESULT, m_Grid.getName() + "_" + Sextante.getText("[fuzzy]"),
92
               IRasterLayer.RASTER_DATA_TYPE_DOUBLE);
93

    
94
      final AnalysisExtent extent = m_Result.getWindowGridExtent();
95

    
96
      m_Grid.setWindowExtent(extent);
97
      m_Grid.setInterpolationMethod(GridWrapper.INTERPOLATION_BSpline);
98

    
99
      final int iNX = m_Grid.getNX();
100
      final int iNY = m_Grid.getNY();
101

    
102
      for (y = 0; (y < iNY) & setProgress(y, iNY); y++) {
103
         for (x = 0; x < iNX; x++) {
104
            dValue = m_Grid.getCellValueAsDouble(x, y);
105
            if (!m_Grid.isNoDataValue(dValue)) {
106
               if ((dValue <= dA) || (dValue >= dD)) {
107
                  m_Result.setCellValue(x, y, 0);
108
               }
109
               else if ((dValue >= dB) && (dValue <= dC)) {
110
                  m_Result.setCellValue(x, y, 1);
111
               }
112
               else {
113
                  if (dValue < dB) {
114
                     dX = dValue - dA;
115
                     dW = dB - dA;
116
                  }
117
                  else {
118
                     dX = dD - dValue;
119
                     dW = dD - dC;
120
                  }
121
                  switch (iType) {
122
                     case 0:
123
                        m_Result.setCellValue(x, y, dX / dW);
124
                        break;
125
                     case 1:
126
                        m_Result.setCellValue(x, y, Math.pow(Math.sin(dX / dW * Math.PI / 2.), 2.));
127
                        break;
128
                     case 2:
129
                        m_Result.setCellValue(x, y, 1. / (1 + Math.pow((dW - dX) / dW, 2.)));
130
                        break;
131
                  }
132
               }
133
            }
134
            else {
135
               m_Result.setNoData(x, y);
136
            }
137
         }
138
      }
139

    
140
      return !m_Task.isCanceled();
141

    
142
   }
143

    
144
}