Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / operation / utils / PointGetAngle.java @ 29798

History | View | Annotate | Download (3.19 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {Iver T.I.}   {Task}
26
 */
27

    
28
package org.gvsig.fmap.geom.operation.utils;
29

    
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryLocator;
32
import org.gvsig.fmap.geom.GeometryManager;
33
import org.gvsig.fmap.geom.Geometry.TYPES;
34
import org.gvsig.fmap.geom.operation.GeometryOperation;
35
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
36
import org.gvsig.fmap.geom.operation.GeometryOperationException;
37
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
38
import org.gvsig.fmap.geom.operation.distance.PointDistance;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.geom.type.GeometryType;
41
import org.gvsig.tools.locator.LocatorException;
42

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

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

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

    
64
                Geometry geom2 = (Geometry)ctx.getAttribute("geom");
65
                GeometryType geomType2 = geom2.getGeometryType();
66

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

    
71
                Point start = (Point)geom;
72
                Point end = (Point)geom;
73

    
74
                double distance;
75
                try {
76
                        distance = ((Double)GeometryLocator.getGeometryManager().invokeOperation(PointDistance.CODE, geom, ctx)).doubleValue();
77
                } catch (LocatorException e) {
78
                        throw new GeometryOperationException(e);
79
                } catch (GeometryOperationNotSupportedException e) {
80
                        throw new GeometryOperationException(e);
81
                }
82
                
83
                double angle = Math.acos((end.getX() - start.getX()) / distance);
84
                
85
                if (start.getY() > end.getY()) {
86
                        angle = -angle;
87
                }
88

    
89
                if (angle < 0) {
90
                        angle += (2 * Math.PI);
91
                }
92

    
93
                return new Double(angle);
94
        }
95

    
96
}
97