Source: api/KMLLayer.js

Retour à la documentation
define([
        "dojo/_base/declare", "spw/api/DataLayer", "esri/request",
        "dojo/_base/array", "dojo/_base/lang", "esri/renderers/SimpleRenderer",
        "spw/api/GeometryConverter", "dojo/_base/Color", "esri/InfoTemplate",
        'esri/SpatialReference'
    ],
    function(declare, DataLayer, request, array, lang, SimpleRenderer, GeometryConverter, Color,
            InfoTemplate, SpatialReference) {

        /**
         * @class spw.api.KMLLayer
         * @classdesc Layer construit sur base d'un fichier KML (précisé par une URL ou par son contenu)
         * @extends {spw.api.DataLayer}
         */
        return declare("spw.api.KMLLayer", [DataLayer], /** @lends spw.api.KMLLayer.prototype */ {

            _uniqueValueInfos: null,

            rawData: null,

            /**
             * Récupère les données et les transforme en features
             * @memberOf spw.api.DataLayer
             */
            fetchData: function(map, surface) {
                if (this.url) {
                    request({
                        url: this.url,
                        handleAs: 'xml'
                    }).then(
                        lang.hitch(this, function(doc) {
                            if (doc == null) {
                                this.emit('error', lang.mixin(new Error('not a valid KML file'), {
                                    type: 'loadError'
                                }));
                                return;
                            }
                            this.geoJSON = GeometryConverter.kmlToGeoJSON(doc);
                            this.features = this.geoJSON.features;

                            this.processKMLFeatures();
                            this.processFeatures();
                        }),
                        lang.hitch(this, function(err) {
                            this.emit('error', lang.mixin(err, {
                                type: 'loadError'
                            }));
                        })
                    );
                }
                else if (this.rawData) {
                    var parser = new DOMParser();

                    var doc = parser.parseFromString(this.rawData.replace(/xsi:schemaLocation="[^"]*"/gim, ""), 'text/xml');

                    this.geoJSON = GeometryConverter.kmlToGeoJSON(doc);
                    this.features = this.geoJSON.features;

                    this.processKMLFeatures();
                    this.processFeatures();
                }
                else if (this.features) {
                    this.processFeatures();
                }
            },

//             _configureRenderer: function() {
//                 if (this.showTemplate) {
//                     this.infoTemplate = new InfoTemplate("Description", "${description}");
//                 }
//
//                 if (this._uniqueValueInfos == null || this._uniqueValueInfos.length <= 0) {
//                     this.inherited(arguments);
//                     return;
//                 }
//
//                 var defaultType = this._uniqueValueInfos[0].symbol.type;
//                 var defaultStyle = this._uniqueValueInfos[0].symbol.style;
//                 var defaultOutlineType = this._uniqueValueInfos[0].symbol.outline.type;
//                 var defaultOutlineStyle = this._uniqueValueInfos[0].symbol.outline.style;
//
// //                var json = {
// //                    "type": "uniqueValue",
// //                    "field1": "name",
// //                    "defaultSymbol": {
// //                        "type": defaultType,
// //                        "style": defaultStyle,
// //                        "color": [0, 50, 200, 128],
// //                        "outline": {
// //                            "type": defaultOutlineType,
// //                            "style": defaultOutlineStyle,
// //                            "color": [190, 190, 0, 255],
// //                            "width": 2
// //                        }
// //                    },
// //                    "uniqueValueInfos": this._uniqueValueInfos
// //                };
//
//                 var simpleJson =  {
//          		   "type": "simple",
//          		   "symbol": {
//          		    "type": "esriSFS",
//          		    "style": "esriSFSSolid",
//          		    "color": this._uniqueValueInfos[0].symbol.color,
//          		    "outline": {
//          		     "type": "esriSLS",
//          		     "style": "esriSLSSolid",
//          		     "color": this._uniqueValueInfos[0].symbol.outline.color,
//          		     "width": this._uniqueValueInfos[0].symbol.outline.width
//          		    }
//          		   },
//          		   "label": "",
//          		   "description": ""
//          		  };
//
//                 this.setRenderer(new SimpleRenderer(simpleJson));
//             },

            _kmlColorToRgb: function(color) {
                if (color == null) {
                    return [0, 50, 200, 128];
                }
                return '#' + color.substring(5) + color.substring(3, 5) + color.substring(1, 3);
            },

            /**
             * Transforme les features KML en features du GraphicsLayer
             */
            processKMLFeatures: function() {
                var newFeatures = [];

                this._uniqueValueInfos = this._uniqueValueInfos || [];

                array.forEach(this.features, lang.hitch(this, function(f) {
                    if (f.properties.fill || f.properties.stroke) {
                        var type = null;
                        var style = null;

                        if (f.geometry.geometries && f.geometry.geometries.length > 0) {
                            if (f.geometry.geometries[0].type === 'Polygon') {
                                type = 'esriSFS';
                                style = 'esriSFSSolid';
                            }
                            else if (f.geometry.geometries[0].type === 'Line' || f.geometry.geometries[0].type === 'LineString') {
                                type = 'esriSLS';
                                style = 'esriSLSSolid';
                            }
                            else {
                                type = 'esriSMS';
                                style = 'esriSMSCircle';
                            }
                        }

                        var fillColor = new Color(this._kmlColorToRgb(f.properties.fill));
                        fillColor = fillColor.toRgba();

                        if (f.properties['fill-opacity']) {
                            fillColor[3] = f.properties['fill-opacity'] * 255;
                        }

                        var strokeColor = null;
                        var strokeWidth = null;

                        strokeColor = new Color(this._kmlColorToRgb(f.properties.stroke));
                        strokeColor = strokeColor.toRgba();

                        if (f.properties['stroke-opacity']) {
                            strokeColor[3] = f.properties['stroke-opacity'] * 255;
                        }

                        strokeWidth = f.properties['stroke-width'] ? f.properties['stroke-width'] : 1;

                        this._uniqueValueInfos.push({
                            "value": f.properties.name,
                            "symbol": {
                                "color": fillColor,
                                "outline": {
                                    "color": strokeColor,
                                    "width": strokeWidth,
                                    "type": "esriSLS",
                                    "style": "esriSLSSolid"
                                },
                                "type": type,
                                "style": style
                            }
                        });
                    }

                    if (f.geometry.geometries) {
                        newFeatures = newFeatures.concat(array.map(f.geometry.geometries, function(g) {
                            return {
                                geometry: g,
                                properties: f.properties/*,
                                srid: 4326*/
                            };
                        }));
                    }
                    else {
                        newFeatures.push(f);
                    }
                }));

                this.features = newFeatures;
            }

        });
    });