修改线空间长度选择

This commit is contained in:
2025-07-10 16:55:46 +08:00
parent 11812253e6
commit b0ae9c7042
2 changed files with 158 additions and 47 deletions

View File

@ -277,18 +277,29 @@ class PolylineObject extends Base {
this._elms.wordsName.forEach(item => { this._elms.wordsName.forEach(item => {
item.value = v item.value = v
}) })
if (this.options.lengthByMeter) {
switch (v) { let _this = this
case '空间长度':
break if (!this.noseToTail) {
case '投影长度': this.computeDistance(
break this.options.positions,
case '地表长度': 2,
break v
default: ).then(res => {
break _this.options.lengthByMeter = res
} if (res > 1000 || res == 1000) {
_this.length = (res / 1000).toFixed(5)
_this.lengthUnit = '千米'
} else {
_this.length = res
_this.lengthUnit = '米'
}
})
} else {
} }
} }
get fitLengthUnit() { get fitLengthUnit() {
@ -538,11 +549,11 @@ class PolylineObject extends Base {
break break
} }
this.options.lengthByMeter = this.computeDistance( // this.options.lengthByMeter = this.computeDistance(
this.options.positions, // this.options.positions,
2, // 2,
ground // ground
) // )
this.lengthUnit = this.lengthUnit this.lengthUnit = this.lengthUnit
this._elms.heightMode && (this._elms.heightMode.value = heightModeName) this._elms.heightMode && (this._elms.heightMode.value = heightModeName)
this._elms.heightModeObject && (this._elms.heightModeObject.legp_searchActive( this._elms.heightModeObject && (this._elms.heightModeObject.legp_searchActive(
@ -1330,12 +1341,18 @@ class PolylineObject extends Base {
PolylineObject.createLabel(that) PolylineObject.createLabel(that)
// that.entity.polyline.positionsLngLat = positions // that.entity.polyline.positionsLngLat = positions
that.renewPolygon(fromDegreesArray) that.renewPolygon(fromDegreesArray)
that.options.lengthByMeter = that.computeDistance(positions, 2, ground) that.computeDistance(
that.lengthUnit = that.options['length-unit'] positions,
syncData(that.sdk, that.options.id) 2
if (that.options.show) { ).then(res => {
setSplitDirection(0, that.options.id) that.options.lengthByMeter = res
} that.lengthUnit = that.options['length-unit']
syncData(that.sdk, that.options.id)
if (that.options.show) {
setSplitDirection(0, that.options.id)
}
})
// if (this.options['nose-to-tail']) { // if (this.options['nose-to-tail']) {
// let array = [] // let array = []
@ -2150,21 +2167,39 @@ class PolylineObject extends Base {
alt: fromDegreesArray[i + 2] alt: fromDegreesArray[i + 2]
}) })
} }
this.options.fitLengthByMeter = this.computeDistance( // this.options.fitLengthByMeter = this.computeDistance(
// array,
// 2,
// ground
// )
let _this = this
this.computeDistance(
array, array,
2, 2,
ground this.options['words-name']
) ).then(res => {
_this.options.fitLengthByMeter
})
} else { } else {
this.options.fitLengthByMeter = this.computeDistance( let _this = this
this.computeDistance(
positions, positions,
2, 2,
ground this.options['words-name']
) ).then(res => {
_this.options.fitLengthByMeter
})
} }
this.fitLengthUnit = this.options['fit-length-unit'] this.fitLengthUnit = this.options['fit-length-unit']
this.options.lengthByMeter = this.computeDistance(positions, 2, ground) let _this = this
this.lengthUnit = this.options['length-unit'] this.computeDistance(
positions,
2,
this.options['words-name']
).then(res => {
_this.options.lengthByMeter
_this.lengthUnit = this.options['length-unit']
})
return fromDegreesArray return fromDegreesArray
} }

View File

