当前位置:网站首页>Cesium 多边形增加文字标签(polygon 加 label)多边形中心点偏移问题解决
Cesium 多边形增加文字标签(polygon 加 label)多边形中心点偏移问题解决
2022-06-28 00:05:00 【最凶残的小海豹】
之前在网上搜到的获取中心点的方法如下:
// 多边形的坐标集合(如果已经获取到了,就跳过这一步)
var polygon_point_arr = polygon_point_entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
// 根据坐标集合构造BoundingSphere获取中心点坐标
let center_point = Cesium.BoundingSphere.fromPoints(polygon_point_arr).center
// 设置点标记的坐标(这一步将中心点转到地球表面)
polygon_point_entity.position = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(center_point);
// 添加点标记
polygon_point_entity.label = {
// 点标记参数
}
但是如果多边形不太规则或者点比较多的情况下,标记的位置就不在中心点了,会有偏移,例如下图
原因:boundingsphere 计算的中心点是外接圆的圆心,所以导致了中心点会在图形外。
相关资料参考的是:https://www.lmlphp.com/user/58093/article/item/771705/
解决方案: 使用 turf.js 来计算。
turf.js 中文网:https://turfjs.fenxianglu.cn/
代码:turf.js 版本是 6.5.0
// 多边形的坐标集合(如果已经获取到了,就跳过这一步)
var polygon_point_arr = polygon_point_entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
// 保存转换后的点数组,这个格式必须按照 turf 的要求来
let turf_arr = [[]];
// 坐标转换
polygon_point_arr.forEach(val => {
let polyObj = {
}
// 空间坐标转世界坐标(弧度) 同 Cesium.Cartographic.fromCartesian
let cartographic = gvEarth.scene.globe.ellipsoid.cartesianToCartographic(val)
// 弧度转为角度(经纬度)
polyObj.lon = Cesium.Math.toDegrees(cartographic.longitude)
polyObj.lat = Cesium.Math.toDegrees(cartographic.latitude)
turf_arr[0].push([polyObj.lon, polyObj.lat])
})
// turf 需要将整个点闭合,所以最后一个点必须和起点重合。
turf_arr[0].push(turf_arr[0][0])
let turf_position = turf.polygon(turf_arr)
let turf_position_point = turf.centerOfMass(turf_position)
// 设置点标记坐标
polygon_point_entity.position = Cesium.Cartesian3.fromDegrees(turf_position_point.geometry.coordinates[0], turf_position_point.geometry.coordinates[1], 0)
// 添加点标记
polygon_point_entity.label = {
// 点标记参数
}

边栏推荐
- Original | 2025 to achieve the "five ones" goal! The four products of Jiefang power are officially released
- PostgreSQL setting auto increment field
- Prometeus 2.35.0 new features
- 什么是数字化?什么是数字化转型?为什么企业选择数字化转型?
- pytorch_lightning.utilities.exceptions.MisconfigurationException: You requested GPUs: [1] But...
- 【sylixos】NEW_ Example of type 1 character drive
- PV operation primitive
- 对比学习中的4种经典训练模式
- 766. 托普利茨矩阵
- 自监督学习与药物发现
猜你喜欢

What is digitalization? What is digital transformation? Why do enterprises choose digital transformation?

Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)

树莓派实现温控风扇智能降温
![pytorch_lightning.utilities.exceptions.MisconfigurationException: You requested GPUs: [1] But...](/img/b4/8921c3ca5cd5a547768489d4a79202.png)
pytorch_lightning.utilities.exceptions.MisconfigurationException: You requested GPUs: [1] But...

Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa
![[embedded foundation] memory (cache, ram, ROM, flash)](/img/bd/d952ebd4033a993dd16cd792bb3e1e.png)
[embedded foundation] memory (cache, ram, ROM, flash)

Interviewer asked: Inheritance of JS
![[Niuke discussion area] Chapter 4: redis](/img/53/f8628c65890f1c68cedab9008c1b84.png)
[Niuke discussion area] Chapter 4: redis
![The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]](/img/56/768f8be9f70bf751f176e40cbb1df2.png)
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]

Numpy----np.tile()函数解析
随机推荐
Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)
【牛客討論區】第四章:Redis
【sylixos】i2c设备驱动创建和使用
fiddle如何使用代理
How to study efficiently
深入解析kubernetes controller-runtime
How fiddle uses agents
Machine learning notes - time series as features
【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
PV operation primitive
嵌入式必学,硬件资源接口详解——基于ARM AM335X开发板 (上)
Chapitre 4: redis
Set collection usage
Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
Self supervised learning and drug discovery
力扣今日题-522. 最长特殊序列
Deep parsing of kubernetes controller runtime
Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
Centos8 operation record command version Yum redis MySQL Nacos JDK
有监督、无监督与半监督学习