Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / rasterize / directionToClosestPoint / DirectionToClosestPointAlgorithm.java @ 59

History | View | Annotate | Download (3.44 KB)

1
package es.unex.sextante.rasterize.directionToClosestPoint;
2

    
3
import java.awt.geom.Point2D;
4

    
5
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
6
import es.unex.sextante.closestpts.Point3D;
7
import es.unex.sextante.core.GeoAlgorithm;
8
import es.unex.sextante.core.Sextante;
9
import es.unex.sextante.dataObjects.IRasterLayer;
10
import es.unex.sextante.dataObjects.IVectorLayer;
11
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
12
import es.unex.sextante.exceptions.RepeatedParameterNameException;
13
import es.unex.sextante.rTree.SextanteRTree;
14
import es.unex.sextante.rasterWrappers.GridCell;
15

    
16
public class DirectionToClosestPointAlgorithm
17
         extends
18
            GeoAlgorithm {
19

    
20
   public static final String LAYER  = "LAYER";
21
   public static final String RESULT = "RESULT";
22

    
23
   protected IVectorLayer     m_Layer;
24

    
25
   private SextanteRTree      m_SearchEngine;
26

    
27

    
28
   @Override
29
   public void defineCharacteristics() {
30

    
31
      setUserCanDefineAnalysisExtent(true);
32
      setGroup(Sextante.getText("Cost_distances_and_routes"));
33
      setName(Sextante.getText("Direction_to_closest_point"));
34

    
35
      try {
36
         m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Point_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT,
37
                  true);
38
         addOutputRasterLayer(RESULT, Sextante.getText("Result"));
39
      }
40
      catch (final RepeatedParameterNameException e) {
41
         Sextante.addErrorToLog(e);
42
      }
43

    
44
   }
45

    
46

    
47
   @Override
48
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
49

    
50
      int x, y;
51
      int iNX, iNY;
52

    
53
      m_Layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
54

    
55
      m_SearchEngine = new SextanteRTree(m_Layer, 0, m_Task);
56
      final IRasterLayer result = getNewRasterLayer(RESULT, m_Layer.getName() + "["
57
                                                            + Sextante.getText("Direction_to_closest_point") + "]",
58
               IRasterLayer.RASTER_DATA_TYPE_DOUBLE);
59

    
60
      iNX = m_AnalysisExtent.getNX();
61
      iNY = m_AnalysisExtent.getNY();
62

    
63
      setProgressText(Sextante.getText("Calculating"));
64
      for (y = 0; (y < iNY) && setProgress(y, iNY); y++) {
65
         for (x = 0; x < iNX; x++) {
66
            result.setCellValue(x, y, getValueAt(x, y));
67
         }
68
      }
69

    
70
      return !m_Task.isCanceled();
71

    
72
   }
73

    
74

    
75
   protected double getValueAt(final int x,
76
                               final int y) {
77

    
78
      final Point2D pt = m_AnalysisExtent.getWorldCoordsFromGridCoords(new GridCell(x, y, 0));
79
      final Point3D closestPt = m_SearchEngine.getClosestPoint(pt.getX(), pt.getY());
80
      final Point2D closestPt2D = new Point2D.Double(closestPt.getX(), closestPt.getY());
81
      final Double y0 = closestPt2D.getY();
82
      final Double y1 = pt.getY();
83
      final Double x0 = closestPt2D.getX();
84
      final Double x1 = pt.getX();
85
      Double angle;
86
      if (((y1 - y0) >= 0) & ((x1 - x0) >= 0)) {
87
         angle = Math.atan((y1 - y0) / (x1 - x0));
88
      }
89
      else {
90
         if (((y1 - y0) >= 0) & ((x1 - x0) < 0)) {
91
            angle = Math.atan((y1 - y0) / (x1 - x0)) + Math.PI;
92
         }
93
         else {
94
            if (((y1 - y0) < 0) & ((x1 - x0) < 0)) {
95
               angle = Math.atan((y1 - y0) / (x1 - x0)) + Math.PI;
96
            }
97
            else {
98
               angle = Math.atan((y1 - y0) / (x1 - x0)) + 2 * Math.PI;
99
            }
100
         }
101
      }
102
      return angle;
103

    
104
   }
105

    
106
}