概述
函数distance()
计算两个坐标对象间的直线距离。
参数:distance(<point_1>, <point_2>)
<point_1>
和<point_2>
:两个坐标对象(point类型)
返回值:
- 计算后的距离(数值类型)
- 距离单位默认为米
一般用法
示例图集:
在一个空图集中,依次运行以下各行语句创建示例图集:
create().node_property(@default, "name").node_property(@default, "lat_long", point)
insert().into(@default).nodes([{_id:"L001", _uuid:1, name:"New York", lat_long:point({latitude:40.7, longitude:-74.0})}, {_id:"L002", _uuid:2, name:"Paris", lat_long:point({latitude:48.5, longitude:2.2})}, {_id:"L003", _uuid:3, name:"Sydney", lat_long:point({latitude:-33.9, longitude:150.9})}, {_id:"L004", _uuid:4, name:"Beijing", lat_long:point({latitude:39.9, longitude:116.3})}])
本例使用异源别名计算,结果换算为公里(千米):
find().nodes({name in ["New York", "Paris"]}) as a
find().nodes({name in ["Sydney", "Beijing"]}) as b
return table(a.name, b.name, distance(a.lat_long, b.lat_long)/1000)
| a.name | b.name | distance(a.point, b.point)/1000 |
|----------|---------|---------------------------------|
| New York | Sydney | 16017.5939640978 |
| Paris | Beijing | 8247.41966611293 |
本例将异源别名做笛卡尔积组合后计算,结果换算为公里(千米):
find().nodes({name in ["New York", "Paris"]}) as a
find().nodes({name in ["Sydney", "Beijing"]}) as b
with distance(a.lat_long, b.lat_long)/1000 as c
return table(a.name, b.name, c)
| a.name | b.name | c |
|----------|---------|------------------|
| New York | Sydney | 16017.5939640978 |
| New York | Beijing | 10992.9752060986 |
| Paris | Sydney | 16967.299225946 |
| Paris | Beijing | 8247.41966611293 |