代码迁移
This commit is contained in:
131
src/Obj/Base/Heatmap/index.js
Normal file
131
src/Obj/Base/Heatmap/index.js
Normal file
@ -0,0 +1,131 @@
|
||||
// import Tools from '../../Tools'
|
||||
import Base from "../index";
|
||||
|
||||
export default class HeatMap extends Base {
|
||||
/**
|
||||
* @constructor
|
||||
* @param sdk
|
||||
* @description 热力图
|
||||
* @param options {object} 属性
|
||||
* @param options.id {string} 唯一标识
|
||||
* @param options.show=true {boolean} 显示/隐藏
|
||||
* @param options.name {string} 名称
|
||||
* @param options.gradient {object} 渐变色
|
||||
* @param {Array.<object>} options.positions 经纬度和高度的列表,值交替 [{lon,lat,alt},...]
|
||||
* @param {Array.<object>} options.data 热力图数据 [{lon,lat,value},...]
|
||||
* */
|
||||
constructor(sdk, options) {
|
||||
super(sdk, options)
|
||||
this.viewer = sdk.viewer
|
||||
this.options.show = (options.show || options.show === false) ? options.show : true
|
||||
this.options.positions = this.options.positions || []
|
||||
this.options.data = this.options.data
|
||||
this.options.gradient = options.gradient || {
|
||||
'0.9': 'red',
|
||||
'0.8': 'orange',
|
||||
'0.7': 'yellow',
|
||||
'0.5': 'blue',
|
||||
'0.3': 'green'
|
||||
}
|
||||
this.entity = {
|
||||
id: this.options.id
|
||||
}
|
||||
if (!this.options.positions || this.options.positions.length < 3) {
|
||||
this._error = '最少需要三个坐标!'
|
||||
console.warn(this._error)
|
||||
window.ELEMENT && window.ELEMENT.Message({
|
||||
message: this._error,
|
||||
type: 'warning',
|
||||
duration: 1500
|
||||
});
|
||||
}
|
||||
else {
|
||||
let array = []
|
||||
for (let i = 0; i < this.options.positions.length; i++) {
|
||||
array.push([this.options.positions[i].lng, this.options.positions[i].lat])
|
||||
}
|
||||
let line = turf.lineString(array);
|
||||
let bbox = turf.bbox(line);
|
||||
this.bounds = {
|
||||
west: bbox[0], south: bbox[1], east: bbox[2], north: bbox[3]
|
||||
}
|
||||
HeatMap.add(this)
|
||||
}
|
||||
}
|
||||
|
||||
static add(that) {
|
||||
let arr = []
|
||||
that.options.positions.forEach(item => {
|
||||
arr.push(item.lng, item.lat)
|
||||
})
|
||||
let data = HeatMap.getData(that)
|
||||
let heatMap = that.createHeatMap(data.max, data.data)
|
||||
that.entity = new Cesium.Entity({
|
||||
id: that.options.id,
|
||||
show: that.options.show,
|
||||
polygon: {
|
||||
hierarchy: new Cesium.PolygonHierarchy(
|
||||
Cesium.Cartesian3.fromDegreesArray(arr)
|
||||
),
|
||||
material: heatMap._heatmap._renderer.canvas,
|
||||
zIndex: that.sdk._entityZIndex
|
||||
}
|
||||
})
|
||||
that.sdk._entityZIndex ++
|
||||
that.viewer.entities.add(that.entity)
|
||||
}
|
||||
|
||||
remove() {
|
||||
this.viewer.entities.remove(this.entity)
|
||||
this.entity = null
|
||||
}
|
||||
|
||||
static getData(that) {
|
||||
let len = 1000
|
||||
let data = []
|
||||
if (that.options.data && Array.isArray(that.options.data)) {
|
||||
let max = that.options.data[0].value
|
||||
for (let i = 0; i < that.options.data.length; i++) {
|
||||
let val = that.options.data[i].value
|
||||
max = Math.max(max, val)
|
||||
data.push({
|
||||
x: that.options.data[i].lng,
|
||||
y: that.options.data[i].lat,
|
||||
value: val
|
||||
})
|
||||
}
|
||||
return { max: max, data: data }
|
||||
}
|
||||
else {
|
||||
//构建一些随机数据点
|
||||
let max = 0
|
||||
while (len--) {
|
||||
let val = Math.floor(Math.random() * 1000)
|
||||
max = Math.max(max, val)
|
||||
let point = {
|
||||
x: Math.random() * (that.bounds.east - that.bounds.west) + that.bounds.west,
|
||||
y: Math.random() * (that.bounds.north - that.bounds.south) + that.bounds.south,
|
||||
value: val
|
||||
}
|
||||
data.push(point)
|
||||
}
|
||||
return { max: max, data: data }
|
||||
}
|
||||
}
|
||||
|
||||
createHeatMap(max, data) {
|
||||
let heatMap = CesiumHeatmap.create(
|
||||
this.bounds, // 矩形坐标
|
||||
{ // heatmap相应参数
|
||||
backgroundColor: "rgba(0,0,0,0)",
|
||||
radius: 20,
|
||||
maxOpacity: .5,
|
||||
minOpacity: 0,
|
||||
blur: .75,
|
||||
gradient: this.options.gradient
|
||||
}
|
||||
)
|
||||
heatMap.setWGS84Data(0, max, data);
|
||||
return heatMap
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user