Statistics
| Revision:

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

History | View | Annotate | Download (5.15 KB)

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

    
3
import java.util.ArrayList;
4

    
5
import es.unex.sextante.core.GeoAlgorithm;
6
import es.unex.sextante.core.AnalysisExtent;
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
import es.unex.sextante.rasterWrappers.GridCell;
12

    
13
public class SumOfCostFromAllPointsAlgorithm
14
         extends
15
            GeoAlgorithm {
16

    
17
   private static final int   NO_DATA  = -1;
18

    
19
   public static final String COST     = "COST";
20
   public static final String FEATURES = "FEATURES";
21
   public static final String ACCCOST  = "ACCCOST";
22

    
23
   int                        m_iNX, m_iNY;
24
   IRasterLayer               m_Cost;
25
   IRasterLayer               m_Features;
26
   IRasterLayer               m_AccCost, m_TotalCost;
27
   ArrayList                  m_CentralPoints, m_AdjPoints;
28

    
29

    
30
   @Override
31
   public void defineCharacteristics() {
32

    
33
      setName(Sextante.getText("Sum_of_cost_to_all_points"));
34
      setGroup(Sextante.getText("Cost_distances_and_routes"));
35
      setUserCanDefineAnalysisExtent(true);
36
      setIsDeterminatedProcess(false);
37

    
38
      try {
39
         m_Parameters.addInputRasterLayer(COST, Sextante.getText("Unitary_cost"), true);
40
         m_Parameters.addInputRasterLayer(FEATURES, Sextante.getText("Origin-destination_points"), true);
41
         addOutputRasterLayer(ACCCOST, Sextante.getText("Accumulated_cost"));
42
      }
43
      catch (final RepeatedParameterNameException e) {
44
         Sextante.addErrorToLog(e);
45
      }
46

    
47
   }
48

    
49

    
50
   @Override
51
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
52

    
53
      int x, y;
54
      double dValue;
55

    
56
      m_CentralPoints = new ArrayList();
57
      m_AdjPoints = new ArrayList();
58

    
59
      m_Cost = m_Parameters.getParameterValueAsRasterLayer(COST);
60
      m_Features = m_Parameters.getParameterValueAsRasterLayer(FEATURES);
61

    
62
      m_TotalCost = getNewRasterLayer(ACCCOST, Sextante.getText("Accumulated_cost"), IRasterLayer.RASTER_DATA_TYPE_DOUBLE);
63

    
64
      final AnalysisExtent extent = m_TotalCost.getWindowGridExtent();
65
      m_AccCost = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_DOUBLE, extent);
66

    
67
      m_Cost.setWindowExtent(extent);
68
      m_Cost.setInterpolationMethod(IRasterLayer.INTERPOLATION_BSpline);
69

    
70
      m_Features.setWindowExtent(extent);
71
      m_Features.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
72

    
73
      m_iNX = m_Cost.getNX();
74
      m_iNY = m_Cost.getNY();
75

    
76
      m_TotalCost.setNoDataValue(NO_DATA);
77
      m_TotalCost.assign(0.0);
78

    
79
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
80
         for (x = 0; x < m_iNX; x++) {
81
            dValue = m_Features.getCellValueAsDouble(x, y);
82
            if ((dValue != 0.0) && !m_Features.isNoDataValue(dValue)) {
83
               m_CentralPoints.clear();
84
               m_CentralPoints.add(new GridCell(x, y, 0));
85
               m_AccCost.setCellValue(x, y, 0.0);
86
               m_AccCost.setNoDataValue(NO_DATA);
87
               m_AccCost.assignNoData();
88
               calculateCost();
89
               if (m_Task.isCanceled()) {
90
                  return false;
91
               }
92
               m_AccCost.multiply(dValue);
93
               m_TotalCost.add(m_AccCost);
94
            }
95
         }
96
      }
97

    
98
      return !m_Task.isCanceled();
99

    
100
   }
101

    
102

    
103
   private void calculateCost() {
104

    
105
      int i, j;
106
      int iPt;
107
      int x, y, x2, y2;
108
      double dAccCost;
109
      double dCost1, dCost2;
110
      double dPrevAccCost;
111
      GridCell cell;
112

    
113
      final double dDist[][] = new double[3][3];
114

    
115
      for (i = -1; i < 2; i++) {
116
         for (j = -1; j < 2; j++) {
117
            dDist[i + 1][j + 1] = Math.sqrt(i * i + j * j);
118
         }
119
      }
120

    
121
      while (m_CentralPoints.size() != 0) {
122
         for (iPt = 0; iPt < m_CentralPoints.size(); iPt++) {
123
            cell = (GridCell) m_CentralPoints.get(iPt);
124
            x = cell.getX();
125
            y = cell.getY();
126
            dCost1 = m_Cost.getCellValueAsDouble(x, y);
127
            for (i = -1; i < 2; i++) {
128
               for (j = -1; j < 2; j++) {
129
                  x2 = x + i;
130
                  y2 = y + j;
131
                  dCost2 = m_Cost.getCellValueAsDouble(x2, y2);
132
                  if (!m_Cost.isNoDataValue(dCost1) && !m_Cost.isNoDataValue(dCost2)) {
133
                     dAccCost = 0;
134
                     dAccCost = m_AccCost.getCellValueAsDouble(x, y);
135
                     dAccCost += ((dCost1 + dCost2) / 2.0 * dDist[i + 1][j + 1]);
136
                     dPrevAccCost = m_AccCost.getCellValueAsDouble(x2, y2);
137
                     if (m_AccCost.isNoDataValue(dPrevAccCost) || (dPrevAccCost > dAccCost)) {
138
                        m_AccCost.setCellValue(x2, y2, dAccCost);
139
                        m_AdjPoints.add(new GridCell(x2, y2, 0));
140
                     }
141
                  }
142
               }
143
            }
144
         }
145

    
146
         m_CentralPoints = m_AdjPoints;
147
         m_AdjPoints = new ArrayList();
148

    
149
         if (m_Task.isCanceled()) {
150
            return;
151
         }
152

    
153
      }
154
   }
155

    
156
}