Skip to content

Commit

Permalink
Updating calkmeans gevi ndfiSMA tct
Browse files Browse the repository at this point in the history
  • Loading branch information
ytarazona committed Oct 29, 2020
1 parent 611bf5c commit 64aa89a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
12 changes: 8 additions & 4 deletions R/calkmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @author Yonatan Tarazona
#'
#' @section References:
#' Tarazona, Y., Mar?a, Miyasiro-L?pez. (2020). Monitoring tropical forest degradation using
#' Tarazona, Y., Maria, Miyasiro-Lopez. (2020). Monitoring tropical forest degradation using
#' remote sensing. Challenges and opportunities in the Madre de Dios region, Peru. Remote
#' Sensing Applications: Society and Environment, 19, 100337.
#'
Expand Down Expand Up @@ -38,16 +38,19 @@
#' data(FTdata)
#'
#' # Selecting the best k value
#' best_k <- calkmeans(img = img[[1:2]], k = NULL, iter.max = 10,
#' best_k <- calkmeans(img = image[[1:2]], k = NULL, iter.max = 10,
#' algo = c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen"), iter = 30)
#' # Jambu Elbow
#' plot(best_k)
#'
#'
#'# Selecting the best embedded algorithm in kmeans
#' best_algo <- calkmeans(img = img[[1:2]], k = 4, iter.max = 10,
#' best_algo <- calkmeans(img = image[[1:2]], k = 4, iter.max = 10,
#' algo = c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen"), iter = 30)
#'
#' # Choose the algorithm with the highest value
#' best_algo
#'
#' @export
#'

