Newer
Older
urbanLifeline_YanAn / public / static / libs / mapbox / extend / WMSLayer.js
@zhangqy zhangqy on 3 Oct 3 KB first commit
(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ?
		module.exports = factory() :
		typeof define === 'function' && define.amd ? define(factory) :
			(global = typeof globalThis !== 'undefined' ? globalThis :
				global || self, global.mapboxgl1.WMSLayer = factory());
}(window,(function(){

	function generateUUID() {
		let d = new Date().getTime();
		let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
			let r = (d + Math.random()*16)%16 | 0;
			d = Math.floor(d/16);
			return (c=='x' ? r : (r&0x3|0x8)).toString(16);
		});
		return uuid;
	}

	function get(url, callback, async) {
		var xhr = new XMLHttpRequest();
		xhr.open('GET', url, async === false ? false : true);
		xhr.responseType = "arraybuffer";
		xhr.onabort = function (event) {
			callback(true, null);
		};
		xhr.onload = function (event) {
			if (!xhr.status || xhr.status >= 200 && xhr.status < 300) {
				var source;
				source = xhr.response;
				if (source) {
					try {
						source = eval("(" + source + ")");
					} catch (e) {
					}
				}
				if (source) {
					callback(false, source);
				} else {
					callback(false, null);
				}
			}
		};
		xhr.onerror = function (e) {
			callback(true, null);
		};
		xhr.send(null);
		return xhr;
	}

	return class WMSLayer {
		static WMS_PARAMS={
			bbox:'{bbox-epsg-3857}',
			format:'image/png',
			service:'WMS',
			version:'1.1.1',
			request:'GetMap',
			srs:'EPSG:3857',
			transparent:true,
			width:256,
			height:256,
		};
		id=null;
		map=null;
		type="raster";
		params=null;
		constructor(options={id:'',url:'',layers:''}) {
			this.params = Object.assign(WMSLayer.WMS_PARAMS,options);
			this.id = options.id || generateUUID();
		}

		remove(){
			if(!this.map) return console.warn(`Please trigger "addTo" This method is called after the "map" instance is passed in;`);
			let layer = this.map.getLayer(this.id);
			if(!layer) return console.warn(`The current layer has not been added to the map;`);
			this.map.removeLayer(this.id);
			this.map.removeSource(this.id);
		}

		addTo(map){
			if(!map) return console.warn(`Configure the map instance;`);
			this.map = map;
			if(!this.params.url || !this.params.layers) return console.error(`The current object is missing important parameters. Please add them and trigger the method again; url:${this.params.url};url:${this.params.layers}`);
			let reqParams = Object.keys(this.params).map(key => key != 'url' && `${key}=${this.params[key]}`).filter(Boolean).join('&');
			this.map.addSource(this.id, {'type':this.type, 'tiles': [`${this.params.url}?${reqParams}`], 'tileSize': this.params.width || 256});
			this.map.addLayer({'id':this.id, 'type':this.type, 'source': this.id, 'paint': {}});
		}

		show(){
			this.setLayerParams({visibility:'visible'});
		}

		hide(){
			this.setLayerParams({visibility:'none'});
		}

		setOpacity(opacity){
			this.setLayerParams({},{'raster-opacity':opacity});
		}

		setInitializeParams(params={}){
			this.params = Object.assign(this.params,params);
		}

		setLayerParams(layout={},paint={}){
			let layoutKeys = Object.keys(layout);
			let paintKeys = Object.keys(paint);
			layoutKeys.forEach(key => this.map.setLayoutProperty(this.id,key,layout[key]));
			paintKeys.forEach(key => this.map.setPaintProperty(this.id,key,paint[key]));
		}
	}
})));