Debian Codegraph @

src/com/drew/metadata/exif/GpsDescriptor.java

raw symbols SCIP
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 package com.drew.metadata.exif;
22
23 import com.drew.lang.GeoLocation;
24 import com.drew.lang.Rational;
25 import com.drew.lang.annotations.NotNull;
26 import com.drew.lang.annotations.Nullable;
27 import com.drew.metadata.TagDescriptor;
28
29 import java.text.DecimalFormat;
30
31 import 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 */
38 @SuppressWarnings("WeakerAccess")
39 public 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();
61 case TAG_DOP:
62 return getGpsDopDescription();
63 case TAG_SPEED_REF:
64 return getGpsSpeedRefDescription();
65 case TAG_SPEED:
66 return getGpsSpeedDescription();
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);
75 case TAG_DEST_LATITUDE:
76 return getGpsDestLatitudeDescription();
77 case TAG_DEST_LONGITUDE:
78 return getGpsDestLongitudeDescription();
79 case TAG_DEST_DISTANCE_REF:
80 return getGpsDestinationReferenceDescription();
81 case TAG_DEST_DISTANCE:
82 return getGpsDestDistanceDescription();
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();
91 case TAG_PROCESSING_METHOD:
92 return getGpsProcessingMethodDescription();
93 case TAG_AREA_INFORMATION:
94 return getGpsAreaInformationDescription();
95 case TAG_DIFFERENTIAL:
96 return getGpsDifferentialDescription();
97 case TAG_H_POSITIONING_ERROR:
98 return getGpsHPositioningErrorDescription();
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
128 Rational[] timeComponents = _directory.getRationalArray(TAG_TIME_STAMP);
129 DecimalFormat df = new DecimalFormat("00.000");
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()));
136 }
137
138 @Nullable
139 public String getGpsDestLatitudeDescription()
140 {
141 return getGeoLocationDimension(TAG_DEST_LATITUDE, TAG_DEST_LATITUDE_REF, "S");
142 }
143
144 @Nullable
145 public String getGpsDestLongitudeDescription()
146 {
147 return getGeoLocationDimension(TAG_DEST_LONGITUDE, TAG_DEST_LONGITUDE_REF, "W");
148 }
149
150 @Nullable
151 private String getGeoLocationDimension(int tagValue, int tagRef, String positiveRef)
152 {
153 Rational[] values = _directory.getRationalArray(tagValue);
154 String ref = _directory.getString(tagRef);
155
156 if (values == null || values.length != 3 || ref == null)
157 return null;
158
159 Double dec = GeoLocation.degreesMinutesSecondsToDecimal(
160 values[0], values[1], values[2], ref.equalsIgnoreCase(positiveRef));
161
162 return dec == null ? null : GeoLocation.decimalToDegreesMinutesSecondsString(dec);
163 }
164
165 @Nullable
166 public String getGpsDestinationReferenceDescription()
167 {
168 final String value = _directory.getString(TAG_DEST_DISTANCE_REF);
169 if (value == null)
170 return null;
171 String distanceRef = value.trim();
172 if ("K".equalsIgnoreCase(distanceRef)) {
173 return "kilometers";
174 } else if ("M".equalsIgnoreCase(distanceRef)) {
175 return "miles";
176 } else if ("N".equalsIgnoreCase(distanceRef)) {
177 return "knots";
178 } else {
179 return "Unknown (" + distanceRef + ")";
180 }
181 }
182
183 @Nullable
184 public String getGpsDestDistanceDescription()
185 {
186 final Rational value = _directory.getRational(TAG_DEST_DISTANCE);
187 if (value == null)
188 return null;
189 final String unit = getGpsDestinationReferenceDescription();
190 return String.format("%s %s",
191 new DecimalFormat("0.##").format(value.doubleValue()),
192 unit == null ? "unit" : unit.toLowerCase());
193 }
194
195 @Nullable
196 public String getGpsDirectionDescription(int tagType)
197 {
198 Rational angle = _directory.getRational(tagType);
199 // provide a decimal version of rational numbers in the description, to avoid strings like "35334/199 degrees"
200 String value = angle != null
201 ? new DecimalFormat("0.##").format(angle.doubleValue())
202 : _directory.getString(tagType);
203 return value == null || value.trim().length() == 0 ? null : value.trim() + " degrees";
204 }
205
206 @Nullable
207 public String getGpsDirectionReferenceDescription(int tagType)
208 {
209 final String value = _directory.getString(tagType);
210 if (value == null)
211 return null;
212 String gpsDistRef = value.trim();
213 if ("T".equalsIgnoreCase(gpsDistRef)) {
214 return "True direction";
215 } else if ("M".equalsIgnoreCase(gpsDistRef)) {
216 return "Magnetic direction";
217 } else {
218 return "Unknown (" + gpsDistRef + ")";
219 }
220 }
221
222 @Nullable
223 public String getGpsDopDescription()
224 {
225 final Rational value = _directory.getRational(TAG_DOP);
226 return value == null ? null : new DecimalFormat("0.##").format(value.doubleValue());
227 }
228
229 @Nullable
230 public String getGpsSpeedRefDescription()
231 {
232 final String value = _directory.getString(TAG_SPEED_REF);
233 if (value == null)
234 return null;
235 String gpsSpeedRef = value.trim();
236 if ("K".equalsIgnoreCase(gpsSpeedRef)) {
237 return "km/h";
238 } else if ("M".equalsIgnoreCase(gpsSpeedRef)) {
239 return "mph";
240 } else if ("N".equalsIgnoreCase(gpsSpeedRef)) {
241 return "knots";
242 } else {
243 return "Unknown (" + gpsSpeedRef + ")";
244 }
245 }
246
247 @Nullable
248 public String getGpsSpeedDescription()
249 {
250 final Rational value = _directory.getRational(TAG_SPEED);
251 if (value == null)
252 return null;
253 final String unit = getGpsSpeedRefDescription();
254 return String.format("%s %s",
255 new DecimalFormat("0.##").format(value.doubleValue()),
256 unit == null ? "unit" : unit.toLowerCase());
257 }
258
259 @Nullable
260 public String getGpsMeasureModeDescription()
261 {
262 final String value = _directory.getString(TAG_MEASURE_MODE);
263 if (value == null)
264 return null;
265 String gpsSpeedMeasureMode = value.trim();
266 if ("2".equalsIgnoreCase(gpsSpeedMeasureMode)) {
267 return "2-dimensional measurement";
268 } else if ("3".equalsIgnoreCase(gpsSpeedMeasureMode)) {
269 return "3-dimensional measurement";
270 } else {
271 return "Unknown (" + gpsSpeedMeasureMode + ")";
272 }
273 }
274
275 @Nullable
276 public String getGpsStatusDescription()
277 {
278 final String value = _directory.getString(TAG_STATUS);
279 if (value == null)
280 return null;
281 String gpsStatus = value.trim();
282 if ("A".equalsIgnoreCase(gpsStatus)) {
283 return "Active (Measurement in progress)";
284 } else if ("V".equalsIgnoreCase(gpsStatus)) {
285 return "Void (Measurement Interoperability)";
286 } else {
287 return "Unknown (" + gpsStatus + ")";
288 }
289 }
290
291 @Nullable
292 public String getGpsAltitudeRefDescription()
293 {
294 return getIndexedDescription(TAG_ALTITUDE_REF, "Sea level", "Below sea level");
295 }
296
297 @Nullable
298 public String getGpsAltitudeDescription()
299 {
300 final Rational value = _directory.getRational(TAG_ALTITUDE);
301 return value == null ? null : new DecimalFormat("0.##").format(value.doubleValue()) + " metres";
302 }
303
304 @Nullable
305 public String getGpsProcessingMethodDescription()
306 {
307 return getEncodedTextDescription(TAG_PROCESSING_METHOD);
308 }
309
310 @Nullable
311 public String getGpsAreaInformationDescription()
312 {
313 return getEncodedTextDescription(TAG_AREA_INFORMATION);
314 }
315
316 @Nullable
317 public String getGpsDifferentialDescription()
318 {
319 return getIndexedDescription(TAG_DIFFERENTIAL, "No Correction", "Differential Corrected");
320 }
321
322 @Nullable
323 public String getGpsHPositioningErrorDescription()
324 {
325 final Rational value = _directory.getRational(TAG_H_POSITIONING_ERROR);
326 return value == null ? null : new DecimalFormat("0.##").format(value.doubleValue()) + " metres";
327 }
328
329 @Nullable
330 public String getDegreesMinutesSecondsDescription()
331 {
332 GeoLocation location = _directory.getGeoLocation();
333 return location == null ? null : location.toDMSString();
334 }
335 }