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