shuishen
2023-05-08 e24f70ababdee2b4fdce71c6b80a9bc928fa0331
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
/*
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2023-03-31 10:52:25
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2023-05-06 16:52:41
 * @FilePath: \srs-police-affairs\src\utils\turfPolygon.js
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
 */
import * as turf from '@turf/turf'
 
function getAllRegionCenter (data) {
    let arr = []
 
    data.forEach(item => {
        if (item.position && item.position.length > 0) {
            arr.push(getRegionCenter(item.position))
        }
    })
 
    arr.push(arr[0])
 
    if (arr.length == 1) {
        return arr[0]
    } else if (arr.length == 2) {
 
        return turf.centroid(turf.midpoint(turf.point(arr[0]), turf.point(arr[1]))).geometry.coordinates
    } else {
        arr.push(arr[0])
 
        return turf.centroid(turf.polygon([arr])).geometry.coordinates
    }
}
 
function getRegionCenter (params) {
    let arr = []
 
    params.forEach(areaArray => {
        JSON.parse(areaArray).coordinates.forEach(item => {
            arr.push(turf.centroid(turf.polygon([item[0]])).geometry.coordinates)
        })
    })
 
    if (arr.length == 1) {
        return arr[0]
    } else if (arr.length == 2) {
 
        return turf.centroid(turf.midpoint(turf.point(arr[0]), turf.point(arr[1]))).geometry.coordinates
    } else {
        arr.push(arr[0])
 
        return turf.centroid(turf.polygon([arr])).geometry.coordinates
    }
}
 
export const computerCapacity = (data, type = '') => {
 
    if (type != '') {
 
        const center = getAllRegionCenter(data)
 
        if (type == 'all') {
 
            global.viewer.flyToPosition(
                new global.DC.Position(
                    Number(center[0] + 0.03),
                    Number(center[1] - 0.30),
                    Number(36000),
                    Number(-3),
                    Number(-45),
                    Number(0)
                ),
                function () { },
                3
            )
 
        } else {
 
            global.viewer.flyToPosition(
                new global.DC.Position(
                    Number(center[0] + 0.0046),
                    Number(center[1] - 0.04),
                    Number(4000),
                    Number(-3),
                    Number(-45),
                    Number(0)
                ),
                function () { },
                3
            )
 
        }
    } else {
        let pointArr = []
        data.forEach(item => {
            if (item.longitude && item.latitude) {
                pointArr.push(turf.point([item.longitude, item.latitude]))
                return
            }
 
            if (item.lng && item.lat) {
                pointArr.push(turf.point([item.lng, item.lat]))
                return
            }
        })
 
        const features = turf.featureCollection(pointArr)
 
        const scope = turf.envelope(features).bbox
 
        // const poly = turf.polygon([[[0, 29], [3.5, 29], [2.5, 32], [0, 29]]])
 
        global.viewer.flyToBounds(scope,
            { heading: 0, pitch: -45, roll: 0 },
            (e) => { },
            3
        )
    }
}
 
export const getMapBounds = (data, type = '') => {
    let pointArr = []
 
    data.forEach(item => {
        pointArr.push(turf.point([item.x, item.y]))
        return
    })
 
    const features = turf.featureCollection(pointArr)
 
    const scope = turf.envelope(features).bbox
 
    // const poly = turf.polygon([[[0, 29], [3.5, 29], [2.5, 32], [0, 29]]])
 
    return scope
}
 
export const getLineSpeed = (data) => {
    var line = turf.lineString(data)
    var length = turf.length(line, { units: 'miles' })
 
    return length * 1000 / 50
}