shuishen
2022-07-29 71ac571768baab6ca36057be77075b80cfe74739
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<template>
    <div v-loading="loading"
         element-loading-text="拼命加载中"
         element-loading-spinner="el-icon-loading"
         element-loading-background="rgba(0, 0, 0, 0.8)"
         v-loading.fullscreen.lock="loading">
 
        <soilWater :legendsDetail="legendData"></soilWater>
 
        <div id="popup"
             class="ol-popup"
             style="width: 72px;"
             v-show="bigRiverVisible">
            <a href="#"
               id="popup-closer"
               class="ol-popup-closer"></a>
            <div id="popup-content"
                 class="real-river-box">
                {{bigRiverRealData != null ? bigRiverRealData.NAME : ''}}
            </div>
        </div>
 
        <div class="soil-time-change">
            <el-date-picker v-model="tableTime"
                            type="date"
                            placeholder="选择日期"
                            align="right"
                            :picker-options="timePickerOptions"
                            size="small"
                            @change="searchTable">
            </el-date-picker>
        </div>
 
    </div>
</template>
 
<script>
 
import axios from 'axios'
import { Style, Fill } from 'ol/style.js'
import Feature from 'ol/Feature.js'
import { Polygon } from 'ol/geom'
import Overlay from 'ol/Overlay.js'
import ImageStatic from 'ol/source/ImageStatic.js'
import Image from 'ol/layer/Image.js'
 
import store from '@/store/' // 全局状态
import { getSoilList, getSoilPng } from '@/api/soilOrWater/index.js'
 
