如何计算 RethinkDB 中地理空间查询的距离?
How do I calculate distance on a geospatial query in RethinkDB?
我正在尝试 运行 允许我过滤特定文档的查询,然后检查存储在那里的坐标与我传入的新坐标之间的距离。
我试过这个:
r.db('food').table('fruits')
.hasFields(['origin', 'region'])
.filter({region: 'North America'})
.pluck('gpsLocation')
.distance(r.point(37.759056, 105.015018))
但我收到此错误:e: Expected type DATUM but found SEQUENCE:
从文档中我看到我需要
geometry.distance(geometry[, {geoSystem: 'WGS84', unit: 'm'}])
但我不确定如何让我的查询达到 return。 gpsLocation
是 fruits
table 上的一个索引,如果有区别的话。
您不能 pluck
索引。我认为你的意思是 field
,因为你想用 pluck
提取该字段的数据的方式。然后您从该字段创建一个具有相同名称的索引。如果我的假设是正确的,下面是我的答案。如果不是,您需要更新您的问题,因为您无法提取索引。
问题是错误的数据类型。根据 https://rethinkdb.com/api/javascript/distance/,命令语法看起来像这样:
geometry.distance(geometry[, {geoSystem: 'WGS84', unit: 'm'}]) → number
r.distance(geometry, geometry[, {geoSystem: 'WGS84', unit: 'm'}]) → number
这意味着 distance
可以调用 r
,传递两个 geometyr
对象,或者调用 geometry
对象,并将另一个几何对象作为其传递第一个参数。
您的查询正在返回一个 STREAM。你可以通过阅读API的文档知道它的数据类型,或者直接使用typeOf
.
r.db('food').table('fruits')
.hasFields(['origin', 'region'])
.filter({region: 'North America'})
.pluck('gpsLocation')
.typeOf()
因此,您必须以某种方式遍历 STREAM,并在流文档上调用 distance
。
r.db('food').table('fruits')
.hasFields(['origin', 'region'])
.filter({region: 'North America'})
.pluck('gpsLocation')
.map(function(location) {
return location.distance(r.point(37.759056, 105.015018))
})
这有点类似于你有一个数组,然后在 JavaScript 中调用 map
遍历数组,运行 元素上的回调函数。虽然在这里,地图功能在服务器上运行。
假设您的 gpsLocation
字段包含一个 geometry
对象,例如具有经度和纬度的点 (https://rethinkdb.com/api/javascript/point/)。
我正在尝试 运行 允许我过滤特定文档的查询,然后检查存储在那里的坐标与我传入的新坐标之间的距离。
我试过这个:
r.db('food').table('fruits')
.hasFields(['origin', 'region'])
.filter({region: 'North America'})
.pluck('gpsLocation')
.distance(r.point(37.759056, 105.015018))
但我收到此错误:e: Expected type DATUM but found SEQUENCE:
从文档中我看到我需要
geometry.distance(geometry[, {geoSystem: 'WGS84', unit: 'm'}])
但我不确定如何让我的查询达到 return。 gpsLocation
是 fruits
table 上的一个索引,如果有区别的话。
您不能 pluck
索引。我认为你的意思是 field
,因为你想用 pluck
提取该字段的数据的方式。然后您从该字段创建一个具有相同名称的索引。如果我的假设是正确的,下面是我的答案。如果不是,您需要更新您的问题,因为您无法提取索引。
问题是错误的数据类型。根据 https://rethinkdb.com/api/javascript/distance/,命令语法看起来像这样:
geometry.distance(geometry[, {geoSystem: 'WGS84', unit: 'm'}]) → number
r.distance(geometry, geometry[, {geoSystem: 'WGS84', unit: 'm'}]) → number
这意味着 distance
可以调用 r
,传递两个 geometyr
对象,或者调用 geometry
对象,并将另一个几何对象作为其传递第一个参数。
您的查询正在返回一个 STREAM。你可以通过阅读API的文档知道它的数据类型,或者直接使用typeOf
.
r.db('food').table('fruits')
.hasFields(['origin', 'region'])
.filter({region: 'North America'})
.pluck('gpsLocation')
.typeOf()
因此,您必须以某种方式遍历 STREAM,并在流文档上调用 distance
。
r.db('food').table('fruits')
.hasFields(['origin', 'region'])
.filter({region: 'North America'})
.pluck('gpsLocation')
.map(function(location) {
return location.distance(r.point(37.759056, 105.015018))
})
这有点类似于你有一个数组,然后在 JavaScript 中调用 map
遍历数组,运行 元素上的回调函数。虽然在这里,地图功能在服务器上运行。
假设您的 gpsLocation
字段包含一个 geometry
对象,例如具有经度和纬度的点 (https://rethinkdb.com/api/javascript/point/)。