| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.drew.lang; |
| 23 |
|
| 24 |
import com.drew.lang.annotations.NotNull; |
| 25 |
import com.drew.lang.annotations.Nullable; |
| 26 |
|
| 27 |
import java.text.DecimalFormat; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public final class GeoLocation |
| 37 |
{ |
| 38 |
private final double _latitude; |
| 39 |
private final double _longitude; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public GeoLocation(double latitude, double longitude) |
| 48 |
{ |
| 49 |
_latitude = latitude; |
| 50 |
_longitude = longitude; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public double getLatitude() |
| 57 |
{ |
| 58 |
return _latitude; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public double getLongitude() |
| 65 |
{ |
| 66 |
return _longitude; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public boolean isZero() |
| 73 |
{ |
| 74 |
return _latitude == 0 && _longitude == 0; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
@NotNull |
| 82 |
public static String decimalToDegreesMinutesSecondsString(double decimal) |
| 83 |
{ |
| 84 |
double[] dms = decimalToDegreesMinutesSeconds(decimal); |
| 85 |
DecimalFormat format = new DecimalFormat("0.##"); |
| 86 |
return String.format("%s\u00B0 %s' %s\"", format.format(dms[0]), format.format(dms[1]), format.format(dms[2])); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@NotNull |
| 94 |
public static double[] decimalToDegreesMinutesSeconds(double decimal) |
| 95 |
{ |
| 96 |
int d = (int)decimal; |
| 97 |
double m = Math.abs((decimal % 1) * 60); |
| 98 |
double s = (m % 1) * 60; |
| 99 |
return new double[] { d, (int)m, s}; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@Nullable |
| 107 |
public static Double degreesMinutesSecondsToDecimal(@NotNull final Rational degs, @NotNull final Rational mins, @NotNull final Rational secs, final boolean isNegative) |
| 108 |
{ |
| 109 |
double decimal = Math.abs(degs.doubleValue()) |
| 110 |
+ mins.doubleValue() / 60.0d |
| 111 |
+ secs.doubleValue() / 3600.0d; |
| 112 |
|
| 113 |
if (Double.isNaN(decimal)) |
| 114 |
return null; |
| 115 |
|
| 116 |
if (isNegative) |
| 117 |
decimal *= -1; |
| 118 |
|
| 119 |
return decimal; |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public boolean equals(final Object o) |
| 124 |
{ |
| 125 |
if (this == o) return true; |
| 126 |
if (o == null || getClass() != o.getClass()) return false; |
| 127 |
GeoLocation that = (GeoLocation) o; |
| 128 |
if (Double.compare(that._latitude, _latitude) != 0) return false; |
| 129 |
if (Double.compare(that._longitude, _longitude) != 0) return false; |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public int hashCode() |
| 135 |
{ |
| 136 |
int result; |
| 137 |
long temp; |
| 138 |
temp = _latitude != +0.0d ? Double.doubleToLongBits(_latitude) : 0L; |
| 139 |
result = (int) (temp ^ (temp >>> 32)); |
| 140 |
temp = _longitude != +0.0d ? Double.doubleToLongBits(_longitude) : 0L; |
| 141 |
result = 31 * result + (int) (temp ^ (temp >>> 32)); |
| 142 |
return result; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
@Override |
| 149 |
@NotNull |
| 150 |
public String toString() |
| 151 |
{ |
| 152 |
return _latitude + ", " + _longitude; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
@NotNull |
| 159 |
public String toDMSString() |
| 160 |
{ |
| 161 |
return decimalToDegreesMinutesSecondsString(_latitude) + ", " + decimalToDegreesMinutesSecondsString(_longitude); |
| 162 |
} |
| 163 |
} |