1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
---|
3 |
|
---|
4 | import java.io.BufferedInputStream;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.UncheckedIOException;
|
---|
9 | import java.net.MalformedURLException;
|
---|
10 | import java.net.URI;
|
---|
11 | import java.nio.file.Files;
|
---|
12 | import java.nio.file.Paths;
|
---|
13 | import java.time.Instant;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Objects;
|
---|
17 | import java.util.function.Supplier;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
20 | import org.openstreetmap.josm.data.imagery.street_level.IImageEntry;
|
---|
21 | import org.openstreetmap.josm.data.imagery.street_level.Projections;
|
---|
22 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
23 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * A remote image entry
|
---|
27 | * @since 18592
|
---|
28 | */
|
---|
29 | public class RemoteEntry implements IImageEntry<RemoteEntry>, ImageMetadata {
|
---|
30 | private final URI uri;
|
---|
31 | private final Supplier<RemoteEntry> firstImage;
|
---|
32 | private final Supplier<RemoteEntry> nextImage;
|
---|
33 | private final Supplier<RemoteEntry> previousImage;
|
---|
34 | private final Supplier<RemoteEntry> lastImage;
|
---|
35 | private int width;
|
---|
36 | private int height;
|
---|
37 | private ILatLon pos;
|
---|
38 | private Integer exifOrientation;
|
---|
39 | private Double elevation;
|
---|
40 | private Double speed;
|
---|
41 | private Double exifImgDir;
|
---|
42 | private ILatLon exifCoor;
|
---|
43 | private Instant exifTime;
|
---|
44 | private Instant exifGpsTime;
|
---|
45 | private Instant gpsTime;
|
---|
46 | private String iptcObjectName;
|
---|
47 | private List<String> iptcKeywords;
|
---|
48 | private String iptcHeadline;
|
---|
49 | private String iptcCaption;
|
---|
50 | private Projections projection;
|
---|
51 | private String title;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Create a new remote entry
|
---|
55 | * @param uri The URI to use
|
---|
56 | * @param firstImage first image supplier
|
---|
57 | * @param nextImage next image supplier
|
---|
58 | * @param lastImage last image supplier
|
---|
59 | * @param previousImage previous image supplier
|
---|
60 | */
|
---|
61 | public RemoteEntry(URI uri, Supplier<RemoteEntry> firstImage, Supplier<RemoteEntry> previousImage,
|
---|
62 | Supplier<RemoteEntry> nextImage, Supplier<RemoteEntry> lastImage) {
|
---|
63 | Objects.requireNonNull(uri);
|
---|
64 | Objects.requireNonNull(firstImage);
|
---|
65 | Objects.requireNonNull(previousImage);
|
---|
66 | Objects.requireNonNull(nextImage);
|
---|
67 | Objects.requireNonNull(lastImage);
|
---|
68 | this.uri = uri;
|
---|
69 | this.firstImage = firstImage;
|
---|
70 | this.previousImage = previousImage;
|
---|
71 | this.nextImage = nextImage;
|
---|
72 | this.lastImage = lastImage;
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public RemoteEntry getNextImage() {
|
---|
77 | return this.nextImage.get();
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public RemoteEntry getPreviousImage() {
|
---|
82 | return this.previousImage.get();
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public RemoteEntry getFirstImage() {
|
---|
87 | return this.firstImage.get();
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public RemoteEntry getLastImage() {
|
---|
92 | return this.lastImage.get();
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public String getDisplayName() {
|
---|
97 | return this.title == null ? this.getImageURI().toString() : this.title;
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public void setWidth(int width) {
|
---|
102 | this.width = width;
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public void setHeight(int height) {
|
---|
107 | this.height = height;
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public void setPos(ILatLon pos) {
|
---|
112 | this.pos = pos;
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public void setSpeed(Double speed) {
|
---|
117 | this.speed = speed;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void setElevation(Double elevation) {
|
---|
122 | this.elevation = elevation;
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public void setExifOrientation(Integer exifOrientation) {
|
---|
127 | this.exifOrientation = exifOrientation;
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public void setExifTime(Instant exifTime) {
|
---|
132 | this.exifTime = exifTime;
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public void setExifGpsTime(Instant exifGpsTime) {
|
---|
137 | this.exifGpsTime = exifGpsTime;
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public void setGpsTime(Instant gpsTime) {
|
---|
142 | this.gpsTime = gpsTime;
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public void setExifCoor(ILatLon exifCoor) {
|
---|
147 | this.exifCoor = exifCoor;
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public void setExifImgDir(Double exifDir) {
|
---|
152 | this.exifImgDir = exifDir;
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public void setIptcCaption(String iptcCaption) {
|
---|
157 | this.iptcCaption = iptcCaption;
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public void setIptcHeadline(String iptcHeadline) {
|
---|
162 | this.iptcHeadline = iptcHeadline;
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public void setIptcKeywords(List<String> iptcKeywords) {
|
---|
167 | this.iptcKeywords = iptcKeywords;
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public void setIptcObjectName(String iptcObjectName) {
|
---|
172 | this.iptcObjectName = iptcObjectName;
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public Integer getExifOrientation() {
|
---|
177 | return this.exifOrientation != null ? this.exifOrientation : 1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Override
|
---|
181 | public File getFile() {
|
---|
182 | return null;
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public URI getImageURI() {
|
---|
187 | return this.uri;
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | public int getWidth() {
|
---|
192 | return this.width;
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public int getHeight() {
|
---|
197 | return this.height;
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public ILatLon getPos() {
|
---|
202 | return this.pos;
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public Double getSpeed() {
|
---|
207 | return this.speed;
|
---|
208 | }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | public Double getElevation() {
|
---|
212 | return this.elevation;
|
---|
213 | }
|
---|
214 |
|
---|
215 | @Override
|
---|
216 | public Double getExifImgDir() {
|
---|
217 | return this.exifImgDir;
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public Instant getLastModified() {
|
---|
222 | if ("file".equals(this.getImageURI().getScheme())) {
|
---|
223 | try {
|
---|
224 | return Files.getLastModifiedTime(Paths.get(this.getImageURI())).toInstant();
|
---|
225 | } catch (IOException e) {
|
---|
226 | throw new UncheckedIOException(e);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | try {
|
---|
230 | return Instant.ofEpochMilli(HttpClient.create(this.getImageURI().toURL(), "HEAD").getResponse().getLastModified());
|
---|
231 | } catch (MalformedURLException e) {
|
---|
232 | throw new JosmRuntimeException(e);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | public boolean hasExifTime() {
|
---|
238 | return this.exifTime != null;
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Override
|
---|
242 | public Instant getExifGpsInstant() {
|
---|
243 | return this.exifGpsTime;
|
---|
244 | }
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | public boolean hasExifGpsTime() {
|
---|
248 | return this.exifGpsTime != null;
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public ILatLon getExifCoor() {
|
---|
253 | return this.exifCoor;
|
---|
254 | }
|
---|
255 |
|
---|
256 | @Override
|
---|
257 | public Instant getExifInstant() {
|
---|
258 | return this.exifTime;
|
---|
259 | }
|
---|
260 |
|
---|
261 | @Override
|
---|
262 | public boolean hasGpsTime() {
|
---|
263 | return this.gpsTime != null;
|
---|
264 | }
|
---|
265 |
|
---|
266 | @Override
|
---|
267 | public Instant getGpsInstant() {
|
---|
268 | return this.gpsTime;
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Override
|
---|
272 | public String getIptcCaption() {
|
---|
273 | return this.iptcCaption;
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | public String getIptcHeadline() {
|
---|
278 | return this.iptcHeadline;
|
---|
279 | }
|
---|
280 |
|
---|
281 | @Override
|
---|
282 | public List<String> getIptcKeywords() {
|
---|
283 | return this.iptcKeywords;
|
---|
284 | }
|
---|
285 |
|
---|
286 | @Override
|
---|
287 | public String getIptcObjectName() {
|
---|
288 | return this.iptcObjectName;
|
---|
289 | }
|
---|
290 |
|
---|
291 | @Override
|
---|
292 | public Projections getProjectionType() {
|
---|
293 | return this.projection;
|
---|
294 | }
|
---|
295 |
|
---|
296 | @Override
|
---|
297 | public InputStream getInputStream() throws IOException {
|
---|
298 | final URI u = getImageURI();
|
---|
299 | if (u.getScheme().contains("file")) {
|
---|
300 | return Files.newInputStream(Paths.get(u));
|
---|
301 | }
|
---|
302 | final HttpClient client = HttpClient.create(u.toURL());
|
---|
303 | InputStream actual = client.connect().getContent();
|
---|
304 | return new BufferedInputStream(actual) {
|
---|
305 | @Override
|
---|
306 | public void close() throws IOException {
|
---|
307 | try {
|
---|
308 | super.close();
|
---|
309 | } finally {
|
---|
310 | client.disconnect();
|
---|
311 | }
|
---|
312 | }
|
---|
313 | };
|
---|
314 | }
|
---|
315 |
|
---|
316 | @Override
|
---|
317 | public void selectImage(ImageViewerDialog imageViewerDialog, IImageEntry<?> entry) {
|
---|
318 | imageViewerDialog.displayImages(Collections.singletonList(entry));
|
---|
319 | }
|
---|
320 |
|
---|
321 | @Override
|
---|
322 | public void setProjectionType(Projections newProjection) {
|
---|
323 | this.projection = newProjection;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Set the display name for this entry
|
---|
328 | * @param text The display name
|
---|
329 | */
|
---|
330 | public void setDisplayName(String text) {
|
---|
331 | this.title = text;
|
---|
332 | }
|
---|
333 |
|
---|
334 | @Override
|
---|
335 | public int hashCode() {
|
---|
336 | return Objects.hash(this.uri, this.pos,
|
---|
337 | this.exifOrientation, this.elevation, this.speed, this.exifImgDir,
|
---|
338 | this.exifCoor, this.exifTime, this.exifGpsTime, this.gpsTime,
|
---|
339 | this.iptcObjectName, this.iptcCaption, this.iptcHeadline, this.iptcKeywords,
|
---|
340 | this.projection, this.title);
|
---|
341 | }
|
---|
342 |
|
---|
343 | @Override
|
---|
344 | public boolean equals(Object obj) {
|
---|
345 | if (this == obj) {
|
---|
346 | return true;
|
---|
347 | }
|
---|
348 | if (obj != null && obj.getClass() == this.getClass()) {
|
---|
349 | RemoteEntry other = this.getClass().cast(obj);
|
---|
350 | return Objects.equals(this.uri, other.uri)
|
---|
351 | && Objects.equals(this.elevation, other.elevation)
|
---|
352 | && Objects.equals(this.exifCoor, other.exifCoor)
|
---|
353 | && Objects.equals(this.exifGpsTime, other.exifGpsTime)
|
---|
354 | && Objects.equals(this.exifImgDir, other.exifImgDir)
|
---|
355 | && Objects.equals(this.exifOrientation, other.exifOrientation)
|
---|
356 | && Objects.equals(this.exifTime, other.exifTime)
|
---|
357 | && Objects.equals(this.gpsTime, other.gpsTime)
|
---|
358 | && Objects.equals(this.iptcCaption, other.iptcCaption)
|
---|
359 | && Objects.equals(this.iptcHeadline, other.iptcHeadline)
|
---|
360 | && Objects.equals(this.iptcKeywords, other.iptcKeywords)
|
---|
361 | && Objects.equals(this.iptcObjectName, other.iptcObjectName)
|
---|
362 | && Objects.equals(this.pos, other.pos)
|
---|
363 | && Objects.equals(this.projection, other.projection)
|
---|
364 | && Objects.equals(this.speed, other.speed)
|
---|
365 | && Objects.equals(this.title, other.title);
|
---|
366 | }
|
---|
367 | return false;
|
---|
368 | }
|
---|
369 | }
|
---|