Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.operation.jts / src / main / java / org / gvsig / fmap / geom / operation / utils / PointGetAngle.java @ 42280

History | View | Annotate | Download (3.18 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.geom.operation.utils;
26

    
27
import org.gvsig.fmap.geom.Geometry;
28
import org.gvsig.fmap.geom.Geometry.TYPES;
29
import org.gvsig.fmap.geom.GeometryLocator;
30
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
31
import org.gvsig.fmap.geom.operation.GeometryOperation;
32
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
33
import org.gvsig.fmap.geom.operation.GeometryOperationException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
35
import org.gvsig.fmap.geom.operation.distance.PointDistance;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.fmap.geom.type.GeometryType;
38
import org.gvsig.tools.locator.LocatorException;
39

    
40
/**
41
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
42
 */
43
public class PointGetAngle extends GeometryOperation{
44
    public static final String NAME = "getAngle";
45
        public static final int CODE = GeometryLocator.getGeometryManager().
46
            getGeometryOperationCode(NAME);
47

    
48
        /* (non-Javadoc)
49
         * @see org.gvsig.fmap.geom.operation.GeometryOperation#getOperationIndex()
50
         */
51
        public int getOperationIndex() {
52
                return CODE;
53
        }
54

    
55
        /* (non-Javadoc)
56
         * @see org.gvsig.fmap.geom.operation.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
57
         */
58
        public Object invoke(Geometry geom, GeometryOperationContext ctx)
59
        throws GeometryOperationException {
60

    
61
                Geometry geom2 = (Geometry)ctx.getAttribute("geom");
62
                GeometryType geomType2 = geom2.getGeometryType();
63

    
64
                if ((TYPES.POINT != geom.getType()) && TYPES.POINT != geomType2.getType()){
65
                        throw new UnsupportedOperationException("The distance only can be execudet between two points");
66
                }
67

    
68
                Point start = (Point)geom;
69
                Point end = (Point)geom;
70

    
71
                double distance;
72
                try {
73
                        distance = ((Double)GeometryLocator.getGeometryManager().invokeOperation(PointDistance.CODE, geom, ctx)).doubleValue();
74
                } catch (LocatorException e) {
75
                        throw new GeometryOperationException(e);
76
                } catch (GeometryOperationNotSupportedException e) {
77
                        throw new GeometryOperationException(e);
78
                }
79

    
80
                double angle = Math.acos((end.getX() - start.getX()) / distance);
81

    
82
                if (start.getY() > end.getY()) {
83
                        angle = -angle;
84
                }
85

    
86
                if (angle < 0) {
87
                        angle += (2 * Math.PI);
88
                }
89

    
90
                return new Double(angle);
91
        }
92

    
93
}
94