Skip to content

Commit

Permalink
add ability to find highest level
Browse files Browse the repository at this point in the history
  • Loading branch information
andysouth committed Jan 20, 2020
1 parent b51e4c4 commit e9b038f
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions R/afriadmin.r
Original file line number Diff line number Diff line change
@@ -1,35 +1,67 @@



#todo function to find the highest level for a country


#todo add country outlines for whole continent as single file
#todo mayne if I saved all outlines as a single file it would be easier to subset etc.

#todo allow countrynames as well as iso3
#tood add error checking for countries
#todo add error checking for countries
#todo vectorise iso3 to allow combining countries
#todo maybe vectorise level too

#todo is it good idea to name function same as package ?
afriadmin <- function(iso3 = 'AGO',
level = 2,
level = 'max',
plot = TRUE) {

#GADM_SF_URL = "https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_"
#initially store files directly as downloaded from gadm

path <- system.file(package="afriadmin","/external")

#to allow iso3 in lower case
iso3 <- toupper(iso3)

maxlevel <- maxadmin()

if (level=='max')
{
level <- maxlevel
message("returning max admin level available for ",iso3,", ",maxlevel)
}
else if (level > maxlevel)
{
warning("max admin level available for ",iso3," is ",maxlevel," you requested ",level)
#maybe do this to return mot detailed available
level <- maxlevel
}


filename <- paste0(iso3,'_adm',level,'.sf.rds')

path <- system.file(package="afriadmin")
# path <- system.file(package="afriadmin")
# sf1 <- readRDS(file.path(path, "external/", filename))

sf1 <- readRDS(file.path(path, "external/", filename))
sf1 <- readRDS(file.path(path, filename))

if (plot) plot(sf::st_geometry(sf1))

return(sf1)

}


# return max admin level for a country
# initialy gadm but could be other sources too
# todo should be vectorised
maxadmin <- function(iso3 = 'AGO') {

path <- system.file(package="afriadmin","/external")

allfiles <- list.files(path)
countryfiles <- allfiles[ substr(allfiles,1,3)==iso3 ]
levs <- as.numeric( substr(countryfiles,8,8) )
lev_hi <- max(levs)
lev_hi
}

0 comments on commit e9b038f

Please sign in to comment.