1 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 27 public class Height extends Coordinates<VerticalCRS> implements 28 Quantity<Length> { 29 30 33 public static final VerticalCRS<Height> CRS = new VerticalCRS<Height>() { 34 35 @Override 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 44 protected AbsolutePosition positionOf(Height coordinates, 45 AbsolutePosition position) { 46 position.heightWGS84 = coordinates; 47 return position; 48 } 49 50 @Override 51 public CoordinateSystem getCoordinateSystem() { 52 return VerticalCRS.HEIGHT_CS; 53 } 54 }; 55 56 59 private double _heightInMeter; 60 61 69 public static Height valueOf(double value, Unit<Length> unit) { 70 return new Height(value, unit); 71 } 72 73 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 90 public VerticalCRS getCoordinateReferenceSystem() { 91 return Height.CRS; 92 } 93 94 public int getDimension() { 96 return 1; 97 } 98 99 public double getOrdinate(int dimension) throws IndexOutOfBoundsException { 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 (); 106 } 107 } 108 109 public final double doubleValue(Unit<Length> unit) { 111 return unit.equals(SI.METER) ? _heightInMeter : SI.METER 112 .getConverterTo(unit).convert(_heightInMeter); 113 } 114 115 public final long longValue(Unit<Length> unit) { 117 return Math.round(doubleValue(unit)); 118 } 119 120 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 |