1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.gpx;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.Date;
|
---|
7 | import java.util.Objects;
|
---|
8 | import java.util.function.Consumer;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.coor.CachedLatLon;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.tools.ExifReader;
|
---|
13 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
14 | import org.openstreetmap.josm.tools.Logging;
|
---|
15 |
|
---|
16 | import com.drew.imaging.jpeg.JpegMetadataReader;
|
---|
17 | import com.drew.lang.CompoundException;
|
---|
18 | import com.drew.metadata.Directory;
|
---|
19 | import com.drew.metadata.Metadata;
|
---|
20 | import com.drew.metadata.MetadataException;
|
---|
21 | import com.drew.metadata.exif.ExifIFD0Directory;
|
---|
22 | import com.drew.metadata.exif.GpsDirectory;
|
---|
23 | import com.drew.metadata.jpeg.JpegDirectory;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Stores info about each image
|
---|
27 | * @since 14205 (extracted from gui.layer.geoimage.ImageEntry)
|
---|
28 | */
|
---|
29 | public class GpxImageEntry implements Comparable<GpxImageEntry>, Cloneable {
|
---|
30 | private File file;
|
---|
31 | private Integer exifOrientation;
|
---|
32 | private LatLon exifCoor;
|
---|
33 | private Double exifImgDir;
|
---|
34 | private Date exifTime;
|
---|
35 | /**
|
---|
36 | * Flag isNewGpsData indicates that the GPS data of the image is new or has changed.
|
---|
37 | * GPS data includes the position, speed, elevation, time (e.g. as extracted from the GPS track).
|
---|
38 | * The flag can used to decide for which image file the EXIF GPS data is (re-)written.
|
---|
39 | */
|
---|
40 | private boolean isNewGpsData;
|
---|
41 | /** Temporary source of GPS time if not correlated with GPX track. */
|
---|
42 | private Date exifGpsTime;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * The following values are computed from the correlation with the gpx track
|
---|
46 | * or extracted from the image EXIF data.
|
---|
47 | */
|
---|
48 | private CachedLatLon pos;
|
---|
49 | /** Speed in kilometer per hour */
|
---|
50 | private Double speed;
|
---|
51 | /** Elevation (altitude) in meters */
|
---|
52 | private Double elevation;
|
---|
53 | /** The time after correlation with a gpx track */
|
---|
54 | private Date gpsTime;
|
---|
55 |
|
---|
56 | private int width;
|
---|
57 | private int height;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * When the correlation dialog is open, we like to show the image position
|
---|
61 | * for the current time offset on the map in real time.
|
---|
62 | * On the other hand, when the user aborts this operation, the old values
|
---|
63 | * should be restored. We have a temporary copy, that overrides
|
---|
64 | * the normal values if it is not null. (This may be not the most elegant
|
---|
65 | * solution for this, but it works.)
|
---|
66 | */
|
---|
67 | private GpxImageEntry tmp;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Constructs a new {@code GpxImageEntry}.
|
---|
71 | */
|
---|
72 | public GpxImageEntry() {}
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Constructs a new {@code GpxImageEntry}.
|
---|
76 | * @param file Path to image file on disk
|
---|
77 | */
|
---|
78 | public GpxImageEntry(File file) {
|
---|
79 | setFile(file);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Returns width of the image this GpxImageEntry represents.
|
---|
84 | * @return width of the image this GpxImageEntry represents
|
---|
85 | * @since 13220
|
---|
86 | */
|
---|
87 | public int getWidth() {
|
---|
88 | return width;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Returns height of the image this GpxImageEntry represents.
|
---|
93 | * @return height of the image this GpxImageEntry represents
|
---|
94 | * @since 13220
|
---|
95 | */
|
---|
96 | public int getHeight() {
|
---|
97 | return height;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns the position value. The position value from the temporary copy
|
---|
102 | * is returned if that copy exists.
|
---|
103 | * @return the position value
|
---|
104 | */
|
---|
105 | public CachedLatLon getPos() {
|
---|
106 | if (tmp != null)
|
---|
107 | return tmp.pos;
|
---|
108 | return pos;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Returns the speed value. The speed value from the temporary copy is
|
---|
113 | * returned if that copy exists.
|
---|
114 | * @return the speed value
|
---|
115 | */
|
---|
116 | public Double getSpeed() {
|
---|
117 | if (tmp != null)
|
---|
118 | return tmp.speed;
|
---|
119 | return speed;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Returns the elevation value. The elevation value from the temporary
|
---|
124 | * copy is returned if that copy exists.
|
---|
125 | * @return the elevation value
|
---|
126 | */
|
---|
127 | public Double getElevation() {
|
---|
128 | if (tmp != null)
|
---|
129 | return tmp.elevation;
|
---|
130 | return elevation;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Returns the GPS time value. The GPS time value from the temporary copy
|
---|
135 | * is returned if that copy exists.
|
---|
136 | * @return the GPS time value
|
---|
137 | */
|
---|
138 | public Date getGpsTime() {
|
---|
139 | if (tmp != null)
|
---|
140 | return getDefensiveDate(tmp.gpsTime);
|
---|
141 | return getDefensiveDate(gpsTime);
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Convenient way to determine if this entry has a GPS time, without the cost of building a defensive copy.
|
---|
146 | * @return {@code true} if this entry has a GPS time
|
---|
147 | * @since 6450
|
---|
148 | */
|
---|
149 | public boolean hasGpsTime() {
|
---|
150 | return (tmp != null && tmp.gpsTime != null) || gpsTime != null;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Returns associated file.
|
---|
155 | * @return associated file
|
---|
156 | */
|
---|
157 | public File getFile() {
|
---|
158 | return file;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Returns EXIF orientation
|
---|
163 | * @return EXIF orientation
|
---|
164 | */
|
---|
165 | public Integer getExifOrientation() {
|
---|
166 | return exifOrientation != null ? exifOrientation : 1;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Returns EXIF time
|
---|
171 | * @return EXIF time
|
---|
172 | */
|
---|
173 | public Date getExifTime() {
|
---|
174 | return getDefensiveDate(exifTime);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Convenient way to determine if this entry has a EXIF time, without the cost of building a defensive copy.
|
---|
179 | * @return {@code true} if this entry has a EXIF time
|
---|
180 | * @since 6450
|
---|
181 | */
|
---|
182 | public boolean hasExifTime() {
|
---|
183 | return exifTime != null;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Returns the EXIF GPS time.
|
---|
188 | * @return the EXIF GPS time
|
---|
189 | * @since 6392
|
---|
190 | */
|
---|
191 | public Date getExifGpsTime() {
|
---|
192 | return getDefensiveDate(exifGpsTime);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Convenient way to determine if this entry has a EXIF GPS time, without the cost of building a defensive copy.
|
---|
197 | * @return {@code true} if this entry has a EXIF GPS time
|
---|
198 | * @since 6450
|
---|
199 | */
|
---|
200 | public boolean hasExifGpsTime() {
|
---|
201 | return exifGpsTime != null;
|
---|
202 | }
|
---|
203 |
|
---|
204 | private static Date getDefensiveDate(Date date) {
|
---|
205 | if (date == null)
|
---|
206 | return null;
|
---|
207 | return new Date(date.getTime());
|
---|
208 | }
|
---|
209 |
|
---|
210 | public LatLon getExifCoor() {
|
---|
211 | return exifCoor;
|
---|
212 | }
|
---|
213 |
|
---|
214 | public Double getExifImgDir() {
|
---|
215 | if (tmp != null)
|
---|
216 | return tmp.exifImgDir;
|
---|
217 | return exifImgDir;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Sets the width of this GpxImageEntry.
|
---|
222 | * @param width set the width of this GpxImageEntry
|
---|
223 | * @since 13220
|
---|
224 | */
|
---|
225 | public void setWidth(int width) {
|
---|
226 | this.width = width;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Sets the height of this GpxImageEntry.
|
---|
231 | * @param height set the height of this GpxImageEntry
|
---|
232 | * @since 13220
|
---|
233 | */
|
---|
234 | public void setHeight(int height) {
|
---|
235 | this.height = height;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Sets the position.
|
---|
240 | * @param pos cached position
|
---|
241 | */
|
---|
242 | public void setPos(CachedLatLon pos) {
|
---|
243 | this.pos = pos;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Sets the position.
|
---|
248 | * @param pos position (will be cached)
|
---|
249 | */
|
---|
250 | public void setPos(LatLon pos) {
|
---|
251 | setPos(pos != null ? new CachedLatLon(pos) : null);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Sets the speed.
|
---|
256 | * @param speed speed
|
---|
257 | */
|
---|
258 | public void setSpeed(Double speed) {
|
---|
259 | this.speed = speed;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Sets the elevation.
|
---|
264 | * @param elevation elevation
|
---|
265 | */
|
---|
266 | public void setElevation(Double elevation) {
|
---|
267 | this.elevation = elevation;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Sets associated file.
|
---|
272 | * @param file associated file
|
---|
273 | */
|
---|
274 | public void setFile(File file) {
|
---|
275 | this.file = file;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Sets EXIF orientation.
|
---|
280 | * @param exifOrientation EXIF orientation
|
---|
281 | */
|
---|
282 | public void setExifOrientation(Integer exifOrientation) {
|
---|
283 | this.exifOrientation = exifOrientation;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Sets EXIF time.
|
---|
288 | * @param exifTime EXIF time
|
---|
289 | */
|
---|
290 | public void setExifTime(Date exifTime) {
|
---|
291 | this.exifTime = getDefensiveDate(exifTime);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Sets the EXIF GPS time.
|
---|
296 | * @param exifGpsTime the EXIF GPS time
|
---|
297 | * @since 6392
|
---|
298 | */
|
---|
299 | public void setExifGpsTime(Date exifGpsTime) {
|
---|
300 | this.exifGpsTime = getDefensiveDate(exifGpsTime);
|
---|
301 | }
|
---|
302 |
|
---|
303 | public void setGpsTime(Date gpsTime) {
|
---|
304 | this.gpsTime = getDefensiveDate(gpsTime);
|
---|
305 | }
|
---|
306 |
|
---|
307 | public void setExifCoor(LatLon exifCoor) {
|
---|
308 | this.exifCoor = exifCoor;
|
---|
309 | }
|
---|
310 |
|
---|
311 | public void setExifImgDir(Double exifDir) {
|
---|
312 | this.exifImgDir = exifDir;
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Override
|
---|
316 | public GpxImageEntry clone() {
|
---|
317 | try {
|
---|
318 | return (GpxImageEntry) super.clone();
|
---|
319 | } catch (CloneNotSupportedException e) {
|
---|
320 | throw new IllegalStateException(e);
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | @Override
|
---|
325 | public int compareTo(GpxImageEntry image) {
|
---|
326 | if (exifTime != null && image.exifTime != null)
|
---|
327 | return exifTime.compareTo(image.exifTime);
|
---|
328 | else if (exifTime == null && image.exifTime == null)
|
---|
329 | return 0;
|
---|
330 | else if (exifTime == null)
|
---|
331 | return -1;
|
---|
332 | else
|
---|
333 | return 1;
|
---|
334 | }
|
---|
335 |
|
---|
336 | @Override
|
---|
337 | public int hashCode() {
|
---|
338 | return Objects.hash(height, width, isNewGpsData,
|
---|
339 | elevation, exifCoor, exifGpsTime, exifImgDir, exifOrientation, exifTime,
|
---|
340 | file, gpsTime, pos, speed, tmp);
|
---|
341 | }
|
---|
342 |
|
---|
343 | @Override
|
---|
344 | public boolean equals(Object obj) {
|
---|
345 | if (this == obj)
|
---|
346 | return true;
|
---|
347 | if (obj == null || getClass() != obj.getClass())
|
---|
348 | return false;
|
---|
349 | GpxImageEntry other = (GpxImageEntry) obj;
|
---|
350 | return height == other.height
|
---|
351 | && width == other.width
|
---|
352 | && isNewGpsData == other.isNewGpsData
|
---|
353 | && Objects.equals(elevation, other.elevation)
|
---|
354 | && Objects.equals(exifCoor, other.exifCoor)
|
---|
355 | && Objects.equals(exifGpsTime, other.exifGpsTime)
|
---|
356 | && Objects.equals(exifImgDir, other.exifImgDir)
|
---|
357 | && Objects.equals(exifOrientation, other.exifOrientation)
|
---|
358 | && Objects.equals(exifTime, other.exifTime)
|
---|
359 | && Objects.equals(file, other.file)
|
---|
360 | && Objects.equals(gpsTime, other.gpsTime)
|
---|
361 | && Objects.equals(pos, other.pos)
|
---|
362 | && Objects.equals(speed, other.speed)
|
---|
363 | && Objects.equals(tmp, other.tmp);
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Make a fresh copy and save it in the temporary variable. Use
|
---|
368 | * {@link #applyTmp()} or {@link #discardTmp()} if the temporary variable
|
---|
369 | * is not needed anymore.
|
---|
370 | */
|
---|
371 | public void createTmp() {
|
---|
372 | tmp = clone();
|
---|
373 | tmp.tmp = null;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Get temporary variable that is used for real time parameter
|
---|
378 | * adjustments. The temporary variable is created if it does not exist
|
---|
379 | * yet. Use {@link #applyTmp()} or {@link #discardTmp()} if the temporary
|
---|
380 | * variable is not needed anymore.
|
---|
381 | * @return temporary variable
|
---|
382 | */
|
---|
383 | public GpxImageEntry getTmp() {
|
---|
384 | if (tmp == null) {
|
---|
385 | createTmp();
|
---|
386 | }
|
---|
387 | return tmp;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Copy the values from the temporary variable to the main instance. The
|
---|
392 | * temporary variable is deleted.
|
---|
393 | * @see #discardTmp()
|
---|
394 | */
|
---|
395 | public void applyTmp() {
|
---|
396 | if (tmp != null) {
|
---|
397 | pos = tmp.pos;
|
---|
398 | speed = tmp.speed;
|
---|
399 | elevation = tmp.elevation;
|
---|
400 | gpsTime = tmp.gpsTime;
|
---|
401 | exifImgDir = tmp.exifImgDir;
|
---|
402 | isNewGpsData = tmp.isNewGpsData;
|
---|
403 | tmp = null;
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Delete the temporary variable. Temporary modifications are lost.
|
---|
409 | * @see #applyTmp()
|
---|
410 | */
|
---|
411 | public void discardTmp() {
|
---|
412 | tmp = null;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * If it has been tagged i.e. matched to a gpx track or retrieved lat/lon from exif
|
---|
417 | * @return {@code true} if it has been tagged
|
---|
418 | */
|
---|
419 | public boolean isTagged() {
|
---|
420 | return pos != null;
|
---|
421 | }
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * String representation. (only partial info)
|
---|
425 | */
|
---|
426 | @Override
|
---|
427 | public String toString() {
|
---|
428 | return file.getName()+": "+
|
---|
429 | "pos = "+pos+" | "+
|
---|
430 | "exifCoor = "+exifCoor+" | "+
|
---|
431 | (tmp == null ? " tmp==null" :
|
---|
432 | " [tmp] pos = "+tmp.pos);
|
---|
433 | }
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Indicates that the image has new GPS data.
|
---|
437 | * That flag is set by new GPS data providers. It is used e.g. by the photo_geotagging plugin
|
---|
438 | * to decide for which image file the EXIF GPS data needs to be (re-)written.
|
---|
439 | * @since 6392
|
---|
440 | */
|
---|
441 | public void flagNewGpsData() {
|
---|
442 | isNewGpsData = true;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Remove the flag that indicates new GPS data.
|
---|
447 | * The flag is cleared by a new GPS data consumer.
|
---|
448 | */
|
---|
449 | public void unflagNewGpsData() {
|
---|
450 | isNewGpsData = false;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Queries whether the GPS data changed. The flag value from the temporary
|
---|
455 | * copy is returned if that copy exists.
|
---|
456 | * @return {@code true} if GPS data changed, {@code false} otherwise
|
---|
457 | * @since 6392
|
---|
458 | */
|
---|
459 | public boolean hasNewGpsData() {
|
---|
460 | if (tmp != null)
|
---|
461 | return tmp.isNewGpsData;
|
---|
462 | return isNewGpsData;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Extract GPS metadata from image EXIF. Has no effect if the image file is not set
|
---|
467 | *
|
---|
468 | * If successful, fills in the LatLon, speed, elevation, image direction, and other attributes
|
---|
469 | * @since 9270
|
---|
470 | */
|
---|
471 | public void extractExif() {
|
---|
472 |
|
---|
473 | Metadata metadata;
|
---|
474 |
|
---|
475 | if (file == null) {
|
---|
476 | return;
|
---|
477 | }
|
---|
478 |
|
---|
479 | try {
|
---|
480 | metadata = JpegMetadataReader.readMetadata(file);
|
---|
481 | } catch (CompoundException | IOException ex) {
|
---|
482 | Logging.error(ex);
|
---|
483 | setExifTime(null);
|
---|
484 | setExifCoor(null);
|
---|
485 | setPos(null);
|
---|
486 | return;
|
---|
487 | }
|
---|
488 |
|
---|
489 | // Changed to silently cope with no time info in exif. One case
|
---|
490 | // of person having time that couldn't be parsed, but valid GPS info
|
---|
491 | try {
|
---|
492 | setExifTime(ExifReader.readTime(metadata));
|
---|
493 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException ex) {
|
---|
494 | Logging.warn(ex);
|
---|
495 | setExifTime(null);
|
---|
496 | }
|
---|
497 |
|
---|
498 | final Directory dir = metadata.getFirstDirectoryOfType(JpegDirectory.class);
|
---|
499 | final Directory dirExif = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
|
---|
500 | final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class);
|
---|
501 |
|
---|
502 | try {
|
---|
503 | if (dirExif != null) {
|
---|
504 | setExifOrientation(dirExif.getInt(ExifIFD0Directory.TAG_ORIENTATION));
|
---|
505 | }
|
---|
506 | } catch (MetadataException ex) {
|
---|
507 | Logging.debug(ex);
|
---|
508 | }
|
---|
509 |
|
---|
510 | try {
|
---|
511 | if (dir != null) {
|
---|
512 | // there are cases where these do not match width and height stored in dirExif
|
---|
513 | setWidth(dir.getInt(JpegDirectory.TAG_IMAGE_WIDTH));
|
---|
514 | setHeight(dir.getInt(JpegDirectory.TAG_IMAGE_HEIGHT));
|
---|
515 | }
|
---|
516 | } catch (MetadataException ex) {
|
---|
517 | Logging.debug(ex);
|
---|
518 | }
|
---|
519 |
|
---|
520 | if (dirGps == null) {
|
---|
521 | setExifCoor(null);
|
---|
522 | setPos(null);
|
---|
523 | return;
|
---|
524 | }
|
---|
525 |
|
---|
526 | ifNotNull(ExifReader.readSpeed(dirGps), this::setSpeed);
|
---|
527 | ifNotNull(ExifReader.readElevation(dirGps), this::setElevation);
|
---|
528 |
|
---|
529 | try {
|
---|
530 | setExifCoor(ExifReader.readLatLon(dirGps));
|
---|
531 | setPos(getExifCoor());
|
---|
532 | } catch (MetadataException | IndexOutOfBoundsException ex) { // (other exceptions, e.g. #5271)
|
---|
533 | Logging.error("Error reading EXIF from file: " + ex);
|
---|
534 | setExifCoor(null);
|
---|
535 | setPos(null);
|
---|
536 | }
|
---|
537 |
|
---|
538 | try {
|
---|
539 | ifNotNull(ExifReader.readDirection(dirGps), this::setExifImgDir);
|
---|
540 | } catch (IndexOutOfBoundsException ex) { // (other exceptions, e.g. #5271)
|
---|
541 | Logging.debug(ex);
|
---|
542 | }
|
---|
543 |
|
---|
544 | ifNotNull(dirGps.getGpsDate(), this::setExifGpsTime);
|
---|
545 | }
|
---|
546 |
|
---|
547 | private static <T> void ifNotNull(T value, Consumer<T> setter) {
|
---|
548 | if (value != null) {
|
---|
549 | setter.accept(value);
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|