basetype~Coordinate(x, y, z, zDegree)

坐标,由经线分量、纬线分量、高程分量三个维度上的数值组成的坐标对象。坐标仅表示地理实体的位置信息,是组成地理实体的属性之一,但不等于地理实体(包括点状实体)。
坐标对象可以是地理坐标(以经度、纬度表示)、也可以是平面坐标(以米、千米表示)。它的X、Y、Z分别代表经线分量、纬线分量、高程分量。

Summary

Methods:
fromArray

用数组构造坐标对象。

fromString

用字符串构造坐标对象。

addOffset

对当前坐标进行偏移处理。当前坐标被处理后会发生变化。

clone

克隆当前坐标对象。

destroy

销毁坐标对象

equals

判断当前坐标与传入坐标是否相等,返回true则表示相等,反之表示不相等。

format

用字符串模板格式化坐标。

get2Dist

计算当前坐标与传入坐标,在地理坐标系或投影坐标系下的球面距离(单位:米),该距离属于二维坐标距离,计算过程中忽略高程分量。
地理坐标系下,距离的计算采用haversine公式,计算时使用的半径为参考椭球体的平均地球半径(1/3 * (2a + b)),计算结果的最大误差为0.3%。

get3Dist

计算当前坐标与传入坐标间的三维距离(单位:米)。
两点在地面的投影距离和两点的高度差(高度方向上经纬度和米的转换按照赤道位置圆来换算)进行勾股定理计算后的距离。

getByDirectionAndDistance

获取平面上指定方向指定距离下的坐标

getDimension

获取坐标维度。默认返回3,表示三维坐标。

getOrdinate

获取坐标分量。坐标分量指的是坐标对象在各坐标轴上对应的值。

isValid

验证坐标对象是否为指定坐标系下的有效坐标。

minus

当前坐标的各分量直接减去入参坐标的各分量,以计算两个坐标间的差值。

rotate

将坐标对象绕着指定直线(旋转轴)旋转一定的角度,所指定的直线穿过给定点且平行于某一坐标轴。旋转后的坐标可能发生变化。绕轴旋转的本质如下:绕平行于Z轴的直线旋转即是在XY平面内旋转;绕平行于X轴的直线旋转即是在YZ平面内旋转;绕平行于Y轴的直线旋转即是在XZ平面内旋转

scale

缩放坐标。即在坐标的各分量上,分別以指定的倍数缩放。

setOrdinate

修改坐标分量

to2DString

将坐标值的X、Y分量转换为以逗号分隔的字符串

toArray

将坐标值转换为数组

toFixed

精确一个坐标中每个分量的小数点后的位数。小数位数支持0-14,建议最多保留6位小数。在Chrome浏览器的不同版本中进位、舍位规则不一致,导致结果可能不一致。

toString

将坐标值转换为以逗号分隔的字符串

Constructor

new Coordinate(x, y, z, zDegree)

Parameters:
Name Type Default Description
x Number 0

经线分量

y Number 0

纬线分量

z Number 0

高程分量

zDegree Boolean

高程分量是否以度为单位。当使用地理坐标系(如4326)时,该参数有效。如果设置为true,则高程的单位和地图坐标单位一致,均为度;如果设为false,则代表高程的单位为米。在没有设置该值时,使用initState.unitType的设置。

Throws:

当坐标分量参数不是数字时,抛出异常

Example
let coord = new hmap.basetype.Coordinate(120, 30, 0);

Methods

static fromArray(coordArr) → {module:basetype~Coordinate}

用数组构造坐标对象。

Parameters:
Name Type Description
coordArr Array.<Number>

含1-3个数字值组成的数组,依次对应坐标对象的x、y、z三个坐标分量。数组长度不足时,自动用0补全。

Returns:
module:basetype~Coordinate -

一个坐标对象

Example
let coord = hmap.basetype.Coordinate.fromArray([120.2,30,0]);

static fromString(coordStr) → {module:basetype~Coordinate}

用字符串构造坐标对象。

Parameters:
Name Type Description
coordStr String

用逗号分隔的数字构成的字符串,每个数字依次对应坐标对象的x,y,z三个坐标分量。数字的个数超过3时,只使用前3个数;当个数不足3时,其余的坐标分量自动用0补全。

Returns:
module:basetype~Coordinate -

一个坐标对象。

Example
let str1 = "120,20,30,123,12";
let coord= hmap.basetype.Coordinate.fromString(str1);//输出结果:new hmap.basetype.Coordinate(120,20,30)
let str2 = "120";
let coord= hmap.basetype.Coordinate.fromString(str2);//输出结果:new hmap.basetype.Coordinate(120,0,0)

addOffset(offset) → {module:basetype~Coordinate}

对当前坐标进行偏移处理。当前坐标被处理后会发生变化。

Parameters:
Name Type Description
offset module:basetype~Offset

偏移量。

Throws:

当入参不是module:basetype~Offset时,抛出异常

Returns:
module:basetype~Coordinate -

偏移后的坐标。