Expand Down Expand Up @@ -191,7 +194,7 @@ calkmeans <- function(img, k = NULL, iter.max = 10, algo = c("Hartigan-Wong", "L

resulFinal <- c(vecIner.hw, vecIner.l, vecIner.f, vecIner.m)

return(structure(resulFinal, class = "calkmeans"))
return(resulFinal)

}

Expand All @@ -202,6 +205,7 @@ calkmeans <- function(img, k = NULL, iter.max = 10, algo = c("Hartigan-Wong", "L

#' Plot for the "calkmeans" class
#'
#' @export
#'
plot.calkmeans <- function(x, xlab, ylab, type, main, cex, ...){

Expand Down
15 changes: 11 additions & 4 deletions R/gevi.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
#' Sentinel2MSI \tab coastal,blue,green,red,nir-1,mir-1,mir-2 \cr
#' }
#'
#' @param x It could be RasterStack or RasterBrick.
#' @param img It could be RasterStack or RasterBrick.
#' @param sat Specify satellite and sensor type (Landsat5TM, Landsat7ETM or Landsat8OLI).
#'
#' @importFrom raster as.matrix
#'
#' @examples
#' library(ForesToolboxRS)
#' library(raster)
Expand All @@ -30,11 +32,16 @@
#' data(FTdata)
#'
#' # Tasseled-cap using Landsat8OLI
#' tasscap <- tct(x = img, sat = "Landsat8OLI")
#' gevi_index <- gevi(img = image/10000, sat = "Landsat8OLI")
#'
#' # Improving the plot histogram
#' gevi_index[gevi_index > 1] <- NA
#' gevi_index[gevi_index < -1] <- NA
#' plot(gevi_index)
#'
#' @export
#'
gevi <- function(x, sat = "Landsat8OLI"){
gevi <- function(img, sat = "Landsat8OLI"){

if (sat == "Landsat4TM"){
coefc <- matrix(c(0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863,
Expand Down Expand Up @@ -74,7 +81,7 @@ gevi <- function(x, sat = "Landsat8OLI"){
c("B1","B2","B3","B4","B8","B11","B12")))
} else stop("Satellite not supported.", call. = TRUE)

val <- as.matrix(x)%*%t(coefc)
val <- as.matrix(img)%*%t(coefc)

bgw <- img[[1:3]]

Expand Down
8 changes: 2 additions & 6 deletions R/ndfiSMA.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,21 @@
#' data(FTdata)
#'
#' # Unmix the image
#' ndfi <- ndfiSMA(img, procesLevel="SR")
#' ndfi <- ndfiSMA(img = image, procesLevel="SR")
#' plot(ndfi)
#'
ndfiSMA <- function(img, procesLevel="SR", verbose = FALSE){

if (is(img, "RasterStack") | is(img, "RasterBrick")) {
df <- as.matrix(img)
} else {
stop(class(img), " This class is not supported yet.", call. = TRUE)
stop(class(img), ": This class is not supported yet. It must be RasterStack or RasterBrick", call. = TRUE)
}

if (procesLevel=="SR") {

if(verbose){
message(paste0(paste0(rep("*",10), collapse = ""), " Endmembers in surface reflectance " , paste0(rep("*",10), collapse = "")))
print(model_algo)
}

# Endmembers
Expand All @@ -66,7 +65,6 @@ ndfiSMA <- function(img, procesLevel="SR", verbose = FALSE){

if(verbose){
message(paste0(paste0(rep("*",10), collapse = ""), " Endmembers in TOA value " , paste0(rep("*",10), collapse = "")))
print(model_algo)
}

# Endmembers
Expand All @@ -85,7 +83,6 @@ ndfiSMA <- function(img, procesLevel="SR", verbose = FALSE){

if(verbose){
message(paste0(paste0(rep("*",10), collapse = ""), " Obtaining fractions through least squares " , paste0(rep("*",10), collapse = "")))
print(model_algo)
}

# Spectral Mixture Analysis
Expand Down Expand Up @@ -114,7 +111,6 @@ ndfiSMA <- function(img, procesLevel="SR", verbose = FALSE){

if(verbose){
message(paste0(paste0(rep("*",10), collapse = ""), " Obtaining the NDFI index " , paste0(rep("*",10), collapse = "")))
print(model_algo)
}

gv <- fractions[,1]*100; gv[gv < 0] <- 0 # Green Vegetation
Expand Down
3 changes: 0 additions & 3 deletions R/sma.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ sma <- function(img, endm, verbose = FALSE){

if(verbose){
message(paste0(paste0(rep("*",10), collapse = ""), " Obtaining fractions through least squares " , paste0(rep("*",10), collapse = "")))
print(model_algo)
}

if(dim(df)[2] > dim(endm)[1]){
Expand All @@ -79,7 +78,6 @@ sma <- function(img, endm, verbose = FALSE){

if(verbose){
message(paste0(paste0(rep("*",10), collapse = ""), " Obtaining Root Mean Square Error " , paste0(rep("*",10), collapse = "")))
print(model_algo)
}

# We estimate the RMSE
Expand All @@ -89,7 +87,6 @@ sma <- function(img, endm, verbose = FALSE){

if(verbose){
message(paste0(paste0(rep("*",10), collapse = ""), " Save the fractions on a raster " , paste0(rep("*",10), collapse = "")))
print(model_algo)
}

# We store the fractions on a raster
Expand Down
11 changes: 8 additions & 3 deletions R/tct.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#' @param sat Specify satellite and sensor type (Landsat5TM, Landsat7ETM
#' or Landsat8OLI).
#'
#' @importFrom raster as.matrix
#'
#' @examples
#' library(ForesToolboxRS)
#' library(raster)
Expand All @@ -46,11 +48,14 @@
#' data(FTdata)
#'
#' # Tasseled-cap using Landsat8OLI
#' sat_tct <- tct(img, sat="Landsat8OLI")
#' sat_tct <- tct(img = image, sat="Landsat8OLI")
#' plotRGB(sat_tct, 1,2,3, stretch="lin")
#'
#'@export
#'
tct <- function(x=img, sat="Landsat8OLI"){
tct <- function(img, sat="Landsat8OLI"){

if(!inherits(img, "Raster")) stop("img must be a RasterBrick or RasterStack", call. = TRUE)

if (sat=="Landsat4TM"){
coefc <- matrix(c(0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863,
Expand Down Expand Up @@ -90,7 +95,7 @@ tct <- function(x=img, sat="Landsat8OLI"){
c("B1","B2","B3","B4","B8","B11","B12")))
} else stop("Satellite not supported.")

val <- as.matrix(x)%*%t(coefc)
val <- as.matrix(img)%*%t(coefc)

bgw <- img[[1:3]]

Expand Down

0 comments on commit 64aa89a

Please sign in to comment.