33 Geohash Neighbor
Objective
Create an R function that finds a directional neighbor for a geohash.
We now know how to take a string in R and convert it into an enum and what a &str is.
We’re going to begin building up a gh_neighbor() function which takes two arguments:
pub fn neighbor(
hash_str: &str,
direction: Direction,
) -> Result<String, GeohashError>and returns a geohash as a string. We’re going to create an R wrapper to this function.
Exercise
- Create a new function
gh_encode()which has two arguments:geohash: String, anddirection: Stringand returns aString - Import
neighbor()fromgeohash - Use
as_direction()to process thedirectionarguments - Use
neighbor()to return the resultant geohash (you can unwrap for now)- Note: borrow
geohashasneighbor()expects a&str—i.e.neighbor(&geohash, ...)
- Note: borrow
View solution
#[extendr]
fn gh_neighbor(geohash: String, direction: String) -> String {
let dir = as_direction(direction);
neighbor(&geohash, dir).unwrap()
}