Changeset 6127 in josm for trunk/src/com/drew/metadata/exif/GpsDescriptor.java
- Timestamp:
- 2013-08-09T18:05:11+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/exif/GpsDescriptor.java
r4231 r6127 1 1 /* 2 * This is public domain software - that is, you can do whatever you want 3 * with it, and include it software that is licensed under the GNU or the 4 * BSD license, or whatever other licence you choose, including proprietary 5 * closed source licenses. I do ask that you leave this header in tact. 6 * 7 * If you make modifications to this code that you think would benefit the 8 * wider community, please send me a copy and I'll post it on my site. 9 * 10 * If you make use of this code, I'd appreciate hearing about it. 11 * drew@drewnoakes.com 12 * Latest version of this software kept at 13 * http://drewnoakes.com/ 14 * 15 * Created by dnoakes on 12-Nov-2002 22:27:52 using IntelliJ IDEA. 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/ 16 20 */ 17 21 package com.drew.metadata.exif; 18 22 23 import com.drew.lang.GeoLocation; 19 24 import com.drew.lang.Rational; 20 import com.drew. metadata.Directory;21 import com.drew. metadata.MetadataException;25 import com.drew.lang.annotations.NotNull; 26 import com.drew.lang.annotations.Nullable; 22 27 import com.drew.metadata.TagDescriptor; 23 28 29 import java.text.DecimalFormat; 30 24 31 /** 25 * 32 * Provides human-readable string representations of tag values stored in a <code>GpsDirectory</code>. 33 * 34 * @author Drew Noakes http://drewnoakes.com 26 35 */ 27 public class GpsDescriptor extends TagDescriptor 36 public class GpsDescriptor extends TagDescriptor<GpsDirectory> 28 37 { 29 public GpsDescriptor( Directory directory)38 public GpsDescriptor(@NotNull GpsDirectory directory) 30 39 { 31 40 super(directory); 32 41 } 33 42 34 public String getDescription(int tagType) throws MetadataException 43 @Nullable 44 public String getDescription(int tagType) 35 45 { 36 46 switch (tagType) { 47 case GpsDirectory.TAG_GPS_VERSION_ID: 48 return getGpsVersionIdDescription(); 37 49 case GpsDirectory.TAG_GPS_ALTITUDE: 38 50 return getGpsAltitudeDescription(); … … 57 69 case GpsDirectory.TAG_GPS_TIME_STAMP: 58 70 return getGpsTimeStampDescription(); 71 case GpsDirectory.TAG_GPS_LONGITUDE: 59 72 // three rational numbers -- displayed in HH"MM"SS.ss 60 case GpsDirectory.TAG_GPS_LONGITUDE:61 73 return getGpsLongitudeDescription(); 62 74 case GpsDirectory.TAG_GPS_LATITUDE: 75 // three rational numbers -- displayed in HH"MM"SS.ss 63 76 return getGpsLatitudeDescription(); 77 case GpsDirectory.TAG_GPS_DIFFERENTIAL: 78 return getGpsDifferentialDescription(); 64 79 default: 65 return _directory.getString(tagType); 66 } 67 } 68 69 public String getGpsLatitudeDescription() throws MetadataException 70 { 71 if (!_directory.containsTag(GpsDirectory.TAG_GPS_LATITUDE)) return null; 72 return getHoursMinutesSecondsDescription(GpsDirectory.TAG_GPS_LATITUDE); 73 } 74 75 public String getGpsLongitudeDescription() throws MetadataException 76 { 77 if (!_directory.containsTag(GpsDirectory.TAG_GPS_LONGITUDE)) return null; 78 return getHoursMinutesSecondsDescription(GpsDirectory.TAG_GPS_LONGITUDE); 79 } 80 81 public String getHoursMinutesSecondsDescription(int tagType) throws MetadataException 82 { 83 Rational[] components = _directory.getRationalArray(tagType); 84 // TODO create an HoursMinutesSecods class ?? 85 int deg = components[0].intValue(); 86 float min = components[1].floatValue(); 87 float sec = components[2].floatValue(); 88 // carry fractions of minutes into seconds -- thanks Colin Briton 89 sec += (min % 1) * 60; 90 return String.valueOf(deg) + "\"" + String.valueOf((int)min) + "'" + String.valueOf(sec); 91 } 92 93 public String getGpsTimeStampDescription() throws MetadataException 80 return super.getDescription(tagType); 81 } 82 } 83 84 @Nullable 85 private String getGpsVersionIdDescription() 86 { 87 return convertBytesToVersionString(_directory.getIntArray(GpsDirectory.TAG_GPS_VERSION_ID), 1); 88 } 89 90 @Nullable 91 public String getGpsLatitudeDescription() 92 { 93 GeoLocation location = _directory.getGeoLocation(); 94 95 if (location == null) 96 return null; 97 98 return GeoLocation.decimalToDegreesMinutesSecondsString(location.getLatitude()); 99 } 100 101 @Nullable 102 public String getGpsLongitudeDescription() 103 { 104 GeoLocation location = _directory.getGeoLocation(); 105 106 if (location == null) 107 return null; 108 109 return GeoLocation.decimalToDegreesMinutesSecondsString(location.getLongitude()); 110 } 111 112 @Nullable 113 public String getGpsTimeStampDescription() 94 114 { 95 115 // time in hour, min, sec 96 if (!_directory.containsTag(GpsDirectory.TAG_GPS_TIME_STAMP)) return null;97 116 int[] timeComponents = _directory.getIntArray(GpsDirectory.TAG_GPS_TIME_STAMP); 98 StringBuffer sbuffer = new StringBuffer(); 99 sbuffer.append(timeComponents[0]); 100 sbuffer.append(":"); 101 sbuffer.append(timeComponents[1]); 102 sbuffer.append(":"); 103 sbuffer.append(timeComponents[2]); 104 sbuffer.append(" UTC"); 105 return sbuffer.toString(); 106 } 107 117 if (timeComponents==null) 118 return null; 119 StringBuilder description = new StringBuilder(); 120 description.append(timeComponents[0]); 121 description.append(":"); 122 description.append(timeComponents[1]); 123 description.append(":"); 124 description.append(timeComponents[2]); 125 description.append(" UTC"); 126 return description.toString(); 127 } 128 129 @Nullable 108 130 public String getGpsDestinationReferenceDescription() 109 131 { 110 if (!_directory.containsTag(GpsDirectory.TAG_GPS_DEST_DISTANCE_REF)) return null; 111 String destRef = _directory.getString(GpsDirectory.TAG_GPS_DEST_DISTANCE_REF).trim(); 112 if ("K".equalsIgnoreCase(destRef)) { 132 final String value = _directory.getString(GpsDirectory.TAG_GPS_DEST_DISTANCE_REF); 133 if (value==null) 134 return null; 135 String distanceRef = value.trim(); 136 if ("K".equalsIgnoreCase(distanceRef)) { 113 137 return "kilometers"; 114 } else if ("M".equalsIgnoreCase(d estRef)) {138 } else if ("M".equalsIgnoreCase(distanceRef)) { 115 139 return "miles"; 116 } else if ("N".equalsIgnoreCase(d estRef)) {140 } else if ("N".equalsIgnoreCase(distanceRef)) { 117 141 return "knots"; 118 142 } else { 119 return "Unknown (" + destRef + ")"; 120 } 121 } 122 143 return "Unknown (" + distanceRef + ")"; 144 } 145 } 146 147 @Nullable 123 148 public String getGpsDirectionDescription(int tagType) 124 149 { 125 if (!_directory.containsTag(tagType)) return null; 126 String gpsDirection = _directory.getString(tagType).trim(); 127 return gpsDirection + " degrees"; 128 } 129 150 Rational angle = _directory.getRational(tagType); 151 // provide a decimal version of rational numbers in the description, to avoid strings like "35334/199 degrees" 152 String value = angle != null 153 ? new DecimalFormat("0.##").format(angle.doubleValue()) 154 : _directory.getString(tagType); 155 if (value==null || value.trim().length()==0) 156 return null; 157 return value.trim() + " degrees"; 158 } 159 160 @Nullable 130 161 public String getGpsDirectionReferenceDescription(int tagType) 131 162 { 132 if (!_directory.containsTag(tagType)) return null; 133 String gpsDistRef = _directory.getString(tagType).trim(); 163 final String value = _directory.getString(tagType); 164 if (value==null) 165 return null; 166 String gpsDistRef = value.trim(); 134 167 if ("T".equalsIgnoreCase(gpsDistRef)) { 135 168 return "True direction"; … … 141 174 } 142 175 176 @Nullable 143 177 public String getGpsSpeedRefDescription() 144 178 { 145 if (!_directory.containsTag(GpsDirectory.TAG_GPS_SPEED_REF)) return null; 146 String gpsSpeedRef = _directory.getString(GpsDirectory.TAG_GPS_SPEED_REF).trim(); 179 final String value = _directory.getString(GpsDirectory.TAG_GPS_SPEED_REF); 180 if (value==null) 181 return null; 182 String gpsSpeedRef = value.trim(); 147 183 if ("K".equalsIgnoreCase(gpsSpeedRef)) { 148 184 return "kph"; … … 156 192 } 157 193 194 @Nullable 158 195 public String getGpsMeasureModeDescription() 159 196 { 160 if (!_directory.containsTag(GpsDirectory.TAG_GPS_MEASURE_MODE)) return null; 161 String gpsSpeedMeasureMode = _directory.getString(GpsDirectory.TAG_GPS_MEASURE_MODE).trim(); 197 final String value = _directory.getString(GpsDirectory.TAG_GPS_MEASURE_MODE); 198 if (value==null) 199 return null; 200 String gpsSpeedMeasureMode = value.trim(); 162 201 if ("2".equalsIgnoreCase(gpsSpeedMeasureMode)) { 163 202 return "2-dimensional measurement"; … … 169 208 } 170 209 210 @Nullable 171 211 public String getGpsStatusDescription() 172 212 { 173 if (!_directory.containsTag(GpsDirectory.TAG_GPS_STATUS)) return null; 174 String gpsStatus = _directory.getString(GpsDirectory.TAG_GPS_STATUS).trim(); 213 final String value = _directory.getString(GpsDirectory.TAG_GPS_STATUS); 214 if (value==null) 215 return null; 216 String gpsStatus = value.trim(); 175 217 if ("A".equalsIgnoreCase(gpsStatus)) { 176 return " Measurement in progess";218 return "Active (Measurement in progress)"; 177 219 } else if ("V".equalsIgnoreCase(gpsStatus)) { 178 return " Measurement Interoperability";220 return "Void (Measurement Interoperability)"; 179 221 } else { 180 222 return "Unknown (" + gpsStatus + ")"; … … 182 224 } 183 225 184 public String getGpsAltitudeRefDescription() throws MetadataException 185 { 186 if (!_directory.containsTag(GpsDirectory.TAG_GPS_ALTITUDE_REF)) return null; 187 int alititudeRef = _directory.getInt(GpsDirectory.TAG_GPS_ALTITUDE_REF); 188 if (alititudeRef == 0) { 226 @Nullable 227 public String getGpsAltitudeRefDescription() 228 { 229 Integer value = _directory.getInteger(GpsDirectory.TAG_GPS_ALTITUDE_REF); 230 if (value==null) 231 return null; 232 if (value == 0) 189 233 return "Sea level"; 190 } else { 191 return "Unknown (" + alititudeRef + ")"; 192 } 193 } 194 195 public String getGpsAltitudeDescription() throws MetadataException 196 { 197 if (!_directory.containsTag(GpsDirectory.TAG_GPS_ALTITUDE)) return null; 198 String alititude = _directory.getRational(GpsDirectory.TAG_GPS_ALTITUDE).toSimpleString(true); 199 return alititude + " metres"; 234 if (value == 1) 235 return "Below sea level"; 236 return "Unknown (" + value + ")"; 237 } 238 239 @Nullable 240 public String getGpsAltitudeDescription() 241 { 242 final Rational value = _directory.getRational(GpsDirectory.TAG_GPS_ALTITUDE); 243 if (value==null) 244 return null; 245 return value.intValue() + " metres"; 246 } 247 248 @Nullable 249 public String getGpsDifferentialDescription() 250 { 251 final Integer value = _directory.getInteger(GpsDirectory.TAG_GPS_DIFFERENTIAL); 252 if (value==null) 253 return null; 254 if (value == 0) 255 return "No Correction"; 256 if (value == 1) 257 return "Differential Corrected"; 258 return "Unknown (" + value + ")"; 259 } 260 261 @Nullable 262 public String getDegreesMinutesSecondsDescription() 263 { 264 GeoLocation location = _directory.getGeoLocation(); 265 266 if (location == null) 267 return null; 268 269 return location.toDMSString(); 200 270 } 201 271 }
Note:
See TracChangeset
for help on using the changeset viewer.