gisthreemap
Version:
基于webGL的三维api
384 lines (375 loc) • 11.3 kB
JavaScript
/**
* entitys 实体类
* 管理实体模型
* 创建
* 销毁
*/
class Entitys {
constructor(viewer, dataSource, mydatasources) {
// // 获取dataSource 存在的状态
// const indataSource = this.getDataSourceByName(sourcetype)
// // 如果存在 先清除所有实体
// if (indataSource) {
// indataSource.entities.removeAll()
// dataSource = indataSource
// } else {
// // 生成新的dataSource
// dataSource = new Cesium.CustomDataSource(sourcetype)
// // dataSource添加到viewer
// this.ThreeViewer.dataSources.add(dataSource)
// }\
this.entitysAction = dataSource ? dataSource.entities : viewer.entities
// viewer.mydatasource = []
// viewer.dataSources.getByName(name)[0]
if (dataSource) {
viewer.dataSources.add(dataSource)
// console.log(viewer.dataSources)
}
}
add(entity) {
return this.entitysAction.add(entity);
}
remove(entity) {
this.entitysAction.remove(entity);
}
removeAll() {
this.entitysAction.removeAll();
}
createEntity() {
return new Cesium.Entity();
}
getPoint(options) {
return new Cesium.PointGraphics(options)
}
/**
* 面
* @param {*} polygon [-120.0, 45.0,-80.0, 45.0,-80.0, 55.0,-120.0, 55.0]
* @param {*} Additional // 额外的参数
* @returns
*/
getPolygon(
polygon,
Additional = {
material: Cesium.Color.ORANGE.withAlpha(0.5),
includeHeight: false
}) {
let options = {}
options.hierarchy = Additional.includeHeight ? Cesium.Cartesian3.fromDegreesArrayHeights(polygon) : new Cesium.Cartesian3.fromDegreesArray(polygon)
// options.hierarchy = Cesium.Cartesian3.fromDegreesArrayHeights(polygon)
//额外的参数
for (let key in Additional) {
options[key] = Additional[key]
}
options.eid = Additional.pid || `polygon_${Math.random()}`
options.id = Additional.pid || `polygon_${Math.random()}`
return new Cesium.PolygonGraphics(options)
}
/**
*
* @param {*} polyline
* @param {*} Additional
* @returns
*/
getPolyline(
polyline,
Additional = {
material: Cesium.Color.ORANGE.withAlpha(0.5),
includeHeight: false
}
) {
let options = {}
options.positions = Additional.includeHeight ? Cesium.Cartesian3.fromDegreesArrayHeights(polyline) : new Cesium.Cartesian3.fromDegreesArray(polyline)
//额外的参数
for (let key in Additional) {
options[key] = Additional[key]
}
options.eid = Additional.pid || `polyline_${Math.random()}`
options.id = Additional.pid || `polygon_${Math.random()}`
return new Cesium.PolylineGraphics(options)
}
/**
*
* @param {*} polyline
* @param {*} Additional
* @returns
*/
getPolylineVolume(
polyline,
Additional = {
material: Cesium.Color.ORANGE.withAlpha(0.5),
includeHeight: false,
radius: 60000.0
}
) {
let options = {}
if (Additional.positions) {
options.positions = Additional.positions
} else {
options.positions = Additional.includeHeight ? Cesium.Cartesian3.fromDegreesArrayHeights(polyline) : new Cesium.Cartesian3.fromDegreesArray(polyline)
}
options.shape = Additional.shape || this.computeCircle(Additional.radius)
//额外的参数
for (let key in Additional) {
options[key] = Additional[key]
}
options.eid = Additional.pid || `polylinevolume_${Math.random()}`
options.id = Additional.pid || `polylinevolume_${Math.random()}`
return new Cesium.PolylineVolumeGraphics(options)
}
/**
* 半径
* @param {*} radius
* @returns
*/
computeCircle(radius) {
const positions = [];
for (let i = 0; i < 360; i++) {
const radians = Cesium.Math.toRadians(i);
positions.push(
new Cesium.Cartesian2(
radius * Math.cos(radians),
radius * Math.sin(radians)
)
);
}
return positions
}
/**
*
* @param {*} options
* @returns
*/
//标签
getLabel(options = { //文字标签
text: '我是标签',
font: '14px sans-serif',
fillColor: Cesium.Color.GOLD,
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
outlineWidth: 2,
showBackground: true,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
pixelOffset: offset == undefined ? new Cesium.Cartesian2(0, 20) : offset,
//heightReference:Cesium.HeightReference.RELATIVE_TO_GROUND
}) {
return new Cesium.LabelGraphics(options)
}
//广告牌
getBillboard(img, width, height, Additional = {
clampToGround: true,
// eyeOffset :new Cesium.Cartesian2(-200, 0),
pixelOffset: new Cesium.Cartesian2(0, -20),
}) {
let options = {}
options.img = img || 'https://p26-passport.byteacctimg.com/img/user-avatar/a7ab372b32558ab9c8e130194a1a78d8~300x300.image',
options.width = width || 35
options.height = height || 35
for (let key in Additional) {
options[key] = Additional[key]
}
return new Cesium.BillboardGraphics(options)
}
// 创建面
createPolygon(polygon,
Additional = {
entityAdditional: {
_name: '',
show: true
},
polygonAdditional: {
material: Cesium.Color.ORANGE.withAlpha(0.5),
includeHeight: false,
_name: ''
}
}
) {
const entity = this.createEntity()
// 挂载额外参数
for (let key in Additional.entityAdditional) {
entity[key] = Additional.entityAdditional[key]
}
// 挂载面
entity.polygon = this.getPolygon(polygon, Additional.polygonAdditional)
entity.eid = Additional.entityAdditional.hasOwnProperty('eid') ? Additional.entityAdditional.eid : `polygon_${Math.random()}`
let entityPolygon = this.add(entity)
return entityPolygon
}
// 创建线
createPolyline(polyline,
Additional = {
entityAdditional: {
_name: '',
show: true
},
polylineAdditional: {
material: Cesium.Color.GREEN.withAlpha(0.5),
includeHeight: false,
_name: '',
width: 500
}
}
) {
const entity = this.createEntity()
// 挂载额外参数
for (let key in Additional.entityAdditional) {
entity[key] = Additional.entityAdditional[key]
}
// 挂载线
entity.polyline = this.getPolyline(polyline, Additional.polylineAdditional)
entity.eid = Additional.entityAdditional.hasOwnProperty('eid') ? Additional.entityAdditional.eid : `polyline_${Math.random()}`
// entity.id = Additional.entityAdditional.hasOwnProperty('eid') ? Additional.entityAdditional.eid : `polyline_${Math.random()}`
let entityPolyline = this.add(entity)
return entityPolyline
}
// 创建线
createPolylineVolume(polyline,
Additional = {
entityAdditional: {
_name: '',
show: true
},
polylineAdditional: {
material: Cesium.Color.GREEN.withAlpha(0.5),
includeHeight: false,
radius: 60000.0,
_name: '',
width: 500
}
}
) {
const entity = this.createEntity()
// 挂载额外参数
for (let key in Additional.entityAdditional) {
entity[key] = Additional.entityAdditional[key]
}
// 挂载线
entity.polylineVolume = this.getPolylineVolume(polyline, Additional.polylineAdditional)
entity.eid = Additional.entityAdditional.hasOwnProperty('eid') ? Additional.entityAdditional.eid : `polyline_${Math.random()}`
let entityPolylineVolume = this.add(entity)
return entityPolylineVolume
}
// 创建点信息
createPoint(position, label = false, point = false, billboard = false, eid) {
let entity = this.createEntity()
entity.position = position
if (point) entity.point = this.getPoint()
if (billboard) entity.billboard = this.getBillboard(billboard)
if (label) entity.label = this.getLabel(label)
entity.eid = eid || `point_${Math.random()}`
let entityPoint = this.add(entity)
return entityPoint
}
//自定义雷达
getCustomRadar(l, r) {
return {
position: l,
orientation: Cesium.Transforms.headingPitchRollQuaternion(l, r),
rectangularSensor: new Cesium.RectangularSensorGraphics({
radius: 380000,
xHalfAngle: Cesium.Math.toRadians(50),
yHalfAngle: Cesium.Math.toRadians(50),
material: new Cesium.Color(0, 1, 1, .4),
lineColor: new Cesium.Color(0, 1, 1, 1),
showScanPlane: true,
scanPlaneColor: new Cesium.Color(0, 1, 1, 1),
scanPlaneMode: "vertical",
scanPlaneRate: 3,
showThroughEllipsoid: !1
})
}
}
/**
* 提示信息实体
* createMsgTip
* showTip 控制器
*/
createMsgTip() {
this._resultTip = this.entitysAction.add({
id: Cesium.createGuid(),
label: {
fillColor: Cesium.Color.YELLOW,
showBackground: true,
font: '14px monospace',
horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
pixelOffset: new Cesium.Cartesian2(0, -10)
}
});
return this._resultTip;
}
/**
* 提示框
* @param {*} bShow
* @param {*} position
* @param {*} message
*/
showTip(label, bShow, position, message, effectOptions) {
label.show = bShow;
if (bShow) {
if (position)
label.position = position;
if (message)
label.label.text = message;
if (effectOptions) {
for (let key in effectOptions) {
if (label.key) {
label.key = effectOptions[key];
}
}
}
}
}
/**
* 绘制各种类型的点
* @param {*} viewer
* @param {*} position
* @param {*} size
* @param {*} color
* @param {*} id
*/
drawPoint(position, size, color, id) {
if (position instanceof Array) {
position = Cesium.Cartesian3.fromDegrees(position[0], position[1], position[2] ? position[2] : 0);
}
let options = {
position: position,
point: {
pixelSize: size,
color: color
}
};
if (id) {
options = _.merge(options, {
id: id
});
}
return this.entitysAction.add(options);
}
/**
* 绘制各种类型的线
* @param {*} viewer
* @param {*} positons
* @param {*} color
* @param {*} depthColor
*/
drawLine(vpositons, color, depthColor) {
return this.entitysAction.add({
polyline: {
positions: positons,
width: 2,
material: new Cesium.PolylineOutlineMaterialProperty({
color: color || Cesium.Color.GREEN,
outlineWidth: 1,
outlineColor: color || Cesium.Color.GREEN
}),
depthFailMaterial: depthColor ?
new Cesium.PolylineOutlineMaterialProperty({
color: depthColor,
outlineWidth: 0,
outlineColor: depthColor
}) :
undefined
}
});
}
}
export default Entitys