10.Cesium坐标和相互转换

1.Cesium坐标:

1.1世界坐标

Cartesian3:笛卡尔空间直角坐标系
new Cesium.Cartesian3(x, y, z)
10.Cesium坐标和相互转换 - 图1
可以看作,以椭球中心为原点的空间直角坐标系中的一个点的坐标。

1.2经纬度

地理坐标系,坐标原点在椭球的质心。
经度:参考椭球面上某点的大地子午面与本初子午面间的两面角。东正西负。
纬度 :参考椭球面上某点的法线与赤道平面的夹角。北正南负。
Cesuim中没有具体的经纬度对象,要得到经纬度首先需要计算为弧度,再进行转换。
10.Cesium坐标和相互转换 - 图2

1.3弧度

Cartographic
new Cesium.Cartographic(longitude, latitude, height)
这里的参数也叫做,longitude,latitude,即经度和纬度。

但是是用弧度表示的经纬度,经纬度其实就是角度,可以看上面的解释。弧度即角度对应弧长是半径的倍数。

角度转弧度 π/180×角度
弧度变角度 180/π×弧度
10.Cesium坐标和相互转换 - 图3

1.4地理坐标系和笛卡尔空间坐标系区别

Cesium中常用的坐标系主要有两种:WGS84坐标系和笛卡尔空间直角坐标系。平时我们常见的某个点的经纬度就是在WGS84坐标系下某个点的坐标,它的坐标原点在椭球的质心;而笛卡尔坐标系主要是用来做空间位置的变化如平移、旋转和缩放等等,它的坐标原点在椭球的中心。
二者的联系如下图
10.Cesium坐标和相互转换 - 图4
笛卡尔空间坐标的原点就是椭球的中心。
在实际应用中用的最多的操作就是(lng, lat, alt)<=>(x, y, z)之间的相互转换,cesiumjs为我们提供了这些转换

1.5还有不常用的坐标系:

1、平面坐标系(Cartesian2);
2、笛卡尔空间直角坐标系(Cartesian3);
3、Cartesian4(unknown,在应用中几乎用不到)
4、Cartographic(地理坐标系下经纬度的弧度表示),通常情况下通过它和WGS84坐标系之间互转。

2.坐标转换

2.1 含义和标识

Pick——屏幕坐标

Cartesian——世界坐标

new Cesium.Cartesian2(1,1) //表示一个二维笛卡尔坐标系,也就是直角坐标系(屏幕坐标系)
new Cesium.Cartesian3(1,1,1) //表示一个三维笛卡尔坐标系,也是直角坐标系(就是真实世界的坐标系)

cartographic——-地理坐标(弧度)

Point——地理坐标(经纬度)

2.2 经纬度和世界坐标

Cesium提供了这两个坐标系的转换方法,是比较简单的,可以直接转换,但是有一点是Cartesian3坐标转换成GPS坐标时,它的结果是弧度值,不是角度值,所以如果需要我们常用的GPS坐标就需要再转换一下,将弧度转换为角度。

经纬度转换为世界坐标

第一种方式:直接转换-经纬度坐标(WGS84)→ Cartesian3

Cesium.Cartesian3.fromDegrees(longitude, latitude, height, ellipsoid, result)
longitude:经度
latitude:纬度
height:高度
ellipsoid:椭球体:

弧度坐标→ Cartesian3

Cesium.Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result) → Cartesian3

第二种方式:先转换成弧度再转换(经纬度坐标(WGS84)→ Cartographic)

以上方式是借助了ellipsoid对象的方法。
var ellipsoid=viewer.scene.globe.ellipsoid;
var cartographic=Cesium.Cartographic.fromDegrees(lng,lat,alt);
var cartesian3=ellipsoid.cartographicToCartesian(cartographic);
console.log(‘x=’ + cartesian3.x + ‘,y=’ + cartesian3.y + ‘,z=’ + cartesian3.z);//单位:米,米,米
Cesium.Cartographic.fromDegrees(longitude, latitude, height, result) → Cartographic

世界坐标转换为经纬度

第一种:经纬度

var ellipsoid=viewer.scene.globe.ellipsoid;
var cartesian3=new Cesium.cartesian3(x,y,z);
var cartographic=ellipsoid.cartesianToCartographic(cartesian3);
var lat=Cesium.Math.toDegrees(cartograhphic.latitude);
var lng=Cesium.Math.toDegrees(cartograhpinc.longitude);
var alt=cartographic.height;

第二种:弧度 Cartesian3→ Cartographic

Cesium.Cartographic.fromCartesian(cartesian, ellipsoid, result)

注意:

