Statistics
| Revision:

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

History | View | Annotate | Download (4.14 KB)

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

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

    
6
import es.unex.sextante.core.GeoAlgorithm;
7
import es.unex.sextante.core.Sextante;
8
import es.unex.sextante.dataObjects.IRasterLayer;
9
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
10
import es.unex.sextante.exceptions.RepeatedParameterNameException;
11

    
12
public class ReclassifyDisjointAlgorithm
13
         extends
14
            GeoAlgorithm {
15

    
16
   private final static int   m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
17
   private final static int   m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
18

    
19
   public static final String INPUT        = "INPUT";
20
   public static final String RECLASS      = "RECLASS";
21

    
22
   int                        m_iNX, m_iNY;
23
   IRasterLayer               m_Window;
24
   IRasterLayer               m_Result;
25
   boolean                    m_IsCellAlreadyVisited[][];
26

    
27

    
28
   @Override
29
   public void defineCharacteristics() {
30

    
31
      setName(Sextante.getText("Reclassify_into_disjoint_classes"));
32
      setGroup(Sextante.getText("Reclassify_raster_layers"));
33
      setUserCanDefineAnalysisExtent(true);
34

    
35
      try {
36
         m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true);
37
         addOutputRasterLayer(RECLASS, Sextante.getText("Reclassify"));
38
      }
39
      catch (final RepeatedParameterNameException e) {
40
         Sextante.addErrorToLog(e);
41
      }
42

    
43
   }
44

    
45

    
46
   @Override
47
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
48

    
49
      int x, y;
50
      int iClass = 0;
51

    
52
      m_Window = m_Parameters.getParameterValueAsRasterLayer(INPUT);
53

    
54
      m_Result = getNewRasterLayer(RECLASS, m_Window.getName() + Sextante.getText("[reclassified]"),
55
               IRasterLayer.RASTER_DATA_TYPE_INT);
56

    
57
      m_Result.assignNoData();
58

    
59
      m_Window.setWindowExtent(m_Result);
60
      m_Window.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
61

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

    
65
      m_IsCellAlreadyVisited = new boolean[m_iNX][m_iNY];
66

    
67
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
68
         for (x = 0; x < m_iNX; x++) {
69
            final double dValue = m_Window.getCellValueAsDouble(x, y);
70
            if (!m_Window.isNoDataValue(dValue)) {
71
               if (!m_IsCellAlreadyVisited[x][y]) {
72
                  iClass++;
73
                  setClass(x, y, iClass);
74
               }
75
            }
76
         }
77
      }
78

    
79
      return !m_Task.isCanceled();
80

    
81

    
82
   }
83

    
84

    
85
   private void setClass(int x,
86
                         int y,
87
                         final int iNewClass) {
88

    
89
      int x2, y2;
90
      int iInitClass;
91
      int iPt;
92
      int n;
93
      int iClass;
94
      ArrayList centralPoints = new ArrayList();
95
      ArrayList adjPoints = new ArrayList();
96
      Point point;
97

    
98
      iInitClass = m_Window.getCellValueAsInt(x, y);
99

    
100
      centralPoints.add(new Point(x, y));
101
      m_IsCellAlreadyVisited[x][y] = true;
102

    
103
      while (centralPoints.size() != 0) {
104
         for (iPt = 0; iPt < centralPoints.size(); iPt++) {
105
            point = (Point) centralPoints.get(iPt);
106
            x = point.x;
107
            y = point.y;
108
            iClass = m_Window.getCellValueAsInt(x, y);
109
            if (!m_Window.isNoDataValue(iClass)) {
110
               for (n = 0; n < 8; n++) {
111
                  x2 = x + m_iOffsetX[n];
112
                  y2 = y + m_iOffsetY[n];
113
                  iClass = m_Window.getCellValueAsInt(x2, y2);
114
                  if (!m_Window.isNoDataValue(iClass)) {
115
                     if (m_IsCellAlreadyVisited[x2][y2] == false) {
116
                        if (iInitClass == iClass) {
117
                           m_IsCellAlreadyVisited[x2][y2] = true;
118
                           m_Result.setCellValue(x2, y2, iNewClass);
119
                           adjPoints.add(new Point(x2, y2));
120

    
121
                        }
122
                     }
123
                  }
124
               }
125
            }
126
         }
127

    
128
         centralPoints = adjPoints;
129
         adjPoints = new ArrayList();
130

    
131
         if (m_Task.isCanceled()) {
132
            return;
133
         }
134

    
135
      }
136

    
137
   }
138

    
139
}