派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-04-29 150a37f948214be4bd183b30f3f2865a6f1c519f
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
import AjaxUtils from '../../../utils/AjaxUtils'
import { PIPELINE_WMS } from '../../../conf/Constants'
 
/**
 * 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,
      SRS: 'EPSG:4326',
      EXCEPTIONS: 'application/vnd.ogc.se_inimage'
    }
    this.layersConfig = layersConfig
    // 存放getfeatureinfo的图层组
    this.featureGroup = this.L.featureGroup({}).addTo(this.map)
 
    this.layers = []
    for (var i = 0; i < layersConfig.length; i++) {
      const config = layersConfig[i]
      for (var k in config) {
        if (k === 'wmsLayers') {
          this.layers.push(config[k])
        }
      }
    }
  }
 
  init () {
    if (this.layers) {
      this.load(this.layers)
      this.clickListener()
    }
  }
 
  add (config) {
    const code = config.code
    for (var k in this.layersConfig) {
      if (code === k) {
        return false
      }
    }
    this.layers.push(config.wmsLayers)
    this.wmsLayer.setParams({ layers: this.layers.join(',') })
  }
 
  remove (config) {
    const wmsLayers = config.wmsLayers
    for (var i = 0; i < this.layers.length; i++) {
      const layerName = this.layers[i]
      if (wmsLayers === layerName) {
        this.layers.splice(i, 1)
      }
    }
    this.wmsLayer.setParams({ layers: this.layers.join(',') })
  }
 
  load (layers) {
    this.wmsLayer = this.L.tileLayer.wms(PIPELINE_WMS, {
      format: 'image/png', // 返回的数据格式
      transparent: true,
      layers: layers.join(',')
    }).addTo(this.map)
  }
 
  clickListener () {
    window.map.on('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 = Object.assign({
        LAYERS: this.layers.join(','),
        QUERY_LAYERS: this.layers.join(','),
        WIDTH: size.x,
        HEIGHT: size.y,
        X: Math.round(point.x),
        Y: Math.round(point.y),
        BBOX: this.map.getBounds().toBBoxString()
      }, this.params)
      AjaxUtils.get4JsonDataByUrl(PIPELINE_WMS, params, (res) => {
        const features = res.data.features
        /**
         * {
         *     title: 'New Tab',
         *     name: newTabName,
         *     content: 'New Tab content'
         * }
         * @type {*[]}
         */
        const popupDatas = []
        if (features) {
          for (var i = 0; i < features.length; i++) {
            const feature = features[i]
            const properties = feature.properties
            this.highlight(feature)
            // const coordinates = feature.geometry.coordinates
            popupDatas.push({
              title: properties.wellname || properties.devicename || properties.name,
              name: feature.id,
              content: properties
            })
          }
        }
        if (popupDatas.length > 0) {
          var myIcon = this.L.divIcon({ className: 'my-div-icon' })
          this.L.marker(e.latlng, {
            icon: myIcon
          }).addTo(this.featureGroup)
            .bindPopup((layer) => {
              this.popupComp.setDatas(popupDatas)
              this.popupComp.setShow()
              return this.popupComp.$el
            }, {
              className: 's-map-popup',
              minWidth: 300,
              closeButton: false,
              autoClose: false
            })
            .openPopup()
        }
      })
    })
  }
 
  reverse (feature) {
    const coordinates = feature.geometry.coordinates
    var latlng = []
    for (var j = 0; j < coordinates.length; j++) {
      const coordinate = coordinates[j]
      var xy = []
      for (var k = 0; k < coordinate.length; k++) {
        const coor = coordinate[k]
        xy.push(coor.reverse())
      }
      latlng.push(xy)
    }
    return latlng
  }
 
  highlight (feature) {
    const type = feature.geometry.type
    if (type === 'MultiLineString') {
      this.L.polyline(this.reverse(feature)).addTo(this.featureGroup)
    } else if (type === 'Point') {
      var myIcon = this.L.divIcon({ className: 'my-div-icon' })
      this.L.marker(feature.geometry.coordinates.reverse(), {
        icon: myIcon
      }).addTo(this.featureGroup)
    }
  }
}
 
export default WmsLayerService