001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.geoimage;
003
004import java.io.BufferedInputStream;
005import java.io.IOException;
006import java.io.InputStream;
007import java.io.UncheckedIOException;
008import java.net.URI;
009import java.nio.file.FileSystemNotFoundException;
010import java.time.Instant;
011import java.util.List;
012
013import org.openstreetmap.josm.data.coor.ILatLon;
014import org.openstreetmap.josm.data.gpx.TimeSource;
015import org.openstreetmap.josm.data.imagery.street_level.Projections;
016
017/**
018 * An interface for images with metadata
019 * @author Taylor Smock
020 * @since 18592, largely extracted from {@link org.openstreetmap.josm.data.gpx.GpxImageEntry}
021 */
022public interface ImageMetadata {
023    /**
024     * Get the image location
025     * @return The image location
026     */
027    URI getImageURI();
028
029    /**
030     * Returns width of the image this ImageMetadata represents.
031     * @return width of the image this ImageMetadata represents
032     */
033    int getWidth();
034
035    /**
036     * Returns height of the image this ImageMetadata represents.
037     * @return height of the image this ImageMetadata represents
038     * @since 18592 (interface), 13220 (GpxImageEntry)
039     */
040    int getHeight();
041
042    /**
043     * Returns the position value. The position value from the temporary copy
044     * is returned if that copy exists.
045     * @return the position value
046     */
047    ILatLon getPos();
048
049    /**
050     * Returns the speed value. The speed value from the temporary copy is
051     * returned if that copy exists.
052     * @return the speed value
053     */
054    Double getSpeed();
055
056    /**
057     * Returns the elevation value. The elevation value from the temporary
058     * copy is returned if that copy exists.
059     * @return the elevation value
060     */
061    Double getElevation();
062
063    /**
064     * Returns the GPS time value. The GPS time value from the temporary copy
065     * is returned if that copy exists.
066     * @return the GPS time value
067     */
068    Instant getGpsInstant();
069
070    /**
071     * Convenient way to determine if this entry has a GPS time, without the cost of building a defensive copy.
072     * @return {@code true} if this entry has a GPS time
073     * @since 18592 (interface), 6450 (GpxImageEntry)
074     */
075    boolean hasGpsTime();
076
077    /**
078     * Returns a display name for this entry
079     * @return a display name for this entry
080     */
081    String getDisplayName();
082
083    /**
084     * Returns EXIF orientation
085     * @return EXIF orientation
086     */
087    default Integer getExifOrientation() {
088        return 1;
089    }
090
091    /**
092     * Returns EXIF time
093     * @return EXIF time
094     * @since 18592 (interface), 17715 (GpxImageEntry)
095     */
096    Instant getExifInstant();
097
098    /**
099     * Convenient way to determine if this entry has a EXIF time, without the cost of building a defensive copy.
100     * @return {@code true} if this entry has a EXIF time
101     * @since 18592 (interface), 6450 (GpxImageEntry)
102     */
103    boolean hasExifTime();
104
105    /**
106     * Returns the EXIF GPS time.
107     * @return the EXIF GPS time
108     * @since 18592 (interface), 17715 (GpxImageEntry)
109     */
110    Instant getExifGpsInstant();
111
112    /**
113     * Convenient way to determine if this entry has a EXIF GPS time, without the cost of building a defensive copy.
114     * @return {@code true} if this entry has a EXIF GPS time
115     * @since 18592 (interface), 6450 (GpxImageEntry)
116     */
117    boolean hasExifGpsTime();
118
119    /**
120     * Return the time value selected with the parameter.
121     * @param timeSource the wanted time value, exifCamTime or exifGpsTime
122     * @return exifInstant or exifGpsInstant value
123     * @since 19426
124     */
125    Instant getTimeSourceInstant(TimeSource timeSource);
126
127    /**
128     * Get the EXIF coordinates
129     * @return The location of the image
130     */
131    ILatLon getExifCoor();
132
133    /**
134     * Get the EXIF direction
135     * @return The image direction
136     */
137    Double getExifImgDir();
138
139    /**
140     * Get the EXIF GPS track direction.
141     * @return The GPS track direction
142     * @since 19387
143     */
144    Double getExifGpsTrack();
145
146    /**
147     * Get the EXIF Horizontal positioning error.
148     * @return The image horizontal positioning error
149     * @since 19387
150     */
151    Double getExifHPosErr();
152
153    /**
154     * Get the GPS Differential mode.
155     * @return The image gnss fix mode
156     * @since 19387
157     */
158    Integer getGpsDiffMode();
159    
160    /**
161     * Get the GPS 2d/3d mode.
162     * @return The image gnss 2d/3d mode
163     * @since 19387
164     */
165    Integer getGps2d3dMode();
166
167    /**
168     * Get the EXIF GPS DOP value.
169     * @return The image GPS DOP value
170     * @since 19387
171     */
172    Double getExifGpsDop();
173
174    /**
175     * Get the GPS datum value.
176     * @return The image GPS datum value
177     * @since 19387
178     */
179    String getExifGpsDatum();
180
181    /**
182     * Get the EXIF GPS processing method.
183     * @return The image GPS processing method
184     * @since 19387
185     */
186    String getExifGpsProcMethod();
187
188    /**
189     * Get the last time the source was modified.
190     * @return The last time the source was modified
191     */
192    Instant getLastModified();
193
194    /**
195     * Sets the width of this ImageMetadata.
196     * @param width set the width of this ImageMetadata
197     * @since 18592 (interface), 13220 (GpxImageEntry)
198     */
199    void setWidth(int width);
200
201    /**
202     * Sets the height of this ImageMetadata.
203     * @param height set the height of this ImageMetadata
204     * @since 18592 (interface), 13220 (GpxImageEntry)
205     */
206    void setHeight(int height);
207
208    /**
209     * Sets the position.
210     * @param pos position (will be cached)
211     */
212    void setPos(ILatLon pos);
213
214    /**
215     * Sets the speed.
216     * @param speed speed
217     */
218    void setSpeed(Double speed);
219
220    /**
221     * Sets the elevation.
222     * @param elevation elevation
223     */
224    void setElevation(Double elevation);
225
226    /**
227     * Sets EXIF orientation.
228     * @param exifOrientation EXIF orientation
229     */
230    void setExifOrientation(Integer exifOrientation);
231
232    /**
233     * Sets EXIF time.
234     * @param exifTime EXIF time
235     * @since 18592 (interface), 17715 (GpxImageEntry)
236     */
237    void setExifTime(Instant exifTime);
238
239    /**
240     * Sets the EXIF GPS time.
241     * @param exifGpsTime the EXIF GPS time
242     * @since 18592 (interface), 17715 (GpxImageEntry)
243     */
244    void setExifGpsTime(Instant exifGpsTime);
245
246    /**
247     * Sets the GPS time.
248     * @param gpsTime the GPS time
249     * @since 18592 (interface), 17715 (GpxImageEntry)
250     */
251    void setGpsTime(Instant gpsTime);
252
253    /**
254     * Sets the EXIF coordinates
255     * @param exifCoor The EXIF coordinates
256     */
257    void setExifCoor(ILatLon exifCoor);
258
259    /**
260     * Sets the EXIF direction
261     * @param exifDir The direction
262     */
263    void setExifImgDir(Double exifDir);
264
265    /**
266     * Sets the EXIF GPS track direction.
267     * @param exifGpsTrack The GPS track direction
268     * @since 19387
269     */
270    void setExifGpsTrack(Double exifGpsTrack);
271
272    /**
273     * Sets the EXIF horizontal positioning error.
274     * @param exifHPosErr the EXIF horizontal positionning error
275     * @since 19387
276     */
277    void setExifHPosErr(Double exifHPosErr);
278
279    /**
280     * Sets the EXIF GPS DOP value.
281     * @param exifGpsDop the EXIF GPS DOP value
282     * @since 19387
283     */
284    void setExifGpsDop(Double exifGpsDop);
285
286    /**
287     * Sets the GPS Differential mode.
288     * @param gpsDiffMode GPS Differential mode
289     * @since 19387
290     */
291    void setGpsDiffMode(Integer gpsDiffMode);
292
293    /**
294     * Sets the GPS 2d/3d mode.
295     * @param gps2d3dMode GPS 2d/3d mode
296     * @since 19387
297     */
298    void setGps2d3dMode(Integer gps2d3dMode);
299
300    /**
301     * Sets the GPS datum value.
302     * @param exifGpsDatum GPS datum
303     * @since 19387
304     */
305    void setExifGpsDatum(String exifGpsDatum);
306
307    /**
308     * Sets the GPS processing method.
309     * @param exifGpsProcMethod GPS processing method
310     * @since 19387
311     */
312    void setExifGpsProcMethod(String exifGpsProcMethod);
313
314    /**
315     * Sets the IPTC caption.
316     * @param iptcCaption the IPTC caption
317     * @since 18592 (interface), 15219 (GpxImageEntry)
318     */
319    void setIptcCaption(String iptcCaption);
320
321    /**
322     * Sets the IPTC headline.
323     * @param iptcHeadline the IPTC headline
324     * @since 18592 (interface), 15219 (GpxImageEntry)
325     */
326    void setIptcHeadline(String iptcHeadline);
327
328    /**
329     * Sets the IPTC keywords.
330     * @param iptcKeywords the IPTC keywords
331     * @since 18592 (interface), 15219 (GpxImageEntry)
332     */
333    void setIptcKeywords(List<String> iptcKeywords);
334
335    /**
336     * Sets the IPTC object name.
337     * @param iptcObjectName the IPTC object name
338     * @since 18592 (interface), 15219 (GpxImageEntry)
339     */
340    void setIptcObjectName(String iptcObjectName);
341
342    /**
343     * Returns the IPTC caption.
344     * @return the IPTC caption
345     * @since 18592 (interface), 15219 (GpxImageEntry)
346     */
347    String getIptcCaption();
348
349    /**
350     * Returns the IPTC headline.
351     * @return the IPTC headline
352     * @since 18592 (interface), 15219 (GpxImageEntry)
353     */
354    String getIptcHeadline();
355
356    /**
357     * Returns the IPTC keywords.
358     * @return the IPTC keywords
359     * @since 18592 (interface), 15219 (GpxImageEntry)
360     */
361    List<String> getIptcKeywords();
362
363    /**
364     * Returns the IPTC object name.
365     * @return the IPTC object name
366     * @since 18592 (interface), 15219 (GpxImageEntry)
367     */
368    String getIptcObjectName();
369
370    /**
371     * Extract GPS metadata from image EXIF. Has no effect if the image file is not set
372     * <p>
373     * If successful, fills in the LatLon, speed, elevation, image direction, and other attributes
374     * @since 18592 (interface), 9270 (GpxImageEntry)
375     */
376    default void extractExif() {
377        try (InputStream original = getInputStream();
378             BufferedInputStream bufferedInputStream = new BufferedInputStream(original)) {
379            ImageUtils.applyExif(this, bufferedInputStream);
380        } catch (IOException e) {
381            throw new UncheckedIOException(e);
382        } catch (IllegalArgumentException | FileSystemNotFoundException e) {
383            throw new UncheckedIOException(new IOException(e));
384        }
385    }
386
387    /**
388     * Get the image input stream
389     * @return The input stream of the image
390     * @throws IOException If something happens during image read. See implementation for details.
391     */
392    InputStream getInputStream() throws IOException;
393
394    /**
395     * Get the projection type for this entry
396     * @return The projection type
397     * @since 18592 (interface), 18246 (GpxImageEntry)
398     */
399    Projections getProjectionType();
400
401    /**
402     * Set the new projection type
403     * @param newProjection The new type
404     */
405    void setProjectionType(Projections newProjection);
406}