Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / lighting / visualExposure / VisualExposureAlgorithm.java @ 59

History | View | Annotate | Download (7.2 KB)

1
package es.unex.sextante.lighting.visualExposure;
2

    
3
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
4
import es.unex.sextante.core.GeoAlgorithm;
5
import es.unex.sextante.core.Sextante;
6
import es.unex.sextante.dataObjects.IRasterLayer;
7
import es.unex.sextante.exceptions.RepeatedParameterNameException;
8

    
9
public class VisualExposureAlgorithm
10
         extends
11
            GeoAlgorithm {
12

    
13
   public static final String DEM        = "DEM";
14
   public static final String FEATURES   = "FEATURES";
15
   public static final String WEIGHTS    = "WEIGHTS";
16
   public static final String METHOD     = "METHOD";
17
   public static final String RADIUS     = "RADIUS";
18
   public static final String RESULT     = "RESULT";
19

    
20
   private int                m_iNX, m_iNY;
21
   private int                m_iMethod;
22
   private int                m_iRadius;
23
   private int                m_iRadius2;
24

    
25
   private IRasterLayer       m_Exposure;
26
   private IRasterLayer       m_DEM      = null;
27
   private IRasterLayer       m_Features = null;
28
   private IRasterLayer       m_Weights  = null;
29

    
30

    
31
   @Override
32
   public boolean processAlgorithm() {
33

    
34
      int x, y;
35
      double dValue;
36

    
37
      try {
38
         m_iMethod = m_Parameters.getParameterValueAsInt(METHOD);
39
         m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
40
         m_Weights = m_Parameters.getParameterValueAsRasterLayer(WEIGHTS);
41
         m_Features = m_Parameters.getParameterValueAsRasterLayer(FEATURES);
42
         m_Exposure = getNewRasterLayer(RESULT, Sextante.getText("Exposure"), IRasterLayer.RASTER_DATA_TYPE_INT);
43

    
44
         //GridExtent extent = m_Exposure.getGridExtent();
45

    
46
         m_DEM.setWindowExtent(getAnalysisExtent());
47
         m_Weights.setWindowExtent(getAnalysisExtent());
48
         m_Features.setWindowExtent(getAnalysisExtent());
49
         m_Features.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
50

    
51
         m_iRadius = (int) Math.ceil(m_Parameters.getParameterValueAsDouble("RADIUS") / m_DEM.getWindowCellSize());
52
         m_iRadius2 = (int) Math.ceil(Math.pow(m_Parameters.getParameterValueAsDouble("RADIUS"), 2.0)
53
                                      / Math.pow(m_DEM.getWindowCellSize(), 2.0));
54

    
55
         m_Exposure.assign(0.0);
56

    
57
         m_iNX = m_DEM.getNX();
58
         m_iNY = m_DEM.getNY();
59

    
60
         for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
61
            for (x = 0; x < m_iNX; x++) {
62
               dValue = m_Features.getCellValueAsDouble(x, y);
63
               if (!m_Features.isNoDataValue(dValue) && (dValue != 0)) {
64
                  if (m_iMethod == 0) {
65
                     Irradiate(x, y);
66
                  }
67
                  else {
68
                     Collect(x, y);
69
                  }
70
               }
71
            }
72

    
73
         }
74

    
75
         m_Exposure.setNoDataValue(0.0);
76

    
77
      }
78
      catch (final Exception e) {
79
         Sextante.addErrorToLog(e);
80
         return false;
81
      }
82

    
83
      return !m_Task.isCanceled();
84
   }
85

    
86

    
87
   @Override
