import { getHost, getToken } from "../../on"; class DTH { constructor(sdk, options = {}) { this.sdk = sdk this.primitives = { building: [], dth: [] } this.options = {...options} this.options.host = this.options.host || getHost() this.temporaryDth = [] this.dth = {} this.HandlePickHouseEvent = new Cesium.Event(); this.initEvents() } /** * @description 注册分户点击的事件回调 * @memberOf DTH * */ houseSelectedCallback(that, cb) { this.HandlePickHouseEvent.addEventListener( cb, that ) } //场景事件 initEvents() { new Cesium.ScreenSpaceEventHandler(this.sdk.viewer.scene.canvas).setInputAction(((e) => { if (!this.isActivate) return; let pickFeature = this.sdk.viewer.scene.pick(e.position); if (pickFeature) { //点击了已有的分户单体化 if (pickFeature.primitive && pickFeature.primitive instanceof Cesium.ClassificationPrimitive && pickFeature.id && pickFeature.id.type == "dth") { this.getIDBypickFeature(pickFeature); //处理点击到的楼层 return; } if (pickFeature.id && pickFeature.id.type === 'highlight') { return; } } this.highlightPrimitive && this.sdk.viewer.scene.primitives.remove(this.highlightPrimitive) let position = this.sdk.viewer.scene.pickPosition(e.position); //屏幕坐标转为笛卡尔空间坐标 if (!position) return; let c = Cesium.Cartographic.fromCartesian(position); //笛卡尔坐标转为经纬度(弧度) let point = [Cesium.Math.toDegrees(c.longitude), Cesium.Math.toDegrees(c.latitude)]; //转为经纬度点 this.queryByPoint(point, c.height); }), Cesium.ScreenSpaceEventType.LEFT_CLICK); } /*根据用户信息查询单体化*/ queryByUserInfo(data) { this.queryByPoint([data.position.lng, data.position.lat], data.position.alt, data.id) } //点查询 点击查询是查询分层的数据 async queryByPoint(point) { let url = "" if (this.options.host.endsWith("yjearth4.0")) { url = this.options.host + '/api/v1/dth/query_by_point' } else { url = this.options.host + '/yjearth4.0/api/v1/dth/query_by_point' } url += '?point=' + JSON.stringify({'lng': point[0],'lat': point[1]}) let response = await fetch(url, { method: 'get', headers: { 'Content-Type': 'application/json', "token": getToken(), "Authorization": "Bearer " + getToken(), } }) if (response.status === 200) { let data = await response.json() if (data.code === 200 || data.code === 0) { this.clearAllDthPrimitive() this.addDthPrimitive(data.data.list) } else { window.ELEMENT && window.ELEMENT.Message({ message: data.msg || data.message, type: 'warning', duration: 1500 }); } } } // 添加房屋Primitive async addBuildingPrimitive(array) { for (let i = 0; i < array.length; i++) { let fromDegreesArray = [] let extrudedHeight = 0 let positions = JSON.parse(array[i].range) for (let m = 0; m < positions.length; m++) { if (extrudedHeight < positions[m].alt) { extrudedHeight = positions[m].alt } fromDegreesArray.push(positions[m].lng, positions[m].lat, 0) } let polygonGeometry = new Cesium.PolygonGeometry({ polygonHierarchy: new Cesium.PolygonHierarchy( Cesium.Cartesian3.fromDegreesArrayHeights(fromDegreesArray) ), // perPositionHeight: true, //使用z坐标 否则高度从0开始 extrudedHeight: 100000000, //拉伸高度 }); this.primitives.building.push(this.sdk.viewer.scene.primitives.add( new Cesium.ClassificationPrimitive({ geometryInstances: new Cesium.GeometryInstance({ id: { ...array[i], }, geometry: Cesium.PolygonGeometry.createGeometry(polygonGeometry), attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor( Cesium.Color.fromCssColorString('rgb(255, 235, 59, 0.4)') ), show: new Cesium.ShowGeometryInstanceAttribute(true), } }), classificationType: Cesium.ClassificationType.CESIUM_3D_TILE, }), 0 )) } } // 根据id删除房屋Primitive clearBuildingPrimitive(id) { for (let i = this.primitives.building.length - 1; i >= 0; i--) { if (id === this.primitives.building[i]._primitiveOptions.geometryInstances[0].id.ID) { this.sdk.viewer.scene.primitives.remove(this.primitives.building[i]) this.primitives.building.splice(i, 1) break } } } // 删除全部房屋Primitive clearAllBuildingPrimitive() { for (let i = this.primitives.building.length - 1; i >= 0; i--) { this.sdk.viewer.scene.primitives.remove(this.primitives.building[i]) } this.primitives.building = [] } // 添加单体化Primitive addDthPrimitive(array) { let readyIndex = 0 let Primitives = [] for (let i = 0; i < array.length; i++) { let positions = JSON.parse(array[i].range) let fromDegreesArray = [] for (let m = 0; m < positions.length; m++) { fromDegreesArray.push(positions[m].lng, positions[m].lat, array[i].bottom) } let polygonGeometry = new Cesium.PolygonGeometry({ polygonHierarchy: new Cesium.PolygonHierarchy( Cesium.Cartesian3.fromDegreesArrayHeights(fromDegreesArray) ), perPositionHeight: true, //使用z坐标 否则高度从0开始 extrudedHeight: array[i].height + array[i].bottom, //拉伸高度 }); let Primitive = new Cesium.ClassificationPrimitive({ geometryInstances: new Cesium.GeometryInstance({ id: { type: 'dth', ...array[i], }, geometry: Cesium.PolygonGeometry.createGeometry(polygonGeometry), attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor( Cesium.Color.fromCssColorString('rgb(255, 235, 59, 0.4)') ), show: new Cesium.ShowGeometryInstanceAttribute(true), } }), classificationType: Cesium.ClassificationType.CESIUM_3D_TILE, }) Primitives.push(Primitive) this.sdk.viewer.scene.primitives.add(Primitive) Primitive._readyPromise.then(()=>{ this.clearDthPrimitive(Primitive._primitiveOptions.geometryInstances[0].id.ID) readyIndex ++ if(readyIndex >= array.length) { this.primitives.dth.push(...Primitives); } }) } } // 根据id删除单体化Primitive clearDthPrimitive(id) { for (let i = this.primitives.dth.length - 1; i >= 0; i--) { if (id === this.primitives.dth[i]._primitiveOptions.geometryInstances[0].id.ID) { this.sdk.viewer.scene.primitives.remove(this.primitives.dth[i]) this.primitives.dth.splice(i, 1) } } } getDthPrimitive(id) { for (let i = this.primitives.dth.length - 1; i >= 0; i--) { if (id === this.primitives.dth[i]._primitiveOptions.geometryInstances[0].id.ID) { return this.primitives.dth[i] } } } // 删除全部单体化Primitive clearAllDthPrimitive() { for (let i = this.primitives.dth.length - 1; i >= 0; i--) { this.sdk.viewer.scene.primitives.remove(this.primitives.dth[i]) } this.primitives.dth = [] } getIDBypickFeature(pickFeature) { //恢复上一个贴对象面显示 if (this.clickHighlightPrimitive) { this.clickHighlightPrimitive.show = true; } this.highlightPrimitive && this.sdk.viewer.scene.primitives.remove(this.highlightPrimitive) this.highlightPrimitive = this.sdk.viewer.scene.primitives.add( new Cesium.ClassificationPrimitive({ geometryInstances: new Cesium.GeometryInstance({ id: { type: 'highlight', }, geometry: pickFeature.primitive._primitiveOptions.geometryInstances[0].geometry, attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor( Cesium.Color.fromCssColorString('rgb(255, 0, 0, 1)') ), show: new Cesium.ShowGeometryInstanceAttribute(true), } }), classificationType: Cesium.ClassificationType.CESIUM_3D_TILE, }) ) this.highlightPrimitive.readyPromise.then(() => { //设置当前点击的贴对象面不显示 pickFeature.primitive.show = false; }) this.clickHighlightPrimitive = pickFeature.primitive; this.handlePickHouse(pickFeature.id) } //处理拾取到的户室信息 handlePickHouse(id) { this.HandlePickHouseEvent.raiseEvent({}); //触发选中事件 通知界面更新 } activate() { this.isActivate = true; } deactivate() { this.isActivate = false; } } export default DTH