export default {
 
    data () {
        return {
            // 表格事件查询
            tableTime: new Date().setTime(new Date().getTime() - 3600 * 1000),
            // 左侧底部时间
            timePickerOptions: {
                shortcuts: [
                    {
                        text: '今天',
                        onClick (picker) {
                            picker.$emit('pick', new Date())
                        }
                    },
                    {
                        text: '昨天',
                        onClick (picker) {
                            const date = new Date()
                            date.setTime(date.getTime() - 3600 * 1000 * 24)
                            picker.$emit('pick', date)
                        }
                    },
                    {
                        text: '一周前',
                        onClick (picker) {
                            const date = new Date()
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
                            picker.$emit('pick', date)
                        }
                    }
                ]
            },
            layerPng: null,
            boxShow: false,
            overlay: null,
            bigRiverVisible: false,
            bigRiverRealData: null,
            // 加载中// 所有图层
            soilIconLayerOne: store.state.openlayers.soilIconLayerOne,
            soilIconLayerTwo: store.state.openlayers.soilIconLayerTwo,
            soilIconLayerThree: store.state.openlayers.soilIconLayerThree,
            soilIconLayerFour: store.state.openlayers.soilIconLayerFour,
            map2D: store.state.openlayers.map2D,
            loading: true,
            // 图例组件的数据
            legendData: [
                {
                    title: '山洪灾害图例',
                    content: [
                        {
                            label: '0-30',
                            type: 'soilIconLayerOne',
                            btnClass: 'btn-one'
                        },
                        {
                            label: '30-48',
                            type: 'soilIconLayerTwo',
                            btnClass: 'btn-two'
                        },
                        {
                            label: '48-60',
                            type: 'soilIconLayerThree',
                            btnClass: 'btn-three'
                        },
                        {
                            label: '60以上',
                            type: 'soilIconLayerFour',
                            btnClass: 'btn-four'
                        }
                    ]
                }
            ]
        }
    },
    created () {
        this.$parent.leftShowClick(false)
    },
    mounted () {
        this.$parent.resize('0', '0', true, '前期土壤含水量')
        this.map2D.addLayer(this.soilIconLayerOne)
        this.map2D.addLayer(this.soilIconLayerTwo)
        this.map2D.addLayer(this.soilIconLayerThree)
        this.map2D.addLayer(this.soilIconLayerFour)
        this.$nextTick(function () {
            this.map2D.on('singleclick', this.layerClick)
        })
        this.bigRiverVisible = false
        // this.getArcgisData()
 
        this.getTurData()
    },
    methods: {
        // 数据表查询事件
        searchTable () {
            if (this.tableTime.length == 0) return
 
            this.getTurData()
        },
        // 处理时间补零操作
        disposeTime (s) {
            return s < 10 ? '0' + s : s
        },
        getTurData () {
            var that = this
 
            if (that.tableTime.length == 0) {
                that.$message({ message: '请选择时间', duration: 2000 })
                return
            }
 
            const startTime = new Date(that.tableTime)
 
            const start =
                startTime.getFullYear() +
                '-' +
                that.disposeTime(startTime.getMonth() + 1) +
                '-' +
                that.disposeTime(startTime.getDate())
 
            getSoilPng({ time: start }).then(res => {
                if (this.layerPng != null) {
                    this.map2D.removeLayer(this.layerPng)
                }
 
                var extent = [114.7, 25.5, 117.7, 28.5]
 
                var img = 'img/' + res.data.data.url
 
                this.layerPng = new Image({
                    zIndex: 6,
                    opacity: 1,
                    source: new ImageStatic({
                        // eslint-disable-next-line import/no-absolute-path
                        url: img,
                        projection: 'EPSG:4326',
                        imageExtent: extent
                    })
                })
 
                this.map2D.addLayer(this.layerPng)
 
                setTimeout(() => {
                    this.loading = false
                }, 2000)
            })
        },
        boxResize (val) {
            this.boxShow = val
        },
        getArcgisData () {
            var that = this
 
            that.soilIconLayerOne.getSource().clear()
            that.soilIconLayerTwo.getSource().clear()
            that.soilIconLayerThree.getSource().clear()
            that.soilIconLayerFour.getSource().clear()
 
            getSoilList().then(res => {
                var arr = []
 
                for (var key in res.data.data[0].listx) {
                    if (key != '') {
                        var a = 0
 
                        res.data.data[0].listx[key].forEach(item => {
                            if (item.soilv > a) {
                                a = item.soilv
                            }
                        })
 
                        var obj = {}
 
                        obj[key] = a
 
                        arr.push(obj)
                    }
                }
 
                for (var keyt in res.data.data[0].listz) {
                    if (keyt != '') {
                        var b = 0
 
                        res.data.data[0].listz[keyt].forEach(item => {
                            if (item.soilv > b) {
                                b = item.soilv
                            }
                        })
 
                        var objt = {}
 
                        objt[keyt] = b
 
                        arr.push(objt)
                    }
                }
 
                axios.get('http://59.55.128.156:6080/arcgis/rest/services/fuzhouxzq/MapServer/0/query?where=1%3D1&text=&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=*&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&returnDistinctValues=false&returnTrueCurves=false&resultOffset=&resultRecordCount=&f=pjson').then(resultData => {
                    resultData.data.features.forEach((item, index) => {
                        if (item.attributes.NAME != '') {
                            arr.forEach(it => {
                                if (String(it[item.attributes.NAME])) {
                                    var a = it[item.attributes.NAME]
                                    if (a >= 0 && a < 30) {
                                        that.addPolygon(item, 'soilIconLayerOne', '#3399ff')
                                    } else if (a >= 30 && a < 48) {
                                        that.addPolygon(item, 'soilIconLayerTwo', '#ffff66')
                                    } else if (a >= 48 && a < 60) {
                                        that.addPolygon(item, 'soilIconLayerThree', '#ff6633')
                                    } else if (a >= 60) {
                                        that.addPolygon(item, 'soilIconLayerFour', '#ff0000')
                                    }
                                }
                            })
                        }
                    })
                    this.loading = false
                })
            })
        },
        // 控制图层显示隐藏事件
        layerControl (item) {
            if (item.type == 'soilIconLayerOne') {
                this.legendData[0].content[0].btnClass == '' ? this.legendData[0].content[0].btnClass = 'btn-one' : this.legendData[0].content[0].btnClass = ''
            } else if (item.type == 'soilIconLayerTwo') {
                this.legendData[0].content[1].btnClass == '' ? this.legendData[0].content[1].btnClass = 'btn-two' : this.legendData[0].content[1].btnClass = ''
            } else if (item.type == 'soilIconLayerThree') {
                this.legendData[0].content[2].btnClass == '' ? this.legendData[0].content[2].btnClass = 'btn-three' : this.legendData[0].content[2].btnClass = ''
            } else if (item.type == 'soilIconLayerFour') {
                this.legendData[0].content[3].btnClass == '' ? this.legendData[0].content[3].btnClass = 'btn-four' : this.legendData[0].content[3].btnClass = ''
            }
 
            this[item.type].setVisible(!this[item.type].getVisible())
        },
        layerClick (e) {
            const that = this
 
            that.bigRiverVisible = false
 
            // 在点击时获取像素区域
            that.bigRiverRealData = null
            var features = this.map2D.getFeaturesAtPixel(e.pixel)
 
            if (features.length > 0) {
                var ind = 0
                var cont = null
                var data = null
 
                features.forEach(item => {
                    if (item.values_.realData) {
                        if (item.values_.realData.mapZindex > ind) {
                            ind = item.values_.realData.mapZindex
                            cont = item.values_.realData
                            data = item.values_
                        }
                    }
                })
 
                this.zoomToExtent(data.geometry.extent_)
 
                if (cont == null) return
 
                var container = document.getElementById('popup')
 
                var closer = document.getElementById('popup-closer')
 
                that.overlay = new Overlay(/** @type {olx.OverlayOptions} */({
                    element: container,
                    autoPan: false,
                    autoPanAnimation: {
                        duration: 0 // 当Popup超出地图边界时,为了Popup全部可见,地图移动的速度.
                    }
                }))
 
                closer.onclick = function () {
                    that.overlay.setPosition(undefined)
                    closer.blur()
                    this.bigRiverVisible = false
                    return false
                }
 
                var coordinate = e.coordinate
 
                that.overlay.setPosition(coordinate)
                that.map2D.addOverlay(that.overlay)
 
                that.bigRiverVisible = true
                that.bigRiverRealData = cont
            }
        },
        // 地图缩放,通过extent范围  extent数据类型为int数组,不能为字符串,如果为字符串则先转换为int数组。
        zoomToExtent (extent) {
            var MapZoom = extent
            var mapPadding = [120, 110, 120, 410]
            // 添加map.getSize(),可以让地图在正常的缩放级别内,而不是出现地图级别小数。
            this.map2D.getView().fit(MapZoom, this.map2D.getSize(), {
                constrainResolution: false,
                earest: false,
                padding: mapPadding
            })
            // 以下为另外一种方法
            // var r = map.getView().getResolutionForExtent(extent, map.getSize());
            // map.getView().setResolution(r);
            // map.getView().setCenter(ol.extent.getCenter(extent));
        },
        getRandomColor () {
            var r = Math.floor(Math.random() * 256)
            var g = Math.floor(Math.random() * 256)
            var b = Math.floor(Math.random() * 256)
            return 'rgb(' + r + ',' + g + ',' + b + ')'
            // return '#' + Math.floor(Math.random() * 0xffffff).toString(16);
        },
        addPolygon (data, type, color) {
            data.attributes.mapZindex = this[type].values_.zIndex
 
            var polygonFeature = new Feature({
                type: 'polygon',
                geometry: new Polygon(data.geometry.rings),
                realData: data.attributes
            })
 
            polygonFeature.setStyle(new Style({
 
                fill: new Fill({
                    color: color
                })
 
            }))
 
            this[type].getSource().addFeature(polygonFeature)
        }
 
    },
    destroyed () {
        this.soilIconLayerOne.getSource().clear()
        this.soilIconLayerTwo.getSource().clear()
        this.soilIconLayerThree.getSource().clear()
        this.soilIconLayerFour.getSource().clear()
 
        if (this.overlay != null) {
            this.map2D.removeOverlay(this.overlay)
        }
 
        if (this.layerPng != null) {
            this.map2D.removeLayer(this.layerPng)
        }
 
        this.map2D.un('singleclick', this.layerClick)
 
        this.map2D.removeLayer(this.soilIconLayerOne)
        this.map2D.removeLayer(this.soilIconLayerTwo)
        this.map2D.removeLayer(this.soilIconLayerThree)
        this.map2D.removeLayer(this.soilIconLayerFour)
 
        this.$parent.leftShowClick(true)
    }
}
</script>
 
<style lang='scss'>
.soil-time-change {
    position: fixed;
    top: 90px;
    left: 50%;
    right: auto;
    bottom: auto;
    margin: auto;
    transform: translate(-50%, 0);
    width: auto;
    height: 36px;
 
    display: flex;
    justify-content: center;
    align-items: center;
 
    z-index: 11;
 
    & > div {
        display: flex;
        justify-content: space-around;
    }
 
    .el-date-editor {
        height: 36px;
        display: flex;
        align-items: center;
 
        .el-input__inner {
            height: 36px;
            line-height: 36px;
        }
    }
}
</style>