source: josm/trunk/src/com/drew/metadata/exif/GpsDescriptor.java@ 15744

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

see #17848 - update to metadata-extractor 2.12.0

File size: 11.5 KB
RevLine 
[8132]1/*
[15217]2 * Copyright 2002-2019 Drew Noakes and contributors
[8132]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 */
21package com.drew.metadata.exif;
22
23import com.drew.lang.GeoLocation;
24import com.drew.lang.Rational;
25import com.drew.lang.annotations.NotNull;
26import com.drew.lang.annotations.Nullable;
27import com.drew.metadata.TagDescriptor;
28
29import java.text.DecimalFormat;
30
31import static com.drew.metadata.exif.GpsDirectory.*;
32
33/**
34 * Provides human-readable string representations of tag values stored in a {@link GpsDirectory}.
35 *
36 * @author Drew Noakes https://drewnoakes.com
37 */
[13061]38@SuppressWarnings("WeakerAccess")
[8132]39public class GpsDescriptor extends TagDescriptor<GpsDirectory>
40{
41 public GpsDescriptor(@NotNull GpsDirectory directory)
42 {
43 super(directory);
44 }
45
46 @Override
47 @Nullable
48 public String getDescription(int tagType)
49 {
50 switch (tagType) {
51 case TAG_VERSION_ID:
52 return getGpsVersionIdDescription();
53 case TAG_ALTITUDE:
54 return getGpsAltitudeDescription();
55 case TAG_ALTITUDE_REF:
56 return getGpsAltitudeRefDescription();
57 case TAG_STATUS:
58 return getGpsStatusDescription();
59 case TAG_MEASURE_MODE:
60 return getGpsMeasureModeDescription();
[15217]61 case TAG_DOP:
62 return getGpsDopDescription();
[8132]63 case TAG_SPEED_REF:
64 return getGpsSpeedRefDescription();
[15217]65 case TAG_SPEED:
66 return getGpsSpeedDescription();
[8132]67 case TAG_TRACK_REF:
68 case TAG_IMG_DIRECTION_REF:
69 case TAG_DEST_BEARING_REF:
70 return getGpsDirectionReferenceDescription(tagType);
71 case TAG_TRACK:
72 case TAG_IMG_DIRECTION:
73 case TAG_DEST_BEARING:
74 return getGpsDirectionDescription(tagType);
[15217]75 case TAG_DEST_LATITUDE:
76 return getGpsDestLatitudeDescription();
77 case TAG_DEST_LONGITUDE:
78 return getGpsDestLongitudeDescription();
[8132]79 case TAG_DEST_DISTANCE_REF:
80 return getGpsDestinationReferenceDescription();
[15217]81 case TAG_DEST_DISTANCE:
82 return getGpsDestDistanceDescription();
[8132]83 case TAG_TIME_STAMP:
84 return getGpsTimeStampDescription();
85 case TAG_LONGITUDE:
86 // three rational numbers -- displayed in HH"MM"SS.ss
87 return getGpsLongitudeDescription();
88 case TAG_LATITUDE:
89 // three rational numbers -- displayed in HH"MM"SS.ss
90 return getGpsLatitudeDescription();
[15217]91 case TAG_PROCESSING_METHOD:
92 return getGpsProcessingMethodDescription();
93 case TAG_AREA_INFORMATION:
94 return getGpsAreaInformationDescription();
[8132]95 case TAG_DIFFERENTIAL:
96 return getGpsDifferentialDescription();
[15217]97 case TAG_H_POSITIONING_ERROR:
98 return getGpsHPositioningErrorDescription();
[8132]99 default:
100 return super.getDescription(tagType);
101 }
102 }
103
104 @Nullable
105 private String getGpsVersionIdDescription()
106 {
107 return getVersionBytesDescription(TAG_VERSION_ID, 1);
108 }
109
110 @Nullable
111 public String getGpsLatitudeDescription()
112 {
113 GeoLocation location = _directory.getGeoLocation();
114 return location == null ? null : GeoLocation.decimalToDegreesMinutesSecondsString(location.getLatitude());
115 }
116
117 @Nullable
118 public String getGpsLongitudeDescription()
119 {
120 GeoLocation location = _directory.getGeoLocation();
121 return location == null ? null : GeoLocation.decimalToDegreesMinutesSecondsString(location.getLongitude());
122 }
123
124 @Nullable
125 public String getGpsTimeStampDescription()
126 {
127 // time in hour, min, sec
[8243]128 Rational[] timeComponents = _directory.getRationalArray(TAG_TIME_STAMP);
[10862]129 DecimalFormat df = new DecimalFormat("00.000");
[8243]130 return timeComponents == null
131 ? null
132 : String.format("%02d:%02d:%s UTC",
133 timeComponents[0].intValue(),
134 timeComponents[1].intValue(),
135 df.format(timeComponents[2].doubleValue()));
[8132]136 }
137
138 @Nullable
[15217]139 public String getGpsDestLatitudeDescription()
140 {
141 Rational[] latitudes = _directory.getRationalArray(TAG_DEST_LATITUDE);
142 String latitudeRef = _directory.getString(TAG_DEST_LATITUDE_REF);
143
144 if (latitudes == null || latitudes.length != 3 || latitudeRef == null)
145 return null;
146
147 Double lat = GeoLocation.degreesMinutesSecondsToDecimal(
148 latitudes[0], latitudes[1], latitudes[2], latitudeRef.equalsIgnoreCase("S"));
149
150 return lat == null ? null : GeoLocation.decimalToDegreesMinutesSecondsString(lat);
151 }
152
153 @Nullable
154 public String getGpsDestLongitudeDescription()
155 {
156 Rational[] longitudes = _directory.getRationalArray(TAG_LONGITUDE);
157 String longitudeRef = _directory.getString(TAG_LONGITUDE_REF);
158
159 if (longitudes == null || longitudes.length != 3 || longitudeRef == null)
160 return null;
161
162 Double lon = GeoLocation.degreesMinutesSecondsToDecimal(
163 longitudes[0], longitudes[1], longitudes[2], longitudeRef.equalsIgnoreCase("W"));
164
165 return lon == null ? null : GeoLocation.decimalToDegreesMinutesSecondsString(lon);
166 }
167
168 @Nullable
[8132]169 public String getGpsDestinationReferenceDescription()
170 {
171 final String value = _directory.getString(TAG_DEST_DISTANCE_REF);
172 if (value == null)
173 return null;
174 String distanceRef = value.trim();
175 if ("K".equalsIgnoreCase(distanceRef)) {
176 return "kilometers";
177 } else if ("M".equalsIgnoreCase(distanceRef)) {
178 return "miles";
179 } else if ("N".equalsIgnoreCase(distanceRef)) {
180 return "knots";
181 } else {
182 return "Unknown (" + distanceRef + ")";
183 }
184 }
185
186 @Nullable
[15217]187 public String getGpsDestDistanceDescription()
188 {
189 final Rational value = _directory.getRational(TAG_DEST_DISTANCE);
190 if (value == null)
191 return null;
192 final String unit = getGpsDestinationReferenceDescription();
193 return String.format("%s %s",
194 new DecimalFormat("0.##").format(value.doubleValue()),
195 unit == null ? "unit" : unit.toLowerCase());
196 }
197
198 @Nullable
[8132]199 public String getGpsDirectionDescription(int tagType)
200 {
201 Rational angle = _directory.getRational(tagType);
202 // provide a decimal version of rational numbers in the description, to avoid strings like "35334/199 degrees"
203 String value = angle != null
204 ? new DecimalFormat("0.##").format(angle.doubleValue())
205 : _directory.getString(tagType);
206 return value == null || value.trim().length() == 0 ? null : value.trim() + " degrees";
207 }
208
209 @Nullable
210 public String getGpsDirectionReferenceDescription(int tagType)
211 {
212 final String value = _directory.getString(tagType);
213 if (value == null)
214 return null;
215 String gpsDistRef = value.trim();
216 if ("T".equalsIgnoreCase(gpsDistRef)) {
217 return "True direction";
218 } else if ("M".equalsIgnoreCase(gpsDistRef)) {
219 return "Magnetic direction";
220 } else {
221 return "Unknown (" + gpsDistRef + ")";
222 }
223 }
224
225 @Nullable
[15217]226 public String getGpsDopDescription()
227 {
228 final Rational value = _directory.getRational(TAG_DOP);
229 return value == null ? null : new DecimalFormat("0.##").format(value.doubleValue());
230 }
231
232 @Nullable
[8132]233 public String getGpsSpeedRefDescription()
234 {
235 final String value = _directory.getString(TAG_SPEED_REF);
236 if (value == null)
237 return null;
238 String gpsSpeedRef = value.trim();
239 if ("K".equalsIgnoreCase(gpsSpeedRef)) {
[15217]240 return "km/h";
[8132]241 } else if ("M".equalsIgnoreCase(gpsSpeedRef)) {
242 return "mph";
243 } else if ("N".equalsIgnoreCase(gpsSpeedRef)) {
244 return "knots";
245 } else {
246 return "Unknown (" + gpsSpeedRef + ")";
247 }
248 }
249
250 @Nullable
[15217]251 public String getGpsSpeedDescription()
252 {
253 final Rational value = _directory.getRational(TAG_SPEED);
254 if (value == null)
255 return null;
256 final String unit = getGpsSpeedRefDescription();
257 return String.format("%s %s",
258 new DecimalFormat("0.##").format(value.doubleValue()),
259 unit == null ? "unit" : unit.toLowerCase());
260 }
261
262 @Nullable
[8132]263 public String getGpsMeasureModeDescription()
264 {
265 final String value = _directory.getString(TAG_MEASURE_MODE);
266 if (value == null)
267 return null;
268 String gpsSpeedMeasureMode = value.trim();
269 if ("2".equalsIgnoreCase(gpsSpeedMeasureMode)) {
270 return "2-dimensional measurement";
271 } else if ("3".equalsIgnoreCase(gpsSpeedMeasureMode)) {
272 return "3-dimensional measurement";
273 } else {
274 return "Unknown (" + gpsSpeedMeasureMode + ")";
275 }
276 }
277
278 @Nullable
279 public String getGpsStatusDescription()
280 {
281 final String value = _directory.getString(TAG_STATUS);
282 if (value == null)
283 return null;
284 String gpsStatus = value.trim();
285 if ("A".equalsIgnoreCase(gpsStatus)) {
286 return "Active (Measurement in progress)";
287 } else if ("V".equalsIgnoreCase(gpsStatus)) {
288 return "Void (Measurement Interoperability)";
289 } else {
290 return "Unknown (" + gpsStatus + ")";
291 }
292 }
293
294 @Nullable
295 public String getGpsAltitudeRefDescription()
296 {
297 return getIndexedDescription(TAG_ALTITUDE_REF, "Sea level", "Below sea level");
298 }
299
300 @Nullable
301 public String getGpsAltitudeDescription()
302 {
303 final Rational value = _directory.getRational(TAG_ALTITUDE);
[15217]304 return value == null ? null : new DecimalFormat("0.##").format(value.doubleValue()) + " metres";
[8132]305 }
306
307 @Nullable
[15217]308 public String getGpsProcessingMethodDescription()
309 {
310 return getEncodedTextDescription(TAG_PROCESSING_METHOD);
311 }
312
313 @Nullable
314 public String getGpsAreaInformationDescription()
315 {
316 return getEncodedTextDescription(TAG_AREA_INFORMATION);
317 }
318
319 @Nullable
[8132]320 public String getGpsDifferentialDescription()
321 {
322 return getIndexedDescription(TAG_DIFFERENTIAL, "No Correction", "Differential Corrected");
323 }
324
325 @Nullable
[15217]326 public String getGpsHPositioningErrorDescription()
327 {
328 final Rational value = _directory.getRational(TAG_H_POSITIONING_ERROR);
329 return value == null ? null : new DecimalFormat("0.##").format(value.doubleValue()) + " metres";
330 }
331
332 @Nullable
[8132]333 public String getDegreesMinutesSecondsDescription()
334 {
335 GeoLocation location = _directory.getGeoLocation();
336 return location == null ? null : location.toDMSString();
337 }
338}
Note: See TracBrowser for help on using the repository browser.