Source: api/CsvAddressMapService.js

Retour à la documentation
define([
        'dojo/_base/declare',
        'dojo/_base/lang',
        'dojo/_base/array',
        'spw/api/GraphicsMapService',
        'spw/api/CsvLayer',
        'spw/api/Utils',
        "dojo/promise/all",
        "dojo/Deferred",
        "esri/graphic", "esri/geometry/Point",
        'esri/toolbars/edit'
    ],
    function(declare, lang, array, GraphicsMapService, CsvLayer, Utils, all, Deferred, Graphic, Point,
             Edit) {

        /**
         * @class spw.api.CsvAddressMapService
         * @classdesc Service du viewer permettant d'afficher le contenu d'un fichier CSV contenant des adresses
         * @extends {spw.api.GraphicsMapService}
         */
        var CsvAddressMapService = declare('spw.api.CsvAddressMapService', [GraphicsMapService], /** @lends spw.api.CsvAddressMapService.prototype */ {

            geolocApiUrl: '//geoservices.wallonie.be/geolocalisation/js/SpwGeolocalisationApi.js',

            apiLoaded: false,

            csvAsJson: null,

            /**
             * Crée le layer Esri sur base de la configuration du MapService.
             * @memberOf spw.api.MapService
             */
            createMapLayer: function() {
                console.log('create map layer');
                this.handleCsvAddress(this.csvAsString).then(lang.hitch(this, function(){
                    // this.layer = new CsvLayer(lang.mixin({
                    //     id: this.serviceId,
                    //     _mapService: this
                    // }, this.csvInfo));
                    // this.inherited(args);
                    this.createLayer();
                }));
            },

            createLayer: function() {
                this.layer = this.spwViewer.get('spwMap').addMapService({
                    type: "DRAW_LAYER",
                    label: "Edition d'adresse",
                    graphics: null,
                    inTOC: false
                });
                this._graphicLayer = this.layer.get('layer');
                this.addPointsOnMap();
                this.createToolbar();
                this._graphicLayer.on('click', lang.hitch(this, function(evt) {
                    try{
                        this._editToolbar.deactivate();
                    }catch(e){
                        debugger;
                    }
                    this._editToolbar.activate(Edit.EDIT_VERTICES|Edit.EDIT_TEXT|Edit.MOVE|Edit.ROTATE|Edit.SCALE, evt.graphic);
                    this._editEndHandler.push(this._editToolbar.on("graphic-move-stop", lang.hitch(this, this.onGraphicEditEnd)));
                }));
            },

            addPointsOnMap: function() {
                var feature = {geometry:{type: 'point'}};
                this.spwViewer.get('spwMap').createDefaultSymbol(feature);
                this.symbol = feature.symbol;
                array.forEach(this.csvAsJson, lang.hitch(this, function(adr, index) {
                    var pt = new Point(adr.X, adr.Y, this.spwViewer.get('spatialReference'));
                    pt.index = index;
                    var graphic = Graphic(pt, this.symbol);
                    this._graphicLayer.add(graphic);
                }));
            },

            createToolbar: function() {
                this._editToolbar = new Edit(this.spwViewer.get('spwMap').get('esriMap'), { textSymbolEditorHolder: this.txtSymbolEditorHolder });
                this._editEndHandler.push(this._editToolbar.on("graphic-move-stop", lang.hitch(this, this.onGraphicEditEnd)));
            },

            onGraphicEditEnd: function(evt) {
                console.log('evt:', evt);
            },

            // MOVE CODE
            handleCsvAddress: function(stringData) {
                var addresses = Utils.csvToJson(stringData);
                var def = new Deferred();
                require([this.geolocApiUrl], lang.hitch(this, function(){
                    if(typeof(spwGeolocalisation) != "undefined"){
                        this.set('apiLoaded', true);
                        this.onGeolocLoaded(addresses, def);
                    }
                }));
                return def;
            },

            onGeolocLoaded: function(addresses, def) {
                this.csvAsJson = addresses;
                var promises = [];
                array.forEach(addresses, lang.hitch(this, function(adr) {
                    var def = new Deferred();
                    spwGeolocalisation.searchAll(
                        adr.STREET + ' ' + adr.HOUSE + ' ' + adr.CITY,
                        lang.hitch(this, function(res){def.resolve({geocodeResult: res, address: adr})}),
                        lang.hitch(this, function(err){def.reject(err)})
                    );
                    promises.push(def);
                }));
                all(promises).then(lang.hitch(this, function(allRes) {
                    this.handleGeolocRes(allRes, def);
                }))
            },


            handleGeolocRes: function(res, def) {
                var addresses = [];
                array.forEach(res, lang.hitch(this, function(result) {
                    var geocodeResult = result.geocodeResult;
                    var address = result.address;
                    if (geocodeResult.errorCode == 0) {
                        var results = array.filter(geocodeResult.resultats, function(r){
                            return r.type == 'POSITION';
                        });
                        address.X = results[0].x;
                        address.Y = results[0].y;
                        addresses.push(address);
                    } else {
                        console.log('ERREUR DE GEOLOC:', result);
                    }
                }));

                this.csvInfo = Utils.jsonToCSV(this.csvAsJson);
                def.resolve();
            }

        });

        return CsvAddressMapService;

    });