fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
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
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
let slope
let contourLine
let graphicLayer
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  addSlope()
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function addSlope() {
  // 剖度坡向
  slope = new mars3d.thing.Slope({
    arrow: {
      scale: 0.3, // 箭头长度的比例(范围0.1-0.9)
      color: Cesium.Color.YELLOW,
      width: 15, // 箭头宽度
      show: true
    },
    tooltip: function (event) {
      const attr = event.graphic?.attr
      return `坡度: ${attr.slopeStr1}  (${attr.slopeStr2})<br />坡向: ${attr.direction}°`
    }
  })
  map.addThing(slope)
 
  slope.on(mars3d.EventType.end, function (event) {
    console.log("分析完成", event)
    // event.data[0] 数组内返回值说明: {
    //     position:position,  //坐标位置
    //     slope: slopeValDou, //度数法值,α(坡度)=arc tan (高程差/水平距离)
    //     slopeStr1: text1,   //度数法值字符串
    //     slopeStr2: text2,   //百分比法值字符串,坡度 = (高程差/水平距离)x100%
    //     direction: slopeAngle //坡向值(0-360度)
    // }
  })
 
  // 渲染效果
  contourLine = new mars3d.thing.ContourLine({
    contourShow: false, // 是否显示等高线
    shadingType: "none", // 地表渲染效果类型:无nono, 高程 elevation, 坡度slope, 坡向aspect
    shadingAlpha: 0.6 /// 地表渲染的透明度
  })
  map.addThing(contourLine)
}
 
// 添加矩形
export function btnDrawExtent(splitNum) {
  clearAll()
  graphicLayer.startDraw({
    type: "rectangle",
    style: {
      color: "#007be6",
      opacity: 0.8,
      outline: false
    },
    success: function (graphic) {
      // 绘制成功后回调
      const positions = graphic.getOutlinePositions(false)
      graphicLayer.clear()
 
      console.log("绘制坐标为", JSON.stringify(mars3d.PointTrans.cartesians2lonlats(positions))) // 方便测试拷贝坐标
 
      contourLine.positions = positions
 
      slope.add(positions, {
        splitNum: splitNum, // splitNum插值分割的个数
        radius: 1, // 缓冲半径(影响坡度坡向的精度)
        count: 4 // 缓冲的数量(影响坡度坡向的精度)会求周边(count*4)个点
      })
    }
  })
}
 
// 绘制多边形
export function btnDraw(splitNum) {
  clearAll()
  graphicLayer.startDraw({
    type: "polygon",
    style: {
      color: "#29cf34",
      opacity: 0.3,
      outline: true,
      outlineColor: "#ffffff",
      clampToGround: true
    },
    success: function (graphic) {
      // 绘制成功后回调
      const positions = graphic.positionsShow
      graphicLayer.clear()
 
      console.log("绘制坐标为", JSON.stringify(mars3d.PointTrans.cartesians2lonlats(positions))) // 方便测试拷贝坐标
 
      contourLine.positions = positions
 
      slope.add(positions, {
        splitNum: splitNum, // splitNum插值分割的个数
        radius: 1, // 缓冲半径(影响坡度坡向的精度)
        count: 4 // 缓冲的数量(影响坡度坡向的精度)会求周边(count*4)个点
      })
    }
  })
}
 
// 添加点
export function btnDrawPoint() {
  clearAll()
 
  graphicLayer.startDraw({
    type: "point",
    style: {
      color: "#ffff00"
    },
    success: function (graphic) {
      const positions = graphic.positionsShow
      graphicLayer.clear()
 
      slope.add(positions)
    }
  })
}
// 改变阴影
export function changeShadingType(val) {
  contourLine.shadingType = val
}
 
export function clearAll() {
  slope.clear()
  contourLine.clear()
}