source: josm/trunk/src/com/drew/lang/GeoLocation.java@ 7497

Last change on this file since 7497 was 6127, checked in by bastiK, 11 years ago

applied #8895 - Upgrade metadata-extractor to v. 2.6.4 (patch by ebourg)

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.7 KB
Line 
1/*
2 * Copyright 2002-2012 Drew Noakes
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * http://drewnoakes.com/code/exif/
19 * http://code.google.com/p/metadata-extractor/
20 */
21
22package com.drew.lang;
23
24import com.drew.lang.annotations.NotNull;
25import com.drew.lang.annotations.Nullable;
26
27/**
28 * Represents a latitude and longitude pair, giving a position on earth in spherical coordinates.
29 * Values of latitude and longitude are given in degrees.
30 * This type is immutable.
31 */
32public final class GeoLocation
33{
34 private final double _latitude;
35 private final double _longitude;
36
37 /**
38 * Instantiates a new instance of {@link GeoLocation}.
39 *
40 * @param latitude the latitude, in degrees
41 * @param longitude the longitude, in degrees
42 */
43 public GeoLocation(double latitude, double longitude)
44 {
45 _latitude = latitude;
46 _longitude = longitude;
47 }
48
49 /**
50 * @return the latitudinal angle of this location, in degrees.
51 */
52 public double getLatitude()
53 {
54 return _latitude;
55 }
56
57 /**
58 * @return the longitudinal angle of this location, in degrees.
59 */
60 public double getLongitude()
61 {
62 return _longitude;
63 }
64
65 /**
66 * @return true, if both latitude and longitude are equal to zero
67 */
68 public boolean isZero()
69 {
70 return _latitude == 0 && _longitude == 0;
71 }
72
73 /**
74 * Converts a decimal degree angle into its corresponding DMS (degrees-minutes-seconds) representation as a string,
75 * of format: {@code -1° 23' 4.56"}
76 */
77 @NotNull
78 public static String decimalToDegreesMinutesSecondsString(double decimal)
79 {
80 double[] dms = decimalToDegreesMinutesSeconds(decimal);
81 return dms[0] + "° " + dms[1] + "' " + dms[2] + '"';
82 }
83
84 /**
85 * Converts a decimal degree angle into its corresponding DMS (degrees-minutes-seconds) component values, as
86 * a double array.
87 */
88 @NotNull
89 public static double[] decimalToDegreesMinutesSeconds(double decimal)
90 {
91 int d = (int)decimal;
92 double m = Math.abs((decimal % 1) * 60);
93 double s = (m % 1) * 60;
94 return new double[] { d, (int)m, s};
95 }
96
97 /**
98 * Converts DMS (degrees-minutes-seconds) rational values, as given in {@link com.drew.metadata.exif.GpsDirectory},
99 * into a single value in degrees, as a double.
100 */
101 @Nullable
102 public static Double degreesMinutesSecondsToDecimal(@NotNull final Rational degs, @NotNull final Rational mins, @NotNull final Rational secs, final boolean isNegative)
103 {
104 double decimal = Math.abs(degs.doubleValue())
105 + mins.doubleValue() / 60.0d
106 + secs.doubleValue() / 3600.0d;
107
108 if (Double.isNaN(decimal))
109 return null;
110
111 if (isNegative)
112 decimal *= -1;
113
114 return decimal;
115 }
116
117 @Override
118 public boolean equals(final Object o)
119 {
120 if (this == o) return true;
121 if (o == null || getClass() != o.getClass()) return false;
122 GeoLocation that = (GeoLocation) o;
123 if (Double.compare(that._latitude, _latitude) != 0) return false;
124 if (Double.compare(that._longitude, _longitude) != 0) return false;
125 return true;
126 }
127
128 @Override
129 public int hashCode()
130 {
131 int result;
132 long temp;
133 temp = _latitude != +0.0d ? Double.doubleToLongBits(_latitude) : 0L;
134 result = (int) (temp ^ (temp >>> 32));
135 temp = _longitude != +0.0d ? Double.doubleToLongBits(_longitude) : 0L;
136 result = 31 * result + (int) (temp ^ (temp >>> 32));
137 return result;
138 }
139
140 /**
141 * @return a string representation of this location, of format: {@code 1.23, 4.56}
142 */
143 @Override
144 @NotNull
145 public String toString()
146 {
147 return _latitude + ", " + _longitude;
148 }
149
150 /**
151 * @return a string representation of this location, of format: {@code -1° 23' 4.56", 54° 32' 1.92"}
152 */
153 @NotNull
154 public String toDMSString()
155 {
156 return decimalToDegreesMinutesSecondsString(_latitude) + ", " + decimalToDegreesMinutesSecondsString(_longitude);
157 }
158}
Note: See TracBrowser for help on using the repository browser.