Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / profiles / flowLineProfile / FlowLineProfileAlgorithm.java @ 59

History | View | Annotate | Download (5.79 KB)

1
package es.unex.sextante.profiles.flowLineProfile;
2

    
3
import java.awt.geom.Point2D;
4
import java.util.ArrayList;
5

    
6
import org.jfree.chart.ChartPanel;
7

    
8
import com.vividsolutions.jts.geom.Coordinate;
9
import com.vividsolutions.jts.geom.GeometryFactory;
10
import com.vividsolutions.jts.geom.LineString;
11

    
12
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput;
13
import es.unex.sextante.core.AnalysisExtent;
14
import es.unex.sextante.core.GeoAlgorithm;
15
import es.unex.sextante.core.OutputObjectsSet;
16
import es.unex.sextante.core.Sextante;
17
import es.unex.sextante.dataObjects.IRasterLayer;
18
import es.unex.sextante.dataObjects.IVectorLayer;
19
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
20
import es.unex.sextante.exceptions.RepeatedParameterNameException;
21
import es.unex.sextante.outputs.OutputVectorLayer;
22
import es.unex.sextante.profiles.profile.ProfileAlgorithm;
23
import es.unex.sextante.rasterWrappers.GridCell;
24

    
25
public class FlowLineProfileAlgorithm
26
         extends
27
            GeoAlgorithm {
28

    
29
   private final static int   m_iOffsetX[]  = { 0, 1, 1, 1, 0, -1, -1, -1 };
30
   private final static int   m_iOffsetY[]  = { 1, 1, 0, -1, -1, -1, 0, 1 };
31

    
32
   public static final String GRAPH         = "GRAPH";
33
   public static final String PROFILEPOINTS = "PROFILEPOINTS";
34
   public static final String POINT         = "POINT";
35
   public static final String LAYERS        = "LAYERS";
36
   public static final String DEM           = "DEM";
37
   public static final String PROFILELINE   = "PROFILELINE";
38

    
39

    
40
   @Override
41
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
42

    
43
      int iDirection;
44
      boolean bContinue = true;
45
      final String sNames[] = { "ID" };
46
      final Class types[] = { Integer.class };
47
      final Object[] value = new Object[1];
48

    
49
      final ArrayList layers = m_Parameters.getParameterValueAsArrayList(LAYERS);
50
      final IRasterLayer dem = m_Parameters.getParameterValueAsRasterLayer(DEM);
51
      Point2D pt = m_Parameters.getParameterValueAsPoint(POINT);
52
      dem.setFullExtent();
53
      final AnalysisExtent extent = dem.getWindowGridExtent();
54
      final GridCell cell = extent.getGridCoordsFromWorldCoords(pt);
55
      final ArrayList coords = new ArrayList();
56
      coords.add(new Coordinate(pt.getX(), pt.getY()));
57

    
58
      do {
59
         iDirection = dem.getDirToNextDownslopeCell(cell.getX(), cell.getY(), false);
60
         if (iDirection >= 0) {
61
            cell.setX(cell.getX() + m_iOffsetX[iDirection]);
62
            cell.setY(cell.getY() + m_iOffsetY[iDirection]);
63
            pt = extent.getWorldCoordsFromGridCoords(cell);
64
            coords.add(new Coordinate(pt.getX(), pt.getY()));
65
         }
66
         else {
67
            bContinue = false;
68
         }
69
      }
70
      while (bContinue && !m_Task.isCanceled());
71

    
72
      if (m_Task.isCanceled()) {
73
         return false;
74
      }
75

    
76
      if (coords.size() > 1) {
77
         final IVectorLayer lines = getNewVectorLayer(PROFILELINE, Sextante.getText("Profile"), IVectorLayer.SHAPE_TYPE_LINE,
78
                  types, sNames);
79
         value[0] = new Double(1);
80
         final GeometryFactory gf = new GeometryFactory();
81
         final Coordinate[] coordinates = new Coordinate[coords.size()];
82
         for (int i = 0; i < coordinates.length; i++) {
83
            coordinates[i] = (Coordinate) coords.get(i);
84
         }
85
         final LineString line = gf.createLineString(coordinates);
86
         lines.addFeature(line, value);
87
         try {
88
            lines.postProcess();//we have to do this to use this layer as input
89
         }
90
         catch (final Exception e) {
91
            throw new GeoAlgorithmExecutionException(e.getMessage());
92
         }
93
         final ProfileAlgorithm profile = new ProfileAlgorithm();
94
         profile.getParameters().getParameter(ProfileAlgorithm.DEM).setParameterValue(dem);
95
         profile.getParameters().getParameter(ProfileAlgorithm.LAYERS).setParameterValue(layers);
96
         profile.getParameters().getParameter(ProfileAlgorithm.ROUTE).setParameterValue(lines);
97
         final OutputObjectsSet outputs = profile.getOutputObjects();
98
         outputs.getOutput(ProfileAlgorithm.PROFILEPOINTS).setOutputChannel(getOutputChannel(PROFILEPOINTS));
99
         if (profile.execute(m_Task, m_OutputFactory)) {
100
            final IVectorLayer profilePts = (IVectorLayer) outputs.getOutput(ProfileAlgorithm.PROFILEPOINTS).getOutputObject();
101
            m_OutputObjects.getOutput(PROFILEPOINTS).setOutputObject(profilePts);
102
            final ChartPanel cp = (ChartPanel) outputs.getOutput(ProfileAlgorithm.GRAPH).getOutputObject();
103
            m_OutputObjects.getOutput(GRAPH).setOutputObject(cp);
104
         }
105
         else {
106
            return false;
107
         }
108
      }
109
      else {
110
         throw new GeoAlgorithmExecutionException("zero lines in layer");
111
      }
112

    
113
      return !m_Task.isCanceled();
114

    
115
   }
116

    
117

    
118
   @Override
119
   public void defineCharacteristics() {
120

    
121
      setName(Sextante.getText("Flow_line_profile"));
122
      setGroup(Sextante.getText("Profiles"));
123
      setUserCanDefineAnalysisExtent(false);
124

    
125
      try {
126
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
127
         m_Parameters.addMultipleInput(LAYERS, Sextante.getText("Additional_layers"),
128
                  AdditionalInfoMultipleInput.DATA_TYPE_RASTER, false);
129
         m_Parameters.addPoint(POINT, Sextante.getText("Starting_point"));
130
         addOutputVectorLayer(PROFILEPOINTS, Sextante.getText("Profile_[points]"), OutputVectorLayer.SHAPE_TYPE_POINT);
131
         addOutputVectorLayer(PROFILELINE, Sextante.getText("Profile_[line]"), OutputVectorLayer.SHAPE_TYPE_LINE);
132
         addOutputChart(GRAPH, Sextante.getText("Profile"));
133
      }
134
      catch (final RepeatedParameterNameException e) {
135
         Sextante.addErrorToLog(e);
136
      }
137

    
138
   }
139

    
140
}