knitr::opts_chunk$set(echo = TRUE)
library(sprawl)
library(tibble)
library(raster)
library(phenoriceR)
library(dplyr)
library(sf)
library(dplyr)
#   ____________________________________________________________________________
#   set input and output folders                                            ####

# set the main folder ----
main_folder <- "/home/lb/my_data/prasia/Data"

# Load the reshuffled shapefile ----

in_shp <- read_vect(file.path(main_folder, 
                              "vector/Ricetlas/riceatlas_asia_reshuffled.shp"))

## Set a region for the analysis ----
Region_name <- "Region_3_-_Central_Luzon"

## "1) Get the polygons of a specific region from the shapefile") ----

in_vect <- dplyr::filter(in_shp, Region == Region_name)
in_vect <- in_vect[1:3] %>%
  sf::st_transform(get_proj4string(
    file.path(main_folder,"orig_mosaic/param_series/decirc/eos_decirc.tif"))) %>% 
  unique()
plot_vect(in_vect, fill_var = "ID")

cat("Create cropped rasters and put them in the \"subsets\" subfolder")
in_rast_folder  <- file.path(main_folder, "orig_mosaic/param_series/")
out_folder      <- file.path(main_folder, "subsets", Region_name)
make_folder(out_folder, type = "dirname", verbose = T)

message("Extracting data on ", Region_name)
pr_extract_subarea(in_rast_folder,
                   in_vect,
                   out_folder  = out_folder)

## 2. Extract the data from the different provinces of the region" ----

in_files <- list.files(
  file.path(out_folder, "param_series/decirc"),
  pattern = "*.RData", 
  full.names = TRUE)
in_files

#### test on one file ----
in_rast <- get(load(in_files[1]))
message("Working on : ", in_files[1])
out <- sprawl::extract_rast(in_rast,
                     in_vect,
                     na.value = 0, 
                     join_geom = FALSE, 
                     id_field = "ID", 
                     verbose = FALSE)
out


## 3. Save results as an RData file for future use" ----
make_folder(file.path(out_folder, "RData"))
save(out, file = file.path(out_folder, "RData",
                           paste(Region_name, "stats.RData", sep = "_")))
gc()


lbusett/phenoriceR documentation built on May 18, 2019, 9:17 p.m.