Source: api/ShapefileLayer.js

Retour à la documentation
define([
        "dojo/_base/declare", "spw/api/DataLayer", "esri/request",
        "spw/api/Utils", "dojo/_base/array", "dojo/_base/lang",
        "esri/InfoTemplate",

        "spw/libs/shp"
    ],
    function(declare, DataLayer, request, Utils, array, lang, InfoTemplate, shp) {

        var getFullUrl = function(url) {
            var a = window.document.createElement('a');
            a.href = url;
            return a.cloneNode(false).href;
        };

        /**
         * @class spw.api.ShapefileLayer
         * @classdesc Layer construit sur base d'un ShapeFile
         * @extends {spw.api.DataLayer}
         */
        return declare("spw.api.ShapefileLayer", [DataLayer], /** @lends spw.api.ShapefileLayer.prototype */ {

            //START : options
            shpData: null,
            dbfData: null,
            //END   : options

            /**
             * @constructs
             */
            constructor: function(options) {
                this._options = options;
                lang.mixin(this, {
                    format: 'SHAPE'
                }, options);

                if (this.showTemplate) {
                    this.infoTemplate = new InfoTemplate("Description", "${*}");
                }
            },

            /**
             * Récupère les données et les transforme en features
             * @memberOf spw.api.DataLayer
             */
            fetchData: function(map, surface) {
                if (this.features) {
                    this.processFeatures();
                } else if (this.shpData == null) {
                    if (getFullUrl(this.url).indexOf(window.location.hostname) < 0) {
                        this.url = this._mapService.spwMap.spwViewer.proxyPageUrl + '?' + this.url;
                    }

                    this.processShapes(this.url);
                }
                else {
                    this.processShapes(this.shpData);
                }
            },

            /**
             * Appelé lors d'une erreur de lecture du shape file
             * @param err l'erreur survenue
             */
            onShpFail: function(err) {
                this.emit('error', lang.mixin(new Error('failed to load ' + this.url), {
                    type: 'loadError'
                }));
                this._mapService.layerError({
                    message: 'Impossible de charger le shapefile' + ((typeof err === 'string') ? ' (' + err + ')' : ''),
                    type: 'loadError'
                });
            },

            noGeometriesInFile: function() {
                this.emit('error', lang.mixin(new Error('failed to load ' + this.url), {
                    type: 'loadError'
                }));
                this._mapService.layerError({
                    message: 'Impossible de charger le shapefile, le fichier ne contient aucune géométrie',
                    type: 'loadError'
                });
            },


            onDbfFail: function() {
                this.emit('error', lang.mixin(new Error('failed to load ' + this.dbfUrl), {
                    type: 'loadError'
                }));
            },

            /**
             * Appelé lorsque le shape file est chargé correctement
             * @param err l'erreur survenue
             */
            onShpComplete: function(oHTTP) {
                var binFile = oHTTP.binaryResponse;
                console.log('got data for ' + this.url + ', parsing shapefile');
                this.processShapes(binFile);
            },

            /**
             * Parcourt le shape file et crée les géométries
             * @param shpBin le shape file
             */
            processShapes: function(shpBin, dbfBin) {
                if (window._unzip) {
                    var unzipped = window._unzip(shpBin);
                    var foundPrj = false;

                    for (var key in unzipped) {
                        if (unzipped.hasOwnProperty(key) && key.indexOf('.prj') > -1) {
                            foundPrj = true;
                            break;
                        }
                    }

                    if (!foundPrj) {
                        console.error('no prj file');
                        this.onShpFail('Le shapefile ne contient pas de fichier précisant la projection spatiale (.prj)');
                        return;
                    }
                }

                shp(shpBin).then(lang.hitch(this, function(geojson) {
                    this.features = array.map(geojson.features, function(feature) { return lang.mixin(feature, {srid: 4326}); });
                    if (this.features.length > 0) {
                        this.processFeatures();
                    } else {
                        this.noGeometriesInFile();
                    }
                }))['catch'](lang.hitch(this, function(err) {
                    console.error(err);
                    this.onShpFail();
                }));
            },

            onDbfComplete: function(oHTTP) {
                var binFile = oHTTP.binaryResponse;
                console.log('got data for ' + this.dbfUrl + ', parsing dbf file');

                this.shpLoader = new BinaryAjax(this.url, lang.hitch(this, function(oHTTPShp) {
                    var binFileShp = oHTTPShp.binaryResponse;
                    console.log('got data for ' + this.url + ', parsing shapefile');
                    this.processShapes(binFileShp, binFile);
                }), lang.hitch(this, this.onShpFail));
            }


        });
    });