Skip to content

Commit

Permalink
use API to call for country and region areas
Browse files Browse the repository at this point in the history
  • Loading branch information
MaIsAp committed Oct 6, 2023
1 parent dac771e commit 6fcc69d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions scripts/SHI/scp_SHI_02_GetAOH.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ print(Sys.getenv("SCRIPT_LOCATION"))
options(timeout = max(60000000, getOption("timeout")))

packages <- c("rjson","remotes","dplyr","tidyr","purrr","terra","stars","sf","readr",
"geodata","gdalcubes","stacatalogue","rredlist","stringr","rnaturalearth","rnaturalearthhires")
"geodata","gdalcubes","stacatalogue","rredlist","stringr")

if (!"gdalcubes" %in% installed.packages()[,"Package"]) remotes::install_git("https://github.com/appelmar/gdalcubes_R.git")
if (!"stacatalogue" %in% installed.packages()[,"Package"]) remotes::install_git("https://github.com/ReseauBiodiversiteQuebec/stac-catalogue")
Expand All @@ -24,6 +24,16 @@ print(input)

source(file.path(path_script,"SHI/funFilterCube_range.R"), echo=TRUE)

get_country<-function(country){
resp <- req_perform( request(paste0("https://geoio.biodiversite-quebec.ca/country_geojson/?country_name=",country)))
geojson_sf(resp_body_string(resp))
}

get_state<-function(country,region){
resp <- req_perform( request(paste0("https://geoio.biodiversite-quebec.ca/state_geojson/?state_name=",region,"&country_name=",country)))
geojson_sf(resp_body_string(resp))
}

# Parameters -------------------------------------------------------------------
# spatial resolution
spat_res <- ifelse(is.null(input$spat_res), 1000 ,input$spat_res)
Expand Down Expand Up @@ -90,10 +100,10 @@ study_area <- data.frame(text=study_area_opt,
))

if(study_area$option == 1){
sf_area_lim1 <- ne_countries(country = country_code,returnclass = 'sf') |> st_make_valid() # country
sf_area_lim1 <- get_country(country_code) |> st_make_valid() # country
}
if(study_area$option == 2){
sf_area_lim1 <- ne_states(geounit=country_code,returnclass = 'sf') |> st_make_valid() |> filter(woe_name==region) # region in a country
sf_area_lim1 <- get_state(country_code,region) |> st_make_valid() # region in a country
}
if(study_area$option == 3){
sf_area_lim1 <- st_read(study_area_path) # user defined area
Expand Down

0 comments on commit 6fcc69d

Please sign in to comment.