Source: api/TiledMapService.js

Retour à la documentation
define([
    'dojo/_base/declare',
    'dojo/_base/array',
    'dojo/_base/lang',
    'spw/api/MapService',
    'esri/layers/ArcGISTiledMapServiceLayer',
    'esri/layers/ImageParameters',
    'esri/tasks/IdentifyTask',
    'esri/tasks/IdentifyParameters',
    'spw/api/ProjectionManager'
],
function(declare, array, lang, MapService, ArcGISTiledMapServiceLayer, ImageParameters,
        IdentifyTask, IdentifyParameters, ProjectionManager) {

    /**
     * @class spw.api.DataMapService
     * @classdesc Service du viewer correspondant à un ArcGISTiledMapServiceLayer d'ESRI
     * @extends {spw.api.MapService}
     */
    var TiledMapService = declare('spw.api.TiledMapService', [MapService], /** @lends spw.api.TiledMapService.prototype */ {

        /**
         * Crée le layer Esri sur base de la configuration du MapService.
         * @memberOf spw.api.MapService
         */
        createMapLayer: function() {
            this.layer = this._createTiledLayer({
                imageFormat:this.get('imageFormat'),
                serviceId: this.get('serviceId'),
                resampling: this.get('resampling'),
                alpha: this.get('alpha'),
                displayLevels: this.displayLevels,
                url: this.get('url')
            });

            this.addClusterAndHeatLayers();

            this.inherited(arguments);
        },

        _createTiledLayer: function(config){
            var imageParameters = new ImageParameters();

            imageParameters.format = config.imageFormat;

            var layerConfig = {
                id: config.serviceId,
                resampling: config.resampling,
                opacity: config.alpha / 100,
                imageParameters:imageParameters
            };

            if(config.displayLevels) {
                layerConfig.displayLevels = config.displayLevels;
            }

            return new ArcGISTiledMapServiceLayer(config.url, layerConfig);
        },

        /**
         * Identifie les données du service à un point donné
         */
        identify: function(options, success, error) {
            var identifyParams = new IdentifyParameters();

            options.ignoreFields = options.ignoreFields || [];
            options.ignoreFields = options.ignoreFields.concat(this.ignoreAttributes);

            identifyParams.tolerance = options.tolerance ? options.tolerance : 5;
            identifyParams.returnGeometry = options.returnGeometry;
            identifyParams.spatialReference = this.spwMap.get('esriMap').spatialReference;

            var identifiableLayers = new Array();

            for(key in this.mapServiceLayers) {
                if(this.mapServiceLayers[key].get('identifiable')
                    && this.mapServiceLayers[key].get('subLayers').length == 0
                    && this.mapServiceLayers[key].isVisible()
                    && this.mapServiceLayers[key].visibleAtScale(this.spwMap.getCurrentScale())) {

                    identifiableLayers.push(this.mapServiceLayers[key].get('layerId'));
                }
            }

            identifyParams.layerIds = identifiableLayers;

            identifyParams.layerOption = options.layerOption ? options.layerOption : IdentifyParameters.LAYER_OPTION_VISIBLE;
            identifyParams.width = this.spwMap.get('esriMap').width;
            identifyParams.height = this.spwMap.get('esriMap').height;

            if (options.mapPoint && options.mapPoint.spatialReference) {
                options.mapPoint = ProjectionManager.getInstance().transform(options.mapPoint.spatialReference.wkid, identifyParams.spatialReference.wkid, options.mapPoint);
            }

            identifyParams.geometry = options.mapPoint;
            identifyParams.mapExtent = this.spwMap.get('esriMap').extent;
            identifyParams.layerDefinitions = this.get('layerDefinitions');

            return new IdentifyTask(this.get('url')).execute(identifyParams,
                lang.hitch(this, function(identifyResults) {
                    var data = array.map(identifyResults, lang.hitch(this, function(result){
                        var r = {};
                        var spwLayer = this.mapServiceLayers[result.layerId];
                        if(spwLayer && spwLayer.fields){
                            //r.OBJECTID = result.feature.attributes.OBJECTID;
                            array.forEach(spwLayer.fields, function(field){
                                if (options.ignoreFields && options.ignoreFields.indexOf(field.name) > -1) {
                                    return;
                                }

                                r[field.alias] = result.feature.attributes[field.name];
                            });
                        } else {
                            for (var key in result.feature.attributes) {
                                if (result.feature.attributes.hasOwnProperty(key)) {
                                    if (options.ignoreFields && options.ignoreFields.indexOf(key) > -1) {
                                        continue;
                                    }

                                    r[key] = result.feature.attributes[key];
                                }
                            }
                        }
                        return {layerId: result.layerId, data: r };
                    }));
                    success(identifyResults, this, data);
                }), error);
        },

        /**
         * Appelé lorsque le layer est chargé
         */
        layerLoaded: function(loadedEvent) {
            if (this.layer.spatialReference && this.layer.spatialReference.wkid !== this.spwMap.spwViewer.get('spatialReference').wkid) {
                this.layerError('Ce service n\'est pas compatible avec le système de projection du fond de plan actuel.');
                return;
            }

            array.forEach(loadedEvent.layer.layerInfos, lang.hitch(this, function(layerInfo) {
                this._mergeLayerInfo(layerInfo);
            }));

            this._afterLayerLoaded();
        }

    });

    return TiledMapService;

});