【发布时间】:2019-07-12 22:28:06
【问题描述】:
我希望将dynamo-geo.js 库的一部分移植到 golang,以便查询到给定点的最近点(存储在 DyanmoDB 中)。
radius 查询是理想的,但如果rectangle 查询是一种更直接的算法,我也很乐意。
这是我想出的按半径查询的代码,但我似乎无法获得覆盖单元格的非空列表。
我的算法有什么问题?
// Query a circular area constructed by a center point and its radius.
// @see https://github.com/rh389/dynamodb-geo.js/blob/6c388b9070014a096885e00fff6c3fc933d9853f/src/GeoDataManager.ts#L229
func queryRadius(lat float64, lng float64, radiusMeters float64) (error) {
earthRadiusMeters := 6367000.0
// Step1: Get the bounding region (rectangle) from the center and the radius
// @see https://github.com/rh389/dynamodb-geo.js/blob/6c388b9070014a096885e00fff6c3fc933d9853f/src/s2/S2Util.ts#L23
centerLatLng := s2.LatLngFromDegrees(lat, lng)
latReferenceUnit := 1.0
if lat > 0.0 {
latReferenceUnit = -1.0
}
latReferenceLatLng := s2.LatLngFromDegrees(lat+latReferenceUnit, lng)
lngReferenceUnit := 1.0
if lng > 0.0 {
lngReferenceUnit = -1.0
}
lngReferenceLatLng := s2.LatLngFromDegrees(lat, lng+lngReferenceUnit)
latForRadius := radiusMeters / centerLatLng.Distance(latReferenceLatLng).Radians() * earthRadiusMeters
lngForRadius := radiusMeters / centerLatLng.Distance(lngReferenceLatLng).Radians() * earthRadiusMeters
minLatLng := s2.LatLngFromDegrees(lat-latForRadius, lng-lngForRadius)
maxLatLng := s2.LatLngFromDegrees(lat+latForRadius, lng+lngForRadius)
boundingRect := s2.RectFromLatLng(minLatLng)
boundingRect = boundingRect.AddPoint(maxLatLng)
// Step2: Compute the CellIDs for the region we want to cover.
// defaults per https://github.com/vekexasia/nodes2-ts/blob/1952d8c1f6cb4a862731ace2d5f74d472ec22e55/src/S2RegionCoverer.ts#L101
rc := &s2.RegionCoverer{MaxLevel: 30, MaxCells: 8, LevelMod: 1}
r := s2.Region(boundingRect.CapBound())
coveringCells := rc.Covering(r)
for _, c := range coveringCells {
log.WithFields(log.Fields{
"Covering Cell": c,
}).Info("=>")
}
return nil
}
【问题讨论】:
-
您的问题解决了吗?我也在寻找此类搜索的解决方案,所以如果您找到了一个可以正常工作的好解决方案,请告诉我。
-
嗨@DanielDudas - 我刚刚发布了到目前为止的答案。感谢任何帮助我到达终点线的帮助。
标签: go geolocation geometry amazon-dynamodb geometry-surface