001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    /**
007     * @author lakshman
008     * 
009     */
010    public class Location {
011            private double lat;
012    
013            private double lon;
014    
015            private double ht;
016    
017            /** in kilometers. */
018            public static final double EarthRadius = 6380;
019    
020            /** lat, lon in degrees and height in kilometers. */
021            public Location(double lat, double lon, double ht) {
022                    init(lat, lon, ht);
023            }
024    
025            public void init(double lat, double lon, double ht) {
026                    this.lat = lat;
027                    this.lon = lon;
028                    this.ht = ht;
029    
030                    if (lat < -90 || lat > 90 || lon < -180 || lon > 180 || ht < 0)
031                            throw new IndexOutOfBoundsException("Invalid earth location" + this);
032    
033            }
034    
035            public double getLatitude() {
036                    return lat;
037            }
038    
039            public double getLongitude() {
040                    return lon;
041            }
042    
043            public double getHeightKms() {
044                    return ht;
045            }
046    
047            public String toString() {
048                    return "[ " + lat + ", " + lon + ", " + ht + "]";
049            }
050    
051            public CPoint getCPoint() {
052                    double r = EarthRadius + ht; // kms
053                    double phi = lon * Math.PI / 180.0; // radians();
054                    double beta = lat * Math.PI / 180.0; // .radians();
055                    double x = r * Math.cos(phi) * Math.cos(beta);
056                    double y = r * Math.sin(phi) * Math.cos(beta);
057                    double z = r * Math.sin(beta);
058    
059                    return new CPoint(x, y, z);
060            }
061    
062            public boolean equals(Location l) {
063                    return ((lat == l.getLatitude()) && (lon == l.getLongitude()) 
064                                    && (ht == l.getHeightKms()));
065            }
066    
067    }