Commit ea81cedd authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

Offer the options to overwrite files. In case the file exists and the user did...

Offer the options to overwrite files. In case the file exists and the user did not choose the option to overwrite, raise an error.
parent c2f076b1
......@@ -68,6 +68,7 @@ save_kml <- function(outPath, step, raster, overwrite) {
#' @param ref_switch vector with switch values
#' @param num_models number of models used for the classification of a habitat
#' @param dummy_raster raster with probabilities for each pixel
#' @param overwrite overwrite file (default TRUE)
#'
#' @return ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
#' 1) Point Shape/GeoJSON represents the pixels which were used to train the models for that habitat.
......@@ -83,22 +84,26 @@ saveSamplePoints <-
ref_samples,
ref_switch,
num_models,
dummy_raster) {
dummy_raster,
overwrite = TRUE) {
collect <- list()
j <- 0
dummy_raster[dummy_raster == num_models] <- NA
dummy_raster[dummy_raster > num_models] <- 1
print("1")
###extract only class samples
for (i in 1:length(ref_samples)) {
if (length(dim(ref_samples[[i]])) != 0)
{
print("1a")
if (is.na(ref_switch[i]) == F) {
print("1b")
j = j + 1
collect[[j]] <-
ref_samples[[i]][which(ref_samples[[i]]@data == 1), ]
} else
{
} else {
print("1c")
j = j + 1
collect[[j]] <-
ref_samples[[i]][which(ref_samples[[i]]@data == 2), ]
......@@ -106,37 +111,73 @@ saveSamplePoints <-
}
}
print("2")
result <- do.call(rbind, collect)
if (!is.null(result)) {
print("3a")
res <- raster::extract(dummy_raster, result)
if (length(which(is.na(res))) > 0) {
print("3aa")
result <- result[-which(is.na(res)), ]
}
} else {
print("3b")
result <-
sp::SpatialPolygonsDataFrame(Sr = sp::SpatialPolygons(list()), data = data.frame())
}
print("4")
raster::crs(result) <- raster::crs(dummy_raster)
print("5")
output_format <- match.arg(output_format)
if (output_format == "geojson") {
file_path <- paste(in_path, "SamplePoints_step_", step, "_", class_name, ".geojson", sep = "")
# Only transform is there points to be saved.
if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
}
if (file.exists(file_path) == TRUE) {
if (overwrite) {
unlink(file_path, recursive = TRUE)
} else {
stop(
paste(
"The file ",
file_path,
" already exists. Please delete it or set overwrite = TRUE",
sep = ""
)
)
}
}
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "SamplePoints_step_", step, "_", class_name, ".geojson", sep = ""),
dsn = file_path,
driver = "GeoJSON",
check_exists = TRUE,
append = FALSE
)
)
} else {
file_path <- paste(in_path, "SamplePoints_step_", step, "_", class_name, ".shp", sep = "")
if (file.exists(file_path) == TRUE) {
if (overwrite) {
unlink(file_path, recursive = TRUE)
} else {
stop(
paste(
"The file ",
file_path,
" already exists. Please delete it or set overwrite = TRUE",
sep = ""
)
)
}
}
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "SamplePoints_step_", step, "_", class_name, ".shp", sep = ""),
dsn = file_path,
driver = "ESRI Shapefile",
check_exists = TRUE,
append = FALSE
......@@ -154,6 +195,7 @@ saveSamplePoints <-
#' @param in_path file path (character) for results of habitat type sampling and probability mapping (same as outPath from function multi_Class_Sampling)
#' @param step step number (numeric)
#' @param class_name name (character) of habitat type for which samples should be selected
#' @param overwrite overwrite file (default TRUE)
#' @param output_format format (character) of output; whether shp (default) or geojson
#'
#' @return ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
......@@ -164,7 +206,13 @@ saveSamplePoints <-
#' @export
###write out selected samples
writeOutSamples <- function(in_path, step, class_name, output_format = c("shp", "geojson")) {
writeOutSamples <-
function(in_path,
step,
class_name,
overwrite = TRUE,
output_format = c("shp", "geojson")) {
run1 <- get(load(paste(in_path, "Run", step, sep = "")))
load(paste(in_path, "threshold_step_", step, sep = ""))
dummy_sample <-
......@@ -213,19 +261,49 @@ writeOutSamples <- function(in_path, step, class_name, output_format = c("shp",
if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
}
file_path <- paste(in_path, "RefHaSa_", class_name, "_", step, ".geojson", sep = "")
if (file.exists(file_path) == TRUE) {
if (overwrite) {
unlink(file_path, recursive = TRUE)
} else {
stop(
paste(
"The file ",
file_path,
" already exists. Please delete it or set overwrite = TRUE",
sep = ""
)
)
}
}
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("RefHaSa_", class_name, "_", step, sep = ""),
dsn = paste(in_path, "RefHaSa_", class_name, "_", step, ".geojson", sep = ""),
dsn = file_path,
driver = "GeoJSON",
check_exists = TRUE,
append = FALSE
)
} else {
file_path <- paste(in_path, "RefHaSa_", class_name, "_", step, ".shp", sep = "")
if (file.exists(file_path) == TRUE) {
if (overwrite) {
unlink(file_path, recursive = TRUE)
} else {
stop(
paste(
"The file ",
file_path,
" already exists. Please delete it or set overwrite = TRUE",
sep = ""
)
)
}
}
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("RefHaSa_", class_name, "_", step, sep = ""),
dsn = paste(in_path, "RefHaSa_", class_name, "_", step, ".shp", sep = ""),
dsn = file_path,
driver = "ESRI Shapefile",
check_exists = TRUE,
append = FALSE
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment