【问题标题】:Determine all geohashes for a given level确定给定级别的所有地理哈希
【发布时间】:2021-02-11 21:04:23
【问题描述】:

我正在使用 R 包 geohashTools,我想知道给定级别/精度的所有可能的地理哈希。一种方法是蛮力。

# 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,或者如果网格太细,它可能真的效率低下。

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

【问题讨论】:

    标签: r geohashing


    【解决方案1】:

    使用 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)
    

    【讨论】:

    • 如果你想file a PR,这看起来像是一个有用的功能,可以添加到geohashTools包中
    • 好主意,谢谢 - 会在某个时候这样做
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2017-02-16
    • 2013-08-25
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多