Files
sdk4.0/src/Obj/Base/GeoJson/index.js
2025-08-13 11:51:39 +08:00

217 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @name: index
* @author: Administrator
* @date: 2023-03-04 10:39
* @descriptionindex
* @update: 2023-03-04 10:39
*/
import { getHost, getToken } from "../../../on";
import Base from '../index'
import Tools from '../../../Tools'
import { syncSplitData } from "../../../Global/SplitScreen";
import { syncData, getSdk as get2DSdk } from '../../../Global/MultiViewportMode'
import { setActiveViewer, closeRotateAround, closeViewFollow} from '../../../Global/global'
class GeoJson extends Base {
/**
* @constructor
* @param sdk
* @param options {object} 参数
* @param options.id {string} id
* @param options.url {string} geojson地址
* @param [options.color=#ef0606] {string} 线条颜色
* @param [options.width=1] {number} 线条宽度
* @example new YJ.Obj.GeoJson(earth,{id:"123",url:""})
* */
constructor(sdk, options = {}) {
super(sdk, options)
this.primitive = undefined
this.positions = []
this.loading = true
}
setDefaultValue() {
super.setDefaultValue()
this.options.host = this.options.host || getHost()
// let url = this.options.url
// if (this.options.host) {
// let o = new URL(this.options.url, this.options.host)
// url = o.href
// }
// this.options.url = url
this.options.color = this.options.color || 'rgb(239, 6, 6, 1)'
this.options.width = this.options.width || 1
}
get show() {
return this.options.show
}
// set show(status) {
// let sdkD = get2DSdk().sdkD
// if (!this.isShowView || !sdkD) {
// this.options.show = status
// }
// if (this.entity) {
// if (!this.showView || this.showView == 3 || !sdkD) {
// for (let i = 0; i < this.entity.entities.values.length; i++) {
// this.entity.entities.values[i].show = this.options.show
// }
// }
// else {
// for (let i = 0; i < this.entity.entities.values.length; i++) {
// this.entity.entities.values[i].show = false
// }
// }
// }
// syncData(this.sdk, this.options.id)
// syncSplitData(this.sdk, this.options.id)
// this.isShowView = false
// }
set show(status) {
this.options.show = status
if (this.entity) {
for (let i = 0; i < this.entity.entities.values.length; i++) {
this.entity.entities.values[i].show = status
}
}
}
async on() {
let url = ""
if (this.options.host.endsWith("yjearth4.0"))
url = this.options.host + '/data/service/getFile'
else
url = this.options.host + '/yjearth4.0/data/service/getFile'
url = url + '?path=' + encodeURIComponent(this.options.url)
let rsp = await fetch(url, {
method: 'get',
headers: {
'Content-Type': 'application/json',
"token": getToken(),
"Authorization": "Bearer " + getToken(),
}
})
let json = await rsp.json()
this.geojson = json
// this.sdk.addIncetance(this.options.id, this)
return GeoJson.addDataToGlobe(this, json.features)
}
/*geojosn暂时只用线的形式*/
static addDataToGlobe(that) {
const geoJsonDataSource = new Cesium.GeoJsonDataSource();
let geojson = that.deepCopyObj(that.geojson)
for (let i = 0; i < geojson.features.length; i++) {
if (!geojson.features[i].id) {
geojson.features[i].id = that.options.id + '_' + i
}
}
// console.log(geojson)
let promise = geoJsonDataSource.load(geojson, {
clampToGround: true,
});
return promise.then(datasource => {
that.entity = datasource
datasource.entities.values.forEach(enetity => {
// console.log(enetity)
let color = Cesium.Color.fromCssColorString(that.options.color)
let colorPolygon = color.withAlpha(0.2)
enetity.show = that.options.show
that.sdk.viewer.entities.add(enetity)
if (enetity.billboard) {
enetity.billboard.heightReference = Cesium.HeightReference.CLAMP_TO_GROUND
enetity.point = new Cesium.PointGraphics({
show: true,
color: color, // 点的颜色
pixelSize: 10, // 点的大小
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
disableDepthTestDistance: Number.POSITIVE_INFINITY // 不应用深度测试
})
}
if (enetity.polyline) {
enetity.polyline.material = color
enetity.polyline.zIndex = that.sdk._entityZIndex
that.sdk._entityZIndex++
}
if (enetity.polygon) {
enetity.polygon.perPositionHeight = false
enetity.polygon.material = colorPolygon
enetity.polygon.zIndex = that.sdk._entityZIndex
enetity.polyline = new Cesium.PolylineGraphics({
positions: enetity.polygon.hierarchy._value.positions,
width: 1,
clampToGround: true,
material: color,
zIndex: that.sdk._entityZIndex
})
that.sdk._entityZIndex++
}
})
that.loading = false
})
}
remove() {
if (this.entity) {
this.entity.entities.values.forEach(enetity => {
this.sdk.viewer.entities.remove(enetity)
})
this.entity = null
this.geojson = {}
}
}
async flyTo() {
if (!this.loading) {
if (this.geojson) {
setActiveViewer(0)
closeRotateAround(this.sdk)
closeViewFollow(this.sdk)
let range = turf.bbox(this.geojson);
if (this.options.customView && this.options.customView.relativePosition && this.options.customView.orientation) {
let orientation = {
heading: Cesium.Math.toRadians(this.options.customView.orientation.heading || 0.0),
pitch: Cesium.Math.toRadians(this.options.customView.orientation.pitch || -60.0),
roll: Cesium.Math.toRadians(this.options.customView.orientation.roll || 0.0)
}
let lng = this.options.customView.relativePosition.lng
let lat = this.options.customView.relativePosition.lat
let alt = this.options.customView.relativePosition.alt
let destination = Cesium.Cartesian3.fromDegrees(lng, lat, alt)
let position = { lng: range[0], lat: range[1] }
// 如果没有高度值,则获取紧贴高度计算
position.alt = await this.getClampToHeight(position)
lng = this.options.customView.relativePosition.lng + position.lng
lat = this.options.customView.relativePosition.lat + position.lat
alt = this.options.customView.relativePosition.alt + position.alt
destination = Cesium.Cartesian3.fromDegrees(lng, lat, alt)
this.sdk.viewer.camera.flyTo({
destination: destination,
orientation: orientation
})
}
else {
this.viewer.camera.flyTo({
destination: Cesium.Rectangle.fromDegrees(...range)
});
}
}
}
}
flicker() { }
}
export default GeoJson