88
   public void defineCharacteristics() {
89

    
90
      final String[] sMethod = { Sextante.getText("Irradiate"), Sextante.getText("Colect_values") };
91

    
92
      setUserCanDefineAnalysisExtent(true);
93
      setGroup(Sextante.getText("Visibility_and_lighting"));
94
      setName(Sextante.getText("Visual_exposure"));
95

    
96
      try {
97
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
98
         m_Parameters.addInputRasterLayer(FEATURES, Sextante.getText("Elements"), true);
99
         m_Parameters.addInputRasterLayer(WEIGHTS, Sextante.getText("Weight"), true);
100
         m_Parameters.addSelection(METHOD, Sextante.getText("Method"), sMethod);
101
         m_Parameters.addNumericalValue(RADIUS, Sextante.getText("Radius"), 1000,
102
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
103
         addOutputRasterLayer(RESULT, Sextante.getText("Visual_exposure"));
104
      }
105
      catch (final RepeatedParameterNameException e) {
106
         Sextante.addErrorToLog(e);
107
      }
108

    
109
   }
110

    
111

    
112
   private void Irradiate(final int x,
113
                          final int y) {
114

    
115
      final int i, j;
116
      int x2, y2;
117
      int dx, dy;
118
      int iXMin, iXMax, iYMin, iYMax;
119
      double dz, z, z2;
120
      final double dWeight = m_Weights.getCellValueAsDouble(x, y);
121

    
122
      z = m_DEM.getCellValueAsDouble(x, y) + m_Features.getCellValueAsDouble(x, y);
123

    
124
      iXMin = Math.max(0, x - m_iRadius);
125
      iXMax = Math.min(m_iNX, x + m_iRadius) + 1;
126
      iYMin = Math.max(0, y - m_iRadius);
127
      iYMax = Math.max(m_iNY, y + m_iRadius) + 1;
128

    
129
      for (y2 = iYMin; y2 < iYMax; y2++) {
130
         for (x2 = iXMin; x2 < iXMax; x2++) {
131
            dx = x - x2;
132
            dy = y - y2;
133
            if ((dx * dx + dy * dy) < m_iRadius2) {
134
               z2 = m_DEM.getCellValueAsDouble(x2, y2);
135
               if (!m_DEM.isNoDataValue(z2)) {
136
                  dz = z - z2;
137
                  if (tracePoint(x2, y2, dx, dy, dz)) {
138
                     m_Exposure.addToCellValue(x2, y2, dWeight);
139
                  }
140
               }
141
            }
142
         }
143
      }
144

    
145
   }
146

    
147

    
148
   private void Collect(final int x,
149
                        final int y) {
150

    
151
      final int i, j;
152
      int x2, y2;
153
      int dx, dy;
154
      int iXMin, iXMax, iYMin, iYMax;
155
      double dz, z, z2;
156

    
157
      z = m_DEM.getCellValueAsDouble(x, y) + m_Features.getCellValueAsDouble(x, y);
158

    
159
      iXMin = Math.max(0, x - m_iRadius);
160
      iXMax = Math.min(m_iNX, x + m_iRadius) + 1;
161
      iYMin = Math.max(0, y - m_iRadius);
162
      iYMax = Math.max(m_iNY, y + m_iRadius) + 1;
163

    
164
      for (y2 = iYMin; y2 < iYMax; y2++) {
165
         for (x2 = iXMin; x2 < iXMax; x2++) {
166
            dx = x - x2;
167
            dy = y - y2;
168
            if ((dx * dx + dy * dy) < m_iRadius2) {
169
               z2 = m_DEM.getCellValueAsDouble(x2, y2);
170
               if (!m_DEM.isNoDataValue(z2)) {
171
                  dz = z - z2;
172
                  if (tracePoint(x2, y2, dx, dy, dz)) {
173
                     m_Exposure.addToCellValue(x, y, m_Weights.getCellValueAsDouble(x2, y2));
174
                  }
175
               }
176
            }
177
         }
178
      }
179

    
180
   }
181

    
182

    
183
   boolean tracePoint(int x,
184
                      int y,
185
                      double dx,
186
                      double dy,
187
                      double dz) {
188

    
189
      double ix, iy, iz, id, d, dist, zmax;
190

    
191
      d = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy);
192

    
193
      zmax = m_DEM.getMaxValue();
194

    
195
      if (d > 0) {
196
         dist = Math.sqrt(dx * dx + dy * dy);
197

    
198
         dx /= d;
199
         dy /= d;
200
         dz /= d;
201

    
202
         d = dist / d;
203

    
204
         id = 0.0;
205
         ix = x + 0.5;
206
         iy = y + 0.5;
207
         iz = m_DEM.getCellValueAsDouble(x, y);
208

    
209
         while (id < dist) {
210
            id += d;
211

    
212
            ix += dx;
213
            iy += dy;
214
            iz += dz;
215

    
216
            x = (int) ix;
217
            y = (int) iy;
218

    
219
            if (!m_DEM.getWindowGridExtent().containsCell(x, y)) {
220
               return true;
221
            }
222
            else if (iz < m_DEM.getCellValueAsDouble(x, y)) {
223
               return false;
224
            }
225
            else if (iz > zmax) {
226
               return true;
227
            }
228
         }
229
      }
230

    
231
      return (true);
232
   }
233

    
234
}