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 @ 42673
History | View | Annotate | Download (3.12 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.operation.GeometryOperation; |
31 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
32 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
33 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
34 |
import org.gvsig.fmap.geom.operation.distance.PointDistance; |
35 |
import org.gvsig.fmap.geom.primitive.Point; |
36 |
import org.gvsig.fmap.geom.type.GeometryType; |
37 |
import org.gvsig.tools.locator.LocatorException; |
38 |
|
39 |
/**
|
40 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
41 |
*/
|
42 |
public class PointGetAngle extends GeometryOperation{ |
43 |
public static final String NAME = "getAngle"; |
44 |
public static final int CODE = GeometryLocator.getGeometryManager(). |
45 |
getGeometryOperationCode(NAME); |
46 |
|
47 |
/* (non-Javadoc)
|
48 |
* @see org.gvsig.fmap.geom.operation.GeometryOperation#getOperationIndex()
|
49 |
*/
|
50 |
public int getOperationIndex() { |
51 |
return CODE;
|
52 |
} |
53 |
|
54 |
/* (non-Javadoc)
|
55 |
* @see org.gvsig.fmap.geom.operation.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
56 |
*/
|
57 |
public Object invoke(Geometry geom, GeometryOperationContext ctx) |
58 |
throws GeometryOperationException {
|
59 |
|
60 |
Geometry geom2 = (Geometry)ctx.getAttribute("geom");
|
61 |
GeometryType geomType2 = geom2.getGeometryType(); |
62 |
|
63 |
if ((TYPES.POINT != geom.getType()) && TYPES.POINT != geomType2.getType()){
|
64 |
throw new UnsupportedOperationException("The distance only can be execudet between two points"); |
65 |
} |
66 |
|
67 |
Point start = (Point)geom; |
68 |
Point end = (Point)geom2; |
69 |
|
70 |
double distance;
|
71 |
try {
|
72 |
distance = ((Double)GeometryLocator.getGeometryManager().invokeOperation(PointDistance.CODE, geom, ctx)).doubleValue();
|
73 |
} catch (LocatorException e) {
|
74 |
throw new GeometryOperationException(e); |
75 |
} catch (GeometryOperationNotSupportedException e) {
|
76 |
throw new GeometryOperationException(e); |
77 |
} |
78 |
|
79 |
double angle = Math.acos((end.getX() - start.getX()) / distance); |
80 |
|
81 |
if (start.getY() > end.getY()) {
|
82 |
angle = -angle; |
83 |
} |
84 |
|
85 |
if (angle < 0) { |
86 |
angle += (2 * Math.PI); |
87 |
} |
88 |
|
89 |
return new Double(angle); |
90 |
} |
91 |
|
92 |
} |
93 |
|