R/30_gadm_remove.R

Defines functions gadm_remove.gadm_sp gadm_remove.gadm_sf

# gadm_remove.gadm_sf ----------------------------------------------------------
# ==============================================================================
gadm_remove.gadm_sf <- function(x, level=NULL, regions=NULL) {
  if (is.null(level)) {
    level <- x$level
  }
  
  if (is.null(regions)) {
    stop("Missing value for regions")
  }
  
  l_name <- gadm_getLevelName(x, level)
  l_sf <- as.data.frame(x$sf)
  l_sf <- l_sf[!l_sf[, l_name] %in% regions, ] 
  x$sf <- sf::st_as_sf(l_sf)
  
  x
}

# gadm_remove.gadm_sp ----------------------------------------------------------
# ==============================================================================
gadm_remove.gadm_sp <- function(x, level=NULL, regions=NULL) {
  if (is.null(level)) {
    level <- x$level 
  }
  if (is.null(regions)) {
    stop("Missing value for regions")
  }
  NAME <- sprintf("NAME_%d", level)
  df1 <- as.data.frame(x$spdf[, NAME])
  colnames(df1) <- c("N")
  df2 <- x$spdf[!df1$N %in% regions,];
  structure(list("basename"=x$basename,
                 "spdf"=df2,
                 "level"=x$level,
                 "L360" = x$L360,
                 "stripped"=FALSE,
                 "hasBGND"  = FALSE),
            class = "gadm_sp")  
}

Try the GADMTools package in your browser

Any scripts or data that you put into this service are public.

GADMTools documentation built on Aug. 5, 2021, 1:06 a.m.