Ironholds / geohash

Geohash generation, decoding and manipulation in R
Other
39 stars 9 forks source link

geohash bounding box #3

Open vecchiocarmelo opened 8 years ago

vecchiocarmelo commented 8 years ago

It will be very useful given a geohash to have a function that return the bounding box (in lat,long)

mestl commented 7 years ago

hash2latlonbbox <- function(hash,precision=7){

' Provides the lat/lon coordinates of the bbox for a hash

' @param hash : character string

' @param precision (optional) : the desired precision for the lonlat

' @description Script provides lat and long of the corners of a geohash 'box'

' @return data.frame of lat long with error

' @seealso geohash pkg

' @export

'

'

library(geohash)

source('is_hash.R'); hash <- is_hash(hash) # in case you want check if it is a valid hash

evenLevelCorner <- matrix(c('b','0','z','p'),2,2) oddLevelCorner <- matrix(c('p','0','z','b'),2,2)

hashBbox <- matrix(rep(hash,4),2,2) # variable holding has of the corner boxes of the original hash box lengthHash <- nchar(hash)

if(lengthHash >= precision) precision <- lengthHash+2 # to provide good enough long lat resolution within m

nrOfBoxing <- precision - lengthHash

for(i in 1:nrOfBoxing) # loop for sub-dividing into smaller hash boxes if(length(hashBbox[1]) %% 2 == 1){ #odd length of hash hashBbox <- matrix(paste(hashBbox,oddLevelCorner,sep=''),2,2)

}else{ # even length of hash
  hashBbox <- matrix(paste(hashBbox,evenLevelCorner,sep=''),2,2)
}

return(gh_decode(hashBbox)) #conversion of hash into lat &lon }

Ironholds commented 7 years ago

Thanks; I imagine the goal is to implement it in C++, but we'll see how it works.

harryprince commented 6 years ago

@Ironholds I've made a PR: https://github.com/Ironholds/geohash/pull/19