KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > geography > coordinates > LatLong


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.geography.coordinates;
10
11 import javax.measure.converters.UnitConverter;
12 import javax.measure.quantities.Angle;
13 import javax.measure.quantities.Scalar;
14 import javax.measure.units.SI;
15 import javax.measure.units.Unit;
16
17 import org.jscience.geography.coordinates.crs.GeographicCRS;
18 import org.opengis.referencing.cs.CoordinateSystem;
19
20 /**
21  * This class represents the {@link GeographicCRS geographic} latitude/longitude
22  * coordinates onto the WGS84 ellipsoid.
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 3.0, February 13, 2006
26  */

27 public class LatLong extends Coordinates<GeographicCRS> {
28
29     /**
30      * Holds the coordinate reference system for all instances of this class.
31      */

32     public static final GeographicCRS<LatLong> CRS = new GeographicCRS<LatLong>() {
33
34         @Override JavaDoc
35         protected LatLong coordinatesOf(AbsolutePosition position) {
36             return LatLong.valueOf(position.latitudeWGS84.doubleValue(SI.RADIAN),
37                     position.longitudeWGS84.doubleValue(SI.RADIAN), SI.RADIAN);
38         }
39
40         @Override JavaDoc
41         protected AbsolutePosition positionOf(LatLong coordinates, AbsolutePosition position) {
42             position.latitudeWGS84 = new Scalar<Angle>(coordinates._latitudeInRadian, SI.RADIAN);
43             position.longitudeWGS84 = new Scalar<Angle>(coordinates._longitudeInRadian, SI.RADIAN);
44             return position;
45         }
46
47         @Override JavaDoc
48         public CoordinateSystem getCoordinateSystem() {
49             return GeographicCRS.LATITUDE_LONGITUDE_CS;
50         }
51
52     };
53     
54     /**
55      * Holds the latitude in radians.
56      */

57     private double _latitudeInRadian;
58
59     /**
60      * Holds the longitude in radians.
61      */

62     private double _longitudeInRadian;
63
64     /**
65      * Returns the surface position corresponding to the specified coordinates.
66      *
67      * @param latitude the latitude value stated in the specified unit.
68      * @param longitude the longitude value stated in the specified unit.
69      * @param unit the angle unit in which the coordinates are stated.
70      * @return the corresponding surface position.
71      */

72     public static LatLong valueOf(double latitude, double longitude,
73             Unit<Angle> unit) {
74         return new LatLong(latitude, longitude, unit);
75     }
76
77     /**
78      * Creates the surface position corresponding to the specified coordinates.
79      *
80      * @param latitude the latitude value stated in the specified unit.
81      * @param longitude the longitude value stated in the specified unit.
82      * @param unit the angle unit in which the coordinates are stated.
83      */

84     public LatLong(double latitude, double longitude, Unit<Angle> unit) {
85         if (unit == SI.RADIAN) {
86             _latitudeInRadian = latitude;
87             _longitudeInRadian = longitude;
88         } else {
89             UnitConverter toRadian = unit.getConverterTo(SI.RADIAN);
90             _latitudeInRadian = toRadian.convert(latitude);
91             _longitudeInRadian = toRadian.convert(longitude);
92         }
93     }
94
95     /**
96      * Returns the latitude value as <code>double</code>
97      *
98      * @param unit the angle unit of the latitude to return.
99      * @return the latitude stated in the specified unit.
100      */

101     public final double latitudeValue(Unit<Angle> unit) {
102         return unit.equals(SI.RADIAN) ? _latitudeInRadian : SI.RADIAN
103                 .getConverterTo(unit).convert(_latitudeInRadian);
104     }
105
106     /**
107      * Returns the longitude value as <code>double</code>
108      *
109      * @param unit the angle unit of the longitude to return.
110      * @return the longitude stated in the specified unit.
111      */

112     public final double longitudeValue(Unit<Angle> unit) {
113         return unit.equals(SI.RADIAN) ? _longitudeInRadian : SI.RADIAN
114                 .getConverterTo(unit).convert(_longitudeInRadian);
115     }
116     
117     @Override JavaDoc
118     public GeographicCRS<LatLong> getCoordinateReferenceSystem() {
119         return CRS;
120     }
121
122     // OpenGIS Interface.
123
public int getDimension() {
124         return 2;
125     }
126
127     // OpenGIS Interface.
128
public double getOrdinate(int dimension) throws IndexOutOfBoundsException JavaDoc {
129         if (dimension == 0) {
130             Unit u = GeographicCRS.LATITUDE_LONGITUDE_CS.getAxis(0).getUnit();
131             return SI.RADIAN.getConverterTo(u).convert(_latitudeInRadian);
132         } else if (dimension == 1) {
133             Unit u = GeographicCRS.LATITUDE_LONGITUDE_CS.getAxis(1).getUnit();
134             return SI.RADIAN.getConverterTo(u).convert(_longitudeInRadian);
135         } else {
136             throw new IndexOutOfBoundsException JavaDoc();
137         }
138     }
139
140 }
Popular Tags