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 jakarta.json.Json;
007import jakarta.json.JsonObject;
008import jakarta.json.JsonObjectBuilder;
009
010import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
011import org.openstreetmap.josm.tools.Utils;
012
013/**
014 *
015 * Simple class representing default layer that might be set in imagery information
016 *
017 * This simple class is needed - as for WMS there is different information needed to specify layer than for WMTS
018 *
019 * @author Wiktor Niesiobedzki
020 *
021 */
022public class DefaultLayer {
023    private final String layerName;
024    private final String tileMatrixSet;
025    private final String style;
026
027    /**
028     * Constructor
029     * @param imageryType for which this layer is defined
030     * @param layerName as returned by getIdentifier for WMTS and getName for WMS
031     * @param style of the layer
032     * @param tileMatrixSet only for WMTS - tileMatrixSet to use
033     */
034    public DefaultLayer(ImageryType imageryType, String layerName, String style, String tileMatrixSet) {
035        this.layerName = layerName == null ? "" : layerName;
036        this.style = style == null ? "" : style;
037        if (imageryType != ImageryType.WMTS && !Utils.isEmpty(tileMatrixSet)) {
038            throw new IllegalArgumentException(tr("{0} imagery has tileMatrixSet defined to: {1}", imageryType, tileMatrixSet));
039        }
040        this.tileMatrixSet = tileMatrixSet == null ? "" : tileMatrixSet;
041    }
042
043    /**
044     * Returns layer name of the default layer.
045     * @return layer name of the default layer
046     */
047    public String getLayerName() {
048        return layerName;
049    }
050
051    /**
052     * Returns default tileMatrixSet. Only usable for WMTS
053     * @return default tileMatrixSet. Only usable for WMTS
054     */
055    public String getTileMatrixSet() {
056        return tileMatrixSet;
057    }
058
059    /**
060     * Returns style for this WMS / WMTS layer to use.
061     * @return style for this WMS / WMTS layer to use
062     */
063    public String getStyle() {
064        return style;
065    }
066
067    /**
068     * Returns JSON representation of the default layer object.
069     * @return JSON representation of the default layer object
070     */
071    public JsonObject toJson() {
072        JsonObjectBuilder ret = Json.createObjectBuilder();
073        ret.add("layerName", layerName);
074        ret.add("style", style);
075        ret.add("tileMatrixSet", tileMatrixSet);
076        return ret.build();
077    }
078
079    /**
080     * Factory method creating DefaultLayer from JSON objects
081     * @param o serialized DefaultLayer object
082     * @param type of ImageryType serialized
083     * @return DefaultLayer instance based on JSON object
084     */
085    public static DefaultLayer fromJson(JsonObject o, ImageryType type) {
086        return new DefaultLayer(type, o.getString("layerName"), o.getString("style"), o.getString("tileMatrixSet"));
087    }
088}