UNPKG

esri-leaflet

Version:

Leaflet plugins for consuming ArcGIS Online and ArcGIS Server services.

181 lines (146 loc) 4.81 kB
import L from 'leaflet'; import { RasterLayer } from './RasterLayer'; import { cleanUrl } from '../Util'; import mapService from '../Services/MapService'; export var DynamicMapLayer = RasterLayer.extend({ options: { updateInterval: 150, layers: false, layerDefs: false, timeOptions: false, format: 'png24', transparent: true, f: 'json' }, initialize: function (options) { options.url = cleanUrl(options.url); this.service = mapService(options); this.service.addEventParent(this); if ((options.proxy || options.token) && options.f !== 'json') { options.f = 'json'; } L.Util.setOptions(this, options); }, getDynamicLayers: function () { return this.options.dynamicLayers; }, setDynamicLayers: function (dynamicLayers) { this.options.dynamicLayers = dynamicLayers; this._update(); return this; }, getLayers: function () { return this.options.layers; }, setLayers: function (layers) { this.options.layers = layers; this._update(); return this; }, getLayerDefs: function () { return this.options.layerDefs; }, setLayerDefs: function (layerDefs) { this.options.layerDefs = layerDefs; this._update(); return this; }, getTimeOptions: function () { return this.options.timeOptions; }, setTimeOptions: function (timeOptions) { this.options.timeOptions = timeOptions; this._update(); return this; }, query: function () { return this.service.query(); }, identify: function () { return this.service.identify(); }, find: function () { return this.service.find(); }, _getPopupData: function (e) { var callback = L.Util.bind(function (error, featureCollection, response) { if (error) { return; } // we really can't do anything here but authenticate or requesterror will fire setTimeout(L.Util.bind(function () { this._renderPopup(e.latlng, error, featureCollection, response); }, this), 300); }, this); var identifyRequest = this.identify().on(this._map).at(e.latlng); if (this.options.layers) { identifyRequest.layers('visible:' + this.options.layers.join(',')); } else { identifyRequest.layers('visible'); } identifyRequest.run(callback); // set the flags to show the popup this._shouldRenderPopup = true; this._lastClick = e.latlng; }, _buildExportParams: function () { var bounds = this._map.getBounds(); var size = this._map.getSize(); var ne = this._map.options.crs.project(bounds.getNorthEast()); var sw = this._map.options.crs.project(bounds.getSouthWest()); var sr = parseInt(this._map.options.crs.code.split(':')[1], 10); // ensure that we don't ask ArcGIS Server for a taller image than we have actual map displaying var top = this._map.latLngToLayerPoint(bounds._northEast); var bottom = this._map.latLngToLayerPoint(bounds._southWest); if (top.y > 0 || bottom.y < size.y) { size.y = bottom.y - top.y; } var params = { bbox: [sw.x, sw.y, ne.x, ne.y].join(','), size: size.x + ',' + size.y, dpi: 96, format: this.options.format, transparent: this.options.transparent, bboxSR: sr, imageSR: sr }; if (this.options.dynamicLayers) { params.dynamicLayers = this.options.dynamicLayers; } if (this.options.layers) { params.layers = 'show:' + this.options.layers.join(','); } if (this.options.layerDefs) { params.layerDefs = JSON.stringify(this.options.layerDefs); } if (this.options.timeOptions) { params.timeOptions = JSON.stringify(this.options.timeOptions); } if (this.options.from && this.options.to) { params.time = this.options.from.valueOf() + ',' + this.options.to.valueOf(); } if (this.service.options.token) { params.token = this.service.options.token; } return params; }, _requestExport: function (params, bounds) { if (this.options.f === 'json') { this.service.request('export', params, function (error, response) { if (error) { return; } // we really can't do anything here but authenticate or requesterror will fire if (this.options.token) { response.href += ('?token=' + this.options.token); } if (response.href) { this._renderImage(response.href, bounds); } else { this._renderImage(response.imageData, bounds, response.contentType); } }, this); } else { params.f = 'image'; this._renderImage(this.options.url + 'export' + L.Util.getParamString(params), bounds); } } }); export function dynamicMapLayer (url, options) { return new DynamicMapLayer(url, options); } export default dynamicMapLayer;