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

Last change on this file since 15884 was 15217, checked in by Don-vip, 5 years ago

see #17848 - update to metadata-extractor 2.12.0

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.8 KB
Line 
1/*
2 * Copyright 2002-2019 Drew Noakes and contributors
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 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21
22package com.drew.lang;
23
24import com.drew.lang.annotations.NotNull;
25import com.drew.lang.annotations.Nullable;
26
27import java.text.DecimalFormat;
28
29/**
30 * Represents a latitude and longitude pair, giving a position on earth in spherical coordinates.
31 * <p>
32 * Values of latitude and longitude are given in degrees.
33 * <p>
34 * This type is immutable.
35 */
36public final class GeoLocation
37{
38 private final double _latitude;
39 private final double _longitude;
40
41 /**
42 * Instantiates a new instance of {@link GeoLocation}.
43 *
44 * @param latitude the latitude, in degrees
45 * @param longitude the longitude, in degrees
46 */
47 public GeoLocation(double latitude, double longitude)
48 {
49 _latitude = latitude;
50 _longitude = longitude;
51 }
52
53 /**
54 * @return the latitudinal angle of this location, in degrees.
55 */
56 public double getLatitude()
57 {
58 return _latitude;
59 }
60
61 /**
62 * @return the longitudinal angle of this location, in degrees.
63 */
64 public double getLongitude()
65 {
66 return _longitude;
67 }
68
69 /**
70 * @return true, if both latitude and longitude are equal to zero
71 */
72 public boolean isZero()
73 {
74 return _latitude == 0 && _longitude == 0;
75 }
76
77 /**
78 * Converts a decimal degree angle into its corresponding DMS (degrees-minutes-seconds) representation as a string,
79 * of format: {@code -1° 23' 4.56"}
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 * Converts a decimal degree angle into its corresponding DMS (degrees-minutes-seconds) component values, as
91 * a double array.
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 * Converts DMS (degrees-minutes-seconds) rational values, as given in {@link com.drew.metadata.exif.GpsDirectory},
104 * into a single value in degrees, as a double.
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 * @return a string representation of this location, of format: {@code 1.23, 4.56}
147 */
148 @Override
149 @NotNull
150 public String toString()
151 {
152 return _latitude + ", " + _longitude;
153 }
154
155 /**
156 * @return a string representation of this location, of format: {@code -1° 23' 4.56", 54° 32' 1.92"}
157 */
158 @NotNull
159 public String toDMSString()
160 {
161 return decimalToDegreesMinutesSecondsString(_latitude) + ", " + decimalToDegreesMinutesSecondsString(_longitude);
162 }
163}
Note: See TracBrowser for help on using the repository browser.