Changeset 11745 in josm for trunk/src/org
- Timestamp:
- 2017-03-19T01:16:39+01:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageEntry.java
r11620 r11745 9 9 10 10 import org.openstreetmap.josm.Main; 11 import org.openstreetmap.josm.data.SystemOfMeasurement;12 11 import org.openstreetmap.josm.data.coor.CachedLatLon; 13 12 import org.openstreetmap.josm.data.coor.LatLon; … … 432 431 433 432 Metadata metadata; 434 Directory dirExif;435 GpsDirectory dirGps;436 433 437 434 if (file == null) { 435 return; 436 } 437 438 try { 439 metadata = JpegMetadataReader.readMetadata(file); 440 } catch (CompoundException | IOException ex) { 441 Main.error(ex); 442 setExifTime(null); 443 setExifCoor(null); 444 setPos(null); 438 445 return; 439 446 } … … 442 449 // of person having time that couldn't be parsed, but valid GPS info 443 450 try { 444 setExifTime(ExifReader.readTime( file));451 setExifTime(ExifReader.readTime(metadata)); 445 452 } catch (RuntimeException ex) { 446 453 Main.warn(ex); … … 448 455 } 449 456 450 try { 451 metadata = JpegMetadataReader.readMetadata(file); 452 dirExif = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); 453 dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 454 } catch (CompoundException | IOException ex) { 455 Main.warn(ex); 456 setExifCoor(null); 457 setPos(null); 458 return; 459 } 457 final Directory dirExif = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); 458 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 460 459 461 460 try { … … 474 473 } 475 474 475 final Double speed = ExifReader.readSpeed(dirGps); 476 if (speed != null) { 477 setSpeed(speed); 478 } 479 480 final Double ele = ExifReader.readElevation(dirGps); 481 if (ele != null) { 482 setElevation(ele); 483 } 484 476 485 try { 477 double speed = dirGps.getDouble(GpsDirectory.TAG_SPEED); 478 String speedRef = dirGps.getString(GpsDirectory.TAG_SPEED_REF); 479 if ("M".equalsIgnoreCase(speedRef)) { 480 // miles per hour 481 speed *= SystemOfMeasurement.IMPERIAL.bValue / 1000; 482 } else if ("N".equalsIgnoreCase(speedRef)) { 483 // knots == nautical miles per hour 484 speed *= SystemOfMeasurement.NAUTICAL_MILE.bValue / 1000; 485 } 486 // default is K (km/h) 487 setSpeed(speed); 488 } catch (MetadataException ex) { 489 Main.debug(ex); 490 } 491 492 try { 493 double ele = dirGps.getDouble(GpsDirectory.TAG_ALTITUDE); 494 int d = dirGps.getInt(GpsDirectory.TAG_ALTITUDE_REF); 495 if (d == 1) { 496 ele *= -1; 497 } 498 setElevation(ele); 499 } catch (MetadataException ex) { 500 Main.debug(ex); 501 } 502 503 try { 504 LatLon latlon = ExifReader.readLatLon(dirGps); 486 final LatLon latlon = ExifReader.readLatLon(dirGps); 505 487 setExifCoor(latlon); 506 488 setPos(getExifCoor()); 507 508 489 } catch (MetadataException | IndexOutOfBoundsException ex) { // (other exceptions, e.g. #5271) 509 490 Main.error("Error reading EXIF from file: " + ex); … … 513 494 514 495 try { 515 Double direction = ExifReader.readDirection(dirGps);496 final Double direction = ExifReader.readDirection(dirGps); 516 497 if (direction != null) { 517 498 setExifImgDir(direction); -
trunk/src/org/openstreetmap/josm/tools/ExifReader.java
r11514 r11745 9 9 10 10 import org.openstreetmap.josm.Main; 11 import org.openstreetmap.josm.data.SystemOfMeasurement; 11 12 import org.openstreetmap.josm.data.coor.LatLon; 12 13 import org.openstreetmap.josm.tools.date.DateUtils; … … 42 43 public static Date readTime(File filename) { 43 44 try { 44 Metadata metadata = JpegMetadataReader.readMetadata(filename); 45 String dateStr = null; 45 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 46 return readTime(metadata); 47 } catch (JpegProcessingException | IOException e) { 48 Main.error(e); 49 } 50 return null; 51 } 52 53 /** 54 * Returns the date/time from the given JPEG file. 55 * @param metadata The EXIF metadata 56 * @return The date/time read in the EXIF section, or {@code null} if not found 57 * @since 11745 58 */ 59 public static Date readTime(Metadata metadata) { 60 try { 61 String dateTimeOrig = null; 46 62 String dateTime = null; 47 String subSeconds = null; 63 String dateTimeDig = null; 64 String subSecOrig = null; 65 String subSec = null; 66 String subSecDig = null; 67 // The date fields are preferred in this order: DATETIME_ORIGINAL 68 // (0x9003), DATETIME (0x0132), DATETIME_DIGITIZED (0x9004). Some 69 // cameras store the fields in the wrong directory, so all 70 // directories are searched. Assume that the order of the fields 71 // in the directories is random. 48 72 for (Directory dirIt : metadata.getDirectories()) { 49 73 if (!(dirIt instanceof ExifDirectoryBase)) { … … 53 77 if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */ && 54 78 !tag.getDescription().matches("\\[[0-9]+ .+\\]")) { 55 // prefer DATETIME_ORIGINAL 56 dateStr = tag.getDescription(); 79 dateTimeOrig = tag.getDescription(); 80 } else if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */) { 81 dateTime = tag.getDescription(); 82 } else if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) { 83 dateTimeDig = tag.getDescription(); 84 } else if (tag.getTagType() == ExifSubIFDDirectory.TAG_SUBSECOND_TIME_ORIGINAL /* 0x9291 */) { 85 subSecOrig = tag.getDescription(); 86 } else if (tag.getTagType() == ExifSubIFDDirectory.TAG_SUBSECOND_TIME /* 0x9290 */) { 87 subSec = tag.getDescription(); 88 } else if (tag.getTagType() == ExifSubIFDDirectory.TAG_SUBSECOND_TIME_DIGITIZED /* 0x9292 */) { 89 subSecDig = tag.getDescription(); 57 90 } 58 if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */) { 59 // prefer DATETIME over DATETIME_DIGITIZED 60 dateTime = tag.getDescription(); 61 } 62 if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */ && dateTime == null) { 63 dateTime = tag.getDescription(); 64 } 65 if (tag.getTagType() == ExifIFD0Directory.TAG_SUBSECOND_TIME_ORIGINAL) { 66 subSeconds = tag.getDescription(); 67 } 68 } 69 } 70 if (dateStr == null) { 91 } 92 } 93 String dateStr = null; 94 String subSeconds = null; 95 if (dateTimeOrig != null) { 96 // prefer TAG_DATETIME_ORIGINAL 97 dateStr = dateTimeOrig; 98 subSeconds = subSecOrig; 99 } else if (dateTime != null) { 100 // TAG_DATETIME is second choice, see #14209 71 101 dateStr = dateTime; 102 subSeconds = subSec; 103 } else if (dateTimeDig != null) { 104 dateStr = dateTimeDig; 105 subSeconds = subSecDig; 72 106 } 73 107 if (dateStr != null) { … … 84 118 return date; 85 119 } 86 } catch (UncheckedParseException | JpegProcessingException | IOExceptione) {120 } catch (UncheckedParseException e) { 87 121 Main.error(e); 88 122 } … … 154 188 * @param filename The JPEG file to read 155 189 * @return The direction of the image when it was captures (in degrees between 0.0 and 359.99), 156 * or {@code null} if missing or if {@code dirGps} is null190 * or {@code null} if not found 157 191 * @since 6209 158 192 */ … … 171 205 * Returns the direction of the given EXIF GPS directory. 172 206 * @param dirGps The EXIF GPS directory 173 * @return The direction of the image when it was capture s(in degrees between 0.0 and 359.99),207 * @return The direction of the image when it was captured (in degrees between 0.0 and 359.99), 174 208 * or {@code null} if missing or if {@code dirGps} is null 175 209 * @since 6209 … … 206 240 } 207 241 return value; 242 } 243 244 /** 245 * Returns the speed of the given JPEG file. 246 * @param filename The JPEG file to read 247 * @return The speed of the camera when the image was captured (in km/h), 248 * or {@code null} if not found 249 * @since 11745 250 */ 251 public static Double readSpeed(File filename) { 252 try { 253 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 254 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 255 return readSpeed(dirGps); 256 } catch (JpegProcessingException | IOException e) { 257 Main.error(e); 258 } 259 return null; 260 } 261 262 /** 263 * Returns the speed of the given EXIF GPS directory. 264 * @param dirGps The EXIF GPS directory 265 * @return The speed of the camera when the image was captured (in km/h), 266 * or {@code null} if missing or if {@code dirGps} is null 267 * @since 11745 268 */ 269 public static Double readSpeed(GpsDirectory dirGps) { 270 if (dirGps != null) { 271 Double speed = dirGps.getDoubleObject(GpsDirectory.TAG_SPEED); 272 if (speed != null) { 273 final String speedRef = dirGps.getString(GpsDirectory.TAG_SPEED_REF); 274 if ("M".equalsIgnoreCase(speedRef)) { 275 // miles per hour 276 speed *= SystemOfMeasurement.IMPERIAL.bValue / 1000; 277 } else if ("N".equalsIgnoreCase(speedRef)) { 278 // knots == nautical miles per hour 279 speed *= SystemOfMeasurement.NAUTICAL_MILE.bValue / 1000; 280 } 281 // default is K (km/h) 282 return speed; 283 } 284 } 285 return null; 286 } 287 288 /** 289 * Returns the elevation of the given JPEG file. 290 * @param filename The JPEG file to read 291 * @return The elevation of the camera when the image was captured (in m), 292 * or {@code null} if not found 293 * @since 11745 294 */ 295 public static Double readElevation(File filename) { 296 try { 297 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 298 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 299 return readElevation(dirGps); 300 } catch (JpegProcessingException | IOException e) { 301 Main.error(e); 302 } 303 return null; 304 } 305 306 /** 307 * Returns the elevation of the given EXIF GPS directory. 308 * @param dirGps The EXIF GPS directory 309 * @return The elevation of the camera when the image was captured (in m), 310 * or {@code null} if missing or if {@code dirGps} is null 311 * @since 11745 312 */ 313 public static Double readElevation(GpsDirectory dirGps) { 314 if (dirGps != null) { 315 Double ele = dirGps.getDoubleObject(GpsDirectory.TAG_ALTITUDE); 316 if (ele != null) { 317 final Integer d = dirGps.getInteger(GpsDirectory.TAG_ALTITUDE_REF); 318 if (d != null && d.intValue() == 1) { 319 ele *= -1; 320 } 321 return ele; 322 } 323 } 324 return null; 208 325 } 209 326
Note:
See TracChangeset
for help on using the changeset viewer.