source: josm/trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java@ 13536

Last change on this file since 13536 was 13536, checked in by stoecker, 6 years ago

add possibility to change map ids (see #14655), add overlay flag for imagery

  • Property svn:eol-style set to native
File size: 46.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Image;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.List;
12import java.util.Locale;
13import java.util.Map;
14import java.util.Objects;
15import java.util.Set;
16import java.util.TreeSet;
17import java.util.regex.Matcher;
18import java.util.regex.Pattern;
19import java.util.stream.Collectors;
20
21import javax.swing.ImageIcon;
22
23import org.openstreetmap.gui.jmapviewer.interfaces.Attributed;
24import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
25import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTileSource;
26import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource.Mapnik;
27import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
28import org.openstreetmap.josm.data.Bounds;
29import org.openstreetmap.josm.data.StructUtils;
30import org.openstreetmap.josm.data.StructUtils.StructEntry;
31import org.openstreetmap.josm.io.Capabilities;
32import org.openstreetmap.josm.io.OsmApi;
33import org.openstreetmap.josm.spi.preferences.Config;
34import org.openstreetmap.josm.spi.preferences.IPreferences;
35import org.openstreetmap.josm.tools.CheckParameterUtil;
36import org.openstreetmap.josm.tools.ImageProvider;
37import org.openstreetmap.josm.tools.LanguageInfo;
38import org.openstreetmap.josm.tools.Logging;
39import org.openstreetmap.josm.tools.MultiMap;
40import org.openstreetmap.josm.tools.Utils;
41
42/**
43 * Class that stores info about an image background layer.
44 *
45 * @author Frederik Ramm
46 */
47public class ImageryInfo extends TileSourceInfo implements Comparable<ImageryInfo>, Attributed {
48
49 /**
50 * Type of imagery entry.
51 */
52 public enum ImageryType {
53 /** A WMS (Web Map Service) entry. **/
54 WMS("wms"),
55 /** A TMS (Tile Map Service) entry. **/
56 TMS("tms"),
57 /** TMS entry for Microsoft Bing. */
58 BING("bing"),
59 /** TMS entry for Russian company <a href="https://wiki.openstreetmap.org/wiki/WikiProject_Russia/kosmosnimki">ScanEx</a>. **/
60 SCANEX("scanex"),
61 /** A WMS endpoint entry only stores the WMS server info, without layer, which are chosen later by the user. **/
62 WMS_ENDPOINT("wms_endpoint"),
63 /** WMTS stores GetCapabilities URL. Does not store any information about the layer **/
64 WMTS("wmts");
65
66
67 private final String typeString;
68
69 ImageryType(String urlString) {
70 this.typeString = urlString;
71 }
72
73 /**
74 * Returns the unique string identifying this type.
75 * @return the unique string identifying this type
76 * @since 6690
77 */
78 public final String getTypeString() {
79 return typeString;
80 }
81
82 /**
83 * Returns the imagery type from the given type string.
84 * @param s The type string
85 * @return the imagery type matching the given type string
86 */
87 public static ImageryType fromString(String s) {
88 for (ImageryType type : ImageryType.values()) {
89 if (type.getTypeString().equals(s)) {
90 return type;
91 }
92 }
93 return null;
94 }
95 }
96
97 /**
98 * Multi-polygon bounds for imagery backgrounds.
99 * Used to display imagery coverage in preferences and to determine relevant imagery entries based on edit location.
100 */
101 public static class ImageryBounds extends Bounds {
102
103 /**
104 * Constructs a new {@code ImageryBounds} from string.
105 * @param asString The string containing the list of shapes defining this bounds
106 * @param separator The shape separator in the given string, usually a comma
107 */
108 public ImageryBounds(String asString, String separator) {
109 super(asString, separator);
110 }
111
112 private List<Shape> shapes = new ArrayList<>();
113
114 /**
115 * Adds a new shape to this bounds.
116 * @param shape The shape to add
117 */
118 public final void addShape(Shape shape) {
119 this.shapes.add(shape);
120 }
121
122 /**
123 * Sets the list of shapes defining this bounds.
124 * @param shapes The list of shapes defining this bounds.
125 */
126 public final void setShapes(List<Shape> shapes) {
127 this.shapes = shapes;
128 }
129
130 /**
131 * Returns the list of shapes defining this bounds.
132 * @return The list of shapes defining this bounds
133 */
134 public final List<Shape> getShapes() {
135 return shapes;
136 }
137
138 @Override
139 public int hashCode() {
140 return Objects.hash(super.hashCode(), shapes);
141 }
142
143 @Override
144 public boolean equals(Object o) {
145 if (this == o) return true;
146 if (o == null || getClass() != o.getClass()) return false;
147 if (!super.equals(o)) return false;
148 ImageryBounds that = (ImageryBounds) o;
149 return Objects.equals(shapes, that.shapes);
150 }
151 }
152
153 /** original name of the imagery entry in case of translation call, for multiple languages English when possible */
154 private String origName;
155 /** (original) language of the translated name entry */
156 private String langName;
157 /** whether this is a entry activated by default or not */
158 private boolean defaultEntry;
159 /** Whether this service requires a explicit EULA acceptance before it can be activated */
160 private String eulaAcceptanceRequired;
161 /** type of the imagery servics - WMS, TMS, ... */
162 private ImageryType imageryType = ImageryType.WMS;
163 private double pixelPerDegree;
164 /** maximum zoom level for TMS imagery */
165 private int defaultMaxZoom;
166 /** minimum zoom level for TMS imagery */
167 private int defaultMinZoom;
168 /** display bounds of imagery, displayed in prefs and used for automatic imagery selection */
169 private ImageryBounds bounds;
170 /** projections supported by WMS servers */
171 private List<String> serverProjections = Collections.emptyList();
172 /** description of the imagery entry, should contain notes what type of data it is */
173 private String description;
174 /** language of the description entry */
175 private String langDescription;
176 /** Text of a text attribution displayed when using the imagery */
177 private String attributionText;
178 /** Link to a reference stating the permission for OSM usage */
179 private String permissionReferenceURL;
180 /** Link behind the text attribution displayed when using the imagery */
181 private String attributionLinkURL;
182 /** Image of a graphical attribution displayed when using the imagery */
183 private String attributionImage;
184 /** Link behind the graphical attribution displayed when using the imagery */
185 private String attributionImageURL;
186 /** Text with usage terms displayed when using the imagery */
187 private String termsOfUseText;
188 /** Link behind the text with usage terms displayed when using the imagery */
189 private String termsOfUseURL;
190 /** country code of the imagery (for country specific imagery) */
191 private String countryCode = "";
192 /**
193 * creation date of the imagery (in the form YYYY-MM-DD;YYYY-MM-DD, where
194 * DD and MM as well as a second date are optional)
195 * @since 11570
196 */
197 private String date;
198 /**
199 * marked as best in other editors
200 * @since 11575
201 */
202 private boolean bestMarked;
203 /**
204 * marked as overlay
205 * @since 13536
206 */
207 private boolean overlay;
208 /**
209 * list of old IDs, only for loading, not handled anywhere else
210 * @since 13536
211 */
212 private Collection<String> oldIds;
213 /** mirrors of different type for this entry */
214 private List<ImageryInfo> mirrors;
215 /** icon used in menu */
216 private String icon;
217 /** is the geo reference correct - don't offer offset handling */
218 private boolean isGeoreferenceValid;
219 /** which layers should be activated by default on layer addition. **/
220 private Collection<DefaultLayer> defaultLayers = Collections.emptyList();
221 // when adding a field, also adapt the ImageryInfo(ImageryInfo)
222 // and ImageryInfo(ImageryPreferenceEntry) constructor, equals method, and ImageryPreferenceEntry
223
224 /**
225 * Auxiliary class to save an {@link ImageryInfo} object in the preferences.
226 */
227 public static class ImageryPreferenceEntry {
228 @StructEntry String name;
229 @StructEntry String d;
230 @StructEntry String id;
231 @StructEntry String type;
232 @StructEntry String url;
233 @StructEntry double pixel_per_eastnorth;
234 @StructEntry String eula;
235 @StructEntry String attribution_text;
236 @StructEntry String attribution_url;
237 @StructEntry String permission_reference_url;
238 @StructEntry String logo_image;
239 @StructEntry String logo_url;
240 @StructEntry String terms_of_use_text;
241 @StructEntry String terms_of_use_url;
242 @StructEntry String country_code = "";
243 @StructEntry String date;
244 @StructEntry int max_zoom;
245 @StructEntry int min_zoom;
246 @StructEntry String cookies;
247 @StructEntry String bounds;
248 @StructEntry String shapes;
249 @StructEntry String projections;
250 @StructEntry String icon;
251 @StructEntry String description;
252 @StructEntry MultiMap<String, String> noTileHeaders;
253 @StructEntry MultiMap<String, String> noTileChecksums;
254 @StructEntry int tileSize = -1;
255 @StructEntry Map<String, String> metadataHeaders;
256 @StructEntry boolean valid_georeference;
257 @StructEntry boolean bestMarked;
258 @StructEntry boolean modTileFeatures;
259 @StructEntry boolean overlay;
260 // TODO: disabled until change of layers is implemented
261 // @StructEntry String default_layers;
262
263 /**
264 * Constructs a new empty WMS {@code ImageryPreferenceEntry}.
265 */
266 public ImageryPreferenceEntry() {
267 // Do nothing
268 }
269
270 /**
271 * Constructs a new {@code ImageryPreferenceEntry} from a given {@code ImageryInfo}.
272 * @param i The corresponding imagery info
273 */
274 public ImageryPreferenceEntry(ImageryInfo i) {
275 name = i.name;
276 id = i.id;
277 type = i.imageryType.getTypeString();
278 url = i.url;
279 pixel_per_eastnorth = i.pixelPerDegree;
280 eula = i.eulaAcceptanceRequired;
281 attribution_text = i.attributionText;
282 attribution_url = i.attributionLinkURL;
283 permission_reference_url = i.permissionReferenceURL;
284 date = i.date;
285 bestMarked = i.bestMarked;
286 overlay = i.overlay;
287 logo_image = i.attributionImage;
288 logo_url = i.attributionImageURL;
289 terms_of_use_text = i.termsOfUseText;
290 terms_of_use_url = i.termsOfUseURL;
291 country_code = i.countryCode;
292 max_zoom = i.defaultMaxZoom;
293 min_zoom = i.defaultMinZoom;
294 cookies = i.cookies;
295 icon = i.icon;
296 description = i.description;
297 if (i.bounds != null) {
298 bounds = i.bounds.encodeAsString(",");
299 StringBuilder shapesString = new StringBuilder();
300 for (Shape s : i.bounds.getShapes()) {
301 if (shapesString.length() > 0) {
302 shapesString.append(';');
303 }
304 shapesString.append(s.encodeAsString(","));
305 }
306 if (shapesString.length() > 0) {
307 shapes = shapesString.toString();
308 }
309 }
310 projections = i.serverProjections.stream().collect(Collectors.joining(","));
311 if (i.noTileHeaders != null && !i.noTileHeaders.isEmpty()) {
312 noTileHeaders = new MultiMap<>(i.noTileHeaders);
313 }
314
315 if (i.noTileChecksums != null && !i.noTileChecksums.isEmpty()) {
316 noTileChecksums = new MultiMap<>(i.noTileChecksums);
317 }
318
319 if (i.metadataHeaders != null && !i.metadataHeaders.isEmpty()) {
320 metadataHeaders = i.metadataHeaders;
321 }
322
323 tileSize = i.getTileSize();
324
325 valid_georeference = i.isGeoreferenceValid();
326 modTileFeatures = i.isModTileFeatures();
327 // TODO disabled until change of layers is implemented
328 // default_layers = i.defaultLayers.stream().collect(Collectors.joining(","));
329 }
330
331 @Override
332 public String toString() {
333 StringBuilder s = new StringBuilder("ImageryPreferenceEntry [name=").append(name);
334 if (id != null) {
335 s.append(" id=").append(id);
336 }
337 s.append(']');
338 return s.toString();
339 }
340 }
341
342 /**
343 * Constructs a new WMS {@code ImageryInfo}.
344 */
345 public ImageryInfo() {
346 super();
347 }
348
349 /**
350 * Constructs a new WMS {@code ImageryInfo} with a given name.
351 * @param name The entry name
352 */
353 public ImageryInfo(String name) {
354 super(name);
355 }
356
357 /**
358 * Constructs a new WMS {@code ImageryInfo} with given name and extended URL.
359 * @param name The entry name
360 * @param url The entry extended URL
361 */
362 public ImageryInfo(String name, String url) {
363 this(name);
364 setExtendedUrl(url);
365 }
366
367 /**
368 * Constructs a new WMS {@code ImageryInfo} with given name, extended and EULA URLs.
369 * @param name The entry name
370 * @param url The entry URL
371 * @param eulaAcceptanceRequired The EULA URL
372 */
373 public ImageryInfo(String name, String url, String eulaAcceptanceRequired) {
374 this(name);
375 setExtendedUrl(url);
376 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
377 }
378
379 /**
380 * Constructs a new {@code ImageryInfo} with given name, url, extended and EULA URLs.
381 * @param name The entry name
382 * @param url The entry URL
383 * @param type The entry imagery type. If null, WMS will be used as default
384 * @param eulaAcceptanceRequired The EULA URL
385 * @param cookies The data part of HTTP cookies header in case the service requires cookies to work
386 * @throws IllegalArgumentException if type refers to an unknown imagery type
387 */
388 public ImageryInfo(String name, String url, String type, String eulaAcceptanceRequired, String cookies) {
389 this(name);
390 setExtendedUrl(url);
391 ImageryType t = ImageryType.fromString(type);
392 this.cookies = cookies;
393 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
394 if (t != null) {
395 this.imageryType = t;
396 } else if (type != null && !type.isEmpty()) {
397 throw new IllegalArgumentException("unknown type: "+type);
398 }
399 }
400
401 /**
402 * Constructs a new {@code ImageryInfo} with given name, url, id, extended and EULA URLs.
403 * @param name The entry name
404 * @param url The entry URL
405 * @param type The entry imagery type. If null, WMS will be used as default
406 * @param eulaAcceptanceRequired The EULA URL
407 * @param cookies The data part of HTTP cookies header in case the service requires cookies to work
408 * @param id tile id
409 * @throws IllegalArgumentException if type refers to an unknown imagery type
410 */
411 public ImageryInfo(String name, String url, String type, String eulaAcceptanceRequired, String cookies, String id) {
412 this(name, url, type, eulaAcceptanceRequired, cookies);
413 setId(id);
414 }
415
416 /**
417 * Constructs a new {@code ImageryInfo} from an imagery preference entry.
418 * @param e The imagery preference entry
419 */
420 public ImageryInfo(ImageryPreferenceEntry e) {
421 super(e.name, e.url, e.id);
422 CheckParameterUtil.ensureParameterNotNull(e.name, "name");
423 CheckParameterUtil.ensureParameterNotNull(e.url, "url");
424 description = e.description;
425 cookies = e.cookies;
426 eulaAcceptanceRequired = e.eula;
427 imageryType = ImageryType.fromString(e.type);
428 if (imageryType == null) throw new IllegalArgumentException("unknown type");
429 pixelPerDegree = e.pixel_per_eastnorth;
430 defaultMaxZoom = e.max_zoom;
431 defaultMinZoom = e.min_zoom;
432 if (e.bounds != null) {
433 bounds = new ImageryBounds(e.bounds, ",");
434 if (e.shapes != null) {
435 try {
436 for (String s : e.shapes.split(";")) {
437 bounds.addShape(new Shape(s, ","));
438 }
439 } catch (IllegalArgumentException ex) {
440 Logging.warn(ex);
441 }
442 }
443 }
444 if (e.projections != null && !e.projections.isEmpty()) {
445 // split generates null element on empty string which gives one element Array[null]
446 serverProjections = Arrays.asList(e.projections.split(","));
447 }
448 attributionText = e.attribution_text;
449 attributionLinkURL = e.attribution_url;
450 permissionReferenceURL = e.permission_reference_url;
451 attributionImage = e.logo_image;
452 attributionImageURL = e.logo_url;
453 date = e.date;
454 bestMarked = e.bestMarked;
455 overlay = e.overlay;
456 termsOfUseText = e.terms_of_use_text;
457 termsOfUseURL = e.terms_of_use_url;
458 countryCode = e.country_code;
459 icon = e.icon;
460 if (e.noTileHeaders != null) {
461 noTileHeaders = e.noTileHeaders.toMap();
462 }
463 if (e.noTileChecksums != null) {
464 noTileChecksums = e.noTileChecksums.toMap();
465 }
466 setTileSize(e.tileSize);
467 metadataHeaders = e.metadataHeaders;
468 isGeoreferenceValid = e.valid_georeference;
469 modTileFeatures = e.modTileFeatures;
470 // TODO disabled until change of layers is implemented
471 // defaultLayers = Arrays.asList(e.default_layers.split(","));
472 }
473
474 /**
475 * Constructs a new {@code ImageryInfo} from an existing one.
476 * @param i The other imagery info
477 */
478 public ImageryInfo(ImageryInfo i) {
479 super(i.name, i.url, i.id);
480 this.noTileHeaders = i.noTileHeaders;
481 this.noTileChecksums = i.noTileChecksums;
482 this.minZoom = i.minZoom;
483 this.maxZoom = i.maxZoom;
484 this.cookies = i.cookies;
485 this.tileSize = i.tileSize;
486 this.metadataHeaders = i.metadataHeaders;
487 this.modTileFeatures = i.modTileFeatures;
488
489 this.origName = i.origName;
490 this.langName = i.langName;
491 this.defaultEntry = i.defaultEntry;
492 this.eulaAcceptanceRequired = null;
493 this.imageryType = i.imageryType;
494 this.pixelPerDegree = i.pixelPerDegree;
495 this.defaultMaxZoom = i.defaultMaxZoom;
496 this.defaultMinZoom = i.defaultMinZoom;
497 this.bounds = i.bounds;
498 this.serverProjections = i.serverProjections;
499 this.description = i.description;
500 this.langDescription = i.langDescription;
501 this.attributionText = i.attributionText;
502 this.permissionReferenceURL = i.permissionReferenceURL;
503 this.attributionLinkURL = i.attributionLinkURL;
504 this.attributionImage = i.attributionImage;
505 this.attributionImageURL = i.attributionImageURL;
506 this.termsOfUseText = i.termsOfUseText;
507 this.termsOfUseURL = i.termsOfUseURL;
508 this.countryCode = i.countryCode;
509 this.date = i.date;
510 this.bestMarked = i.bestMarked;
511 this.overlay = i.overlay;
512 // do not copy field {@code mirrors}
513 this.icon = i.icon;
514 this.isGeoreferenceValid = i.isGeoreferenceValid;
515 this.defaultLayers = i.defaultLayers;
516 }
517
518 @Override
519 public int hashCode() {
520 return Objects.hash(url, imageryType);
521 }
522
523 /**
524 * Check if this object equals another ImageryInfo with respect to the properties
525 * that get written to the preference file.
526 *
527 * The field {@link #pixelPerDegree} is ignored.
528 *
529 * @param other the ImageryInfo object to compare to
530 * @return true if they are equal
531 */
532 public boolean equalsPref(ImageryInfo other) {
533 if (other == null) {
534 return false;
535 }
536
537 // CHECKSTYLE.OFF: BooleanExpressionComplexity
538 return
539 Objects.equals(this.name, other.name) &&
540 Objects.equals(this.id, other.id) &&
541 Objects.equals(this.url, other.url) &&
542 Objects.equals(this.modTileFeatures, other.modTileFeatures) &&
543 Objects.equals(this.bestMarked, other.bestMarked) &&
544 Objects.equals(this.overlay, other.overlay) &&
545 Objects.equals(this.isGeoreferenceValid, other.isGeoreferenceValid) &&
546 Objects.equals(this.cookies, other.cookies) &&
547 Objects.equals(this.eulaAcceptanceRequired, other.eulaAcceptanceRequired) &&
548 Objects.equals(this.imageryType, other.imageryType) &&
549 Objects.equals(this.defaultMaxZoom, other.defaultMaxZoom) &&
550 Objects.equals(this.defaultMinZoom, other.defaultMinZoom) &&
551 Objects.equals(this.bounds, other.bounds) &&
552 Objects.equals(this.serverProjections, other.serverProjections) &&
553 Objects.equals(this.attributionText, other.attributionText) &&
554 Objects.equals(this.attributionLinkURL, other.attributionLinkURL) &&
555 Objects.equals(this.permissionReferenceURL, other.permissionReferenceURL) &&
556 Objects.equals(this.attributionImageURL, other.attributionImageURL) &&
557 Objects.equals(this.attributionImage, other.attributionImage) &&
558 Objects.equals(this.termsOfUseText, other.termsOfUseText) &&
559 Objects.equals(this.termsOfUseURL, other.termsOfUseURL) &&
560 Objects.equals(this.countryCode, other.countryCode) &&
561 Objects.equals(this.date, other.date) &&
562 Objects.equals(this.icon, other.icon) &&
563 Objects.equals(this.description, other.description) &&
564 Objects.equals(this.noTileHeaders, other.noTileHeaders) &&
565 Objects.equals(this.noTileChecksums, other.noTileChecksums) &&
566 Objects.equals(this.metadataHeaders, other.metadataHeaders) &&
567 Objects.equals(this.defaultLayers, other.defaultLayers);
568 // CHECKSTYLE.ON: BooleanExpressionComplexity
569 }
570
571 @Override
572 public boolean equals(Object o) {
573 if (this == o) return true;
574 if (o == null || getClass() != o.getClass()) return false;
575 ImageryInfo that = (ImageryInfo) o;
576 return imageryType == that.imageryType && Objects.equals(url, that.url);
577 }
578
579 @Override
580 public String toString() {
581 return "ImageryInfo{" +
582 "name='" + name + '\'' +
583 ", countryCode='" + countryCode + '\'' +
584 ", url='" + url + '\'' +
585 ", imageryType=" + imageryType +
586 '}';
587 }
588
589 @Override
590 public int compareTo(ImageryInfo in) {
591 int i = countryCode.compareTo(in.countryCode);
592 if (i == 0) {
593 i = name.toLowerCase(Locale.ENGLISH).compareTo(in.name.toLowerCase(Locale.ENGLISH));
594 }
595 if (i == 0) {
596 i = url.compareTo(in.url);
597 }
598 if (i == 0) {
599 i = Double.compare(pixelPerDegree, in.pixelPerDegree);
600 }
601 return i;
602 }
603
604 /**
605 * Determines if URL is equal to given imagery info.
606 * @param in imagery info
607 * @return {@code true} if URL is equal to given imagery info
608 */
609 public boolean equalsBaseValues(ImageryInfo in) {
610 return url.equals(in.url);
611 }
612
613 /**
614 * Sets the pixel per degree value.
615 * @param ppd The ppd value
616 * @see #getPixelPerDegree()
617 */
618 public void setPixelPerDegree(double ppd) {
619 this.pixelPerDegree = ppd;
620 }
621
622 /**
623 * Sets the maximum zoom level.
624 * @param defaultMaxZoom The maximum zoom level
625 */
626 public void setDefaultMaxZoom(int defaultMaxZoom) {
627 this.defaultMaxZoom = defaultMaxZoom;
628 }
629
630 /**
631 * Sets the minimum zoom level.
632 * @param defaultMinZoom The minimum zoom level
633 */
634 public void setDefaultMinZoom(int defaultMinZoom) {
635 this.defaultMinZoom = defaultMinZoom;
636 }
637
638 /**
639 * Sets the imagery polygonial bounds.
640 * @param b The imagery bounds (non-rectangular)
641 */
642 public void setBounds(ImageryBounds b) {
643 this.bounds = b;
644 }
645
646 /**
647 * Returns the imagery polygonial bounds.
648 * @return The imagery bounds (non-rectangular)
649 */
650 public ImageryBounds getBounds() {
651 return bounds;
652 }
653
654 @Override
655 public boolean requiresAttribution() {
656 return attributionText != null || attributionLinkURL != null || attributionImage != null
657 || termsOfUseText != null || termsOfUseURL != null;
658 }
659
660 @Override
661 public String getAttributionText(int zoom, ICoordinate topLeft, ICoordinate botRight) {
662 return attributionText;
663 }
664
665 @Override
666 public String getAttributionLinkURL() {
667 return attributionLinkURL;
668 }
669
670 /**
671 * Return the permission reference URL.
672 * @return The url
673 * @see #setPermissionReferenceURL
674 * @since 11975
675 */
676 public String getPermissionReferenceURL() {
677 return permissionReferenceURL;
678 }
679
680 @Override
681 public Image getAttributionImage() {
682 ImageIcon i = ImageProvider.getIfAvailable(attributionImage);
683 if (i != null) {
684 return i.getImage();
685 }
686 return null;
687 }
688
689 /**
690 * Return the raw attribution logo information (an URL to the image).
691 * @return The url text
692 * @since 12257
693 */
694 public String getAttributionImageRaw() {
695 return attributionImage;
696 }
697
698 @Override
699 public String getAttributionImageURL() {
700 return attributionImageURL;
701 }
702
703 @Override
704 public String getTermsOfUseText() {
705 return termsOfUseText;
706 }
707
708 @Override
709 public String getTermsOfUseURL() {
710 return termsOfUseURL;
711 }
712
713 /**
714 * Set the attribution text
715 * @param text The text
716 * @see #getAttributionText(int, ICoordinate, ICoordinate)
717 */
718 public void setAttributionText(String text) {
719 attributionText = text;
720 }
721
722 /**
723 * Set the attribution image
724 * @param url The url of the image.
725 * @see #getAttributionImageURL()
726 */
727 public void setAttributionImageURL(String url) {
728 attributionImageURL = url;
729 }
730
731 /**
732 * Set the image for the attribution
733 * @param res The image resource
734 * @see #getAttributionImage()
735 */
736 public void setAttributionImage(String res) {
737 attributionImage = res;
738 }
739
740 /**
741 * Sets the URL the attribution should link to.
742 * @param url The url.
743 * @see #getAttributionLinkURL()
744 */
745 public void setAttributionLinkURL(String url) {
746 attributionLinkURL = url;
747 }
748
749 /**
750 * Sets the permission reference URL.
751 * @param url The url.
752 * @see #getPermissionReferenceURL()
753 * @since 11975
754 */
755 public void setPermissionReferenceURL(String url) {
756 permissionReferenceURL = url;
757 }
758
759 /**
760 * Sets the text to display to the user as terms of use.
761 * @param text The text
762 * @see #getTermsOfUseText()
763 */
764 public void setTermsOfUseText(String text) {
765 termsOfUseText = text;
766 }
767
768 /**
769 * Sets a url that links to the terms of use text.
770 * @param text The url.
771 * @see #getTermsOfUseURL()
772 */
773 public void setTermsOfUseURL(String text) {
774 termsOfUseURL = text;
775 }
776
777 /**
778 * Sets the extended URL of this entry.
779 * @param url Entry extended URL containing in addition of service URL, its type and min/max zoom info
780 */
781 public void setExtendedUrl(String url) {
782 CheckParameterUtil.ensureParameterNotNull(url);
783
784 // Default imagery type is WMS
785 this.url = url;
786 this.imageryType = ImageryType.WMS;
787
788 defaultMaxZoom = 0;
789 defaultMinZoom = 0;
790 for (ImageryType type : ImageryType.values()) {
791 Matcher m = Pattern.compile(type.getTypeString()+"(?:\\[(?:(\\d+)[,-])?(\\d+)\\])?:(.*)").matcher(url);
792 if (m.matches()) {
793 this.url = m.group(3);
794 this.imageryType = type;
795 if (m.group(2) != null) {
796 defaultMaxZoom = Integer.parseInt(m.group(2));
797 }
798 if (m.group(1) != null) {
799 defaultMinZoom = Integer.parseInt(m.group(1));
800 }
801 break;
802 }
803 }
804
805 if (serverProjections.isEmpty()) {
806 serverProjections = new ArrayList<>();
807 Matcher m = Pattern.compile(".*\\{PROJ\\(([^)}]+)\\)\\}.*").matcher(url.toUpperCase(Locale.ENGLISH));
808 if (m.matches()) {
809 for (String p : m.group(1).split(",")) {
810 serverProjections.add(p);
811 }
812 }
813 }
814 }
815
816 /**
817 * Returns the entry name.
818 * @return The entry name
819 * @since 6968
820 */
821 public String getOriginalName() {
822 return this.origName != null ? this.origName : this.name;
823 }
824
825 /**
826 * Sets the entry name and handle translation.
827 * @param language The used language
828 * @param name The entry name
829 * @since 8091
830 */
831 public void setName(String language, String name) {
832 boolean isdefault = LanguageInfo.getJOSMLocaleCode(null).equals(language);
833 if (LanguageInfo.isBetterLanguage(langName, language)) {
834 this.name = isdefault ? tr(name) : name;
835 this.langName = language;
836 }
837 if (origName == null || isdefault) {
838 this.origName = name;
839 }
840 }
841
842 /**
843 * Store the id of this info to the preferences and clear it afterwards.
844 */
845 public void clearId() {
846 if (this.id != null) {
847 Collection<String> newAddedIds = new TreeSet<>(Config.getPref().getList("imagery.layers.addedIds"));
848 newAddedIds.add(this.id);
849 Config.getPref().putList("imagery.layers.addedIds", new ArrayList<>(newAddedIds));
850 }
851 setId(null);
852 }
853
854 /**
855 * Determines if this entry is enabled by default.
856 * @return {@code true} if this entry is enabled by default, {@code false} otherwise
857 */
858 public boolean isDefaultEntry() {
859 return defaultEntry;
860 }
861
862 /**
863 * Sets the default state of this entry.
864 * @param defaultEntry {@code true} if this entry has to be enabled by default, {@code false} otherwise
865 */
866 public void setDefaultEntry(boolean defaultEntry) {
867 this.defaultEntry = defaultEntry;
868 }
869
870 /**
871 * Gets the pixel per degree value
872 * @return The ppd value.
873 */
874 public double getPixelPerDegree() {
875 return this.pixelPerDegree;
876 }
877
878 /**
879 * Returns the maximum zoom level.
880 * @return The maximum zoom level
881 */
882 @Override
883 public int getMaxZoom() {
884 return this.defaultMaxZoom;
885 }
886
887 /**
888 * Returns the minimum zoom level.
889 * @return The minimum zoom level
890 */
891 @Override
892 public int getMinZoom() {
893 return this.defaultMinZoom;
894 }
895
896 /**
897 * Returns the description text when existing.
898 * @return The description
899 * @since 8065
900 */
901 public String getDescription() {
902 return this.description;
903 }
904
905 /**
906 * Sets the description text when existing.
907 * @param language The used language
908 * @param description the imagery description text
909 * @since 8091
910 */
911 public void setDescription(String language, String description) {
912 boolean isdefault = LanguageInfo.getJOSMLocaleCode(null).equals(language);
913 if (LanguageInfo.isBetterLanguage(langDescription, language)) {
914 this.description = isdefault ? tr(description) : description;
915 this.langDescription = language;
916 }
917 }
918
919 /**
920 * Return the sorted list of activated Imagery IDs
921 * @since 13536
922 */
923 static public Collection<String> getActiveIds() {
924 ArrayList<String> ids = new ArrayList<String>();
925 IPreferences pref = Config.getPref();
926 if (pref != null) {
927 List<ImageryPreferenceEntry> entries = StructUtils.getListOfStructs(
928 pref, "imagery.entries", null, ImageryPreferenceEntry.class);
929 if (entries != null) {
930 for (ImageryPreferenceEntry prefEntry : entries) {
931 if(prefEntry.id != null && prefEntry.id.length() != 0)
932 ids.add(prefEntry.id);
933 }
934 Collections.sort(ids);
935 }
936 }
937 return ids;
938 }
939
940 /**
941 * Returns a tool tip text for display.
942 * @return The text
943 * @since 8065
944 */
945 public String getToolTipText() {
946 StringBuilder res = new StringBuilder(getName());
947 boolean html = false;
948 String dateStr = getDate();
949 if (dateStr != null && !dateStr.isEmpty()) {
950 res.append("<br>").append(tr("Date of imagery: {0}", dateStr));
951 html = true;
952 }
953 if (bestMarked) {
954 res.append("<br>").append(tr("This imagery is marked as best in this region in other editors."));
955 html = true;
956 }
957 if (overlay) {
958 res.append("<br>").append(tr("This imagery is an overlay."));
959 html = true;
960 }
961 String desc = getDescription();
962 if (desc != null && !desc.isEmpty()) {
963 res.append("<br>").append(Utils.escapeReservedCharactersHTML(desc));
964 html = true;
965 }
966 if (html) {
967 res.insert(0, "<html>").append("</html>");
968 }
969 return res.toString();
970 }
971
972 /**
973 * Returns the EULA acceptance URL, if any.
974 * @return The URL to an EULA text that has to be accepted before use, or {@code null}
975 */
976 public String getEulaAcceptanceRequired() {
977 return eulaAcceptanceRequired;
978 }
979
980 /**
981 * Sets the EULA acceptance URL.
982 * @param eulaAcceptanceRequired The URL to an EULA text that has to be accepted before use
983 */
984 public void setEulaAcceptanceRequired(String eulaAcceptanceRequired) {
985 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
986 }
987
988 /**
989 * Returns the ISO 3166-1-alpha-2 country code.
990 * @return The country code (2 letters)
991 */
992 public String getCountryCode() {
993 return countryCode;
994 }
995
996 /**
997 * Sets the ISO 3166-1-alpha-2 country code.
998 * @param countryCode The country code (2 letters)
999 */
1000 public void setCountryCode(String countryCode) {
1001 this.countryCode = countryCode;
1002 }
1003
1004 /**
1005 * Returns the date information.
1006 * @return The date (in the form YYYY-MM-DD;YYYY-MM-DD, where
1007 * DD and MM as well as a second date are optional)
1008 * @since 11570
1009 */
1010 public String getDate() {
1011 return date;
1012 }
1013
1014 /**
1015 * Sets the date information.
1016 * @param date The date information
1017 * @since 11570
1018 */
1019 public void setDate(String date) {
1020 this.date = date;
1021 }
1022
1023 /**
1024 * Returns the entry icon.
1025 * @return The entry icon
1026 */
1027 public String getIcon() {
1028 return icon;
1029 }
1030
1031 /**
1032 * Sets the entry icon.
1033 * @param icon The entry icon
1034 */
1035 public void setIcon(String icon) {
1036 this.icon = icon;
1037 }
1038
1039 /**
1040 * Get the projections supported by the server. Only relevant for
1041 * WMS-type ImageryInfo at the moment.
1042 * @return null, if no projections have been specified; the list
1043 * of supported projections otherwise.
1044 */
1045 public List<String> getServerProjections() {
1046 return Collections.unmodifiableList(serverProjections);
1047 }
1048
1049 /**
1050 * Sets the list of collections the server supports
1051 * @param serverProjections The list of supported projections
1052 */
1053 public void setServerProjections(Collection<String> serverProjections) {
1054 CheckParameterUtil.ensureParameterNotNull(serverProjections, "serverProjections");
1055 this.serverProjections = new ArrayList<>(serverProjections);
1056 }
1057
1058 /**
1059 * Returns the extended URL, containing in addition of service URL, its type and min/max zoom info.
1060 * @return The extended URL
1061 */
1062 public String getExtendedUrl() {
1063 return imageryType.getTypeString() + (defaultMaxZoom != 0
1064 ? ('['+(defaultMinZoom != 0 ? (Integer.toString(defaultMinZoom) + ',') : "")+defaultMaxZoom+']') : "") + ':' + url;
1065 }
1066
1067 /**
1068 * Gets a unique toolbar key to store this layer as toolbar item
1069 * @return The kay.
1070 */
1071 public String getToolbarName() {
1072 String res = name;
1073 if (pixelPerDegree != 0) {
1074 res += "#PPD="+pixelPerDegree;
1075 }
1076 return res;
1077 }
1078
1079 /**
1080 * Gets the name that should be displayed in the menu to add this imagery layer.
1081 * @return The text.
1082 */
1083 public String getMenuName() {
1084 String res = name;
1085 if (pixelPerDegree != 0) {
1086 res += " ("+pixelPerDegree+')';
1087 }
1088 return res;
1089 }
1090
1091 /**
1092 * Determines if this entry requires attribution.
1093 * @return {@code true} if some attribution text has to be displayed, {@code false} otherwise
1094 */
1095 public boolean hasAttribution() {
1096 return attributionText != null;
1097 }
1098
1099 /**
1100 * Copies attribution from another {@code ImageryInfo}.
1101 * @param i The other imagery info to get attribution from
1102 */
1103 public void copyAttribution(ImageryInfo i) {
1104 this.attributionImage = i.attributionImage;
1105 this.attributionImageURL = i.attributionImageURL;
1106 this.attributionText = i.attributionText;
1107 this.attributionLinkURL = i.attributionLinkURL;
1108 this.termsOfUseText = i.termsOfUseText;
1109 this.termsOfUseURL = i.termsOfUseURL;
1110 }
1111
1112 /**
1113 * Applies the attribution from this object to a tile source.
1114 * @param s The tile source
1115 */
1116 public void setAttribution(AbstractTileSource s) {
1117 if (attributionText != null) {
1118 if ("osm".equals(attributionText)) {
1119 s.setAttributionText(new Mapnik().getAttributionText(0, null, null));
1120 } else {
1121 s.setAttributionText(attributionText);
1122 }
1123 }
1124 if (attributionLinkURL != null) {
1125 if ("osm".equals(attributionLinkURL)) {
1126 s.setAttributionLinkURL(new Mapnik().getAttributionLinkURL());
1127 } else {
1128 s.setAttributionLinkURL(attributionLinkURL);
1129 }
1130 }
1131 if (attributionImage != null) {
1132 ImageIcon i = ImageProvider.getIfAvailable(null, attributionImage);
1133 if (i != null) {
1134 s.setAttributionImage(i.getImage());
1135 }
1136 }
1137 if (attributionImageURL != null) {
1138 s.setAttributionImageURL(attributionImageURL);
1139 }
1140 if (termsOfUseText != null) {
1141 s.setTermsOfUseText(termsOfUseText);
1142 }
1143 if (termsOfUseURL != null) {
1144 if ("osm".equals(termsOfUseURL)) {
1145 s.setTermsOfUseURL(new Mapnik().getTermsOfUseURL());
1146 } else {
1147 s.setTermsOfUseURL(termsOfUseURL);
1148 }
1149 }
1150 }
1151
1152 /**
1153 * Returns the imagery type.
1154 * @return The imagery type
1155 */
1156 public ImageryType getImageryType() {
1157 return imageryType;
1158 }
1159
1160 /**
1161 * Sets the imagery type.
1162 * @param imageryType The imagery type
1163 */
1164 public void setImageryType(ImageryType imageryType) {
1165 this.imageryType = imageryType;
1166 }
1167
1168 /**
1169 * Returns true if this layer's URL is matched by one of the regular
1170 * expressions kept by the current OsmApi instance.
1171 * @return {@code true} is this entry is blacklisted, {@code false} otherwise
1172 */
1173 public boolean isBlacklisted() {
1174 Capabilities capabilities = OsmApi.getOsmApi().getCapabilities();
1175 return capabilities != null && capabilities.isOnImageryBlacklist(this.url);
1176 }
1177
1178 /**
1179 * Sets the map of &lt;header name, header value&gt; that if any of this header
1180 * will be returned, then this tile will be treated as "no tile at this zoom level"
1181 *
1182 * @param noTileHeaders Map of &lt;header name, header value&gt; which will be treated as "no tile at this zoom level"
1183 * @since 9613
1184 */
1185 public void setNoTileHeaders(MultiMap<String, String> noTileHeaders) {
1186 if (noTileHeaders == null || noTileHeaders.isEmpty()) {
1187 this.noTileHeaders = null;
1188 } else {
1189 this.noTileHeaders = noTileHeaders.toMap();
1190 }
1191 }
1192
1193 @Override
1194 public Map<String, Set<String>> getNoTileHeaders() {
1195 return noTileHeaders;
1196 }
1197
1198 /**
1199 * Sets the map of &lt;checksum type, checksum value&gt; that if any tile with that checksum
1200 * will be returned, then this tile will be treated as "no tile at this zoom level"
1201 *
1202 * @param noTileChecksums Map of &lt;checksum type, checksum value&gt; which will be treated as "no tile at this zoom level"
1203 * @since 9613
1204 */
1205 public void setNoTileChecksums(MultiMap<String, String> noTileChecksums) {
1206 if (noTileChecksums == null || noTileChecksums.isEmpty()) {
1207 this.noTileChecksums = null;
1208 } else {
1209 this.noTileChecksums = noTileChecksums.toMap();
1210 }
1211 }
1212
1213 @Override
1214 public Map<String, Set<String>> getNoTileChecksums() {
1215 return noTileChecksums;
1216 }
1217
1218 /**
1219 * Returns the map of &lt;header name, metadata key&gt; indicating, which HTTP headers should
1220 * be moved to metadata
1221 *
1222 * @param metadataHeaders map of &lt;header name, metadata key&gt; indicating, which HTTP headers should be moved to metadata
1223 * @since 8418
1224 */
1225 public void setMetadataHeaders(Map<String, String> metadataHeaders) {
1226 if (metadataHeaders == null || metadataHeaders.isEmpty()) {
1227 this.metadataHeaders = null;
1228 } else {
1229 this.metadataHeaders = metadataHeaders;
1230 }
1231 }
1232
1233 /**
1234 * Gets the flag if the georeference is valid.
1235 * @return <code>true</code> if it is valid.
1236 */
1237 public boolean isGeoreferenceValid() {
1238 return isGeoreferenceValid;
1239 }
1240
1241 /**
1242 * Sets an indicator that the georeference is valid
1243 * @param isGeoreferenceValid <code>true</code> if it is marked as valid.
1244 */
1245 public void setGeoreferenceValid(boolean isGeoreferenceValid) {
1246 this.isGeoreferenceValid = isGeoreferenceValid;
1247 }
1248
1249 /**
1250 * Returns the status of "best" marked status in other editors.
1251 * @return <code>true</code> if it is marked as best.
1252 * @since 11575
1253 */
1254 public boolean isBestMarked() {
1255 return bestMarked;
1256 }
1257
1258 /**
1259 * Returns the overlay indication.
1260 * @return <code>true</code> if it is and overlay.
1261 * @since 13536
1262 */
1263 public boolean isOverlay() {
1264 return overlay;
1265 }
1266
1267 /**
1268 * Sets an indicator that in other editors it is marked as best imagery
1269 * @param bestMarked <code>true</code> if it is marked as best in other editors.
1270 * @since 11575
1271 */
1272 public void setBestMarked(boolean bestMarked) {
1273 this.bestMarked = bestMarked;
1274 }
1275
1276 /**
1277 * Sets overlay indication
1278 * @param overlay <code>true</code> if it is an overlay.
1279 * @since 13536
1280 */
1281 public void setOverlay(boolean overlay) {
1282 this.overlay = overlay;
1283 }
1284
1285 /**
1286 * Adds an old Id.
1287 *
1288 * @param id the Id to be added
1289 * @since 13536
1290 */
1291 public void addOldId(String id) {
1292 if (oldIds == null) {
1293 oldIds = new ArrayList<>();
1294 }
1295 oldIds.add(id);
1296 }
1297
1298 /**
1299 * Get old Ids.
1300 *
1301 * @return collection of ids
1302 * @since 13536
1303 */
1304 public Collection<String> getOldIds() {
1305 return oldIds;
1306 }
1307
1308 /**
1309 * Adds a mirror entry. Mirror entries are completed with the data from the master entry
1310 * and only describe another method to access identical data.
1311 *
1312 * @param entry the mirror to be added
1313 * @since 9658
1314 */
1315 public void addMirror(ImageryInfo entry) {
1316 if (mirrors == null) {
1317 mirrors = new ArrayList<>();
1318 }
1319 mirrors.add(entry);
1320 }
1321
1322 /**
1323 * Returns the mirror entries. Entries are completed with master entry data.
1324 *
1325 * @return the list of mirrors
1326 * @since 9658
1327 */
1328 public List<ImageryInfo> getMirrors() {
1329 List<ImageryInfo> l = new ArrayList<>();
1330 if (mirrors != null) {
1331 int num = 1;
1332 for (ImageryInfo i : mirrors) {
1333 ImageryInfo n = new ImageryInfo(this);
1334 if (i.defaultMaxZoom != 0) {
1335 n.defaultMaxZoom = i.defaultMaxZoom;
1336 }
1337 if (i.defaultMinZoom != 0) {
1338 n.defaultMinZoom = i.defaultMinZoom;
1339 }
1340 n.setServerProjections(i.getServerProjections());
1341 n.url = i.url;
1342 n.imageryType = i.imageryType;
1343 if (i.getTileSize() != 0) {
1344 n.setTileSize(i.getTileSize());
1345 }
1346 if (n.id != null) {
1347 n.id = n.id + "_mirror"+num;
1348 }
1349 if (num > 1) {
1350 n.name = tr("{0} mirror server {1}", n.name, num);
1351 if (n.origName != null) {
1352 n.origName += " mirror server " + num;
1353 }
1354 } else {
1355 n.name = tr("{0} mirror server", n.name);
1356 if (n.origName != null) {
1357 n.origName += " mirror server";
1358 }
1359 }
1360 l.add(n);
1361 ++num;
1362 }
1363 }
1364 return l;
1365 }
1366
1367 /**
1368 * Returns default layers that should be shown for this Imagery (if at all supported by imagery provider)
1369 * If no layer is set to default and there is more than one imagery available, then user will be asked to choose the layer
1370 * to work on
1371 * @return Collection of the layer names
1372 */
1373 public Collection<DefaultLayer> getDefaultLayers() {
1374 return defaultLayers;
1375 }
1376
1377 /**
1378 * Sets the default layers that user will work with
1379 * @param layers set the list of default layers
1380 */
1381 public void setDefaultLayers(Collection<DefaultLayer> layers) {
1382 if (ImageryType.WMTS.equals(this.imageryType)) {
1383 CheckParameterUtil.ensureThat(layers == null ||
1384 layers.isEmpty() ||
1385 layers.iterator().next() instanceof WMTSDefaultLayer, "Incorrect default layer");
1386 }
1387 this.defaultLayers = layers;
1388 }
1389}
Note: See TracBrowser for help on using the repository browser.