Example
let coord =new hmap.basetype.Coordinate(120, 30, 0);
let offset = new hmap.basetype.Offset(2, 1, 0);
let result = coord.addOffset(offset);
//result=new hmap.basetype.Coordinate(122, 31, 0)

clone() → {module:basetype~Coordinate}

克隆当前坐标对象。

Returns:
module:basetype~Coordinate -

一个新的坐标对象,x、y、z分量和当前坐标对象一致。

Example
let coord =new hmap.basetype.Coordinate(120, 30, 0);
let result = coord.clone();

destroy()

销毁坐标对象

Example
coord.destroy();

equals(coord) → {Boolean}

判断当前坐标与传入坐标是否相等,返回true则表示相等,反之表示不相等。

Parameters:
Name Type Description
coord module:basetype~Coordinate

地理坐标

Throws:

当入参不是坐标对象类型时,抛出异常

Returns:
Boolean -

相等返回true;不相等返回false

Example
let coord1 = new hmap.basetype.Coordinate(120, 30, 0);
let coord2 = new hmap.basetype.Coordinate(121, 30, 0);
let result = coord1.equals(coord2);//result=false

format(template, precision) → {String|module:basetype~Coordinate}

用字符串模板格式化坐标。

Parameters:
Name Type Default Description
template String

字符串模板。模板中的{x}将被替换成真实的经线分量;{y}将被替换成真实纬线分量;{z}将被替换成真实高程分量。

precision Number 1

坐标精度,0-13之间的整数,默认为1,即精确到小数点后一位。

Returns:
String | module:basetype~Coordinate -

格式化后的坐标字符串,如果格式化失败(没有匹配上),返回Coordinate本身。

Example
let originCoord = new hmap.basetype.Coordinate(120.325,30.589);
let template = '模版字符串是 ({x},{y})';
let out = originCoord.format(template, 2);//输出结果: '模版字符串是 (120.33,30.59)'

get2Dist(coord, srid) → {Number}

计算当前坐标与传入坐标,在地理坐标系或投影坐标系下的球面距离(单位:米),该距离属于二维坐标距离,计算过程中忽略高程分量。
地理坐标系下,距离的计算采用haversine公式,计算时使用的半径为参考椭球体的平均地球半径(1/3 * (2a + b)),计算结果的最大误差为0.3%。

Parameters:
Name Type Default Description
coord module:basetype~Coordinate

同一坐标系下的目标坐标。

srid Number | String 4326

坐标系标识。可用选项为4326、4490(即CGCS2000)、3857和900913,默认为4326。

Throws:

当第一个参数不是坐标对象时,抛出异常

Returns:
Number -

二维坐标距离(单位:米)

Example
let coord1 = new hmap.basetype.Coordinate(120,30,0);
let coord2 = new hmap.basetype.Coordinate(180,40,0);
let result = coord1.get2Dist(coord2, 4326);
//result=5473489.163137614

get3Dist(coord, srid) → {Number}

计算当前坐标与传入坐标间的三维距离(单位:米)。
两点在地面的投影距离和两点的高度差(高度方向上经纬度和米的转换按照赤道位置圆来换算)进行勾股定理计算后的距离。

Parameters:
Name Type Default Description
coord module:basetype~Coordinate

同一坐标系下的目标坐标。

srid Number | String 4326

坐标系标识。可用选项为4326、4490(即CGCS2000)、3857和900913,默认为4326。

Throws:

当第一个参数不是坐标对象时,抛出异常

Returns:
Number -

三维坐标距离(单位:米)

Example
let coord1 = new hmap.basetype.Coordinate(120,30,0);
let coord2 = new hmap.basetype.Coordinate(180,40,0);
let result = coord1.get3Dist(coord2);//result=5473489.163137614

getByDirectionAndDistance(direction, distance)

获取平面上指定方向指定距离下的坐标

Parameters:
Name Type Description
direction Array.<number> | number

方向向量或与方位角(正北方向为起点,顺时针为正,取值[-360,360])

distance number

距离

getDimension() → {Number}

获取坐标维度。默认返回3,表示三维坐标。

Returns:
Number -

坐标点的维度

Example
let num = coord.getDimension();

getOrdinate(axis) → {Number}

获取坐标分量。坐标分量指的是坐标对象在各坐标轴上对应的值。

Parameters:
Name Type Description
axis String

坐标轴,大写字母。"X":经线坐标轴,"Y":纬线坐标轴,"Z":高程坐标轴。

Throws:

当参数不是"X"或"Y"或"Z"时,抛出异常

Returns:
Number -

指定坐标分量的值

Example
let coord = new hmap.basetype.Coordinate(120,30,0);
let x = coord.getOrdinate("X");

isValid(crs) → {Boolean}

验证坐标对象是否为指定坐标系下的有效坐标。

Parameters:
Name Type Description
crs module:proj~Crs

4326坐标系范围x:[-180,180],y:[-90,90]。900913坐标系范围x:[-20037508.342789,20037508.342789],y:[-20037508.342789,20037508.342789]。

Returns:
Boolean -

true表示坐标在当前坐标系最大的范围内,坐标有效;false表示坐标超过当前坐标系最大的范围,坐标无效。坐标系不被支持时抛出异常。

