Skip to content

Commit

Permalink
small code cleaning
Browse files Browse the repository at this point in the history
I allowed to remove NA when extracting. The output folder is fixed but can be easily changed.
  • Loading branch information
Lorenagzp committed Oct 4, 2019
1 parent 851c3f4 commit 0ab33bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
49 changes: 30 additions & 19 deletions r/extract/extract_from_raster_based_on_polygon_zones.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###################################################################################
#### Script to extract the values from multiband rasters based on vector zones ####
#### Script to extract the values from multiband rasters (Tif, Tiff and BSQ) based on vector zones ####
#### Recopilado por Lorena González, Septiembre 2019 ####
####___________________________________________________________________________####
#### There are two options for the Inputs:
Expand All @@ -21,13 +21,24 @@
#### + individual file per raster with all statistics
#### + datafile with all the bands and dates extracted per field

#### setwd() You can set the Working directory to the source file location

#### Get functions and libraries
source("functions_extract.R") #Check that this file is in the working directory
source("F:\\Dropbox\\Software\\Scripts\\r\\extract\\functions_extract.R") #Check that this file is in the working directory
#require(raster)

## Ask user where to put the output tables
#outFolder <- choose.dir(caption = "Select folder to save output tables")
outFolder <- ("C:\\temp\\r") # Set path fix

#summarize using the selected function
func <- "median" #mean #min #max

#Field in the shp table to use as identifier of the extracting features
ID_field <- "Name"

tryCatch({ ## Put it all inside a handle error function

## Shapefile
## Ask user the list of rasters and zone vectors
in_mode <- menu(c("CSV list of inputs", "Raster Folder and vector file","Raster File and Vector File"), title="How do you want to give the inputs?",graphics = TRUE);

Expand All @@ -36,10 +47,11 @@ tryCatch({ ## Put it all inside a handle error function

#Select the CSV file with the list of inputs
inputList <- askCSV() # read list from file
#Extract for every input in the list
####EXTRACT####
# run for every item in te list
for (i in 1:nrow(inputList)) {
print(paste("Processing... ",i))
extractThis(inputList[i,1],shapefile(inputList[i,2]), inputList[i,3], inputList[i,4],mean,as.double(inputList[i,5])) ## r_file,zones, outFolder, ID_field, fun=mean, buf
extractThis(inputList[i,1],shapefile(inputList[i,2]), inputList[i,3], inputList[i,4],func,as.double(inputList[i,5])) ## r_file,zones, outFolder, ID_field, fun=mean, buf
}

} else if (in_mode == 2 || in_mode == 3){ ## Enter inputs
Expand All @@ -54,37 +66,36 @@ tryCatch({ ## Put it all inside a handle error function
#Ask for the buffer size
##TODO: Add filters to deal with entering other than numbers for the buffer

buf <- as.double(readline(prompt = "Type the buffer size (+Positive to grow, -negative to shrink the feature)"));
buf <- as.double(readline(prompt = "ACTION: Type the buffer size in meters (+Positive to grow, -negative to shrink the feature)"));
}

## Ask user where to put the output tables
#tFolder <- "C:\\" Set path fix
outFolder <- choose.dir(caption = "Select folder to save output tables")


if (in_mode == 2){ #### Process all rasters in a folder ####
## Ask user for Raster folder
rFolder <- choose.dir(caption = "Select folder that contains the rasters to extract")
#list the rasters inside the folder
r_list <- list.files(path = rFolder, full.names= TRUE, pattern = "*.tif$") # Select only Tif files, for example
#list the rasters inside the folder. Return full path name
r_list <- list.files(path = rFolder, full.names= TRUE, pattern = "\\.tif$|\\.bsq$|\\.tiff$") # Select Tif and BSQ for example
## Extract each raster
for (r_file in r_list) {
####EXTRACT####
print(paste("Processing...using =Name= as ID field... ",r_file))
extractThis(r_file,zones, outFolder,ID_field ="Name",fun = mean,buf)
print(paste("Processing... ",r_file))
extractThis(r_file,zones, outFolder,ID_field,fun = func,buf)
}
}

if (in_mode == 3){ #### Ask for Raster file ####
####EXTRACT####
r_file <- askRaster() #Get th raster name
print(paste("Processing...using =Name= as ID field... ",r_file))
print(paste("Processing... ",r_file))
## Run extraction and saves output, indicate statistic
extractThis(r_file,zones, outFolder,ID_field ="Name", fun = mean,buf)
extractThis(r_file,zones, outFolder,ID_field, fun = func,buf)

}
}
print("Finish")
print("Finish extracting")
},
error = function(e){print(c("Se produjo un error: ",e$message))},
warning = function(e){print(paste("Hay advertencias: ", e$message))}
#In case of halting error:
error = function(e){print(c("Se produjo un error: ",e$message))}#,
# In case of warnings:
#warning = function(e){print(paste("Hay advertencias: ", e$message))}
)
2 changes: 1 addition & 1 deletion r/extract/functions_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extractThis <- function(r_file,zones, outFolder, ID_field ="Name",fun=mean,buf=0
zones <- buffer(zones, width = buf, dissolve= FALSE)
}
## With this we get a table with the values for all the plots and bands
table <- r$extract(zones, df=TRUE, fun=fun) #Perform the extraction with the selected statistic function
table <- r$extract(zones, df=TRUE, fun= function(x) fun(x, na.rm = TRUE)) #Perform the extraction with the selected statistic function, remove NA values
## Set ID names from the shapefile
table$ID_sp <- zones@data[,ID_field]
#Save to disk
Expand Down

0 comments on commit 0ab33bb

Please sign in to comment.