001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.geoimage;
003
004import java.io.BufferedInputStream;
005import java.io.File;
006import java.io.IOException;
007import java.io.InputStream;
008import java.io.UncheckedIOException;
009import java.net.MalformedURLException;
010import java.net.URI;
011import java.nio.file.Files;
012import java.nio.file.Paths;
013import java.time.Instant;
014import java.util.Collections;
015import java.util.List;
016import java.util.Objects;
017import java.util.function.Supplier;
018
019import org.openstreetmap.josm.data.coor.ILatLon;
020import org.openstreetmap.josm.data.gpx.TimeSource;
021import org.openstreetmap.josm.data.imagery.street_level.IImageEntry;
022import org.openstreetmap.josm.data.imagery.street_level.Projections;
023import org.openstreetmap.josm.tools.HttpClient;
024import org.openstreetmap.josm.tools.JosmRuntimeException;
025
026/**
027 * A remote image entry
028 * @since 18592
029 */
030public class RemoteEntry implements IImageEntry<RemoteEntry>, ImageMetadata {
031    private final URI uri;
032    private final Supplier<RemoteEntry> firstImage;
033    private final Supplier<RemoteEntry> nextImage;
034    private final Supplier<RemoteEntry> previousImage;
035    private final Supplier<RemoteEntry> lastImage;
036    private int width;
037    private int height;
038    private ILatLon pos;
039    private Integer exifOrientation;
040    private Double elevation;
041    private Integer gpsDiffMode;
042    private Integer gps2d3dMode;
043    private Double exifHPosErr;
044    private Double exifGpsDop;
045    private String exifGpsDatum;
046    private String exifGpsProcMethod;
047    private Double speed;
048    private Double exifImgDir;
049    private Double exifGpsTrack;
050    private ILatLon exifCoor;
051    private Instant exifTime;
052    private Instant exifGpsTime;
053    private Instant gpsTime;
054    private String iptcObjectName;
055    private List<String> iptcKeywords;
056    private String iptcHeadline;
057    private String iptcCaption;
058    private Projections projection;
059    private String title;
060
061    /**
062     * Create a new remote entry
063     * @param uri The URI to use
064     * @param firstImage first image supplier
065     * @param nextImage next image supplier
066     * @param lastImage last image supplier
067     * @param previousImage previous image supplier
068     */
069    public RemoteEntry(URI uri, Supplier<RemoteEntry> firstImage, Supplier<RemoteEntry> previousImage,
070                       Supplier<RemoteEntry> nextImage, Supplier<RemoteEntry> lastImage) {
071        Objects.requireNonNull(uri);
072        Objects.requireNonNull(firstImage);
073        Objects.requireNonNull(previousImage);
074        Objects.requireNonNull(nextImage);
075        Objects.requireNonNull(lastImage);
076        this.uri = uri;
077        this.firstImage = firstImage;
078        this.previousImage = previousImage;
079        this.nextImage = nextImage;
080        this.lastImage = lastImage;
081    }
082
083    @Override
084    public RemoteEntry getNextImage() {
085        return this.nextImage.get();
086    }
087
088    @Override
089    public RemoteEntry getPreviousImage() {
090        return this.previousImage.get();
091    }
092
093    @Override
094    public RemoteEntry getFirstImage() {
095        return this.firstImage.get();
096    }
097
098    @Override
099    public RemoteEntry getLastImage() {
100        return this.lastImage.get();
101    }
102
103    @Override
104    public String getDisplayName() {
105        return this.title == null ? this.getImageURI().toString() : this.title;
106    }
107
108    @Override
109    public void setWidth(int width) {
110        this.width = width;
111    }
112
113    @Override
114    public void setHeight(int height) {
115        this.height = height;
116    }
117
118    @Override
119    public void setPos(ILatLon pos) {
120        this.pos = pos;
121    }
122
123    @Override
124    public void setSpeed(Double speed) {
125        this.speed = speed;
126    }
127
128    /**
129     * @since 19387
130     */
131    @Override
132    public void setGpsDiffMode(Integer gpsDiffMode) {
133        this.gpsDiffMode = gpsDiffMode;
134    }
135
136    /**
137     * @since 19387
138     */
139    @Override
140    public void setGps2d3dMode(Integer gps2d3dMode) {
141        this.gps2d3dMode = gps2d3dMode;
142    }
143
144    /**
145     * @since 19387
146     */
147    @Override
148    public void setExifGpsDatum(String exifGpsDatum) {
149        this.exifGpsDatum = exifGpsDatum;
150    }
151
152    /**
153     * @since 19387
154     */
155    @Override
156    public void setExifGpsProcMethod(String exifGpsProcMethod) {
157        this.exifGpsProcMethod = exifGpsProcMethod;
158    }
159
160    @Override
161    public void setElevation(Double elevation) {
162        this.elevation = elevation;
163    }
164
165    @Override
166    public void setExifOrientation(Integer exifOrientation) {
167        this.exifOrientation = exifOrientation;
168    }
169
170    @Override
171    public void setExifTime(Instant exifTime) {
172        this.exifTime = exifTime;
173    }
174
175    @Override
176    public void setExifGpsTime(Instant exifGpsTime) {
177        this.exifGpsTime = exifGpsTime;
178    }
179
180    @Override
181    public void setGpsTime(Instant gpsTime) {
182        this.gpsTime = gpsTime;
183    }
184
185    @Override
186    public void setExifCoor(ILatLon exifCoor) {
187        this.exifCoor = exifCoor;
188    }
189
190    @Override
191    public void setExifImgDir(Double exifDir) {
192        this.exifImgDir = exifDir;
193    }
194
195    /**
196     * @since 19387
197     */
198    @Override
199    public void setExifGpsTrack(Double exifGpsTrack) {
200        this.exifGpsTrack = exifGpsTrack;
201    }
202
203    /**
204     * @since 19387
205     */
206    @Override
207    public void setExifHPosErr(Double exifHPosError) {
208        this.exifHPosErr = exifHPosError;
209    }
210
211    /**
212     * @since 19387
213     */
214    @Override
215    public void setExifGpsDop(Double exifGpsDop) {
216        this.exifGpsDop = exifGpsDop;
217    }
218
219    @Override
220    public void setIptcCaption(String iptcCaption) {
221        this.iptcCaption = iptcCaption;
222    }
223
224    @Override
225    public void setIptcHeadline(String iptcHeadline) {
226        this.iptcHeadline = iptcHeadline;
227    }
228
229    @Override
230    public void setIptcKeywords(List<String> iptcKeywords) {
231        this.iptcKeywords = iptcKeywords;
232    }
233
234    @Override
235    public void setIptcObjectName(String iptcObjectName) {
236        this.iptcObjectName = iptcObjectName;
237    }
238
239    @Override
240    public Integer getExifOrientation() {
241        return this.exifOrientation != null ? this.exifOrientation : 1;
242    }
243
244    @Override
245    public File getFile() {
246        return null;
247    }
248
249    @Override
250    public URI getImageURI() {
251        return this.uri;
252    }
253
254    @Override
255    public int getWidth() {
256        return this.width;
257    }
258
259    @Override
260    public int getHeight() {
261        return this.height;
262    }
263
264    @Override
265    public ILatLon getPos() {
266        return this.pos;
267    }
268
269    @Override
270    public Double getSpeed() {
271        return this.speed;
272    }
273
274    @Override
275    public Double getElevation() {
276        return this.elevation;
277    }
278
279    /**
280     * @since 19387
281     */
282    @Override
283    public Integer getGpsDiffMode() {
284        return this.gpsDiffMode;
285    }
286    
287    /**
288     * @since 19387
289     */
290    @Override
291    public Integer getGps2d3dMode() {
292        return this.gps2d3dMode;
293    }
294
295    /**
296     * @since 19387
297     */
298    @Override
299    public String getExifGpsDatum() {
300        return this.exifGpsDatum;
301    }
302
303    /**
304     * @since 19387
305     */
306    @Override
307    public String getExifGpsProcMethod() {
308        return this.exifGpsProcMethod;
309    }
310
311    /**
312     * @since 19387
313     */
314    @Override
315    public Double getExifImgDir() {
316        return this.exifImgDir;
317    }
318
319    /**
320     * @since 19387
321     */
322    @Override
323    public Double getExifGpsTrack() {
324        return this.exifGpsTrack;
325    }
326
327    /**
328     * @since 19387
329     */
330    @Override
331    public Double getExifHPosErr() {
332        return this.exifHPosErr;
333    }
334
335    /**
336     * @since 19387
337     */
338    @Override
339    public Double getExifGpsDop() {
340        return this.exifGpsDop;
341    }
342
343    @Override
344    public Instant getLastModified() {
345        if ("file".equals(this.getImageURI().getScheme())) {
346            try {
347                return Files.getLastModifiedTime(Paths.get(this.getImageURI())).toInstant();
348            } catch (IOException e) {
349                throw new UncheckedIOException(e);
350            }
351        }
352        try {
353            return Instant.ofEpochMilli(HttpClient.create(this.getImageURI().toURL(), "HEAD").getResponse().getLastModified());
354        } catch (MalformedURLException e) {
355            throw new JosmRuntimeException(e);
356        }
357    }
358
359    @Override
360    public boolean hasExifTime() {
361        return this.exifTime != null;
362    }
363
364    @Override
365    public Instant getTimeSourceInstant(TimeSource timeSource) {
366        return this.getTimeSourceInstant(timeSource);
367    }
368
369    @Override
370    public Instant getExifGpsInstant() {
371        return this.exifGpsTime;
372    }
373
374    @Override
375    public boolean hasExifGpsTime() {
376        return this.exifGpsTime != null;
377    }
378
379    @Override
380    public ILatLon getExifCoor() {
381        return this.exifCoor;
382    }
383
384    @Override
385    public Instant getExifInstant() {
386        return this.exifTime;
387    }
388
389    @Override
390    public boolean hasGpsTime() {
391        return this.gpsTime != null;
392    }
393
394    @Override
395    public Instant getGpsInstant() {
396        return this.gpsTime;
397    }
398
399    @Override
400    public String getIptcCaption() {
401        return this.iptcCaption;
402    }
403
404    @Override
405    public String getIptcHeadline() {
406        return this.iptcHeadline;
407    }
408
409    @Override
410    public List<String> getIptcKeywords() {
411        return this.iptcKeywords;
412    }
413
414    @Override
415    public String getIptcObjectName() {
416        return this.iptcObjectName;
417    }
418
419    @Override
420    public Projections getProjectionType() {
421        return this.projection;
422    }
423
424    @Override
425    public InputStream getInputStream() throws IOException {
426        final URI u = getImageURI();
427        if (u.getScheme().contains("file")) {
428            return Files.newInputStream(Paths.get(u));
429        }
430        final HttpClient client = HttpClient.create(u.toURL());
431        InputStream actual = client.connect().getContent();
432        return new BufferedInputStream(actual) {
433            @Override
434            public void close() throws IOException {
435                try {
436                    super.close();
437                } finally {
438                    client.disconnect();
439                }
440            }
441        };
442    }
443
444    @Override
445    public void selectImage(ImageViewerDialog imageViewerDialog, IImageEntry<?> entry) {
446        imageViewerDialog.displayImages(Collections.singletonList(entry));
447    }
448
449    @Override
450    public void setProjectionType(Projections newProjection) {
451        this.projection = newProjection;
452    }
453
454    /**
455     * Set the display name for this entry
456     * @param text The display name
457     */
458    public void setDisplayName(String text) {
459        this.title = text;
460    }
461
462    @Override
463    public int hashCode() {
464        return Objects.hash(this.uri, this.pos,
465                this.exifOrientation, this.elevation, this.speed, this.exifImgDir,
466                this.exifGpsTrack, this.exifCoor, this.exifTime, this.exifGpsTime,
467                this.gpsTime, this.exifHPosErr, this.exifGpsDop, this.gpsDiffMode,
468                this.gps2d3dMode, this.exifGpsDatum, this.exifGpsProcMethod,
469                this.iptcObjectName, this.iptcCaption, this.iptcHeadline,
470                this.iptcKeywords, this.projection, this.title);
471    }
472
473    @Override
474    public boolean equals(Object obj) {
475        if (this == obj) {
476            return true;
477        }
478        if (obj != null && obj.getClass() == this.getClass()) {
479            RemoteEntry other = this.getClass().cast(obj);
480            return Objects.equals(this.uri, other.uri)
481                    && Objects.equals(this.elevation, other.elevation)
482                    && Objects.equals(this.exifCoor, other.exifCoor)
483                    && Objects.equals(this.exifGpsTime, other.exifGpsTime)
484                    && Objects.equals(this.exifImgDir, other.exifImgDir)
485                    && Objects.equals(this.exifGpsTrack, other.exifGpsTrack)
486                    && Objects.equals(this.exifHPosErr, other.exifHPosErr)
487                    && Objects.equals(this.exifGpsDop, other.exifGpsDop)
488                    && Objects.equals(this.exifOrientation, other.exifOrientation)
489                    && Objects.equals(this.exifTime, other.exifTime)
490                    && Objects.equals(this.gpsTime, other.gpsTime)
491                    && Objects.equals(this.iptcCaption, other.iptcCaption)
492                    && Objects.equals(this.iptcHeadline, other.iptcHeadline)
493                    && Objects.equals(this.iptcKeywords, other.iptcKeywords)
494                    && Objects.equals(this.iptcObjectName, other.iptcObjectName)
495                    && Objects.equals(this.pos, other.pos)
496                    && Objects.equals(this.projection, other.projection)
497                    && Objects.equals(this.speed, other.speed)
498                    && Objects.equals(this.gpsDiffMode, other.gpsDiffMode)
499                    && Objects.equals(this.gps2d3dMode, other.gps2d3dMode)
500                    && Objects.equals(this.exifGpsDatum, other.exifGpsDatum)
501                    && Objects.equals(this.exifGpsProcMethod, other.exifGpsProcMethod)
502                    && Objects.equals(this.title, other.title);
503        }
504        return false;
505    }
506}