(no commit message)
[utils] / gps / src / main / java / org / wamblee / gps / geometry / SphericalCoordinateSystem.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.gps.geometry;
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 /**
25  * Represents the coordinate system for a GPS measurement identified by
26  * <ul>
27  *   <li> x1: latitude in degrees </li>
28  *   <li> x2: longitude in degrees </li>
29  *   <li> x3: elevation in meters </li>
30  * </ul>
31  * This coordinate system models the earth as a sphere of a specific radius. 
32  *
33  * @author Erik Brakkee
34  */
35 public class SphericalCoordinateSystem implements CoordinateSystem {
36     /**
37      * Earth radius in meters. 
38      */
39     private static final double EARTH_RADIUS = 6371000; 
40
41  
42     /* (non-Javadoc)
43      * @see org.wamblee.gpx.CoordinateSystem#toReferenceSystem(org.wamblee.gpx.Coordinates)
44      */
45     public Coordinates toReferenceSystem(Coordinates aCoordinates) {
46         double latrad = radians(aCoordinates.getX1()); 
47         double lonrad = radians(aCoordinates.getX2());
48         double coslat = cos(latrad); 
49         double sinlat = sin(latrad);
50         double coslon = cos(lonrad);
51         double sinlon = sin(lonrad); 
52         
53         double trueElevation = EARTH_RADIUS + aCoordinates.getX3(); 
54         return new Coordinates(trueElevation*coslat*coslon,
55                                trueElevation*coslat*sinlon,
56                                trueElevation*sinlat); 
57         
58     }
59     
60     private double radians(double aDegrees) { 
61         return aDegrees/180.0*PI; 
62     }
63 }