Commit ad90ffb0 authored by Daniela Rabe's avatar Daniela Rabe
Browse files

Merge branch 'master' into carsten_optimization

parents 38b12292 8edae516
...@@ -140,7 +140,6 @@ multi_Class_Sampling <- function(in.raster, ...@@ -140,7 +140,6 @@ multi_Class_Sampling <- function(in.raster,
} }
} }
print(paste(paste("Habitat", 0), "Starting"))
for (i in step:r) { for (i in step:r) {
if (i == r) { if (i == r) {
last = T last = T
......
...@@ -68,6 +68,7 @@ save_kml <- function(outPath, step, raster, overwrite) { ...@@ -68,6 +68,7 @@ save_kml <- function(outPath, step, raster, overwrite) {
#' @param ref_switch vector with switch values #' @param ref_switch vector with switch values
#' @param num_models number of models used for the classification of a habitat #' @param num_models number of models used for the classification of a habitat
#' @param dummy_raster raster with probabilities for each pixel #' @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 #' @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. #' 1) Point Shape/GeoJSON represents the pixels which were used to train the models for that habitat.
...@@ -83,7 +84,8 @@ saveSamplePoints <- ...@@ -83,7 +84,8 @@ saveSamplePoints <-
ref_samples, ref_samples,
ref_switch, ref_switch,
num_models, num_models,
dummy_raster) { dummy_raster,
overwrite = TRUE) {
collect <- list() collect <- list()
j <- 0 j <- 0
dummy_raster[dummy_raster == num_models] <- NA dummy_raster[dummy_raster == num_models] <- NA
...@@ -97,8 +99,7 @@ saveSamplePoints <- ...@@ -97,8 +99,7 @@ saveSamplePoints <-
j = j + 1 j = j + 1
collect[[j]] <- collect[[j]] <-
ref_samples[[i]][which(ref_samples[[i]]@data == 1), ] ref_samples[[i]][which(ref_samples[[i]]@data == 1), ]
} else } else {
{
j = j + 1 j = j + 1
collect[[j]] <- collect[[j]] <-
ref_samples[[i]][which(ref_samples[[i]]@data == 2), ] ref_samples[[i]][which(ref_samples[[i]]@data == 2), ]
...@@ -120,23 +121,53 @@ saveSamplePoints <- ...@@ -120,23 +121,53 @@ saveSamplePoints <-
output_format <- match.arg(output_format) output_format <- match.arg(output_format)
if (output_format == "geojson") { if (output_format == "geojson") {
file_path <- paste(in_path, "SamplePoints_step_", step, "_", class_name, ".geojson", sep = "")
# Only transform is there points to be saved. # Only transform is there points to be saved.
if (nrow(result) > 0) { if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326")) 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( sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326), obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""), layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "SamplePoints_step_", step, "_", class_name, ".geojson", sep = ""), dsn = file_path,
driver = "GeoJSON", driver = "GeoJSON",
check_exists = TRUE, check_exists = TRUE,
append = FALSE append = FALSE
) )
} else { } 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( sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326), obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""), 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", driver = "ESRI Shapefile",
check_exists = TRUE, check_exists = TRUE,
append = FALSE append = FALSE
...@@ -154,6 +185,7 @@ saveSamplePoints <- ...@@ -154,6 +185,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 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 step step number (numeric)
#' @param class_name name (character) of habitat type for which samples should be selected #' @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 #' @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 #' @return ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
...@@ -164,7 +196,13 @@ saveSamplePoints <- ...@@ -164,7 +196,13 @@ saveSamplePoints <-
#' @export #' @export
###write out selected samples ###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 = ""))) run1 <- get(load(paste(in_path, "Run", step, sep = "")))
load(paste(in_path, "threshold_step_", step, sep = "")) load(paste(in_path, "threshold_step_", step, sep = ""))
dummy_sample <- dummy_sample <-
...@@ -213,19 +251,49 @@ writeOutSamples <- function(in_path, step, class_name, output_format = c("shp", ...@@ -213,19 +251,49 @@ writeOutSamples <- function(in_path, step, class_name, output_format = c("shp",
if (nrow(result) > 0) { if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326")) 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( sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326), obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("RefHaSa_", class_name, "_", step, sep = ""), layer = paste("RefHaSa_", class_name, "_step_", step, sep = ""),
dsn = paste(in_path, "RefHaSa_", class_name, "_", step, ".geojson", sep = ""), dsn = file_path,
driver = "GeoJSON", driver = "GeoJSON",
check_exists = TRUE, check_exists = TRUE,
append = FALSE append = FALSE
) )
} else { } 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( sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326), obj = sf::st_as_sf(x = result, crs = 4326),
layer = paste("RefHaSa_", class_name, "_", step, sep = ""), layer = paste("RefHaSa_", class_name, "_", step, sep = ""),
dsn = paste(in_path, "RefHaSa_", class_name, "_", step, ".shp", sep = ""), dsn = file_path,
driver = "ESRI Shapefile", driver = "ESRI Shapefile",
check_exists = TRUE, check_exists = TRUE,
append = FALSE 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