1 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 27 public class LatLong extends Coordinates<GeographicCRS> { 28 29 32 public static final GeographicCRS<LatLong> CRS = new GeographicCRS<LatLong>() { 33 34 @Override 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 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 48 public CoordinateSystem getCoordinateSystem() { 49 return GeographicCRS.LATITUDE_LONGITUDE_CS; 50 } 51 52 }; 53 54 57 private double _latitudeInRadian; 58 59 62 private double _longitudeInRadian; 63 64 72 public static LatLong valueOf(double latitude, double longitude, 73 Unit<Angle> unit) { 74 return new LatLong(latitude, longitude, unit); 75 } 76 77 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 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 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 118 public GeographicCRS<LatLong> getCoordinateReferenceSystem() { 119 return CRS; 120 } 121 122 public int getDimension() { 124 return 2; 125 } 126 127 public double getOrdinate(int dimension) throws IndexOutOfBoundsException { 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 (); 137 } 138 } 139 140 } | Popular Tags |