All files / src/Services/Config ConfigInterface.js

0.99% Statements 1/101
0% Branches 0/66
0% Functions 0/12
0.99% Lines 1/101

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324                                                                    1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 
/**
 * Response object for {@link module:Services~getConfig Gp.Services.getConfig ()} invocation when successful. Received as the argument of onSuccess callback function.
 *
 * @property {Object} generalOptions - General properties for default map configuration resources.
 * @property {Object} generalOptions.apiKeys - Object that associates apiKey (s) with an array of resources IDs availables with that key.
 * @property {String} generalOptions.title - Underlying web service Title.
 * @property {String} generalOptions.defaultGMLGFIStyle - XSL URL used by default to translate an XML GetFeatureInfo response into an HTML array.
 * @property {String} generalOptions.theme - default theme (FIXME : for what ?)
 * @property {Array.<Float>} generalOptions.wgs84Resolutions - geographical resolutions Array for each [zoom level of the Geoportal platform]{@link http://api.ign.fr/tech-docs-js/webmaster/layers.html#Geoportal_resolutions} from 0 to 21. Expressed in degrees/pixel.
 * @property {Object} layers - Associative array mapping resources availables IDs (keys) with their properties (values given as {@link Gp.Services.Config.Layer}).
 * @property {Object} tileMatrixSets - Associative Array mapping TileMatrixSets IDs (keys) availables with their properties (values given as {@link Gp.Services.Config.TileMatrixSet}).
 *
 * @namespace
 * @alias Gp.Services.GetConfigInterface
 */
function ConfigInterface () {
    if (!(this instanceof ConfigInterface)) {
        throw new TypeError("ConfigInterface constructor cannot be called as a function.");
    }
 
    this.generalOptions = {
        apiKeys : {},
        title : null,
        defaultGMLGFIStyle : null,
        theme : null,
        wgs84Resolutions : []
    };
 
    this.layers = {};
 
    this.tileMatrixSets = {};
}
 
