Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / hydrology / createExclusionAreas / CreateExclusionAreasAlgorithm.java @ 59

History | View | Annotate | Download (4.37 KB)

1

    
2

    
3
package es.unex.sextante.hydrology.createExclusionAreas;
4

    
5
import java.awt.Point;
6
import java.awt.geom.Point2D;
7
import java.util.ArrayList;
8

    
9
import es.unex.sextante.core.GeoAlgorithm;
10
import es.unex.sextante.core.Sextante;
11
import es.unex.sextante.dataObjects.IRasterLayer;
12
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
13
import es.unex.sextante.exceptions.RepeatedParameterNameException;
14
import es.unex.sextante.rasterWrappers.GridCell;
15

    
16

    
17
public class CreateExclusionAreasAlgorithm
18
         extends
19
            GeoAlgorithm {
20

    
21
   private final static int   m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
22
   private final static int   m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
23

    
24
   public static final String RESULT       = "RESULT";
25
   public static final String OUTLET_POINT = "OUTLET_POINT";
26
   public static final String INPUT        = "INPUT";
27

    
28
   int                        m_iNX, m_iNY;
29
   IRasterLayer               m_Grid;
30
   boolean                    m_IsCellAlreadyVisited[][];
31
   private IRasterLayer       m_ExclusionAreas;
32

    
33

    
34
   @Override
35
   public void defineCharacteristics() {
36

    
37
      setName(Sextante.getText("CreateExclusionAreas"));
38
      setGroup(Sextante.getText("Basic_hydrological_analysis"));
39
      setUserCanDefineAnalysisExtent(false);
40

    
41
      try {
42
         m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true);
43
         m_Parameters.addPoint(OUTLET_POINT, Sextante.getText("Outlet_point"));
44
         addOutputRasterLayer(RESULT, Sextante.getText("Result"), 0);
45
      }
46
      catch (final RepeatedParameterNameException e) {
47
         Sextante.addErrorToLog(e);
48
      }
49

    
50
   }
51

    
52

    
53
   @Override
54
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
55

    
56
      m_Grid = m_Parameters.getParameterValueAsRasterLayer(INPUT);
57

    
58
      m_Grid.setFullExtent();
59
      m_Grid.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
60

    
61
      m_iNX = m_Grid.getNX();
62
      m_iNY = m_Grid.getNY();
63

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

    
66
      m_ExclusionAreas = getNewRasterLayer(RESULT, Sextante.getText("ExclusionAreas"), IRasterLayer.RASTER_DATA_TYPE_INT,
67
               m_Grid.getLayerGridExtent());
68

    
69
      m_ExclusionAreas.assign(m_Grid);
70
      m_ExclusionAreas.setNoDataValue(0.0);
71

    
72
      final Point2D pt = m_Parameters.getParameterValueAsPoint(OUTLET_POINT);
73
      final GridCell cell = m_Grid.getLayerGridExtent().getGridCoordsFromWorldCoords(pt);
74

    
75
      exclude(cell.getX(), cell.getY());
76

    
77
      m_ExclusionAreas.setNoData(cell.getX(), cell.getY());
78

    
79
      return !m_Task.isCanceled();
80

    
81
   }
82

    
83

    
84
   private void exclude(int x,
85
                        int y) {
86

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

    
97
      iInitClass = m_Grid.getCellValueAsInt(x, y);
98

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

    
102
      while ((centralPoints.size() != 0) && !m_Task.isCanceled()) {
103
         for (iPt = 0; iPt < centralPoints.size(); iPt++) {
104
            dArea += m_Grid.getWindowCellSize() * m_Grid.getWindowCellSize();
105
            point = (Point) centralPoints.get(iPt);
106
            x = point.x;
107
            y = point.y;
108
            double dClass = m_Grid.getCellValueAsInt(x, y);
109
            if (!m_Grid.isNoDataValue(dClass)) {
110
               for (n = 0; n < 8; n++) {
111
                  x2 = x + m_iOffsetX[n];
112
                  y2 = y + m_iOffsetY[n];
113
                  dClass = m_Grid.getCellValueAsDouble(x2, y2);
114
                  if (!m_Grid.isNoDataValue(dClass)) {
115
                     iClass = (int) dClass;
116
                     if (m_IsCellAlreadyVisited[x2][y2] == false) {
117
                        if (iInitClass == iClass) {
118
                           m_IsCellAlreadyVisited[x2][y2] = true;
119
                           adjPoints.add(new Point(x2, y2));
120
                           m_ExclusionAreas.setNoData(x2, y2);
121
                        }
122
                     }
123
                  }
124
               }
125
            }
126
         }
127

    
128
         centralPoints = adjPoints;
129
         System.out.println(centralPoints.size());
130
         adjPoints = new ArrayList();
131

    
132
      }
133

    
134
      m_ExclusionAreas.setNoData(x, y);
135

    
136
   }
137

    
138
}