确定给定级别的所有地理哈希

问题描述

我正在使用 R 包 geohashTools 并且我想知道给定级别/精度的所有可能的 geohashes。一种方法是使用蛮力。

# All possible coordinates                                            
coord <- expand.grid(lon = seq(-180,180,0.1),lat = seq(-90,89.9,0.1))                       
                                                                      
# Load library                                                        
library(geohashTools)                                                 
                                                                      
# Get all unique geohashes                                            
geohashes <- unique(gh_encode(coord$lat,coord$lon,precision = 2L))  
                                                                      

虽然这可行,但我在猜测我的网格需要的分辨率是多少,因此,如果网格太粗,我可能会错过 geohashes,或者如果网格太细,它可能会非常低效。

问:是否有更有效的方法来确定给定级别的所有地理散列?

解决方法

使用 geohashTools 使用的 32 位列表中的可能字符,您可以通过将每个值粘贴到给定前缀上来构建下一级哈希码。像这样的功能有效。

library(data.table)    

gh_fill <- function(geohashes,precision){
  if(uniqueN(nchar(geohashes)) > 1){
    stop("Input Geohashes must all have the same precision level.")
  }
  if(sum(grepl("['ailo]",geohashes)) > 0){
    stop("Invalid Geohash; Valid characters: [0123456789bcdefghjkmnpqrstuvwxyz]")
  }
  
  new_levels <- precision - nchar(geohashes[1])
  
  base32 <- unlist(strsplit("0123456789bcdefghjkmnpqrstuvwxyz",split = ""))
  
  grid <- do.call(CJ,append(list(geohashes),replicate(new_levels,base32,FALSE)))
  
  do.call(paste0,grid)
  
}

# Run like so: 
gh_fill("c6",3L)
# or 
gh_fill(c("c6","c7"),3L)