派生自 wuyushui/SewerAndRainNetwork

ChenZeping
2021-04-29 70fd7ce1af45948ab2080d2036d345fd3b4c15a6
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
import AjaxUtils from '../../../utils/AjaxUtils'
import { PIPELINE_WMS } from '../../../conf/Constants'
 
/**
 * wms 图层组的管理
 */
class WmsGroupLayerService {
  constructor (config) {
    this.config = config
    this.L = window.L
    // 存放getfeatureinfo的图层组
    this.featureGroup = this.L.featureGroup({})
    this.map = window.map
    this.popupComp = window.popupComp
    // {groupName:{url:'.../wms',layers:[]}}
    this.groups = []
    this.map.on('click', (e) => this.click(e))
  }
 
  init () {
    this.layer = this.L.featureGroup({}).addTo(this.map)
    this.initGroup(this.config)
    this.load()
  }
 
  hide (config) {
    const layerName = config.layerName
    for (var i = 0; i < this.groups.length; i++) {
      const group = this.groups[i]
      for (var k in group) {
        const v = group[k]
        const index = v.layers.indexOf(layerName)
        if (index >= 0) {
          delete v.layers[index]
        }
      }
    }
  }
 
  initGroup (config) {
    const groupName = this.config.groupName
    const layers = config.layers
    layers && this.initGroup(layers)
    for (var i = 0; i < config.length; i++) {
      const layerConfig = config[i]
      const layerName = layerConfig.layerName
      const group = this.groups[groupName]
      if (group) {
        group.layers.push(layerName)
      } else {
        this.groups[groupName] = {
          url: '',
          layers: [layerName]
        }
      }
    }
  }
 
  load () {
    for (var k in this.groups) {
      console.log(k)
      this.L.tileLayer.wms(PIPELINE_WMS, {
        format: 'image/png', // 返回的数据格式
        transparent: true,
        layers: k // todo
      }).addTo(this.layer).bringToFront()
    }
  }
 
  click (e) {
    this.featureGroup.clearLayers()
    var point = this.map.latLngToContainerPoint(e.latlng, this.map.getZoom())
    var size = this.map.getSize()
    // const bbox = this.L.latLngBounds(this.L.latLng(e.latlng.lng, e.latlng.lat)).toBBoxString()
    const params = {
      VERSION: '1.1.1',
      SERVICE: 'WMS',
      REQUEST: 'GetFeatureInfo',
      // bbox: bbox,
      FORMAT: 'image/png',
      INFO_FORMAT: 'application/json',
      TRANSPARENT: true,
      LAYERS: 'sewer:pipeline_group',
      QUERY_LAYERS: 'sewer:pipeline_group', // todo
      FEATURE_COUNT: 50,
      SRS: 'EPSG:4326',
      WIDTH: size.x,
      HEIGHT: size.y,
      EXCEPTIONS: 'application/vnd.ogc.se_inimage',
      X: Math.round(point.x),
      Y: Math.round(point.y),
      BBOX: this.map.getBounds().toBBoxString()
    }
 
    AjaxUtils.get4JsonDataByUrl(PIPELINE_WMS, params, (res) => {
      let features = res.data.features
      features = features[0]
      var myIcon = this.L.divIcon({ className: 'my-div-icon' })
      this.L.marker(features.geometry.coordinates.reverse(), {
        icon: myIcon
      }).addTo(this.featureGroup)
        .bindPopup((layer) => {
          this.popupComp.setDatas({ feature: features })
          this.popupComp.setShow()
          return this.popupComp.$el
        }, {
          className: 's-map-popup',
          minWidth: 300,
          closeButton: false,
          autoClose: false
        })
        .openPopup()
    })
  }
}
 
export default WmsGroupLayerService