Statistics
| Revision:

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

History | View | Annotate | Download (3.42 KB)

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

    
3
import es.unex.sextante.core.GeoAlgorithm;
4
import es.unex.sextante.core.Sextante;
5
import es.unex.sextante.dataObjects.IRasterLayer;
6
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
7
import es.unex.sextante.exceptions.RepeatedParameterNameException;
8
import es.unex.sextante.parameters.FixedTableModel;
9

    
10
public class CombineGridsAlgorithm
11
         extends
12
            GeoAlgorithm {
13

    
14
   public static final String GRID   = "GRID";
15
   public static final String GRID2  = "GRID2";
16
   public static final String RESULT = "RESULT";
17
   public static final String LUT    = "LUT";
18

    
19

    
20
   @Override
21
   public void defineCharacteristics() {
22

    
23
      final String[] sColumnNames = { Sextante.getText("Valor_in_grid_1"), Sextante.getText("Valor_in_grid_2"),
24
               Sextante.getText("New_value") };
25

    
26
      setName(Sextante.getText("Combine_grids"));
27
      setGroup(Sextante.getText("Raster_categories_analysis"));
28
      setUserCanDefineAnalysisExtent(true);
29

    
30
      try {
31
         m_Parameters.addInputRasterLayer(GRID, Sextante.getText("Grid_1"), true);
32
         m_Parameters.addInputRasterLayer(GRID2, Sextante.getText("Grid_2"), true);
33
         m_Parameters.addFixedTable(LUT, Sextante.getText("Look-up_table"), sColumnNames, 1, false);
34
         addOutputRasterLayer(RESULT, Sextante.getText("Grid_combination"));
35
      }
36
      catch (final RepeatedParameterNameException e) {
37
         Sextante.addErrorToLog(e);
38
      }
39

    
40
   }
41

    
42

    
43
   @Override
44
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
45

    
46
      int i;
47
      int x, y;
48
      int iNX, iNY;
49
      double dCellValue, dCellValue2;
50
      double dTableValue, dTableValue2;
51
      double dValue;
52

    
53
      final IRasterLayer grid = m_Parameters.getParameterValueAsRasterLayer(GRID);
54
      final IRasterLayer grid2 = m_Parameters.getParameterValueAsRasterLayer(GRID2);
55

    
56
      final IRasterLayer result = getNewRasterLayer(RESULT, grid.getName() + " + " + grid2.getName(),
57
               IRasterLayer.RASTER_DATA_TYPE_DOUBLE);
58

    
59
      final FixedTableModel lut = (FixedTableModel) m_Parameters.getParameterValueAsObject(LUT);
60

    
61
      grid.setWindowExtent(result.getWindowGridExtent());
62
      grid.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
63
      grid2.setWindowExtent(result.getWindowGridExtent());
64
      grid2.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
65

    
66
      iNX = grid.getNX();
67
      iNY = grid.getNY();
68

    
69
      for (y = 0; (y < iNY) && setProgress(y, iNY); y++) {
70
         for (x = 0; x < iNX; x++) {
71
            dCellValue = grid.getCellValueAsDouble(x, y);
72
            dCellValue2 = grid2.getCellValueAsDouble(x, y);
73
            for (i = 0; i < lut.getRowCount(); i++) {
74
               dTableValue = Double.parseDouble(lut.getValueAt(i, 0).toString());
75
               if (dTableValue == dCellValue) {
76
                  dTableValue2 = Double.parseDouble(lut.getValueAt(i, 1).toString());
77
                  if (dTableValue2 == dCellValue2) {
78
                     dValue = Double.parseDouble(lut.getValueAt(i, 2).toString());
79
                     result.setCellValue(x, y, dValue);
80
                     break;
81
                  }
82
               }
83
            }
84
            if (i >= lut.getRowCount()) {
85
               result.setNoData(x, y);
86
            }
87
         }
88
      }
89

    
90
      return !m_Task.isCanceled();
91

    
92

    
93
   }
94

    
95

    
96
}