@ -139,30 +139,107 @@ class Tools {
* @param [fractionDigits=2] 保留小数点位数 * @param [fractionDigits=2] 保留小数点位数
* @param [ground=true] 是否贴地 * @param [ground=true] 是否贴地
* */ * */
computeDistance(positions = [], fractionDigits = 2, ground = true) { // computeDistance(positions = [], fractionDigits = 2, ground = true) {
// if (positions.length < 2) {
// return 0
// } else {
// let length = 0
// if (ground) {
// let lineString = []
// positions.forEach((item) => {
// lineString.push([item.lng, item.lat, item.alt])
// })
// let line = turf.lineString(lineString)
// length = turf.length(line) * 1000
// }
// else {
// for (let i = 0; i < positions.length - 1; i++) {
// const position1 = Cesium.Cartesian3.fromDegrees(positions[i].lng, positions[i].lat, positions[i].alt);
// const position2 = Cesium.Cartesian3.fromDegrees(positions[i + 1].lng, positions[i + 1].lat, positions[i + 1].alt);
// const distance = Cesium.Cartesian3.distance(position1, position2);
// length = length + distance
// }
// }
// return length.toFixed(fractionDigits)
// }
// }
async computeDistance(positions = [], fractionDigits = 2, type = '空间长度') {
if (positions.length < 2) { if (positions.length < 2) {
return 0 return 0
} else { } else {
let length = 0 let length = 0
if (ground) { switch (type) {
let lineString = [] case '空间长度':
positions.forEach((item) => { for (let i = 0; i < positions.length - 1; i++) {
lineString.push([item.lng, item.lat, item.alt]) const position1 = Cesium.Cartesian3.fromDegrees(positions[i].lng, positions[i].lat, positions[i].alt);
}) const position2 = Cesium.Cartesian3.fromDegrees(positions[i + 1].lng, positions[i + 1].lat, positions[i + 1].alt);
let line = turf.lineString(lineString) const distance = Cesium.Cartesian3.distance(position1, position2);
length = turf.length(line) * 1000 length = length + distance
} }
else { break
for (let i = 0; i < positions.length - 1; i++) { case '投影长度':
const position1 = Cesium.Cartesian3.fromDegrees(positions[i].lng, positions[i].lat, positions[i].alt); for (let i = 0; i < positions.length - 1; i++) {
const position2 = Cesium.Cartesian3.fromDegrees(positions[i + 1].lng, positions[i + 1].lat, positions[i + 1].alt); const position1 = Cesium.Cartesian3.fromDegrees(positions[i].lng, positions[i].lat, 0);
const distance = Cesium.Cartesian3.distance(position1, position2); const position2 = Cesium.Cartesian3.fromDegrees(positions[i + 1].lng, positions[i + 1].lat, 0);
length = length + distance const distance = Cesium.Cartesian3.distance(position1, position2);
} length = length + distance
}
break
case '地表长度':
let meters
let lineString2 = []
positions.forEach((item) => {
lineString2.push([item.lng, item.lat, item.alt])
})
let line2 = turf.lineString(lineString2)
let d = turf.length(line2) * 1000
meters = d > 20 ? d / 20 : d
let res = this.chunkLine(positions, meters)
let coordinates = []
res.forEach((Feature, index) => {
if (index === 0) {
coordinates = [...Feature.geometry.coordinates]
} else {
coordinates.push(Feature.geometry.coordinates[1])
}
})
let arr = []
for (const item of coordinates) {
const index = coordinates.indexOf(item);
let r = await this.sampleHeight({ lng: item[0], lat: item[1], alt: 0 }, index)
r.position.alt = r.position.alt < 0 ? 0 : r.position.alt
arr.push(r)
}
let l = arr.length - 1
arr.forEach((item, index) => {
if (index !== l) {
let posi = [item.position, arr[index + 1].position]
let d1 = 0
for (let i = 0; i < posi.length - 1; i++) {
const position1 = Cesium.Cartesian3.fromDegrees(posi[i].lng, posi[i].lat, posi[i].alt);
const position2 = Cesium.Cartesian3.fromDegrees(posi[i + 1].lng, posi[i + 1].lat, posi[i + 1].alt);
const distance = Cesium.Cartesian3.distance(position1, position2);
d1 = d1 + distance
}
let d2 = Math.abs(item.position.alt - arr[index + 1].position.alt)
let d3 = Math.sqrt(d1 * d1 + d2 * d2)
length += d3
}
})
break
default:
break;
} }
return length.toFixed(fractionDigits) return length.toFixed(fractionDigits)
} }
} }
async sampleHeight(p1, index) {
let p2 = await this.sampleHeightMostDetailed([p1])
p1.alt = p2[0].height
return { position: p1, index }
}
/**@description 计算多边形面积 /**@description 计算多边形面积
* @method computeArea * @method computeArea
@ -555,7 +632,6 @@ class Tools {
// dashSize: newParam.dashSize, // dashSize: newParam.dashSize,
space: newParam.space space: newParam.space
}) })
console.log(newParam.space)
break break
case 6: //流动虚线2 case 6: //流动虚线2
material = new Cesium.FlowDashedLineFlowMaterialProperty({ material = new Cesium.FlowDashedLineFlowMaterialProperty({