派生自 wuyushui/SewerAndRainNetwork

chenyabin
2021-04-25 ab0971ff6391321fb903d8fad8dea3627e4b69ad
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import WfsLayerService from './WfsLayerService'
import WmsLayerService from './WmsLayerService'
import { clone } from '../../../utils/utils'
import { logicMapper } from '../../../conf/Constants'
class LayerFactory {
  constructor (options) {
    this.L = options.L
    this.map = window.map
    this.layers = {}
    this.layersLogic = {}
    this.minZoomLayers = {}
  }
 
  init (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
        layers && this.init(config.layers)
        childLayer && this.init(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 wfs = config.wfs
    var wms = config.wms
 
    const file = logicMapper[code]
    var logic = this.layersLogic[code]
    if (!logic) {
      if (file) {
        var BusiLayer = require('../logic/' + file)
        logic = new BusiLayer()
      } else if (wfs) {
        logic = new WfsLayerService(config)
      } else if (wms) {
        logic = new WmsLayerService(config)
      }
    }
    this.layersLogic[code] = logic
    return logic
  }
 
  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
  }
 
  show (config) {
    var layer = this.layers[config.code]
    var logic = this.loadLogic(config)
    if (layer) {
      if (!this.map.hasLayer(layer)) {
        layer.addTo(this.map)
      }
    } else {
      logic && logic.init(this.addLayer(config), this.L)
    }
    logic && logic.create && logic.create()
  }
 
  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()
  }
 
  /**
     * 控制显示的级别
     * @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)
        }
      }
    }
  }
 
  /**
   *
   * 根据传的 feature对象定位,
   * @param code
   * @param feature
   */
  flyByFeature (feature, code) {
    const type = feature.geometry.type
    var point = []
    switch (type) {
      case 'Point':
        point = clone(feature.geometry.coordinates)
        break
      case 'MultiLineString':
        var coordinates = feature.geometry.coordinates
        point = coordinates[parseInt(coordinates.length / 2)][0]
        break
    }
    window.map.flyTo(point.reverse(), 15)
    code && this.openPopup(code, feature.id)
  }
 
  openPopup (layerId, id) {
    const layer = this.layers[layerId]
 
    if (layer.eachLayer) {
      layer.eachLayer(function (layer) {
        const layers = layer.getLayers()
        for (var i = 0; i < layers.length; i++) {
          const lay = layers[i]
          const feature = lay.feature
          lay.closePopup()
          if (feature.id === id) {
            lay.openPopup()
            break
          }
        }
      })
    }
    /* for (var k in this.layers) {
          var layerGroup = this.layers[k]
          layerGroup.eachLayer(function (layer) {
            console.log(layer)
            console.log(layer.getAttribution())
          })
          var layers = layerGroup.getLayers()
          if (layers) {
            for (var m = 0; m < layers.length; m++) {
              var layer = layers[m]
              console.log(layer)
              console.log(layer.getLayerId(val.id))
              /!* var feature = layer.feature
              if (feature.id === layerId) {
                this.map.flyToBounds(bound)
                return layer
              } *!/
            }
          }
        } */
    return null
  }
 
  findLayerById (layer, id) {
    const layers = layer.getLayers
    if (layers) {
      this.findLayerById(layer.getLayers(), id)
    } else {
      layer.eachLayer(function (layer) {
        console.log(layer)
      })
    }
  }
}
 
export default LayerFactory