需要注意cesium内部的角度单位是弧度,因此输入计算或输出显示时需要转换。
转换到笛卡尔坐标系后就能运用计算机图形学中的仿射变换知识进行空间位置变换如平移旋转缩放。
cesiumjs为我们提供了很有用的变换工具类,Cesium.Cartesian3(相当于Point3D)Cesium.Matrix3(3x3矩阵,用于描述旋转变换),Cesium.Matrix4(4x4矩阵,用于描述旋转加平移变换),Cesium.Quaternion(四元数,用于描述围绕某个向量旋转一定角度的变换)。
下面举个例子:
一个局部坐标为p1(x,y,z)的点,将它的局部坐标原点放置到loc(lng,lat,alt)上,局部坐标的z轴垂直于地表,局部坐标的y轴指向正北,并围绕这个z轴旋转d度,求此时p1(x,y,z)变换成全局坐标笛卡尔坐p2(x1,y1,z1)是多少?
var rotate = Cesium.Math.toRadians(d);//转成弧度
var quat = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Z, rotate); //quat为围绕这个z轴旋转d度的四元数
var rot_mat3 = Cesium.Matrix3.fromQuaternion(quat);//rot_mat3为根据四元数求得的旋转矩阵
var v = new Cesium.Cartesian3(x, y, z);//p1的局部坐标
var m = Cesium.Matrix4.fromRotationTranslation(rot_mat3, Cesium.Cartesian3.ZERO); m2为旋转加平移的4x4变换矩阵,这里平移为(0, 0, 0) ,故填个Cesium.Cartesian3.ZERO
m = Cesium.Matrix4.multiplyByTranslation(m, v);//m = m X v
var cart3 = ellipsoid.cartographicToCartesian(Cesium.Cartographic.fromDegrees(lng, lat, alt)); //得到局部坐标原点的全局坐标
var m1 = Cesium.Transforms.eastNorthUpToFixedFrame(cart3);//m1为局部坐标的z轴垂直于地表,局部坐标的y轴指向正北的4x4变换矩阵
m = Cesium.Matrix4.multiplyTransformation(m, m1);//m = m X m1
var p2 = Cesium.Matrix4.getTranslation(m);//根据最终变换矩阵m得到p2
console.log(‘x=’ + p2.x + ‘,y=’ + p2.y + ‘,z=’ + p2.z);

2.3 弧度和经纬度

经纬度转弧度:

Cesium.CesiumMath.toRadians(degrees)
或者
var cartographic = Cesium.Cartographic.fromDegree(point);

弧度转经纬度:

Cesium.CesiumMath.toDegrees(radians)
或者
var point=[ cartographic.longitude / Math.PI 180, cartographic.latitude / Math.PI 180];

2.4 屏幕坐标和世界坐标相互转换

屏幕转世界坐标:

3D模式下:

var pick = new Cesium.Cartesian2(window.innerWidth, window.innerHeight)
var cartesian = scene.globe.pick(viewer.camera.getPickRay(pick), scene)
注意这里屏幕坐标一定要在球上,否则生成出的cartesian对象是undefined

2D模式下:

var pick = new Cesium.Cartesian2(0, 0);
var cartesian = viewer.camera.pickEllipsoid(pick, viewer.scene.globe.ellipsoid);

世界坐标转屏幕坐标

Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, Cartesian3);
结果是Cartesian2对象,取出X,Y即为屏幕坐标。

2.5Cartesian2——-new Cesium.Cartesian2(x, y)

1. Cartesian3→ Cartesian2:Cesium.Cartesian2.fromCartesian3(cartesian, result)→ Cartesian2

2.5 经纬度坐标和弧度坐标也可以通过Cesium.Math来转换

Cesium.CesiumMath.toDegrees(radians) → Number
Cesium.CesiumMath.toRadians(degrees) → Number

3.Cartesian3 API
Cartesian3的源码比较简单,所以我以翻译为主,辅以自己的理解等等。
Cartesian的翻译为”笛卡尔”,实际上Cartesian3是来储存点的X/Y/Z坐标的,其中的方法也是服务于这一点,请大家记住这一点。以下就简称之为C3。
position: Cesium.Cartesian3.fromDegrees(120.941169,27.9932,30.0),

position: new Cesium.Cartesian3(-2400405.756824246, 5367797.512905386, 2470981.8394490457)

.fromSpherical函数:

Converts the provided Spherical into Cartesian3 coordinates
把球形转化为C3坐标

.fromElements函数:

Creates a Cartesian3 instance from x, y and z coordinates.
使用X/Y/Z坐标给C3赋值

.clone函数:

Duplicates a Cartesian3 instance.
复制C3实例并返回

.fromCartesian4对象:

Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the x, y, and z properties of the Cartesian4 and drops w.
函数方法与clone函数相同,即返回值为C3实例

.packedLength对象:

The number of elements used to pack the object into an array.
返回值为3

.pack函数:

Stores the provided instance into the provided array.
将提供的实例置入数组(可以为空)中,返回该数组

