Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridCalculus / volume / VolumeAlgorithm.java @ 59

History | View | Annotate | Download (4.57 KB)

1
package es.unex.sextante.gridCalculus.volume;
2

    
3
import java.text.DecimalFormat;
4

    
5
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
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.docEngines.html.HTMLDoc;
10
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
11
import es.unex.sextante.exceptions.RepeatedParameterNameException;
12

    
13
public class VolumeAlgorithm
14
         extends
15
            GeoAlgorithm {
16

    
17
   public static final String BASE                                           = "BASE";
18
   public static final String METHOD                                         = "METHOD";
19
   public static final String GRID                                           = "GRID";
20
   public static final String VOL                                            = "VOL";
21

    
22
   public static final int    METHOD_ONLY_OVER_BASE_LEVEL                    = 0;
23
   public static final int    METHOD_ONLY_UNDER_BASE_LEVEL                   = 1;
24
   public static final int    SUM_OVER_BASE_LEVEL_SUBSTRACT_UNDER_BASE_LEVEL = 2;
25
   public static final int    SUM_VOLUMES_BOTH_OVER_AND_UNDER_BASE_LEVEL     = 3;
26

    
27
   private int                m_iNX, m_iNY;
28
   private double             m_BaseLevel;
29
   private IRasterLayer       m_Grid                                         = null;
30
   private int                m_iMethod;
31

    
32

    
33
   @Override
34
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
35

    
36
      m_Grid = m_Parameters.getParameterValueAsRasterLayer(GRID);
37
      m_iMethod = m_Parameters.getParameterValueAsInt(METHOD);
38
      m_BaseLevel = m_Parameters.getParameterValueAsDouble(BASE);
39

    
40
      m_Grid.setWindowExtent(m_AnalysisExtent);
41

    
42
      m_iNX = m_Grid.getNX();
43
      m_iNY = m_Grid.getNY();
44

    
45
      return calculateVolumes();
46

    
47
   }
48

    
49

    
50
   @Override
51
   public void defineCharacteristics() {
52

    
53
      final String sMethod[] = { Sextante.getText("Only_volumes_over_base_level"),
54
               Sextante.getText("Only_volumes_below_base_level"),
55
               Sextante.getText("Add_volumes_over_base_level_and_substract_volumes_below_it"),
56
               Sextante.getText("Add_volumes_both_over_and_below_base_level") };
57

    
58
      setName(Sextante.getText("Volume_calculation"));
59
      setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
60
      setUserCanDefineAnalysisExtent(true);
61

    
62
      try {
63
         m_Parameters.addInputRasterLayer(GRID, Sextante.getText("Elevation"), true);
64
         m_Parameters.addNumericalValue(BASE, Sextante.getText("Base_level"),
65
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 1000, Double.NEGATIVE_INFINITY, Double.MAX_VALUE);
66
         m_Parameters.addSelection(METHOD, Sextante.getText("Method"), sMethod);
67
         addOutputText(VOL, Sextante.getText("Volume"));
68
      }
69
      catch (final RepeatedParameterNameException e) {
70
         Sextante.addErrorToLog(e);
71
      }
72

    
73
   }
74

    
75

    
76
   private boolean calculateVolumes() {
77

    
78
      int x, y;
79
      double z;
80
      double dVolume = 0;
81

    
82
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
83
         for (x = 0; x < m_iNX; x++) {
84
            z = m_Grid.getCellValueAsDouble(x, y);
85
            if (!m_Grid.isNoDataValue(z)) {
86
               z = z - m_BaseLevel;
87
               switch (m_iMethod) {
88
                  case 0:
89
                     if (z > 0.0) {
90
                        dVolume += z;
91
                     }
92
                     break;
93
                  case 1:
94
                     if (z < 0.0) {
95
                        dVolume -= z;
96
                     }
97
                     break;
98
                  case 2:
99
                     dVolume += z;
100
                     break;
101

    
102
                  case 3:
103
                     dVolume += Math.abs(z);
104
                     break;
105
               }
106
            }
107
         }
108
      }
109

    
110
      if (m_Task.isCanceled()) {
111
         return false;
112
      }
113
      else {
114
         dVolume *= (m_Grid.getWindowCellSize() * m_Grid.getWindowCellSize());
115

    
116
         final DecimalFormat df = new DecimalFormat("##.##");
117
         final HTMLDoc doc = new HTMLDoc();
118
         doc.open(Sextante.getText("Volumes"));
119
         doc.addHeader(Sextante.getText("Volumes"), 2);
120
         doc.startUnorderedList();
121
         doc.addListElement(Sextante.getText("Calculated_volume") + ": " + df.format(dVolume));
122
         doc.close();
123
         addOutputText(VOL, Sextante.getText("Volume") + "[" + m_Grid.getName() + "]", doc.getHTMLCode());
124
         return true;
125
      }
126

    
127
   }
128

    
129
}