Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / extendLinesLayerWithGrids / ExtendLinesLayerWithGridsAlgorithm.java @ 59

History | View | Annotate | Download (5.86 KB)

1

    
2

    
3
package es.unex.sextante.vectorTools.extendLinesLayerWithGrids;
4

    
5
import java.util.ArrayList;
6

    
7
import com.vividsolutions.jts.geom.Coordinate;
8
import com.vividsolutions.jts.geom.Geometry;
9

    
10
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput;
11
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
12
import es.unex.sextante.core.GeoAlgorithm;
13
import es.unex.sextante.core.Sextante;
14
import es.unex.sextante.dataObjects.IFeature;
15
import es.unex.sextante.dataObjects.IFeatureIterator;
16
import es.unex.sextante.dataObjects.IRasterLayer;
17
import es.unex.sextante.dataObjects.IVectorLayer;
18
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter;
19
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
20
import es.unex.sextante.exceptions.RepeatedParameterNameException;
21
import es.unex.sextante.outputs.IOutputChannel;
22
import es.unex.sextante.outputs.OutputVectorLayer;
23
import es.unex.sextante.parameters.RasterLayerAndBand;
24
import es.unex.sextante.rasterWrappers.GridWrapper;
25
import es.unex.sextante.shapesTools.ShapesTools;
26

    
27

    
28
/**
29
 * Sample the values of Grids on the first and last vertex of each line on a linestring layer. It creates a new Vector Layer
30
 * adding two attributes: <i>'1_'rasterlayer</i> for first vertex and <i>'2_'rasterlayer</i> for last vertex.
31
 * 
32
 * Based on ExtendPointsLayerWithGridsAlgorithm
33
 * 
34
 * @author Nacho Varela
35
 * 
36
 */
37

    
38
//TODO Calculate slope, differences between values, etc...
39
public class ExtendLinesLayerWithGridsAlgorithm
40
         extends
41
            GeoAlgorithm {
42

    
43
   public static final String LAYER       = "LAYER";
44
   public static final String GRIDS       = "GRIDS";
45
   public static final String INTERPOLATE = "INTERPOLATE";
46
   public static final String RESULT      = "RESULT";
47

    
48
   private IVectorLayer       m_Layer;
49
   private ArrayList          m_Grids;
50

    
51

    
52
   @Override
53
   public void defineCharacteristics() {
54

    
55
      setName(Sextante.getText("Sample_lines_extreme_points"));
56
      setGroup(Sextante.getText("Tools_for_line_layers"));
57
      setUserCanDefineAnalysisExtent(true);
58

    
59
      try {
60
         m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Input_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true);
61
         m_Parameters.addMultipleInput(GRIDS, Sextante.getText("Raster_layers"), AdditionalInfoMultipleInput.DATA_TYPE_BAND, true);
62
         m_Parameters.addBoolean(INTERPOLATE, Sextante.getText("Use_interpolation"), true);
63
         addOutputVectorLayer(RESULT, Sextante.getText("Sampled_lines"), OutputVectorLayer.SHAPE_TYPE_LINE, LAYER);
64
      }
65
      catch (final RepeatedParameterNameException e) {
66
         Sextante.addErrorToLog(e);
67
      }
68

    
69
   }
70

    
71

    
72
   @Override
73
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
74

    
75
      int i, j;
76
      int iTotalProgress;
77
      int iProgress = 0;
78
      int iLayer;
79
      int iShapeCount;
80
      double[][] dValues;
81
      boolean bInterpolate;
82
      IRasterLayer grid;
83

    
84
      m_Layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
85
      m_Grids = (ArrayList) m_Parameters.getParameterValueAsObject(GRIDS);
86
      bInterpolate = m_Parameters.getParameterValueAsBoolean(INTERPOLATE);
87

    
88
      if (!m_bIsAutoExtent) {
89
         m_Layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
90
      }
91

    
92
      iShapeCount = m_Layer.getShapesCount();
93

    
94
      dValues = new double[2 * m_Grids.size()][iShapeCount];
95
      iTotalProgress = 2 * iShapeCount * m_Grids.size();
96
      final IFeatureIterator iter = m_Layer.iterator();
97
      for (iLayer = 0; iLayer < m_Grids.size(); iLayer++) {
98
         i = 0;
99
         final RasterLayerAndBand rab = (RasterLayerAndBand) m_Grids.get(iLayer);
100
         final IRasterLayer raster = rab.getRasterLayer();
101
         final int iBand = rab.getBand();
102
         raster.setFullExtent();
103
         if (!bInterpolate) {
104
            raster.setInterpolationMethod(GridWrapper.INTERPOLATION_NearestNeighbour);
105
         }
106
         while (iter.hasNext() && setProgress(iProgress, iTotalProgress)) {
107
            final IFeature feature = iter.next();
108
            final Geometry geom = feature.getGeometry();
109
            final Coordinate[] coords = geom.getCoordinates();
110
            dValues[iLayer][i] = raster.getValueAt(coords[0].x, coords[0].y, iBand);
111
            dValues[iLayer + 1][i] = raster.getValueAt(coords[coords.length - 1].x, coords[coords.length - 1].y, iBand);
112
            iProgress++;
113
            i++;
114
         }
115
      }
116

    
117
      if (m_Task.isCanceled()) {
118
         return false;
119
      }
120
      // 2 * m_Grids.size() because first and last vertex has its own attribute
121
      final Object[][] values = new Object[2 * m_Grids.size()][iShapeCount];
122
      final String[] sNames = new String[2 * m_Grids.size()];
123
      final Class[] types = new Class[2 * m_Grids.size()];
124
      for (i = 0; i < m_Grids.size(); i++) {
125
         final RasterLayerAndBand rab = (RasterLayerAndBand) m_Grids.get(i);
126
         grid = rab.getRasterLayer();
127
         sNames[i] = "1_" + grid.getName();
128

    
129
         types[i] = Double.class;
130
         for (j = 0; j < iShapeCount; j++) {
131
            values[i][j] = new Double(dValues[i][j]);
132
         }
133
         sNames[(i + 1)] = "2_" + grid.getName();
134

    
135
         types[(i + 1)] = Double.class;
136
         for (j = 0; j < (iShapeCount); j++) {
137
            values[i][j] = new Double(dValues[i][j]);
138
            values[(i + 1)][j] = new Double(dValues[(i + 1)][j]);
139
         }
140
      }
141
      final IOutputChannel channel = getOutputChannel(RESULT);
142
      final OutputVectorLayer out = new OutputVectorLayer();
143
      out.setName(RESULT);
144
      out.setOutputChannel(channel);
145
      out.setDescription(m_Layer.getName() + "[" + Sextante.getText("sampled") + "]");
146
      out.setOutputObject(ShapesTools.addFields(m_OutputFactory, m_Layer, channel, sNames, values, types));
147
      addOutputObject(out);
148

    
149
      return !m_Task.isCanceled();
150

    
151
   }
152

    
153
}