import { getHost, getToken } from '../../../on' import Base from '../index' import { generatePagination } from '../../Element/Pagination' import { regLeftClickCallback, regRightClickCallback } from '../../../Global/ClickCallback' import richText from '../../Element/richText' import Dialog from '../../Element/Dialog' import { openAllNodeList as OpenAllNodeList } from './openAllNodeList' import { openNodeListById as OpenNodeListById } from './openNodeListById' import { syncData } from '../../../Global/MultiViewportMode' import { syncSplitData } from "../../../Global/SplitScreen"; import { attributeElm } from '../../Element/elm_html' import { Proj } from '../../../Tools/proj' import { legp } from '../../Element/datalist' import { setActiveViewer, getGroundCover, closeRotateAround, closeViewFollow } from '../../../Global/global' import { addCluster, remove_entity_from_cluster } from '../../../Global/cluster/cluster' let colors = require('./color.json') class Vector extends Base { #loadEvent = void 0 #loaded = false #textEntity = [] constructor(sdk, options = {}, _Dialog = {}) { super(sdk, options) this.viewer = sdk.viewer if (!this.options.path) { this.error = '未提供路径!' if (window.ELEMENT) { window.ELEMENT.Message.closeAll() window.ELEMENT.Message({ message: this.error, type: 'warning', duration: 1500 }) } console.warn(this.error) return } this.colors = colors this.options.head_tables = options.head_tables || [] this.options.fileName = options.fileName || '未命名对象' if (!this.options.path.endsWith('.kml')) { ; (this.options.color = options.color || 'rgba(0,255,184,0.5)'), (this.options.opacity = options.opacity || options.opacity === 0 ? options.opacity : 1) } this.options.show = options.show || options.show === false ? options.show : true this.total = 0 this.page = 1 this.pageSize = 20 this.currentData = [] this.data = [] this.list = [] this.imgEntity = [] this.Dialog = _Dialog this._elms = {} this.sdk.addIncetance(this.options.id, this) this.disaster_type_colors = colors[this.options.disaster_type] ? { ...colors[this.options.disaster_type] } : void 0 for (const key in this.disaster_type_colors) { this.disaster_type_colors[key] = this.cmykToRgb( this.disaster_type_colors[key] ) } } get type() { return 'vector' } get show() { return this.options.show } set show(v) { this.setShow(v) } get opacity() { return this.options.opacity } set opacity(v) { this.options.opacity = v if (this.entity) { this.entity.entities.values.forEach(enetity => { if (enetity.point) { enetity.point.color = enetity.point.color._value.withAlpha(v) } if (enetity.polygon) { enetity.polygon.material = enetity.polygon.material.color._value.withAlpha( v ) } if (enetity.polyline) { enetity.polyline.material = enetity.polyline.material.color._value.withAlpha( v ) } }) } } on() { if (!this.sdk || !this.viewer || !this.options.path) { return } return this.init() } async init() { let url = '' this.options.host = this.options.host || getHost() if (this.options.host.endsWith('yjearth4.0')) { url = this.options.host + '/api/v1/vector/load2' } else { url = this.options.host + '/yjearth4.0/api/v1/vector/load2' } url += '?path=' + this.options.path // url = 'json/shp.json' let response = await fetch(url, { method: 'get', headers: { 'Content-Type': 'application/json', token: getToken(), Authorization: 'Bearer ' + getToken() } }) if (response.status === 200) { let arrayBuffer = await response.arrayBuffer() let uint8Array = new Uint8Array(arrayBuffer) let string = this.decompressGzip(uint8Array) if(this.data.length===0) { this.data = JSON.parse(string) } await this.formatData() return Vector.create(this) } } // 格式化数据 async formatData() { let features = [] let _this = this const posConvert = (data, src, dst) => { if (data) { if (Array.isArray(data)) { let flag = false for (let i = 0; i < data.length; i++) { if (typeof data[i] === 'object') { posConvert(data[i], src, dst) } else { flag = true break } } if (flag) { let x = Number(data[0]) let y = Number(data[1]) let res = proj4(src, dst, [x, y]) data[0] = Number(res[0].toFixed(10)) data[1] = Number(res[1].toFixed(10)) } } else { if (data instanceof Object) { posConvert(data.coordinates, src, dst) posConvert(data.geometries, src, dst) } } } } for (let i = 0; i < this.data.list.length; i++) { let crs_src = '' let crs_dst = '' for (let [key, item] of this.name_map) { if (item.def === this.data.list[i].crs_src) { crs_src = item.epsg } if (item.def === this.data.list[i].crs_dst) { crs_dst = item.epsg } } for (let m = 0; m < this.data.list[i].features.length; m++) { this.data.list[i].features[m].properties.id if(!this.data.list[i].features[m].properties) { this.data.list[i].features[m].properties = {} } if(!this.data.list[i].features[m].properties.id) { this.data.list[i].features[m].properties.id = Cesium.createGuid() } this.data.list[i].features[m].id = this.data.list[i].features[m].properties.id posConvert( this.data.list[i].features[m].geometry, this.data.list[i].crs_src, this.data.list[i].crs_dst || '+proj=longlat +datum=WGS84 +no_defs' ) this.data.list[i].features[m].geometry.geometries || (this.data.list[i].features[m].geometry.geometries = []) let range = turf.bbox(this.data.list[i].features[m].geometry) if (range && range[0] && range[0] != Infinity) { this.data.list[i].features[m].geometry.range = turf.bbox( this.data.list[i].features[m] ) } features.push(this.data.list[i].features[m]) } } this.geojson = { type: 'FeatureCollection', features: features } // console.log(this.geojson) // for (let i = 0; i < this.geojson.features.length; i++) { // this.geojson.features[i].id = this.geojson.features[i].properties.id // this.geojson.features[i].geometry.range = turf.bbox(this.geojson.features[i]); // } } get fileName() { return this.options.fileName } set fileName(v) { this.options.fileName = v this._elms.fileName && (this._elms.fileName.value = v) } get field() { return this.options.field } set field(v) { this.options.field = v // for (let i = 0; i < this.#textEntity.length; i++) { // const img = new Image(); // img.src = Vector.getcanvas(this.#textEntity[i].properties[this.options.field] && this.#textEntity[i].properties[this.options.field]._value + '') // img.onload = async () => { // this.#textEntity[i].billboard.image = Vector.getcanvas(this.#textEntity[i].properties[this.options.field] && this.#textEntity[i].properties[this.options.field]._value + '') // } // } let label = v for (let index = 0; index < this.options.head_tables.length; index++) { if (this.options.head_tables[index].key === v) { label = this.options.head_tables[index].label break } } this._elms.field && (this._elms.field.value = label) } get img() { return this.options.img } set img(v) { if (!Array.isArray(v)) { v = [v] } this.options.img = v Vector.createImage(this) } get color() { return this.options.color } set color(v) { this.options.color = v if (!this.entity) { return } this.entity.entities.values.forEach(enetity => { if (enetity.point) { enetity.point.color = Cesium.Color.fromCssColorString(v).withAlpha( this.opacity ) } if (enetity.polygon) { enetity.polygon.material = Cesium.Color.fromCssColorString(v).withAlpha( this.opacity ) } if (enetity.polyline) { enetity.polyline.material = Cesium.Color.fromCssColorString( v ).withAlpha(this.opacity) } }) } set onRightClick(val) { if (val && typeof val !== 'function') { console.error('val:', val, '不是一个function') } else { if (this.rightClickCallBack == null && this.options && this.options.id) { regRightClickCallback(this.options.id, this.rightClickCB, this) } this.rightClickCallBack = val } } leftClickCB(mo, id, p, that) { if (that.clickCallBack && typeof that.clickCallBack === 'function') { that.clickCallBack(mo, id, p) } } static create(that) { // that.entity = new Cesium.PrimitiveCollection(); // that.viewer.scene.primitives.add(that.entity) // Vector.calculationScope(that, that.data) // Vector.createPoints(that, that.data.points) // Vector.createPolylines(that, that.data.polylines) // Vector.createPolygons(that, that.data.polygons) let bbox = turf.bbox(that.geojson) if (bbox && bbox[0] && bbox[0] != Infinity) { that.range = bbox } that.options.img && Vector.createImage(that) const getColor = level => { // level = Number(level) let color = that.options.color if ( that.disaster_type_colors && that.disaster_type_colors.hasOwnProperty(level) ) { color = that.disaster_type_colors[level] } return color } if ( that.options.path.endsWith('.kml') || that.options.path.endsWith('.kmz') ) { let url = '' that.options.host = that.options.host || getHost() if (that.options.host.endsWith('yjearth4.0')) { url = that.options.host + '/api/v1/vector/getKml' } else { url = that.options.host + '/yjearth4.0/api/v1/vector/getKml' } url += '?path=' + that.options.path // url = 'json/shp.json' fetch(url, { method: 'get', headers: { 'Content-Type': 'application/json', token: getToken(), Authorization: 'Bearer ' + getToken() } }).then(response => { if (response.status === 200) { response.blob().then(data => { data.text().then(kml => { if (that.options.path.endsWith('.kmz')) { kml = url } else { let parser = new DOMParser() // new Parser kml = parser.parseFromString(kml, 'text/xml') // Parse string let placemark = kml.getElementsByTagName('Placemark') for (let i = 0; i < placemark.length; i++) { placemark[i].id = that.geojson.features[i].id } } Cesium.KmlDataSource.load(kml, { camera: that.viewer.scene.camera, canvas: that.viewer.scene.canvas, clampToGround: true }).then(datasource => { createEntity(datasource) }) }) }) } }) } else { const geoJsonDataSource = new Cesium.GeoJsonDataSource() let promise = geoJsonDataSource.load(that.geojson, { clampToGround: true }) let Instances = [] return promise.then(datasource => { createEntity(datasource) }) } function createEntity(datasource) { that.entity = datasource // that.viewer.dataSources.add(datasource); datasource.entities.values.forEach((entity, index) => { // if(!that.geojson.features[index]) { // return // } // that.data.list[i].features[index].id = that.data.list[i].features[index].properties.id = entity.id // that.geojson.features[index].id = that.geojson.features[index].properties.id = entity.id entity.show = that.options.show entity.type = 'vector' entity.parentId = that.options.id if (!entity.properties) { entity.properties = {} } // that.sdk.viewer.entities.add(entity) addCluster(that.sdk, entity) let color = getColor( entity.properties && entity.properties[that.options.fxdj] && entity.properties[that.options.fxdj]._value ) if (entity.billboard) { // entity.billboard.show = false entity.billboard.heightReference = Cesium.HeightReference.CLAMP_TO_GROUND entity.point = new Cesium.PointGraphics({ show: true, // position: new Cesium.Cartesian3(entity.position._value.x, entity.position._value.y, entity.position._value.z), // 点的位置 color: Cesium.Color.fromCssColorString( color || 'rgba(0,255,184,0.5)' ).withAlpha(that.opacity), // 点的颜色 pixelSize: 10, // 点的大小 heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, disableDepthTestDistance: new Cesium.CallbackProperty(function () { return getGroundCover() ? undefined : Number.POSITIVE_INFINITY }, false) }) } if (entity.polyline) { let material = entity.polyline.material if (color) { material = Cesium.Color.fromCssColorString(color).withAlpha( that.opacity ) } entity.polyline = new Cesium.PolylineGraphics({ positions: entity.polyline.positions._value, width: entity.polyline.width && entity.polyline.width.getValue(), clampToGround: true, material: material, zIndex: that.sdk._entityZIndex }) // let polyline = new Cesium.GroundPolylineGeometry({ // positions: entity.polyline.positions._value, // width: 2 // }); // Instances.push(new Cesium.GeometryInstance({ // geometry: polyline, // attributes: { // color: Cesium.ColorGeometryInstanceAttribute.fromColor( // Cesium.Color.fromCssColorString('rgba(0,255,184,0.5)') // ) // } // })) // console.log(entity.polyline) } if (entity.polygon && entity.polygon.material) { entity.polygon.perPositionHeight = false entity.polygon.extrudedHeight = undefined let material = entity.polygon.material.color._value if (color) { material = Cesium.Color.fromCssColorString(color) } if (that.opacity || that.opacity === 0) { material = material.withAlpha(that.opacity) } let data = entity.kml || entity.kmz if (data) { let img = data.extendedData && data.extendedData.BaseTexturePath && data.extendedData.BaseTexturePath.value ? data.extendedData.BaseTexturePath.value : undefined if (img) { entity.polygon.material.image = img entity.polygon.material.repeat = new Cesium.Cartesian2(1, 1) } entity.polygon.material = new Cesium.ImageMaterialProperty({ image: img, // 贴图的URL repeat: new Cesium.Cartesian2(1, 1), color: material }) } entity.polygon.zIndex = that.sdk._entityZIndex if ( that.options.path.endsWith('.kml') || that.options.path.endsWith('.kmz') ) { let material = color ? Cesium.Color.fromCssColorString('#000000') : entity.polygon.outlineColor.getValue() if (that.opacity || that.opacity === 0) { material = material.withAlpha(that.opacity) } if (entity.polygon.outlineWidth) { entity.polyline = new Cesium.PolylineGraphics({ positions: entity.polygon.hierarchy._value.positions, width: entity.polygon.outlineWidth.getValue(), clampToGround: true, material: material, zIndex: that.sdk._entityZIndex }) } } else { entity.polyline = new Cesium.PolylineGraphics({ positions: entity.polygon.hierarchy._value.positions, width: 1, clampToGround: true, material: Cesium.Color.fromCssColorString('#000000').withAlpha( that.opacity ), zIndex: that.sdk._entityZIndex }) // 通过生成canvas创建billboard,增加贴地高度的加载速度 // if (entity.billboard && entity.position && entity.position.getValue()) { // const img = new Image(); // img.src = Vector.getcanvas(entity.properties[that.options.field] && entity.properties[that.options.field]._value + '') // img.onload = async function () { // let position = entity.position.getValue() // let entity2 = new Cesium.Entity({ // show: that.options.show, // id: entity.id + '_text', // parentId: that.options.id, // position: new Cesium.Cartesian3(position.x, position.y, position.z), // 点的位置 // billboard: { // image: img, // pixelOffset: new Cesium.Cartesian2(0, -15), // verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // disableDepthTestDistance: new Cesium.CallbackProperty(function () { // return getGroundCover() ? undefined : 100000000 // }, false) // } // }) // entity2.properties = entity.properties // that.#textEntity.push(entity2) // that.sdk.viewer.entities.add(entity2) // } // } if (entity.point) { entity.label = new Cesium.LabelGraphics({ text: new Cesium.CallbackProperty(function () { return ( entity.properties[that.options.field] && entity.properties[that.options.field]._value + '' ) }, false), outlineColor: Cesium.Color.BLACK, outlineWidth: 10, font: '16px Microsoft YaHei', fillColor: Cesium.Color.fromCssColorString('#ffeb3b'), verticalOrigin: Cesium.VerticalOrigin.BOTTOM, pixelOffset: new Cesium.Cartesian2(0, -15), style: Cesium.LabelStyle.FILL_AND_OUTLINE, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, disableDepthTestDistance: new Cesium.CallbackProperty(function () { return getGroundCover() ? undefined : Number.POSITIVE_INFINITY }, false) }) } } that.sdk._entityZIndex++ } if ( that.options.path.endsWith('.kml') || that.options.path.endsWith('.kmz') ) { if (entity.label) { entity.label.font = '16px Microsoft YaHei' let feature = that.geojson.features[index] entity.billboard.heightReference = Cesium.HeightReference.CLAMP_TO_GROUND entity.properties = feature ? feature.properties : {} if (!entity.properties.id && !entity.properties.Id && !entity.properties.ID) { entity.properties.addProperty('id', entity.id) } entity.label.text = new Cesium.CallbackProperty(function () { return entity.properties[that.options.field] && entity.properties[that.options.field]._value + '' }, false) } } else { if (entity.point) { entity.label = new Cesium.LabelGraphics({ text: new Cesium.CallbackProperty(function () { return ( entity.properties[that.options.field] && entity.properties[that.options.field]._value + '' ) }, false), outlineColor: Cesium.Color.BLACK, outlineWidth: 10, font: '16px Microsoft YaHei', fillColor: Cesium.Color.fromCssColorString('#ffeb3b'), verticalOrigin: Cesium.VerticalOrigin.BOTTOM, pixelOffset: new Cesium.Cartesian2(0, -15), style: Cesium.LabelStyle.FILL_AND_OUTLINE, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, disableDepthTestDistance: new Cesium.CallbackProperty(function () { return getGroundCover() ? undefined : Number.POSITIVE_INFINITY }, false) }) } } }) if (!that.options.field) { const properties = ['id', 'Id', 'ID', 'name', 'Name', 'NAME', 'address', 'Address', 'ADDRESS', 'text', 'Text', 'TEXT', 'label', 'Label', 'LABEL']; const field = properties.find(prop => that.geojson.features[0].properties[prop] !== undefined); if (field) { that.options.field = field; } else { for (let key in that.geojson.features[0].properties) { that.options.field = key; break; } } } // for (let i = 0; i < that.#textEntity.length; i++) { // that.entity.entities.add(that.#textEntity[i]) // } that.#loaded = true if (that.#loadEvent) { that.#loadEvent() } // let primitive = new Cesium.GroundPolylinePrimitive({ // geometryInstances: Instances, // appearance: new Cesium.PolylineColorAppearance() // }); // that.viewer.scene.primitives.add(primitive); // that.viewer.zoomTo(datasource) } } static createImage(that) { //(primitive无法贴地?) that.removeImage() if (!that.range) { return } for (let i = 0; i < that.options.img.length; i++) { let entity = that.sdk.viewer.entities.add({ show: that.options.show, rectangle: { coordinates: Cesium.Rectangle.fromDegrees(...that.range), material: new Cesium.ImageMaterialProperty({ image: that.options.img[i], transparent: true }) } }) that.imgEntity.push(entity) } } static getcanvas(text) { const canvas = document.createElement('canvas') const ctx = canvas.getContext('2d') ctx.font = '16px YaHei' let texts = text.split('\n') let canvasWidth = 0 let canvasHeight = 0 for (let i = 0; i < texts.length; i++) { const t = texts[i] const width = ctx.measureText(t).width if (width > canvasWidth) { canvasWidth = width } canvasHeight += 16 } canvasHeight = canvasHeight + 10 + (texts.length - 1) * 5 canvasWidth = canvasWidth + 30 canvas.width = canvasWidth canvas.height = canvasHeight const linearGradient = ctx.createLinearGradient( 0, 0, canvasWidth, canvasHeight ) ctx.fillStyle = linearGradient ctx.fillRect(0, 0, canvasWidth, canvasHeight) ctx.fillStyle = '#ffffff' ctx.font = '16px YaHei' ctx.strokeStyle = '#000000' ctx.strokeText( texts, 15, 16 * texts.length + (10 * 10) / 16 + texts.length * 5 ) ctx.fillText( texts, 15, 16 * texts.length + (10 * 10) / 16 + texts.length - 1 * 5 ) ctx.moveTo(canvasWidth / 2, canvasHeight) ctx.stroke() ctx.closePath() return canvas.toDataURL() } async edit(status = false, DialogEvent = {}) { this.originalOptions = this.deepCopyObj(this.options) if (this._DialogObject && this._DialogObject.close) { this._DialogObject.close() this._DialogObject = null } if (status) { this._DialogObject = await new Dialog(this.sdk, this.options, { title: '编辑属性', left: '180px', top: '100px', confirmCallBack: options => { this.fileName = this.fileName.trim() if (!this.fileName) { this.fileName = '未命名对象' } this.originalOptions.fileName = this.fileName this.originalOptions.field = this.field this._DialogObject.close() DialogEvent.confirmCallBack && DialogEvent.confirmCallBack({ id: this.options.id, fileName: this.originalOptions.fileName, field: this.originalOptions.field }) }, closeCallBack: () => { this.reset() DialogEvent.closeCallBack && DialogEvent.closeCallBack() } }) let html = `
暂无数据
暂无数据