convert GPS coordinates to C-SQUARES, resolution 0.05°×0.05°

This is a simple C-Squares encoder in R.

FYI, C-Squares is a geocoding algorithm that assigns GPS coordinates to a spatial grid with a chosen resolution. Its specifications is here: https://www.cmar.csiro.au/csquares/spec1-1.htm

My code only works with the resolution 0.05°×0.05° for the time being

# requires {dplyr}

Q_CSQ <- c("threshold" = 0L, "north east" = 1L, "south east" = 3L, "south west" = 5L, "north west" = 7L)
sQ_CSQ <- c("threshold" = 5L, "south west" = 1L, "south east" = 2L, "north west" = 3L, "north east" = 4L)

#' @param lon,lat GPS coords
return(dplyr::case_when(
lon <  threshold & lat >= threshold ~ quad["north west"],
lon >= threshold & lat >= threshold ~ quad["north east"],
lon >= threshold & lat <  threshold ~ quad["south east"],
lon <  threshold & lat <  threshold ~ quad["south west"],
TRUE                                ~ NA_integer_
))
}

#' @title extract tens, units, tenths, hundredths
#' @param x numeric vector
retrieve_decimal <- function(x) {
remainder <- abs(x)

tens <- as.integer(x / 10)
remainder <- remainder - 10 * tens

units <- as.integer(remainder)
remainder <- remainder - units

tenths <- as.integer(10 * remainder)
remainder <- remainder - tenths / 10

hundredths <- as.integer(100 * remainder)

return(list("tens" = tens, "units" = units, "tenths" = tenths, "hundredths" = hundredths))
}

#' @title identify 3'×3' (0.05°×0.05°) C-SQUARES from GPS coordinates
#' @param LONGITUDE,LATITUDE GPS coords, accept vectors
#' @return cells id
identify_33cells_csquares <- function(LONGITUDE, LATITUDE) {

# extract tens, units, tenths, hundredths
vec_lon <- retrieve_decimal(LONGITUDE)
vec_lat <- retrieve_decimal(LATITUDE)

quad_units      <- identify_quadrant(vec_lon$units, vec_lat$units,      sQ_CSQ)
quad_tenths     <- identify_quadrant(vec_lon$tenths, vec_lat$tenths,     sQ_CSQ)
quad_hundredths <- identify_quadrant(vec_lon$hundredths, vec_lat$hundredths, sQ_CSQ)

return(sprintf("%d%d%02d:%d%d%d:%d%d%d:%d",
quad_tens,   vec_lat$tens, vec_lon$tens,
quad_units,  vec_lat$units, vec_lon$units,
quad_tenths, vec_lat$tenths, vec_lon$tenths,