94f7ba7b3e5140f30c794b18a3b9375e19b11413
[utils] / gps / src / org / wamblee / gpx / Wgs84CoordinateSystem.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.gpx;
18
19 import static java.lang.Math.PI;
20 import static java.lang.Math.cos;
21 import static java.lang.Math.sin;
22
23 /**
24  * Represents the WGS 84 coordinate system for a GPS measurement identified by
25  * <ul>
26  *   <li> x1: latitude in degrees </li>
27  *   <li> x2: longitude in degrees </li>
28  *   <li> x3: elevation in meters </li>
29  * </ul>
30  * WGS84 models the earth as an ellipse.
31  */
32 public class Wgs84CoordinateSystem implements CoordinateSystem {
33     /*
34      * Ellipse parameters
35      */
36     
37     /**
38      * The radius of the ellipse at the equator
39      */
40     private static final double A = 6378137.000;
41     
42     /**
43      * The distance of the North and South poles to the center of the ellipse. 
44      */
45     private static final double B = 6356752.314;  
46
47  
48     /* (non-Javadoc)
49      * @see org.wamblee.gpx.CoordinateSystem#toReferenceSystem(org.wamblee.gpx.Coordinates)
50      */
51     public Coordinates toReferenceSystem(Coordinates aCoordinates) {
52         double latrad = radians(aCoordinates.getX1()); 
53         double lonrad = radians(aCoordinates.getX2());
54         double coslat = cos(latrad); 
55         double sinlat = sin(latrad);
56         double coslon = cos(lonrad);
57         double sinlon = sin(lonrad); 
58         
59         double r = A*B/Math.sqrt(B*B*coslat*coslat + A*A*sinlat*sinlat) + aCoordinates.getX3();
60         
61         return new Coordinates(r*coslat*coslon,
62                                r*coslat*sinlon,
63                                r*sinlat); 
64         
65     }
66     
67     private double radians(double aDegrees) { 
68         return aDegrees/180.0*PI; 
69     }
70 }