Source: api/DataMapService.js

Retour à la documentation
define([
        'dojo/_base/declare',
        'spw/api/GraphicsMapService',
        'spw/api/DataLayer', 'dojo/_base/array', 'dojo/_base/lang',"spw/api/GeometryConverter",
        "esri/SpatialReference", "spw/api/MessageManager", "esri/graphic","spw/api/ProjectionManager", "spw/api/SpwViewer"
    ],
    function(declare, GraphicsMapService, DataLayer, array, lang,GeometryConverter,
             SpatialReference, MessageManager, Graphic, ProjectionManager, SpwViewer) {

        /**
         * @class spw.api.DataMapService
         * @classdesc Service du viewer permettant d'afficher des données JSON récupérées depuis une URL
         * @extends {spw.api.MapService}
         */
        var DataMapService = declare('spw.api.DataMapService', [GraphicsMapService], /** @lends spw.api.DataMapService.prototype */ {

            label: 'Renommer',
            hasLegend: false,
            inTOC: true,
            minScale: 0,
            maxScale: 0,
            loaded: false,
            visible: true,
            toLoad: true,
            editable: true,

            //Style property to configure rendering of graphics on the map
            style: null,

            newLayer: true,
            currentDrawing: false,

            defaultProperties: [],
            canDefineDefaultProperties: false,

            /**
             * Crée le layer Esri sur base de la configuration du MapService.
             * @memberOf spw.api.MapService
             */

            constructor: function(opts) {
                lang.mixin(this, opts);
                this._propertiesToKeep.push('graphics');
            },

            globalType: 'DATA_MAP_SERVICE',
            createMapLayer: function() {
                if(!this.layer) {
                    var _graphicsJson = this.layer ? this.layer.graphicsToJson() : this.graphicsJSON;
                    if (!_graphicsJson && this.graphics) {
                        _graphicsJson = this.graphics;
                    }
                    var opts = {
                        id: this.serviceId,
                        minScale: 0,
                        maxScale: 0,
                        spwMap: this.spwMap,
                        graphicsJSON: _graphicsJson,
                        style: this.style,
                        parentLayer: this
                    };
                    var mixedParameters = lang.mixin({}, opts, this.data);
                    this.layer = new DataLayer(mixedParameters);
                }

                this.inherited(arguments);

                if (this.geoJsonInfo) {
                    this.importData(this.geoJsonInfo);
                }

                var loadedFunction = function(){
                    this.set('loaded', true);
                    this.emit(this.events.MapServiceLoaded, this);

                    if(this.spwMap){
                        this.spwMap.mapServiceLoaded(this);
                    }

                }.bind(this);

                if(this.layer.dataLoaded) {
                    loadedFunction();
                } else {
                    this.layer.on('dataLoaded', loadedFunction);
                }
            },

            reset: function() {
                this.graphics = this.layer.graphicsToJson();
                this.layer.graphicsJSON = this.graphics;
                this.inherited(arguments);
            },

            importData: function(geoJsonFeatures) {
                this.layer.importData(geoJsonFeatures);
            },

            exportData: function() {
                var layer = this.get('layer');
                if (layer) {
                    var graphics = layer.graphics;
                    var geoJsonObject = {type: 'FeatureCollection', features: []};
                    geoJsonObject.features = array.map(graphics, lang.hitch(this, function(g) {
                        var feature = GeometryConverter.esriToGeoJSON(g);
                        var symbol = g.symbol;
                        if (symbol) {
                            feature.symbol = symbol.toJson();
                        }
                        return feature;
                    }));
                    var crs = {
                        type: "EPSG",
                        properties: {
                            code: this.spwMap.getSpatialReference().wkid
                        }
                    };
                    geoJsonObject.crs = crs;
                    geoJsonObject.layer = this.label;
                    return geoJsonObject;
                }
                return {};
            },

            downloadData: function() {
                var geoJsonObject = this.exportData();
                var blob = new Blob([JSON.stringify(geoJsonObject)], {type: "application/json"});
                var url = URL.createObjectURL(blob);
                var link = document.createElement('a');
                link.setAttribute('href', url);
                link.style.display = 'none';
                link.setAttribute('download', this.label + '.json');
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            },

            setGraphics: function(graphics) {
                if (graphics == null) {
                    return;
                }

                this.layer.jsonToGraphics(graphics);
            },
            getServiceConfig: function(toKeep) {
                var cfg = this.inherited(arguments);

                toKeep = toKeep || this._propertiesToKeep;

                if (toKeep.indexOf('graphics')) {
                    cfg.graphics = this.get('layer').graphicsToJson();
                }

                cfg.editable = this.editable;

                return cfg;
            }

        });

        return DataMapService;

    });