001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.imagery;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Image;
007import java.util.ArrayList;
008import java.util.Arrays;
009import java.util.Collection;
010import java.util.Collections;
011import java.util.List;
012import java.util.Locale;
013import java.util.Map;
014import java.util.Objects;
015import java.util.Set;
016import java.util.TreeSet;
017import java.util.regex.Matcher;
018import java.util.regex.Pattern;
019import java.util.stream.Collectors;
020
021import javax.swing.ImageIcon;
022
023import org.openstreetmap.gui.jmapviewer.interfaces.Attributed;
024import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
025import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTileSource;
026import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource.Mapnik;
027import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
028import org.openstreetmap.josm.Main;
029import org.openstreetmap.josm.data.Bounds;
030import org.openstreetmap.josm.data.Preferences.pref;
031import org.openstreetmap.josm.io.Capabilities;
032import org.openstreetmap.josm.io.OsmApi;
033import org.openstreetmap.josm.tools.CheckParameterUtil;
034import org.openstreetmap.josm.tools.ImageProvider;
035import org.openstreetmap.josm.tools.LanguageInfo;
036import org.openstreetmap.josm.tools.MultiMap;
037
038/**
039 * Class that stores info about an image background layer.
040 *
041 * @author Frederik Ramm
042 */
043public class ImageryInfo extends TileSourceInfo implements Comparable<ImageryInfo>, Attributed {
044
045    /**
046     * Type of imagery entry.
047     */
048    public enum ImageryType {
049        /** A WMS (Web Map Service) entry. **/
050        WMS("wms"),
051        /** A TMS (Tile Map Service) entry. **/
052        TMS("tms"),
053        /** An HTML proxy (previously used for Yahoo imagery) entry. **/
054        HTML("html"),
055        /** TMS entry for Microsoft Bing. */
056        BING("bing"),
057        /** TMS entry for Russian company <a href="https://wiki.openstreetmap.org/wiki/WikiProject_Russia/kosmosnimki">ScanEx</a>. **/
058        SCANEX("scanex"),
059        /** A WMS endpoint entry only stores the WMS server info, without layer, which are chosen later by the user. **/
060        WMS_ENDPOINT("wms_endpoint"),
061        /** WMTS stores GetCapabilities URL. Does not store any information about the layer **/
062        WMTS("wmts");
063
064
065        private final String typeString;
066
067        ImageryType(String urlString) {
068            this.typeString = urlString;
069        }
070
071        /**
072         * Returns the unique string identifying this type.
073         * @return the unique string identifying this type
074         * @since 6690
075         */
076        public final String getTypeString() {
077            return typeString;
078        }
079
080        /**
081         * Returns the imagery type from the given type string.
082         * @param s The type string
083         * @return the imagery type matching the given type string
084         */
085        public static ImageryType fromString(String s) {
086            for (ImageryType type : ImageryType.values()) {
087                if (type.getTypeString().equals(s)) {
088                    return type;
089                }
090            }
091            return null;
092        }
093    }
094
095    /**
096     * Multi-polygon bounds for imagery backgrounds.
097     * Used to display imagery coverage in preferences and to determine relevant imagery entries based on edit location.
098     */
099    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    /** The data part of HTTP cookies header in case the service requires cookies to work */
158    private String cookies;
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 behing 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    /** mirrors of different type for this entry */
191    private List<ImageryInfo> mirrors;
192    /** icon used in menu */
193    private String icon;
194    private boolean isGeoreferenceValid;
195    private boolean isEpsg4326To3857Supported;
196    /** which layers should be activated by default on layer addition. **/
197    private Collection<String> defaultLayers = Collections.emptyList();
198    // when adding a field, also adapt the ImageryInfo(ImageryInfo)
199    // and ImageryInfo(ImageryPreferenceEntry) constructor, equals method, and ImageryPreferenceEntry
200
201    /**
202     * Auxiliary class to save an {@link ImageryInfo} object in the preferences.
203     */
204    public static class ImageryPreferenceEntry {
205        @pref String name;
206        @pref String id;
207        @pref String type;
208        @pref String url;
209        @pref double pixel_per_eastnorth;
210        @pref String eula;
211        @pref String attribution_text;
212        @pref String attribution_url;
213        @pref String logo_image;
214        @pref String logo_url;
215        @pref String terms_of_use_text;
216        @pref String terms_of_use_url;
217        @pref String country_code = "";
218        @pref int max_zoom;
219        @pref int min_zoom;
220        @pref String cookies;
221        @pref String bounds;
222        @pref String shapes;
223        @pref String projections;
224        @pref String icon;
225        @pref String description;
226        @pref MultiMap<String, String> noTileHeaders;
227        @pref MultiMap<String, String> noTileChecksums;
228        @pref int tileSize = -1;
229        @pref Map<String, String> metadataHeaders;
230        @pref boolean valid_georeference;
231        @pref boolean supports_epsg_4326_to_3857_conversion;
232        // TODO: disabled until change of layers is implemented
233        // @pref String default_layers;
234
235        /**
236         * Constructs a new empty WMS {@code ImageryPreferenceEntry}.
237         */
238        public ImageryPreferenceEntry() {
239            // Do nothing
240        }
241
242        /**
243         * Constructs a new {@code ImageryPreferenceEntry} from a given {@code ImageryInfo}.
244         * @param i The corresponding imagery info
245         */
246        public ImageryPreferenceEntry(ImageryInfo i) {
247            name = i.name;
248            id = i.id;
249            type = i.imageryType.getTypeString();
250            url = i.url;
251            pixel_per_eastnorth = i.pixelPerDegree;
252            eula = i.eulaAcceptanceRequired;
253            attribution_text = i.attributionText;
254            attribution_url = i.attributionLinkURL;
255            logo_image = i.attributionImage;
256            logo_url = i.attributionImageURL;
257            terms_of_use_text = i.termsOfUseText;
258            terms_of_use_url = i.termsOfUseURL;
259            country_code = i.countryCode;
260            max_zoom = i.defaultMaxZoom;
261            min_zoom = i.defaultMinZoom;
262            cookies = i.cookies;
263            icon = i.icon;
264            description = i.description;
265            if (i.bounds != null) {
266                bounds = i.bounds.encodeAsString(",");
267                StringBuilder shapesString = new StringBuilder();
268                for (Shape s : i.bounds.getShapes()) {
269                    if (shapesString.length() > 0) {
270                        shapesString.append(';');
271                    }
272                    shapesString.append(s.encodeAsString(","));
273                }
274                if (shapesString.length() > 0) {
275                    shapes = shapesString.toString();
276                }
277            }
278            projections = i.serverProjections.stream().collect(Collectors.joining(","));
279            if (i.noTileHeaders != null && !i.noTileHeaders.isEmpty()) {
280                noTileHeaders = new MultiMap<>(i.noTileHeaders);
281            }
282
283            if (i.noTileChecksums != null && !i.noTileChecksums.isEmpty()) {
284                noTileChecksums = new MultiMap<>(i.noTileChecksums);
285            }
286
287            if (i.metadataHeaders != null && !i.metadataHeaders.isEmpty()) {
288                metadataHeaders = i.metadataHeaders;
289            }
290
291            tileSize = i.getTileSize();
292
293            valid_georeference = i.isGeoreferenceValid();
294            supports_epsg_4326_to_3857_conversion = i.isEpsg4326To3857Supported();
295            // TODO disabled until change of layers is implemented
296            // default_layers = i.defaultLayers.stream().collect(Collectors.joining(","));
297        }
298
299        @Override
300        public String toString() {
301            StringBuilder s = new StringBuilder("ImageryPreferenceEntry [name=").append(name);
302            if (id != null) {
303                s.append(" id=").append(id);
304            }
305            s.append(']');
306            return s.toString();
307        }
308    }
309
310    /**
311     * Constructs a new WMS {@code ImageryInfo}.
312     */
313    public ImageryInfo() {
314        super();
315    }
316
317    /**
318     * Constructs a new WMS {@code ImageryInfo} with a given name.
319     * @param name The entry name
320     */
321    public ImageryInfo(String name) {
322        super(name);
323    }
324
325    /**
326     * Constructs a new WMS {@code ImageryInfo} with given name and extended URL.
327     * @param name The entry name
328     * @param url The entry extended URL
329     */
330    public ImageryInfo(String name, String url) {
331        this(name);
332        setExtendedUrl(url);
333    }
334
335    /**
336     * Constructs a new WMS {@code ImageryInfo} with given name, extended and EULA URLs.
337     * @param name The entry name
338     * @param url The entry URL
339     * @param eulaAcceptanceRequired The EULA URL
340     */
341    public ImageryInfo(String name, String url, String eulaAcceptanceRequired) {
342        this(name);
343        setExtendedUrl(url);
344        this.eulaAcceptanceRequired = eulaAcceptanceRequired;
345    }
346
347    /**
348     * Constructs a new {@code ImageryInfo} with given name, url, extended and EULA URLs.
349     * @param name The entry name
350     * @param url The entry URL
351     * @param type The entry imagery type. If null, WMS will be used as default
352     * @param eulaAcceptanceRequired The EULA URL
353     * @param cookies The data part of HTTP cookies header in case the service requires cookies to work
354     * @throws IllegalArgumentException if type refers to an unknown imagery type
355     */
356    public ImageryInfo(String name, String url, String type, String eulaAcceptanceRequired, String cookies) {
357        this(name);
358        setExtendedUrl(url);
359        ImageryType t = ImageryType.fromString(type);
360        this.cookies = cookies;
361        this.eulaAcceptanceRequired = eulaAcceptanceRequired;
362        if (t != null) {
363            this.imageryType = t;
364        } else if (type != null && !type.trim().isEmpty()) {
365            throw new IllegalArgumentException("unknown type: "+type);
366        }
367    }
368
369    public ImageryInfo(String name, String url, String type, String eulaAcceptanceRequired, String cookies, String id) {
370        this(name, url, type, eulaAcceptanceRequired, cookies);
371        setId(id);
372    }
373
374    /**
375     * Constructs a new {@code ImageryInfo} from an imagery preference entry.
376     * @param e The imagery preference entry
377     */
378    public ImageryInfo(ImageryPreferenceEntry e) {
379        super(e.name, e.url, e.id);
380        CheckParameterUtil.ensureParameterNotNull(e.name, "name");
381        CheckParameterUtil.ensureParameterNotNull(e.url, "url");
382        description = e.description;
383        cookies = e.cookies;
384        eulaAcceptanceRequired = e.eula;
385        imageryType = ImageryType.fromString(e.type);
386        if (imageryType == null) throw new IllegalArgumentException("unknown type");
387        pixelPerDegree = e.pixel_per_eastnorth;
388        defaultMaxZoom = e.max_zoom;
389        defaultMinZoom = e.min_zoom;
390        if (e.bounds != null) {
391            bounds = new ImageryBounds(e.bounds, ",");
392            if (e.shapes != null) {
393                try {
394                    for (String s : e.shapes.split(";")) {
395                        bounds.addShape(new Shape(s, ","));
396                    }
397                } catch (IllegalArgumentException ex) {
398                    Main.warn(ex);
399                }
400            }
401        }
402        if (e.projections != null) {
403            serverProjections = Arrays.asList(e.projections.split(","));
404        }
405        attributionText = e.attribution_text;
406        attributionLinkURL = e.attribution_url;
407        attributionImage = e.logo_image;
408        attributionImageURL = e.logo_url;
409        termsOfUseText = e.terms_of_use_text;
410        termsOfUseURL = e.terms_of_use_url;
411        countryCode = e.country_code;
412        icon = e.icon;
413        if (e.noTileHeaders != null) {
414            noTileHeaders = e.noTileHeaders.toMap();
415        }
416        if (e.noTileChecksums != null) {
417            noTileChecksums = e.noTileChecksums.toMap();
418        }
419        setTileSize(e.tileSize);
420        metadataHeaders = e.metadataHeaders;
421        isEpsg4326To3857Supported = e.supports_epsg_4326_to_3857_conversion;
422        isGeoreferenceValid = e.valid_georeference;
423        // TODO disabled until change of layers is implemented
424        // defaultLayers = Arrays.asList(e.default_layers.split(","));
425    }
426
427    /**
428     * Constructs a new {@code ImageryInfo} from an existing one.
429     * @param i The other imagery info
430     */
431    public ImageryInfo(ImageryInfo i) {
432        super(i.name, i.url, i.id);
433        this.defaultEntry = i.defaultEntry;
434        this.cookies = i.cookies;
435        this.eulaAcceptanceRequired = null;
436        this.imageryType = i.imageryType;
437        this.pixelPerDegree = i.pixelPerDegree;
438        this.defaultMaxZoom = i.defaultMaxZoom;
439        this.defaultMinZoom = i.defaultMinZoom;
440        this.bounds = i.bounds;
441        this.serverProjections = i.serverProjections;
442        this.attributionText = i.attributionText;
443        this.attributionLinkURL = i.attributionLinkURL;
444        this.attributionImage = i.attributionImage;
445        this.attributionImageURL = i.attributionImageURL;
446        this.termsOfUseText = i.termsOfUseText;
447        this.termsOfUseURL = i.termsOfUseURL;
448        this.countryCode = i.countryCode;
449        this.icon = i.icon;
450        this.description = i.description;
451        this.noTileHeaders = i.noTileHeaders;
452        this.noTileChecksums = i.noTileChecksums;
453        this.metadataHeaders = i.metadataHeaders;
454        this.isEpsg4326To3857Supported = i.isEpsg4326To3857Supported;
455        this.isGeoreferenceValid = i.isGeoreferenceValid;
456        this.defaultLayers = i.defaultLayers;
457    }
458
459    @Override
460    public int hashCode() {
461        return Objects.hash(url, imageryType);
462    }
463
464    /**
465     * Check if this object equals another ImageryInfo with respect to the properties
466     * that get written to the preference file.
467     *
468     * The field {@link #pixelPerDegree} is ignored.
469     *
470     * @param other the ImageryInfo object to compare to
471     * @return true if they are equal
472     */
473    public boolean equalsPref(ImageryInfo other) {
474        if (other == null) {
475            return false;
476        }
477
478        return
479                Objects.equals(this.name, other.name) &&
480                Objects.equals(this.id, other.id) &&
481                Objects.equals(this.url, other.url) &&
482                Objects.equals(this.cookies, other.cookies) &&
483                Objects.equals(this.eulaAcceptanceRequired, other.eulaAcceptanceRequired) &&
484                Objects.equals(this.imageryType, other.imageryType) &&
485                Objects.equals(this.defaultMaxZoom, other.defaultMaxZoom) &&
486                Objects.equals(this.defaultMinZoom, other.defaultMinZoom) &&
487                Objects.equals(this.bounds, other.bounds) &&
488                Objects.equals(this.serverProjections, other.serverProjections) &&
489                Objects.equals(this.attributionText, other.attributionText) &&
490                Objects.equals(this.attributionLinkURL, other.attributionLinkURL) &&
491                Objects.equals(this.attributionImageURL, other.attributionImageURL) &&
492                Objects.equals(this.attributionImage, other.attributionImage) &&
493                Objects.equals(this.termsOfUseText, other.termsOfUseText) &&
494                Objects.equals(this.termsOfUseURL, other.termsOfUseURL) &&
495                Objects.equals(this.countryCode, other.countryCode) &&
496                Objects.equals(this.icon, other.icon) &&
497                Objects.equals(this.description, other.description) &&
498                Objects.equals(this.noTileHeaders, other.noTileHeaders) &&
499                Objects.equals(this.noTileChecksums, other.noTileChecksums) &&
500                Objects.equals(this.metadataHeaders, other.metadataHeaders) &&
501                Objects.equals(this.defaultLayers, other.defaultLayers);
502    }
503
504    @Override
505    public boolean equals(Object o) {
506        if (this == o) return true;
507        if (o == null || getClass() != o.getClass()) return false;
508        ImageryInfo that = (ImageryInfo) o;
509        return imageryType == that.imageryType && Objects.equals(url, that.url);
510    }
511
512    @Override
513    public String toString() {
514        return "ImageryInfo{" +
515                "name='" + name + '\'' +
516                ", countryCode='" + countryCode + '\'' +
517                ", url='" + url + '\'' +
518                ", imageryType=" + imageryType +
519                '}';
520    }
521
522    @Override
523    public int compareTo(ImageryInfo in) {
524        int i = countryCode.compareTo(in.countryCode);
525        if (i == 0) {
526            i = name.toLowerCase(Locale.ENGLISH).compareTo(in.name.toLowerCase(Locale.ENGLISH));
527        }
528        if (i == 0) {
529            i = url.compareTo(in.url);
530        }
531        if (i == 0) {
532            i = Double.compare(pixelPerDegree, in.pixelPerDegree);
533        }
534        return i;
535    }
536
537    public boolean equalsBaseValues(ImageryInfo in) {
538        return url.equals(in.url);
539    }
540
541    /**
542     * Sets the pixel per degree value.
543     * @param ppd The ppd value
544     * @see #getPixelPerDegree()
545     */
546    public void setPixelPerDegree(double ppd) {
547        this.pixelPerDegree = ppd;
548    }
549
550    /**
551     * Sets the maximum zoom level.
552     * @param defaultMaxZoom The maximum zoom level
553     */
554    public void setDefaultMaxZoom(int defaultMaxZoom) {
555        this.defaultMaxZoom = defaultMaxZoom;
556    }
557
558    /**
559     * Sets the minimum zoom level.
560     * @param defaultMinZoom The minimum zoom level
561     */
562    public void setDefaultMinZoom(int defaultMinZoom) {
563        this.defaultMinZoom = defaultMinZoom;
564    }
565
566    /**
567     * Sets the imagery polygonial bounds.
568     * @param b The imagery bounds (non-rectangular)
569     */
570    public void setBounds(ImageryBounds b) {
571        this.bounds = b;
572    }
573
574    /**
575     * Returns the imagery polygonial bounds.
576     * @return The imagery bounds (non-rectangular)
577     */
578    public ImageryBounds getBounds() {
579        return bounds;
580    }
581
582    @Override
583    public boolean requiresAttribution() {
584        return attributionText != null || attributionImage != null || termsOfUseText != null || termsOfUseURL != null;
585    }
586
587    @Override
588    public String getAttributionText(int zoom, ICoordinate topLeft, ICoordinate botRight) {
589        return attributionText;
590    }
591
592    @Override
593    public String getAttributionLinkURL() {
594        return attributionLinkURL;
595    }
596
597    @Override
598    public Image getAttributionImage() {
599        ImageIcon i = ImageProvider.getIfAvailable(attributionImage);
600        if (i != null) {
601            return i.getImage();
602        }
603        return null;
604    }
605
606    @Override
607    public String getAttributionImageURL() {
608        return attributionImageURL;
609    }
610
611    @Override
612    public String getTermsOfUseText() {
613        return termsOfUseText;
614    }
615
616    @Override
617    public String getTermsOfUseURL() {
618        return termsOfUseURL;
619    }
620
621    /**
622     * Set the attribution text
623     * @param text The text
624     * @see #getAttributionText(int, ICoordinate, ICoordinate)
625     */
626    public void setAttributionText(String text) {
627        attributionText = text;
628    }
629
630    /**
631     * Set the attribution image
632     * @param url The url of the image.
633     * @see #getAttributionImageURL()
634     */
635    public void setAttributionImageURL(String url) {
636        attributionImageURL = url;
637    }
638
639    /**
640     * Set the image for the attribution
641     * @param res The image resource
642     * @see #getAttributionImage()
643     */
644    public void setAttributionImage(String res) {
645        attributionImage = res;
646    }
647
648    /**
649     * Sets the URL the attribution should link to.
650     * @param url The url.
651     * @see #getAttributionLinkURL()
652     */
653    public void setAttributionLinkURL(String url) {
654        attributionLinkURL = url;
655    }
656
657    /**
658     * Sets the text to display to the user as terms of use.
659     * @param text The text
660     * @see #getTermsOfUseText()
661     */
662    public void setTermsOfUseText(String text) {
663        termsOfUseText = text;
664    }
665
666    /**
667     * Sets a url that links to the terms of use text.
668     * @param text The url.
669     * @see #getTermsOfUseURL()
670     */
671    public void setTermsOfUseURL(String text) {
672        termsOfUseURL = text;
673    }
674
675    /**
676     * Sets the extended URL of this entry.
677     * @param url Entry extended URL containing in addition of service URL, its type and min/max zoom info
678     */
679    public void setExtendedUrl(String url) {
680        CheckParameterUtil.ensureParameterNotNull(url);
681
682        // Default imagery type is WMS
683        this.url = url;
684        this.imageryType = ImageryType.WMS;
685
686        defaultMaxZoom = 0;
687        defaultMinZoom = 0;
688        for (ImageryType type : ImageryType.values()) {
689            Matcher m = Pattern.compile(type.getTypeString()+"(?:\\[(?:(\\d+)[,-])?(\\d+)\\])?:(.*)").matcher(url);
690            if (m.matches()) {
691                this.url = m.group(3);
692                this.imageryType = type;
693                if (m.group(2) != null) {
694                    defaultMaxZoom = Integer.parseInt(m.group(2));
695                }
696                if (m.group(1) != null) {
697                    defaultMinZoom = Integer.parseInt(m.group(1));
698                }
699                break;
700            }
701        }
702
703        if (serverProjections.isEmpty()) {
704            serverProjections = new ArrayList<>();
705            Matcher m = Pattern.compile(".*\\{PROJ\\(([^)}]+)\\)\\}.*").matcher(url.toUpperCase(Locale.ENGLISH));
706            if (m.matches()) {
707                for (String p : m.group(1).split(",")) {
708                    serverProjections.add(p);
709                }
710            }
711        }
712    }
713
714    /**
715     * Returns the entry name.
716     * @return The entry name
717     * @since 6968
718     */
719    public String getOriginalName() {
720        return this.origName != null ? this.origName : this.name;
721    }
722
723    /**
724     * Sets the entry name and handle translation.
725     * @param language The used language
726     * @param name The entry name
727     * @since 8091
728     */
729    public void setName(String language, String name) {
730        boolean isdefault = LanguageInfo.getJOSMLocaleCode(null).equals(language);
731        if (LanguageInfo.isBetterLanguage(langName, language)) {
732            this.name = isdefault ? tr(name) : name;
733            this.langName = language;
734        }
735        if (origName == null || isdefault) {
736            this.origName = name;
737        }
738    }
739
740    /**
741     * Store the id of this info to the preferences and clear it afterwards.
742     */
743    public void clearId() {
744        if (this.id != null) {
745            Collection<String> newAddedIds = new TreeSet<>(Main.pref.getCollection("imagery.layers.addedIds"));
746            newAddedIds.add(this.id);
747            Main.pref.putCollection("imagery.layers.addedIds", newAddedIds);
748        }
749        setId(null);
750    }
751
752    /**
753     * Determines if this entry is enabled by default.
754     * @return {@code true} if this entry is enabled by default, {@code false} otherwise
755     */
756    public boolean isDefaultEntry() {
757        return defaultEntry;
758    }
759
760    /**
761     * Sets the default state of this entry.
762     * @param defaultEntry {@code true} if this entry has to be enabled by default, {@code false} otherwise
763     */
764    public void setDefaultEntry(boolean defaultEntry) {
765        this.defaultEntry = defaultEntry;
766    }
767
768    /**
769     * Return the data part of HTTP cookies header in case the service requires cookies to work
770     * @return the cookie data part
771     */
772    @Override
773    public String getCookies() {
774        return this.cookies;
775    }
776
777    /**
778     * Gets the pixel per degree value
779     * @return The ppd value.
780     */
781    public double getPixelPerDegree() {
782        return this.pixelPerDegree;
783    }
784
785    /**
786     * Returns the maximum zoom level.
787     * @return The maximum zoom level
788     */
789    @Override
790    public int getMaxZoom() {
791        return this.defaultMaxZoom;
792    }
793
794    /**
795     * Returns the minimum zoom level.
796     * @return The minimum zoom level
797     */
798    @Override
799    public int getMinZoom() {
800        return this.defaultMinZoom;
801    }
802
803    /**
804     * Returns the description text when existing.
805     * @return The description
806     * @since 8065
807     */
808    public String getDescription() {
809        return this.description;
810    }
811
812    /**
813     * Sets the description text when existing.
814     * @param language The used language
815     * @param description the imagery description text
816     * @since 8091
817     */
818    public void setDescription(String language, String description) {
819        boolean isdefault = LanguageInfo.getJOSMLocaleCode(null).equals(language);
820        if (LanguageInfo.isBetterLanguage(langDescription, language)) {
821            this.description = isdefault ? tr(description) : description;
822            this.langDescription = language;
823        }
824    }
825
826    /**
827     * Returns a tool tip text for display.
828     * @return The text
829     * @since 8065
830     */
831    public String getToolTipText() {
832        String desc = getDescription();
833        if (desc != null && !desc.isEmpty()) {
834            return "<html>" + getName() + "<br>" + desc + "</html>";
835        }
836        return getName();
837    }
838
839    /**
840     * Returns the EULA acceptance URL, if any.
841     * @return The URL to an EULA text that has to be accepted before use, or {@code null}
842     */
843    public String getEulaAcceptanceRequired() {
844        return eulaAcceptanceRequired;
845    }
846
847    /**
848     * Sets the EULA acceptance URL.
849     * @param eulaAcceptanceRequired The URL to an EULA text that has to be accepted before use
850     */
851    public void setEulaAcceptanceRequired(String eulaAcceptanceRequired) {
852        this.eulaAcceptanceRequired = eulaAcceptanceRequired;
853    }
854
855    /**
856     * Returns the ISO 3166-1-alpha-2 country code.
857     * @return The country code (2 letters)
858     */
859    public String getCountryCode() {
860        return countryCode;
861    }
862
863    /**
864     * Sets the ISO 3166-1-alpha-2 country code.
865     * @param countryCode The country code (2 letters)
866     */
867    public void setCountryCode(String countryCode) {
868        this.countryCode = countryCode;
869    }
870
871    /**
872     * Returns the entry icon.
873     * @return The entry icon
874     */
875    public String getIcon() {
876        return icon;
877    }
878
879    /**
880     * Sets the entry icon.
881     * @param icon The entry icon
882     */
883    public void setIcon(String icon) {
884        this.icon = icon;
885    }
886
887    /**
888     * Get the projections supported by the server. Only relevant for
889     * WMS-type ImageryInfo at the moment.
890     * @return null, if no projections have been specified; the list
891     * of supported projections otherwise.
892     */
893    public List<String> getServerProjections() {
894        return Collections.unmodifiableList(serverProjections);
895    }
896
897    /**
898     * Sets the list of collections the server supports
899     * @param serverProjections The list of supported projections
900     */
901    public void setServerProjections(Collection<String> serverProjections) {
902        CheckParameterUtil.ensureParameterNotNull(serverProjections, "serverProjections");
903        this.serverProjections = new ArrayList<>(serverProjections);
904    }
905
906    /**
907     * Returns the extended URL, containing in addition of service URL, its type and min/max zoom info.
908     * @return The extended URL
909     */
910    public String getExtendedUrl() {
911        return imageryType.getTypeString() + (defaultMaxZoom != 0
912            ? ('['+(defaultMinZoom != 0 ? (Integer.toString(defaultMinZoom) + ',') : "")+defaultMaxZoom+']') : "") + ':' + url;
913    }
914
915    /**
916     * Gets a unique toolbar key to store this layer as toolbar item
917     * @return The kay.
918     */
919    public String getToolbarName() {
920        String res = name;
921        if (pixelPerDegree != 0) {
922            res += "#PPD="+pixelPerDegree;
923        }
924        return res;
925    }
926
927    /**
928     * Gets the name that should be displayed in the menu to add this imagery layer.
929     * @return The text.
930     */
931    public String getMenuName() {
932        String res = name;
933        if (pixelPerDegree != 0) {
934            res += " ("+pixelPerDegree+')';
935        }
936        return res;
937    }
938
939    /**
940     * Determines if this entry requires attribution.
941     * @return {@code true} if some attribution text has to be displayed, {@code false} otherwise
942     */
943    public boolean hasAttribution() {
944        return attributionText != null;
945    }
946
947    /**
948     * Copies attribution from another {@code ImageryInfo}.
949     * @param i The other imagery info to get attribution from
950     */
951    public void copyAttribution(ImageryInfo i) {
952        this.attributionImage = i.attributionImage;
953        this.attributionImageURL = i.attributionImageURL;
954        this.attributionText = i.attributionText;
955        this.attributionLinkURL = i.attributionLinkURL;
956        this.termsOfUseText = i.termsOfUseText;
957        this.termsOfUseURL = i.termsOfUseURL;
958    }
959
960    /**
961     * Applies the attribution from this object to a tile source.
962     * @param s The tile source
963     */
964    public void setAttribution(AbstractTileSource s) {
965        if (attributionText != null) {
966            if ("osm".equals(attributionText)) {
967                s.setAttributionText(new Mapnik().getAttributionText(0, null, null));
968            } else {
969                s.setAttributionText(attributionText);
970            }
971        }
972        if (attributionLinkURL != null) {
973            if ("osm".equals(attributionLinkURL)) {
974                s.setAttributionLinkURL(new Mapnik().getAttributionLinkURL());
975            } else {
976                s.setAttributionLinkURL(attributionLinkURL);
977            }
978        }
979        if (attributionImage != null) {
980            ImageIcon i = ImageProvider.getIfAvailable(null, attributionImage);
981            if (i != null) {
982                s.setAttributionImage(i.getImage());
983            }
984        }
985        if (attributionImageURL != null) {
986            s.setAttributionImageURL(attributionImageURL);
987        }
988        if (termsOfUseText != null) {
989            s.setTermsOfUseText(termsOfUseText);
990        }
991        if (termsOfUseURL != null) {
992            if ("osm".equals(termsOfUseURL)) {
993                s.setTermsOfUseURL(new Mapnik().getTermsOfUseURL());
994            } else {
995                s.setTermsOfUseURL(termsOfUseURL);
996            }
997        }
998    }
999
1000    /**
1001     * Returns the imagery type.
1002     * @return The imagery type
1003     */
1004    public ImageryType getImageryType() {
1005        return imageryType;
1006    }
1007
1008    /**
1009     * Sets the imagery type.
1010     * @param imageryType The imagery type
1011     */
1012    public void setImageryType(ImageryType imageryType) {
1013        this.imageryType = imageryType;
1014    }
1015
1016    /**
1017     * Returns true if this layer's URL is matched by one of the regular
1018     * expressions kept by the current OsmApi instance.
1019     * @return {@code true} is this entry is blacklisted, {@code false} otherwise
1020     */
1021    public boolean isBlacklisted() {
1022        Capabilities capabilities = OsmApi.getOsmApi().getCapabilities();
1023        return capabilities != null && capabilities.isOnImageryBlacklist(this.url);
1024    }
1025
1026    /**
1027     * Sets the map of &lt;header name, header value&gt; that if any of this header
1028     * will be returned, then this tile will be treated as "no tile at this zoom level"
1029     *
1030     * @param noTileHeaders Map of &lt;header name, header value&gt; which will be treated as "no tile at this zoom level"
1031     * @since 9613
1032     */
1033    public void setNoTileHeaders(MultiMap<String, String> noTileHeaders) {
1034       if (noTileHeaders == null) {
1035           this.noTileHeaders = null;
1036       } else {
1037            this.noTileHeaders = noTileHeaders.toMap();
1038       }
1039    }
1040
1041    @Override
1042    public Map<String, Set<String>> getNoTileHeaders() {
1043        return noTileHeaders;
1044    }
1045
1046    /**
1047     * Sets the map of &lt;checksum type, checksum value&gt; that if any tile with that checksum
1048     * will be returned, then this tile will be treated as "no tile at this zoom level"
1049     *
1050     * @param noTileChecksums Map of &lt;checksum type, checksum value&gt; which will be treated as "no tile at this zoom level"
1051     * @since 9613
1052     */
1053    public void setNoTileChecksums(MultiMap<String, String> noTileChecksums) {
1054        if (noTileChecksums == null) {
1055            this.noTileChecksums = null;
1056        } else {
1057            this.noTileChecksums = noTileChecksums.toMap();
1058        }
1059    }
1060
1061    @Override
1062    public Map<String, Set<String>> getNoTileChecksums() {
1063        return noTileChecksums;
1064    }
1065
1066    /**
1067     * Returns the map of &lt;header name, metadata key&gt; indicating, which HTTP headers should
1068     * be moved to metadata
1069     *
1070     * @param metadataHeaders map of &lt;header name, metadata key&gt; indicating, which HTTP headers should be moved to metadata
1071     * @since 8418
1072     */
1073    public void setMetadataHeaders(Map<String, String> metadataHeaders) {
1074        this.metadataHeaders = metadataHeaders;
1075    }
1076
1077    /**
1078     * Gets the flag if epsg 4326 to 3857 is supported
1079     * @return The flag.
1080     */
1081    public boolean isEpsg4326To3857Supported() {
1082        return isEpsg4326To3857Supported;
1083    }
1084
1085    /**
1086     * Sets the flag that epsg 4326 to 3857 is supported
1087     * @param isEpsg4326To3857Supported The flag.
1088     */
1089    public void setEpsg4326To3857Supported(boolean isEpsg4326To3857Supported) {
1090        this.isEpsg4326To3857Supported = isEpsg4326To3857Supported;
1091    }
1092
1093    /**
1094     * Gets the flag if the georeference is valid.
1095     * @return <code>true</code> if it is valid.
1096     */
1097    public boolean isGeoreferenceValid() {
1098        return isGeoreferenceValid;
1099    }
1100
1101    /**
1102     * Sets an indicator that the georeference is valid
1103     * @param isGeoreferenceValid <code>true</code> if it is marked as valid.
1104     */
1105    public void setGeoreferenceValid(boolean isGeoreferenceValid) {
1106        this.isGeoreferenceValid = isGeoreferenceValid;
1107    }
1108
1109    /**
1110     * Adds a mirror entry. Mirror entries are completed with the data from the master entry
1111     * and only describe another method to access identical data.
1112     *
1113     * @param entry the mirror to be added
1114     * @since 9658
1115     */
1116    public void addMirror(ImageryInfo entry) {
1117       if (mirrors == null) {
1118           mirrors = new ArrayList<>();
1119       }
1120       mirrors.add(entry);
1121    }
1122
1123    /**
1124     * Returns the mirror entries. Entries are completed with master entry data.
1125     *
1126     * @return the list of mirrors
1127     * @since 9658
1128     */
1129    public List<ImageryInfo> getMirrors() {
1130       List<ImageryInfo> l = new ArrayList<>();
1131       if (mirrors != null) {
1132           for (ImageryInfo i : mirrors) {
1133               ImageryInfo n = new ImageryInfo(this);
1134               if (i.defaultMaxZoom != 0) {
1135                   n.defaultMaxZoom = i.defaultMaxZoom;
1136               }
1137               if (i.defaultMinZoom != 0) {
1138                   n.defaultMinZoom = i.defaultMinZoom;
1139               }
1140               n.setServerProjections(i.getServerProjections());
1141               n.url = i.url;
1142               n.imageryType = i.imageryType;
1143               if (i.getTileSize() != 0) {
1144                   n.setTileSize(i.getTileSize());
1145               }
1146               l.add(n);
1147           }
1148       }
1149       return l;
1150    }
1151
1152    /**
1153     * Returns default layers that should be shown for this Imagery (if at all supported by imagery provider)
1154     * If no layer is set to default and there is more than one imagery available, then user will be asked to choose the layer
1155     * to work on
1156     * @return Collection of the layer names
1157     */
1158    public Collection<String> getDefaultLayers() {
1159        return defaultLayers;
1160    }
1161
1162    /**
1163     * Sets the default layers that user will work with
1164     * @param layers set the list of default layers
1165     */
1166    public void setDefaultLayers(Collection<String> layers) {
1167        this.defaultLayers = layers;
1168    }
1169}