ConfigInterface.prototype = {
 
    /*
     * Constructor (alias)
     */
    constructor : ConfigInterface,
 
    /**
     * Check if config is loaded for a given key
     *
     * @param {String} apiKey - Access key to Geoportal platform
     * @returns {Boolean} isKeyConfLoaded - true if config of the key is already loaded, false otherwise
     */
    isKeyConfLoaded : function (apiKey) {
        if (!apiKey) {
            return;
        }
        if (this.generalOptions.apiKeys[apiKey]) {
            return true;
        }
        return false;
    },
 
    /**
     * Returns an array of Geoportal layers identifiers, corresponding to an API contract key.
     *
     * @param {String} apiKey - Access key to Geoportal platform
     * @returns {Array} apiKeys - Array of geoportal layers identifiers
     */
    getLayersId : function (apiKey) {
        return this.generalOptions.apiKeys[apiKey];
    },
 
    /**
     * Returns the id of a Geoportal layer with its name and its service.
     *
     * @param {String} layerName - name of the layer (ex. "ORTHOIMAGERY.ORTHOPHOTOS")
     * @param {String} service   - name of the service (ex. "WMS" ou "WMTS")
     * @returns {String} Id of the layer (ex. "ORTHOIMAGERY.ORTHOPHOTOS$GEOPORTAIL:OGC:WMTS")
     */
    getLayerId : function (layerName, service) {
        if (!layerName || !service) {
            return;
        }
        var layerId = null;
 
        // layer
        // key : [layerName]$[contexte]:OGC:[service]
        // ex : "ORTHOIMAGERY.ORTHOPHOTOS$GEOPORTAIL:OGC:WMTS"
 
        // service
        // key : [layerName]$[contexte];[service]
        // ex : PositionOfInterest$OGC:OPENLS;ReverseGeocode
 
        if (this.layers) {
            var layers = this.layers;
            for (var key in layers) {
                if (layers.hasOwnProperty(key)) {
                    var parts = key.split("$");
                    if (layerName === parts[0]) {
                        if (parts[1]) {
                            var servicePartsLayer = parts[1].split(":");
                            var servicePartsService = parts[1].split(";");
 
                            if (servicePartsService[1] === service.toUpperCase()) {
                                layerId = key;
                                break;
                            }
                            if (servicePartsLayer[2] === service.toUpperCase()) {
                                layerId = key;
                                break;
                            }
                        }
                    }
                }
            }
        }
        if (!layerId) {
            return;
        }
        return layerId;
    },
 
    /**
     * Returns an associative array of Geoportal layers configurations, corresponding to an API contract key.
     * If no key is specified, all layers from configuration are returned.
     *
     * @param {String} apiKey - Access key to Geoportal platform
     * @returns {Object} layers - Object which properties are layers identifiers.
     */
    getLayersConf : function (apiKey) {
        var layers = {};
        var layersIdArray = this.getLayersId(apiKey);
        if (layersIdArray) {
            for (var i = 0; i < layersIdArray.length; i++) {
                var lyrId = layersIdArray[i];
                layers[lyrId] = this.layers[lyrId];
            }
        }
        return layers;
    },
 
    /**
     * Returns a geoportal layer configuration, given its identifier
     *
     * @param {String} layerId - Geoportal layer identifier (e.g. "GEOGRAPHICALGRIDSYSTEMS.MAPS$GEOPORTAIL:OGC:WMTS")
     * @return {Object} layer - Layer configuration
     * @see Gp.Services.Config.Layer
     */
    getLayerConf : function (layerId) {
        if (!this.layers) {
            return;
        }
        return this.layers[layerId];
    },
 
    /**
     * Get all parameters needed to display a WFS, WMS or WMTS layer given its name, its service and its key
     *
     * @param {String} layerName - name of the layer (ex. "ORTHOIMAGERY.ORTHOPHOTOS")
     * @param {String} service   - name of the service (ex. "WMS" ou "WMTS")
     * @param {String} [apiKey]  - Contract API key
     * @returns {Object} params  - params of the service (WFS, WMS or WMTS) for the given layer
     * @returns {String} params.url        - Url of the service to reach to display the layer
     * @returns {String} params.version    - Version of the service
     * @returns {String} params.styles     - Default style of the layer
     * @returns {String} params.format     - Default format of the layer
     * @returns {String} params.projection - Default projection of the layer
     * @returns {Number} params.minScale   - Min scale denominator of the layer
     * @returns {Number} params.maxScale   - Max scale denominator of the layer
     * @returns {Gp.BBox} params.extent    - Extent of the layer, in the projection of the layer
     * @returns {Array} params.legends     - Array of legends associated to the layer
     * @returns {Array} params.title       - Name of the layer, readable by a human
     * @returns {Array} params.description - Description of the layer
     * @returns {String} params.[TMSLink]          - Id of the Tile Matrix Set (TMS), in the case of WMTS layer
     * @returns {Gp.Point} params.[matrixOrigin]   - Originof the tile matrix (top left corner), in the case of WMTS layer
     * @returns {Array} params.[nativeResolutions] - Array with the resolution of each level of the tile matrix set, in the case of WMTS layer
     * @returns {Array} params.[matrixIds]         - Array with the ID of each level of the tile matrix set, in the case of WMTS layer
     */
    getLayerParams : function (layerName, service) {
        var params = {};
 
        if ((service === "WMS" || Object.keys(this.tileMatrixSets).length !== 0) && Object.keys(this.layers).length !== 0) {
            // get the layerId of the layer
            var layerId = this.getLayerId(layerName, service);
 
            if (layerId) {
                // get the layer Conf Object
                var layerConf = this.getLayerConf(layerId);
 
                var keys = this.getLayerKey(layerId);
                if (keys.length === 0) {
                    return;
                }
 
                // get services params
                for (var i = 0; i < keys.length; i++) {
                    // only one serverUrl is saved in Gp.Config : with multiKeys, we have to retrieve the key used in the serverUrl property
                    if (layerConf.serviceParams.serverUrl[keys[i]]) {
                        params.url = layerConf.serviceParams.serverUrl[keys[i]];
                    }
                }
 
                if (service !== "WFS") {
                    const wmsTypeRegex = /\/v\//;
                    // WMS vector style always empty (not in getCap)
                    if (wmsTypeRegex.test(params.url)) {
                        params.styles = " ";
                    } else {
                        // WMS raster style is defined in getCap
                        params.styles = layerConf.styles[0].name;
                    }
                }
 
                params.version = layerConf.serviceParams.version;
                params.format = (layerConf.formats && layerConf.formats.length) ? layerConf.formats[0].name : "";
                params.projection = layerConf.defaultProjection;
 
                // get layer info and constraints
                params.minScale = layerConf.globalConstraint.minScaleDenominator;
                params.maxScale = layerConf.globalConstraint.maxScaleDenominator;
                params.extent = layerConf.globalConstraint.bbox;
                params.legends = layerConf.legends;
                params.title = layerConf.title;
                params.description = layerConf.description;
 
                if (service === "WMS") {
                    params.metadata = layerConf.metadata;
                }
 
                // Informations  non disponibles avec les getCap
                // params.metadata = layerConf.getMetadata();
                // params.originators = layerConf.getOriginators();
                // params.quicklookUrl = layerConf.getQuicklookUrl();
 
                // WMTS : get the tileMatrixSetLimits
                if (layerConf.wmtsOptions) {
                    params.tileMatrixSetLimits = layerConf.wmtsOptions.tileMatrixSetLimits;
                    var TMSLink = layerConf.wmtsOptions.tileMatrixSetLink;
                    if (TMSLink) {
                        params.TMSLink = TMSLink;
                        var tmsConf = this.getTMSConf(TMSLink);
                        // Get matrix origin : Gp.Point = Object{x:Float, y:Float}
                        // params.matrixOrigin = tmsConf.getTopLeftCorner();
                        params.matrixIds = Object.keys(tmsConf.tileMatrices);
                        params.tileMatrices = tmsConf.tileMatrices;
                        // by default, pseudo mercator resolutions
                        params.nativeResolutions = tmsConf.nativeResolutions || this.getTMSConf("PM").nativeResolutions;
                    }
                }
            }
        }
        return params;
    },
 
    /**
     * Get the contract key(s) associated to a given layer.
     *
     * @param {String} layerId - Geoportal layer identifier (e.g. "GEOGRAPHICALGRIDSYSTEMS.MAPS$GEOPORTAIL:OGC:WMTS")
     * @return {Array} layerKey - array of key(s) associated to the given layer
     */
    getLayerKey : function (layerId) {
        var layerKey = [];
        if (this.generalOptions && this.generalOptions.apiKeys && Object.keys(this.generalOptions.apiKeys).length !== 0) {
            var resourcesByKey = this.generalOptions.apiKeys;
            for (var key in resourcesByKey) {
                var resourcesArray = resourcesByKey[key];
                resourcesArray.forEach(function (arrayLayerId) {
                    if (arrayLayerId === layerId) {
                        layerKey.push(key);
                    }
                });
            }
        }
        return layerKey;
    },
 
    /**
     * Returns an associative array of Tile Matrix Sets configurations.
     *
     * @return {Object} tileMatrixSets - Object which properties are TMS identifiers
     *
     */
    getTileMatrixSets : function () {
        return this.tileMatrixSets;
    },
 
    /**
     * Returns a Tile Matrix Sets configuration, given its identifier.
     *
     * @param {String} tmsID - Tile Matrix Set identifier (e.g. : "PM")
     * @return {Object} tileMatrixSet - Tile Matrix Set configuration
     * @see Gp.Services.Config.TileMatrixSet
     */
    getTMSConf : function (tmsID) {
        if (!this.tileMatrixSets) {
            return;
        }
        return this.tileMatrixSets[tmsID];
    },
 
    /**
     * Get global constraints for a given Layer : extent, minScale, maxScale, projection
     *
     * @param {String} layerId - Geoportal layer identifier (e.g. "GEOGRAPHICALGRIDSYSTEMS.MAPS$GEOPORTAIL:OGC:WMTS")
     * @returns {Object} params - layer constraints
     * @returns {String} params.projection - default layer projection
     * @returns {Number} params.minScale   - layer min scale denominator
     * @returns {Number} params.maxScale   - layer max scale denominator
     * @returns {Gp.BBox} params.extent    - layer extent expressed in the layer projection
     */
    getGlobalConstraints : function (layerId) {
        var params = {};
 
        if (layerId) {
            // get layer configuration object
            var layerConf = this.getLayerConf(layerId);
            params.projection = layerConf.defaultProjection;
            params.minScale = layerConf.globalConstraint.minScaleDenominator;
            params.maxScale = layerConf.globalConstraint.maxScaleDenominator;
            params.extent = layerConf.globalConstraint.bbox;
        }
 
        return params;
    }
 
};
 
export default ConfigInterface;