派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-05-20 ec4d5c1827487f4c901b69bd9eae58e111e82b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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