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

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

see #15713 - rename dirty mode

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