Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.geometry / org.gvsig.expressionevaluator.geometry.lib / org.gvsig.expressionevaluator.geometry.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / spatial / PointByAngleFunction.java @ 45612

History | View | Annotate | Download (2.71 KB)

1
package org.gvsig.expressionevaluator.impl.function.spatial;
2

    
3
import org.apache.commons.lang3.Range;
4
import org.apache.commons.lang3.StringUtils;
5
import org.apache.commons.math.util.MathUtils;
6
import org.gvsig.expressionevaluator.Interpreter;
7
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
8
import org.gvsig.fmap.geom.Geometry;
9
import org.gvsig.fmap.geom.GeometryLocator;
10
import org.gvsig.fmap.geom.primitive.Point;
11

    
12
public class PointByAngleFunction extends AbstractGeometryFunction {
13

    
14
    public PointByAngleFunction() {
15
        super("OGC", "PointByAngle", Range.between(3, 5));
16
    }
17

    
18
    @Override
19
    public boolean allowConstantFolding() {
20
        return true;
21
    }
22

    
23
    @Override
24
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
25
        Object arg1 = getObject(args, 0);
26
        try {
27
            arg1 = getDouble(args, 0);
28
        } catch (Exception ex) {
29
            arg1 = getObject(args, 0);
30
        }
31
        // default value in sexagesimal grades 360?
32
        // operations in radians
33
        String unit = "s";
34
        Point pointGeom;           
35
        double distance;
36
        double angle;
37
        if (!(arg1 instanceof Number)) {
38
            //3 or 4 args
39
            //(geom, distance, angle, unit='s')
40
       
41
            Geometry argGeom = GeometryLocator.getGeometryManager().createFrom(arg1);
42
            if(argGeom == null) {
43
                throw new RuntimeException("Geometry can't be null");
44
            }
45
            pointGeom = argGeom.centroid();
46
            if (args.length == 4) {
47
                unit = this.getStr(args, 3);
48
            }
49
            
50
            distance = getDouble(args, 1);
51
            angle = getDouble(args, 2);
52
        } else {
53
            //4 or 5 args
54
            //(x, y, distance, angle, unit='s')
55

    
56
            double x1 = (double) arg1;
57
            double x2 = getDouble(args, 1);
58
            pointGeom = GeometryLocator.getGeometryManager().createPoint(x1, x2, Geometry.SUBTYPES.GEOM2D);
59
            if(pointGeom == null) {
60
                throw new RuntimeException("Geometry can't be null");
61
            }
62
            if (args.length == 5) {
63
                unit = this.getStr(args, 4);
64
            }
65
            
66
            distance = getDouble(args, 2);
67
            angle = getDouble(args, 3);
68
        }
69

    
70
        if (StringUtils.equals(unit, "s")){
71
            angle = MathUtils.round(Math.toRadians(angle), 8);
72
        } else if (StringUtils.equals(unit, "g")) {
73
            angle = Math.PI * angle / 200;
74
        }
75

    
76
        double deltaX = distance * MathUtils.round(Math.cos(angle), 8);
77
        double deltaY = distance * MathUtils.round(Math.sin(angle), 8);
78
       
79
        pointGeom.move(deltaX, deltaY);
80
        return pointGeom;
81

    
82
    }
83

    
84
}