Statistics
| Revision:

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

History | View | Annotate | Download (6.53 KB)

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

    
3
import java.awt.Point;
4
import java.util.ArrayList;
5

    
6
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
7
import es.unex.sextante.core.GeoAlgorithm;
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

    
13
public class ThresholdBufferAlgorithm
14
         extends
15
            GeoAlgorithm {
16

    
17
   private static final int   BUFFER                                = 1;
18
   private static final int   FEATURE                               = 2;
19
   private final static int   m_iOffsetX[]                          = { 0, 1, 1, 1, 0, -1, -1, -1 };
20
   private final static int   m_iOffsetY[]                          = { 1, 1, 0, -1, -1, -1, 0, 1 };
21

    
22
   public static final String FEATURES                              = "FEATURES";
23
   public static final String VALUES                                = "VALUES";
24
   public static final String THRESHOLD                             = "THRESHOLD";
25
   public static final String METHOD                                = "METHOD";
26
   public static final String GLOBAL_THRESHOLD                      = "GLOBAL_THRESHOLD";
27
   public static final String BUFFER_LAYER                          = "BUFFER_LAYER";
28

    
29
   public static final int    THRESHOLD_TYPE_ABSOLUTE               = 0;
30
   public static final int    THRESHOLD_TYPE_RELATIVE_TO_CELL_VALUE = 0;
31

    
32
   int                        m_iNX, m_iNY;
33
   int                        m_iThresholdType;
34
   double                     m_dThreshold;
35
   boolean                    m_bThresholdGridDefined;
36
   IRasterLayer               m_Features, m_Threshold, m_Values;
37
   IRasterLayer               m_Buffer;
38

    
39

    
40
   @Override
41
   public void defineCharacteristics() {
42

    
43
      final String[] sMethod = { Sextante.getText("Absolute"), Sextante.getText("Relative_to_cell_value") };
44

    
45
      setName(Sextante.getText("Threshold_buffer"));
46
      setGroup(Sextante.getText("Buffers"));
47
      setUserCanDefineAnalysisExtent(true);
48

    
49
      try {
50
         m_Parameters.addInputRasterLayer(FEATURES, Sextante.getText("Layer"), true);
51
         m_Parameters.addInputRasterLayer(VALUES, Sextante.getText("Threshold_parameter"), true);
52
         m_Parameters.addInputRasterLayer(THRESHOLD, Sextante.getText("Threshold_values"), false);
53
         m_Parameters.addSelection(METHOD, Sextante.getText("Threshold"), sMethod);
54
         m_Parameters.addNumericalValue(GLOBAL_THRESHOLD, Sextante.getText("global_threshold"),
55
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 100, Double.NEGATIVE_INFINITY, Double.MAX_VALUE);
56
         addOutputRasterLayer(BUFFER_LAYER, Sextante.getText("Buffer"));
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 dValue;
70

    
71
      m_iThresholdType = m_Parameters.getParameterValueAsInt(METHOD);
72
      m_dThreshold = m_Parameters.getParameterValueAsDouble(GLOBAL_THRESHOLD);
73
      m_Features = m_Parameters.getParameterValueAsRasterLayer(FEATURES);
74
      m_Values = m_Parameters.getParameterValueAsRasterLayer(VALUES);
75
      m_Threshold = m_Parameters.getParameterValueAsRasterLayer(THRESHOLD);
76

    
77
      m_Buffer = getNewRasterLayer(BUFFER_LAYER, Sextante.getText("Buffer") + "[" + m_Features.getName() + "]",
78
               IRasterLayer.RASTER_DATA_TYPE_BYTE);
79

    
80
      m_Features.setWindowExtent(m_Buffer.getWindowGridExtent());
81
      m_Features.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
82
      m_Values.setWindowExtent(m_Buffer.getWindowGridExtent());
83
      if (m_Threshold != null) {
84
         m_Threshold.setWindowExtent(m_Buffer.getWindowGridExtent());
85
         m_bThresholdGridDefined = true;
86
      }
87
      else {
88
         m_bThresholdGridDefined = false;
89
      }
90

    
91
      m_Buffer.assign(0.0);
92

    
93
      m_iNX = m_Features.getNX();
94
      m_iNY = m_Features.getNY();
95

    
96
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
97
         for (x = 0; x < m_iNX; x++) {
98
            dValue = m_Features.getCellValueAsDouble(x, y);
99
            if ((dValue != 0) && !m_Features.isNoDataValue(dValue)) {
100
               bufferPoint(x, y);
101
            }
102
         }
103
      }
104

    
105
      return !m_Task.isCanceled();
106

    
107
   }
108

    
109

    
110
   private void bufferPoint(int x,
111
                            int y) {
112

    
113
      int x2, y2;
114
      int iValue;
115
      int iPt;
116
      int n;
117
      double dBaseValue;
118
      double dThreshold;
119
      double dValue;
120
      final ArrayList centralPoints = new ArrayList();
121
      final ArrayList adjPoints = new ArrayList();
122
      Point point;
123

    
124
      dBaseValue = m_Values.getCellValueAsDouble(x, y);
125

    
126
      if (m_bThresholdGridDefined) {
127
         dThreshold = m_Threshold.getCellValueAsDouble(x, y);
128
      }
129
      else {
130
         dThreshold = m_dThreshold;
131
      }
132

    
133
      centralPoints.add(new Point(x, y));
134
      m_Buffer.setCellValue(x, y, FEATURE);
135

    
136
      while (centralPoints.size() != 0) {
137
         for (iPt = 0; iPt < centralPoints.size(); iPt++) {
138
            point = (Point) centralPoints.get(iPt);
139
            x = point.x;
140
            y = point.y;
141
            dValue = m_Values.getCellValueAsDouble(x, y);
142
            if (!m_Values.isNoDataValue(dValue)) {
143
               for (n = 0; n < 8; n++) {
144
                  x2 = x + m_iOffsetX[n];
145
                  y2 = y + m_iOffsetY[n];
146
                  dValue = m_Values.getCellValueAsDouble(x2, y2);
147
                  if (!m_Values.isNoDataValue(dValue)) {
148
                     iValue = m_Buffer.getCellValueAsInt(x2, y2);
149
                     if (iValue == 0) {
150
                        if (m_iThresholdType == ThresholdBufferAlgorithm.THRESHOLD_TYPE_RELATIVE_TO_CELL_VALUE) {
151
                           dValue = Math.abs(dValue - dBaseValue);
152
                        }
153
                        if (dValue < dThreshold) {
154
                           m_Buffer.setCellValue(x2, y2, BUFFER);
155
                           adjPoints.add(new Point(x2, y2));
156
                        }
157
                     }
158
                  }
159
               }
160
            }
161
         }
162

    
163
         centralPoints.clear();
164
         for (iPt = 0; iPt < adjPoints.size(); iPt++) {
165
            point = (Point) adjPoints.get(iPt);
166
            centralPoints.add(new Point(point.x, point.y));
167
         }
168
         adjPoints.clear();
169

    
170
      }
171

    
172
   }
173

    
174
}