Commit 92e61fe8 authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

Add function to saveSamplePoints directly from the memory objects.

parent 82fbde22
......@@ -9,6 +9,7 @@ export(multi_Class_Sampling)
export(plot_configuration)
export(plot_results)
export(sample_nb)
export(saveSamplePoints)
export(save_class_tiff)
export(save_kml)
export(save_run)
......
......@@ -54,3 +54,80 @@ save_kml <- function(outPath, step, raster, overwrite) {
raster::KML(kml, paste(outPath, paste("step_", step, sep = ""), sep = ""), overwrite = overwrite, zip = '')
}
#' Save Sample Points
#'
#' Saves the reference sample points
#'
#' @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 output_format format (character) of output; whether shp (default) or geojson
#' @param ref_samples list of reference sample points
#' @param ref_switch vector with switch values
#' @param dummy_raster raster with classified pixels marked as 1
#'
#' @return ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
#' 1) Point Shape/GeoJSON represents pixel that belong to selected habitat type and can be used as reference for further model building.
#' ESRI shapefiles have the same CRS as the input raster. GeoJSON files are in the standard CRS of GeoJSON (EPSG:4326).
#'
#'
#' @export
saveSamplePoints <-
function(in_path,
step,
class_name,
output_format = c("shp", "geojson"),
ref_samples,
ref_switch,
dummy_raster) {
collect <- list()
j <- 0
###extract only class samples
for (i in 1:length(ref_samples)) {
if (length(dim(ref_samples[[i]])) != 0)
{
if (is.na(ref_switch[i]) == F) {
j = j + 1
collect[[j]] <-
ref_samples[[i]][which(ref_samples[[i]]@data == 1), ]
} else
{
j = j + 1
collect[[j]] <-
ref_samples[[i]][which(ref_samples[[i]]@data == 2), ]
}
}
}
result <- do.call(rbind, collect)
res <- raster::extract(dummy_raster, result)
if (length(which(is.na(res))) > 0) {
res <- result[-which(is.na(res)), ]
}
raster::crs(res) <- raster::crs(dummy_raster)
output_format <- match.arg(output_format)
if (output_format == "geojson") {
res <- sp::spTransform(res, sp::CRS("+init=epsg:4326"))
rgdal::writeOGR(
obj = res,
layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "SamplePoints_step_", step, "_", class_name, ".geojson", sep = ""),
driver = "GeoJSON",
check_exists = TRUE,
overwrite_layer = TRUE
)
} else {
rgdal::writeOGR(
obj = res,
layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "SamplePoints_step_", step, "_", class_name, ".shp", sep = ""),
driver = "ESRI Shapefile",
check_exists = TRUE,
overwrite_layer = TRUE
)
}
}
\ No newline at end of file
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