import { WMS_URL } from '../../../conf/Constants' import WmsLayerList from '../dataset/WmsLayerList' /** * todo 得考虑一个图层配置了多个 wmsLayers的情况 */ class WmsLayerService { constructor (layersConfig) { this.L = window.L this.map = window.map this.popupComp = window.popupComp // wms getfeatureinfo 默认参数 this.params = { VERSION: '1.1.1', SERVICE: 'WMS', REQUEST: 'GetFeatureInfo', // bbox: bbox, FORMAT: 'image/png', INFO_FORMAT: 'application/json', TRANSPARENT: true, FEATURE_COUNT: 50, maxZoom: 21, SRS: 'EPSG:4326', EXCEPTIONS: 'application/vnd.ogc.se_inimage' } this.layersConfig = layersConfig // 存放getfeatureinfo的图层组 this.featureGroup = this.L.featureGroup({}).addTo(this.map) this.wmsLayerList = new WmsLayerList() if (layersConfig) { for (var i = 0; i < layersConfig.length; i++) { const config = layersConfig[i] this.wmsLayerList.addConfig(config) } } this.load() } init () { } addAll (configs) { for (let i = 0; i < configs.length; i++) { const config = configs[i] const layers = config.layers if (layers) { this.addAll(layers) } this.wmsLayerList.addConfig(config) } this.reload() } add (config) { this.wmsLayerList.addConfig(config) this.reload() } removeAll (configs) { for (let i = 0; i < configs.length; i++) { const config = configs[i] const layers = config.layers if (layers) { this.removeAll(layers) } this.wmsLayerList.remove(config.typeName, config.filter) } this.reload() } remove (config) { this.wmsLayerList.remove(config.typeName, config.filter) this.reload() } reload () { const layers = this.wmsLayerList.getLayers() || '' const filter = this.wmsLayerList.getFilters() || '' const params = {} params.cql_filter = filter params.layers = layers this.wmsLayer.setParams(params, false) } load () { const layers = this.wmsLayerList.getLayers() const filter = this.wmsLayerList.getFilters() const params = { format: 'image/png', // 返回的数据格式 transparent: true, maxZoom: 21, BBOX: this.map.getBounds().toBBoxString() } if (layers) { params.layers = layers } if (filter.length > 0) { params.cql_filter = filter } this.wmsLayer = this.L.tileLayer.wms(WMS_URL, params).addTo(this.map) } } export default WmsLayerService