.unpack函数:

Retrieves an instance from a packed array
从数组中根据位置检索一个实例并返回

array的数据结构:[ x , y , z , x , y , z , x , y , z , x ……]

.packArray函数:

Flattens an array of Cartesian3s into an array of components.
由C3的数组转换为一维的数组,并返回

.unpackArray函数:

Unpacks an array of cartesian components into an array of Cartesian3s.
由一维的数组转换为C3的数组,并返回

C3数组的数据结构:[C3_1 , C3_2 , C3_3 ……]

.fromArray对象:

Creates a Cartesian3 from three consecutive elements in an array.
为unpack函数的返回值

.maximumComponent函数:

Computes the value of the maximum component for the supplied Cartesian.
计算C3中X/Y/Z的最大值并返回

.minimumComponent函数:

Computes the value of the minimum component for the supplied Cartesian.
计算C3中X/Y/Z的最小值并返回

.minimumByComponent函数:

Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
分别比较两个C3实例的X/Y/Z并返回一个各坐标均为最小的CS实例

.maximumByComponent函数:

Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
分别比较两个C3实例的X/Y/Z并返回一个各坐标均为最大的CS实例

.magnitudeSquared函数:

Computes the provided Cartesian’s squared magnitude.
计算X/Y/Z的平方和并返回

.magnitude函数:

Computes the Cartesian’s magnitude (length).
进行magnitudeSquared后的根号计算并返回

.distance函数:

Computes the distance between two points.
计算两个点之间的距离

.distanceSquared函数:

Computes the squared distance between two points. Comparing squared distances
计算两点之间的距离的平方根

.normalize函数:

Computes the normalized form of the supplied Cartesian.
计算成为统一的C3格式

.dot函数:

Computes the dot (scalar) product of two Cartesians.(物理意义是什么?经常用到)
计算两个Cartesian的点(标量)乘积

.multiplyComponents函数:

Computes the componentwise product of two Cartesians.
计算两个C3实例的乘积(分量积)后的C3实例

.divideComponents函数:

Computes the componentwise quotient of two Cartesians.
计算两个C3实例的相除(分量商)后的C3实例

.add函数:

Computes the componentwise sum of two Cartesians.
计算两个C3实例的相加(分量和)后的C3实例

.subtract函数:

Computes the componentwise difference of two Cartesians.
计算两个C3实例的相减(分量差)后的C3实例

.multiplyByScalar函数:

Multiplies the provided Cartesian componentwise by the provided scalar.
计算C3实例与已知级别相乘并返回

.divideByScalar函数:

Multiplies the provided Cartesian componentwise by the provided scalar.
计算C3实例与已知级别相除并返回

.negate函数:

Negates the provided Cartesian.
计算C3实例的相反数并返回

.abs函数:

Computes the absolute value of the provided Cartesian.
计算C3实例的绝对值并返回

.lerp函数:

Computes the linear interpolation or extrapolation at t using the provided cartesians.
根据t的值和两个C3实例(对t来说,一个对应的值为0,一个为1)进行内插或外推

.angleBetween函数:

Returns the angle, in radians, between the provided Cartesians.
以弧度制单位返回两个C3实例间的角度

.mostOrthogonalAxis函数:

Returns the axis that is most orthogonal to the provided Cartesian.(还不是很懂)
返回与提供的C3实例正交的轴

.projectVector函数:

Projects vector a onto vector b
把向量a投影到向量b上返回其结果

.equals函数:

Compares the provided Cartesians componentwise and returns
比较两个C3实例是否相同

.equalsArray函数:

private
比较C3实例是否与数组中某偏移量后的X/Y/Z相同

.equalsEpsilon函数:

Compares the provided Cartesians componentwise and returns
根据一定的容忍度(误差圆)来判断两个C3实例是否相等

.cross函数:

Computes the cross (outer) product of two Cartesians.(还不是很懂)
计算两个C3实例之间的叉积

.fromDegrees函数:

Returns a Cartesian3 position from longitude and latitude values given in degrees.
由经纬度(角度单位)计算C3坐标并返回,通过调用fromRadians函数

.fromRadians函数:

Returns a Cartesian3 position from longitude and latitude values given in radians.
由给定的经纬度(弧度单位)计算C3并返回

.fromDegreesArray函数:

Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
由给定的经纬度(角度单位)数组生成一个C3数组并返回

.fromRadiansArray函数:

Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
由给定的经纬度(弧度单位)数组生成一个C3数组并返回

.fromDegreesArrayHeights函数:

Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
由给定的坐标数据(角度单位)的数组生成一个C3数组并返回,数组数据结构如下
[-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0 ……]

.fromRadiansArrayHeights函数:

Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
由给定的坐标数据(角度单位)的数组生成一个C3数组并返回,数组数据结构如下
[-2.007, 0.645, 100000.0, -1.867, .575, 150000.0 ……]
.ZERO对象:An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
.UNIT_X对象:An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
.UNIT_Y对象:An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
.UNIT_Z对象:An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).

