KickJava   Java API By Example, From Geeks To Geeks.

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


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.Length;
13 import javax.measure.quantities.Quantity;
14 import javax.measure.units.SI;
15 import javax.measure.units.Unit;
16
17 import org.jscience.geography.coordinates.crs.VerticalCRS;
18 import org.opengis.referencing.cs.CoordinateSystem;
19
20 /**
21  * This class represents the {@link VerticalCRS vertical} height above the
22  * WGS84 ellipsoid.
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 3.0, February 6, 2006
26  */

27 public class Height extends Coordinates<VerticalCRS> implements
28         Quantity<Length> {
29
30     /**
31      * Holds the coordinate reference system for all instances of this class.
32      */

33     public static final VerticalCRS<Height> CRS = new VerticalCRS<Height>() {
34
35         @Override JavaDoc
36         protected Height coordinatesOf(AbsolutePosition position) {
37             if (position.heightWGS84 instanceof Height)
38                 return (Height) position.heightWGS84;
39             return Height.valueOf(position.heightWGS84.doubleValue(SI.METER),
40                     SI.METER);
41         }
42
43         @Override JavaDoc
44         protected AbsolutePosition positionOf(Height coordinates,
45                 AbsolutePosition position) {
46             position.heightWGS84 = coordinates;
47             return position;
48         }
49
50         @Override JavaDoc
51         public CoordinateSystem getCoordinateSystem() {
52             return VerticalCRS.HEIGHT_CS;
53         }
54     };
55
56     /**
57      * Holds the height in meters.
58      */

59     private double _heightInMeter;
60
61     /**
62      * Returns the vertical position corresponding to the specified coordinates.
63      *
64      * @param value the height above the WGS84 ellipsoid stated in the
65      * specified unit.
66      * @param unit the length unit in which the height is stated.
67      * @return the corresponding vertical position.
68      */

69     public static Height valueOf(double value, Unit<Length> unit) {
70         return new Height(value, unit);
71     }
72
73     /**
74      * Creates a vertical position corresponding to the specified coordinates.
75      *
76      * @param value the height above the WGS84 ellipsoid stated in the
77      * specified unit.
78      * @param unit the length unit in which the height is stated.
79      */

80     public Height(double value, Unit<Length> unit) {
81         if (unit == SI.METER) {
82             _heightInMeter = value;
83         } else {
84             UnitConverter toMeter = unit.getConverterTo(SI.METER);
85             _heightInMeter = toMeter.convert(value);
86         }
87     }
88
89     @Override JavaDoc
90     public VerticalCRS getCoordinateReferenceSystem() {
91         return Height.CRS;
92     }
93
94     // OpenGIS Interface.
95
public int getDimension() {
96         return 1;
97     }
98
99     // OpenGIS Interface.
100
public double getOrdinate(int dimension) throws IndexOutOfBoundsException JavaDoc {
101         if (dimension == 0) {
102             Unit u = VerticalCRS.HEIGHT_CS.getAxis(0).getUnit();
103             return SI.METER.getConverterTo(u).convert(_heightInMeter);
104         } else {
105             throw new IndexOutOfBoundsException JavaDoc();
106         }
107     }
108
109     // Implements Quantity<Length>
110
public final double doubleValue(Unit<Length> unit) {
111         return unit.equals(SI.METER) ? _heightInMeter : SI.METER
112                 .getConverterTo(unit).convert(_heightInMeter);
113     }
114
115     // Implements Quantity<Length>
116
public final long longValue(Unit<Length> unit) {
117         return Math.round(doubleValue(unit));
118     }
119
120     // Implements Quantity<Length>
121
public int compareTo(Quantity<Length> arg0) {
122         double arg0InMeter = arg0.doubleValue(SI.METER);
123         return (_heightInMeter > arg0InMeter) ? 1
124                 : (_heightInMeter < arg0InMeter) ? -1 : 0;
125     }
126
127 }
Popular Tags