Example
let isValid = coord.isValid(new hmap.proj.Crs('4326'));

minus(coord) → {module:basetype~Offset}

当前坐标的各分量直接减去入参坐标的各分量,以计算两个坐标间的差值。

Parameters:
Name Type Description
coord module:basetype~Coordinate

同一坐标系下的目标坐标。

Throws:

当参数不是坐标对象时,抛出异常

Returns:
module:basetype~Offset -

偏移量对象,表示两个坐标间的差值。

Example
let coord1 = new hmap.basetype.Coordinate(120,30,0);
let coord2 = new hmap.basetype.Coordinate(122,30,0);
let result = coord1.minus(coord2);
//result=new hmap.basetype.Offset(-2, 0, 0)

rotate(axis, angle, center) → {module:basetype~Coordinate}

将坐标对象绕着指定直线(旋转轴)旋转一定的角度,所指定的直线穿过给定点且平行于某一坐标轴。旋转后的坐标可能发生变化。绕轴旋转的本质如下:绕平行于Z轴的直线旋转即是在XY平面内旋转;绕平行于X轴的直线旋转即是在YZ平面内旋转;绕平行于Y轴的直线旋转即是在XZ平面内旋转

Parameters:
Name Type Default Description
axis String Z

坐标轴,旋转轴平行于该坐标轴并与坐标轴同向。可选值为"X","Y","Z",缺省值为"Z"。

angle Number 0

旋转的角度,超过360度时,取angle/360的余数。逆时针为正,顺时针为负。缺省值为0。

center module:basetype~Coordinate

旋转轴所穿过的点,可选项。该参数为空时,绕坐标轴旋转。

Throws:

参数类型不对时,都会抛出异常

Returns:
module:basetype~Coordinate -

旋转后的坐标

Example
let coord = new hmap.basetype.Coordinate(120,30,0);
let center = new hmap.basetype.Coordinate(122,32,0);
let result = coord.rotate("Z", 180, center);

scale(scaleX, scaleY, scaleZ) → {module:basetype~Coordinate}

缩放坐标。即在坐标的各分量上,分別以指定的倍数缩放。

Parameters:
Name Type Default Description
scaleX Number 1

经线分量缩放倍数,如果0<scaleX<1,为缩小;若scaleX>1,则放大。缺省值为1,即不进行缩放。

scaleY Number 1

纬线分量缩放倍数,如果0<scaleY<1,为缩小;若scaleY>1,则放大。缺省值为1,即不进行缩放。

scaleZ Number 1

高程分量缩放倍数,如果0<scaleZ<1,为缩小;若scaleZ>1,则放大。缺省值为1,即不进行缩放。

Throws:

当参数不是数值类型或者是小于等于0的数值类型时,抛出异常。

Returns:
module:basetype~Coordinate -

缩放后的新坐标。

Example
let coord = new hmap.basetype.Coordinate(120,30,0);
let result = coord.scale(2, 2, 2);
//result = new hmap.basetype.Coordinate(240, 60, 0)

setOrdinate(axis, val) → {module:basetype~Coordinate}

修改坐标分量

Parameters:
Name Type Default Description
axis String X

坐标轴标识。"X":经线坐标轴,"Y":纬线坐标轴,"Z":高程坐标轴。缺省值为"X"。

val Number

坐标分量值

Throws:

第一个参数必须为大写的"X"、"Y"、"Z",否则抛出异常;第二个参数不是数值类型,抛出异常

Returns:
module:basetype~Coordinate -

修改后的坐标

Example
let coord = new hmap.basetype.Coordinate(120,30,0);
coord.setOrdinate("X",100);//coord修改后的坐标分量值为(100,30,0)

to2DString() → {String}

将坐标值的X、Y分量转换为以逗号分隔的字符串

Returns:
String -

字符串形式的坐标值

Example
let coord = new hmap.basetype.Coordinate(120,30,0);
let result = coord.to2DString();
//result = "120,30"

toArray() → {Array.<Number>}

将坐标值转换为数组

Returns:
Array.<Number> -

数组形式的坐标值,顺序依次为经线分量X、纬线分量Y和高程分量Z。

Example
let coord = new hmap.basetype.Coordinate(120,30,0);
let result = coord.toArray();
//result = [120, 30, 0]

toFixed(precision) → {module:basetype~Coordinate}

精确一个坐标中每个分量的小数点后的位数。小数位数支持0-14,建议最多保留6位小数。在Chrome浏览器的不同版本中进位、舍位规则不一致,导致结果可能不一致。

Parameters:
Name Type Description
precision Number

小数位数,0-14之间的整数。参数非法时,坐标不会被精确。

Returns:
module:basetype~Coordinate -

被精确到指定的小数位数的坐标对象。返回的坐标对象中的X、Y、Z值的位数不一定等于要精确的位数,后面的0会被省略。

Example
let result = coord.toFixed(6);

toString() → {String}

将坐标值转换为以逗号分隔的字符串

Returns:
String -

字符串形式的坐标值

Example
let coord = new hmap.basetype.Coordinate(120,30,0);
let result = coord.toString();
//result = "120,30,0"