.prototype.clone函数:

Duplicates this Cartesian3 instance.
调用clone函数,复制这个C3实例并返回

.prototype.equals函数:

Compares this Cartesian against the provided Cartesian componentwise and returns
调用equals函数,比较该实例与给定实例的相等关系

.prototype.equalsEpsilon函数:

Compares this Cartesian against the provided Cartesian componentwise and returns
调用equalsEpsilon函数,比较该实例域给定实例在误差范围内(给定的容忍度)的比较结果

.prototype.toString函数:

Creates a string representing this Cartesian in the format ‘(x, y, z)’.

表格形式的函数解释

函数 返回类型 说明
clone(result) Cartesian3 复制坐标点
equals(right) Boolean 判断与坐标点rigtht是否相等
equalsEpsilon(right,relativeEpsilon,absoluteEpsilon) Boolean 在容差范围内是否相等
abs(cartesian, result) Cartesian3 返回坐标值x、y、z的绝对值新坐标点。
add(left, right, result) Cartesian3 返回由坐标点left、right的x、y、z分别相加组合的坐标点。
angleBetween(left, right) Number 返回两个坐标点的角度
clone(cartesian, result) Cartesian3 复制坐标点
cross(left, right, result) Cartesian3 计算两个坐标点的交叉乘积
distanceSquared(left, right) Number 返回距离的平方
divideByScalar(cartesian, scalar, result) Cartesian3 返回坐标值x、y,z除以scalar后重新组合的坐标。
divideComponents(left, right, result) Cartesian3 返回left坐标x、y、z分别除以right坐标x、y、z后的重新组合的坐标
dot(left, right) Number 返回left坐标x、y、z分别乘以right坐标x、y、z后的重新组合的坐标
equals(left, right) Cartesian3 判断坐标点left、right是否相等。
fromArray(array, startingIndex, result) Cartesian3 从数组中返回重新组合的坐标点
fromCartesian4(cartesian, result) Cartesian3 从Cartesian4中转为Cartesian3坐标点
fromDegrees(longitude,latitude,height,ellipsoid,result) Cartesian3 从角度值组合坐标值,其中前三个参数分别为经度、纬度、高程。Ellipsoid为椭球类型,默认为wgs84
fromDegreesArray(coordinates,ellipsoid,result) Cartesian3 从给定的角度单位数组数组中组合坐标点
 
fromDegreesArrayHeights(coordinates,ellipsoid,result) Array.
fromElements(x, y, z, result) Cartesian3 指定x、y、z组合新的坐标点
fromRadians(longitude,latitude,height,ellipsoid,result) Cartesian3 从弧度值来创建坐标点
fromRadiansArray(coordinates,ellipsoid,result) Array. 由指定的弧度单位的经纬度组成坐标点并返回。
     
fromRadiansArrayHeights(coordinates,ellipsoid,result)
fromSpherical(spherical, result)
lerp(start, end, t, result) Cartesian3 用于坐标点start、end间的插值。
magnitude(cartesian) Number 计算坐标点x、y、z平方之和计算坐标点x、y、z平方之和
magnitudeSquared(cartesian) Number 计算坐标点x、y、z平方之和
maximumByComponent(first,second,result) Cartesian3 返回坐标first,second中最大的x、y、z组成的坐标result
maximumComponent(cartesian) Number 返回坐标值最大值,要么是x、要么是y、要么是z
midpoint(left, right, result) Cartesian3 返回坐标点left、right的中点。
minimumByComponent(first,second,result) Cartesian3 返回坐标first,second中最小的x、y、z组成的坐标result
minimumComponent(cartesian) Number 返回坐标值最小值,要么是x、要么是y、要么是z
mostOrthogonalAxis(cartesian, result) Cartesian3 返回与cartesian最正交的坐标轴
multiplyByScalar(cartesian,scalar,result) Cartesian3 坐标点按某一个比例值缩放、即乘以scalar值,返回新的坐标点result。
multiplyComponents(left,right,result) Cartesian3 返回x=left.xright.x、y=left.yright.y、z=left.z*right.z、组成的坐标点、
negate(cartesian, result) Cartesian3 返回cartesian坐标值取反的新坐标
normalize(cartesian, result) Cartesian3 坐标点归一化,返回结果保存在result中。
projectVector(a, b, result) Cartesian3 返回投影向量
subtract(left,right,result) Cartesian3 返回坐标点相减后的点,其中x=left.x-rigt.x,y=left.x-right.y,
z=left.z-right.z
pack(value, array, startingIndex) Array. 将坐标值置于数组中,返回数组。
packArray(array, result) Array. 将坐标数据array转为一维数组。