派生自 wuyushui/SewerAndRainNetwork

XingChuan
2021-05-30 2d5e75bbc04d8c4c5c7dc6bb141ad16ffa2d9936
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import WfsLayerService from './WfsLayerService'
import { logicMapper, SERVICE_TYPE } from '../../../conf/Constants'
import WmsLayerService from './WmsLayerService'
 
/**
 *  init 只初始化一次
 *  start 每次调用图层显示show()时,都会调用
 *  destory 每次调用图层隐藏hide()时,都会调用
 *
 */
class LayerFactory {
  constructor (options) {
    this.L = options.L
    this.map = window.map
    this.layers = {}
    this.layersLogic = {}
    this.minZoomLayers = {}
    this.wmsLayers = []
    this.wmsLayerService = null
  }
 
  init (layerConfig) {
    // wms服务只需要初始化一次
    this.wmsLayerService = new WmsLayerService()
    this.wmsLayerService.init()
    this.initConfig(layerConfig)
  }
 
  initConfig (layerConfig) {
    // 1. 遍历layer config
    if (layerConfig) {
      for (var i = 0; i < layerConfig.length; i++) {
        var config = layerConfig[i]
        var layers = config.layers
        var childLayer = config.childLayer
        var checked = config.checked
        /* if (config.groupName) {
          const wmsGroupLayerService = new WmsLayerGroupService(config)
          wmsGroupLayerService.init()
          continue
        } */
        layers && this.initConfig(config.layers)
        childLayer && this.initConfig(config.childLayer)
 
        this.initMinZoom(config)
        this.loadLogic(config)
        checked && this.show(config)
      }
    }
  }
 
  initMinZoom (config) {
    const minZoom = parseInt(config.minZoom)
    if (minZoom) {
      var configs = this.minZoomLayers[minZoom]
      if (configs) {
        configs[configs.length] = config
      } else {
        configs = [config]
      }
      this.minZoomLayers[minZoom] = configs
    }
  }
 
  loadLogic (config) {
    var code = config.code
    var type = config.type
 
    const file = logicMapper[code]
    var logic = this.layersLogic[code]
    if (!logic) {
      if (file) {
        var BusiLayer = require('../logic/' + file)
        logic = new BusiLayer()
      } else if (type === SERVICE_TYPE.WFS) {
        logic = new WfsLayerService(config)
      }
    }
    this.layersLogic[code] = logic
    return logic
  }
 
  /**
   * 1. 先调用处理逻辑的 initLayer ,如果没有 就创建一个 featureGroup
   * 2. 如果存在事件逻辑的话,绑定tooltip,click事件
   * 3. 将layer添加到map
   * 4. 返回layer
   * @param config
   * @returns layer
   */
  addLayer (config) {
    var code = config.code
    var logic = this.loadLogic(config)
    var layer = (logic && logic.initLayer && logic.initLayer((this.L))) || this.L.featureGroup({})
 
    if (logic.bindTooltip) {
      // 全局tips位置
      layer.bindTooltip(logic.bindTooltip, { direction: 'top', offset: [0, -15], sticky: false })
    }
    // 调用click事件
    if (logic.clickListener) {
      layer.on('click', logic.clickListener)
    }
    layer.addTo(this.map)
    this.layers[code] = layer
    return layer
  }
 
  showAll (configs) {
    if (Array.isArray(configs)) {
      for (let i = 0; i < configs.length; i++) {
        const config = configs[i]
        this.show(config)
      }
      this.wmsLayerService && this.wmsLayerService.addAll(configs)
    }
  }
 
  /**
   * 如果 存在已经加载了的对象,就直接加到map
   * 如果 不存在则 调用 addLayer 及 逻辑类的init 进行初始化操作
   * 如果 存在start函数,则调用
   * @param config
   */
  show (config) {
    var index = config.index
    var layer = this.layers[config.code]
    var logic = this.loadLogic(config)
    if (layer) {
      if (!this.map.hasLayer(layer)) {
        index && layer.setZIndex(index)
        layer.addTo(this.map)
      }
    } else {
      logic && logic.init(this.addLayer(config), this.L, config)
    }
    logic && logic.start && logic.start()
    this.wmsLayerService && this.wmsLayerService.add(config)
  }
 
  hideAll (configs) {
    if (Array.isArray(configs)) {
      for (let i = 0; i < configs.length; i++) {
        this.hide(configs[i])
      }
      this.wmsLayerService && this.wmsLayerService.removeAll(configs)
    }
  }
 
  hide (config) {
    const code = config.code
    const layer = this.layers[code]
    layer && this.map.removeLayer(layer)
    const logic = this.loadLogic(config)
    logic && logic.destory && logic.destory()
    this.wmsLayerService && this.wmsLayerService.remove(config)
  }
 
  /**
     * 控制显示的级别
     * @param layerConfig
     */
  initEvent (layerConfig) {
    // this.map.on('zoomend ', () => this.toggleByZoom())
  }
 
  toggleByZoom () {
    const zoom = this.map.getZoom()
    for (var k in this.minZoomLayers) {
      const configs = this.minZoomLayers[k]
      for (var j in configs) {
        const config = configs[j]
        const checked = config.checked
        // console.log(zoom)
        // console.log(k)
        if (checked && zoom > k) {
          this.show(config)
        } else if (checked && zoom < k) {
          this.hide(config)
        }
      }
    }
  }
}
 
export default LayerFactory