Commit 5cf2a9ca authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

Move the writeOutSamples function to save_files.r

parent 60e28f3f
......@@ -130,4 +130,89 @@ saveSamplePoints <-
overwrite_layer = TRUE
)
}
}
#' Sample Collection for Habitat Types
#'
#'Writes out a set of samples (SpatialPointsDataFrame) into ESRI shapefiles or a GeoJSON file for a selected habitat type. Each point represents a valid sample location that identifies the selected habitat type.
#'
#' @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
#'
#' @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
###write out selected samples
writeOutSamples <- function(in_path, step, class_name, output_format = c("shp", "geojson")) {
run1 <- get(load(paste(in_path, "Run", step, sep = "")))
load(paste(in_path, "threshold_step_", step, sep = ""))
dummy_sample <-
raster::raster(paste(in_path, "step_", step, "_", class_name, ".tif", sep =
""))
length_threshold <- length(threshold)
thres <- threshold[length_threshold]
dummy_sample[dummy_sample < thres] <- NA
dummy_sample[dummy_sample >= thres] <- 1
collect <- list()
j <- 0
###extract only class samples
for (i in 1:length(run1@ref_samples)) {
if (length(dim(run1@ref_samples[[i]])) != 0)
{
if (is.na(run1@switch[i]) == F) {
j = j + 1
collect[[j]] <-
run1@ref_samples[[i]][which(run1@ref_samples[[i]]@data == 1), ]
} else
{
j = j + 1
collect[[j]] <-
run1@ref_samples[[i]][which(run1@ref_samples[[i]]@data == 2), ]
}
}
}
result <- do.call(rbind, collect)
res <- raster::extract(dummy_sample, result)
if (length(which(is.na(res))) > 0) {
res <- result[-which(is.na(res)), ]
}
output_format <- match.arg(output_format)
if (output_format == "geojson") {
raster::crs(res) <- sp::proj4string(dummy_sample)
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 {
raster::crs(res) <- sp::proj4string(dummy_sample)
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
#' Sample Collection for Habitat Types
#'
#'Writes out a set of samples (SpatialPointsDataFrame) into ESRI shapefiles or a GeoJSON file for a selected habitat type. Each point represents a valid sample location that identifies the selected habitat type.
#'
#' @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
#'
#' @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
###write out selected samples
writeOutSamples <- function (in_path, step, class_name, output_format = c("shp", "geojson")) {
paste(in_path, "step_", step, "_", class_name, ".tif", sep = "")
run1 <- get(load(paste(in_path, "Run", step, sep = "")))
load(paste(in_path, "threshold_step_", step, sep = ""))
dummy_sample <-
raster::raster(paste(in_path, "step_", step, "_", class_name, ".tif", sep =
""))
length_threshold <- length(threshold)
thres <- threshold[length_threshold]
dummy_sample[dummy_sample < thres] <- NA
dummy_sample[dummy_sample >= thres] <- 1
collect <- list()
j <- 0
###extract only class samples
for (i in 1:length(run1@ref_samples)) {
if (length(dim(run1@ref_samples[[i]])) != 0)
{
if (is.na(run1@switch[i]) == F) {
j = j + 1
collect[[j]] <-
run1@ref_samples[[i]][which(run1@ref_samples[[i]]@data == 1), ]
} else
{
j = j + 1
collect[[j]] <-
run1@ref_samples[[i]][which(run1@ref_samples[[i]]@data == 2), ]
}
}
}
result <- do.call(rbind, collect)
res <- raster::extract(dummy_sample, result)
if (length(which(is.na(res))) > 0) {
res <- result[-which(is.na(res)), ]
}
output_format <- match.arg(output_format)
if (output_format == "geojson") {
raster::crs(res) <- sp::proj4string(dummy_sample)
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 {
raster::crs(res) <- sp::proj4string(dummy_sample)
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
)
}
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/save_files.r
\name{saveSamplePoints}
\alias{saveSamplePoints}
\title{Save Sample Points}
\usage{
saveSamplePoints(
in_path,
step,
class_name,
output_format = c("shp", "geojson"),
ref_samples,
ref_switch,
dummy_raster
)
}
\arguments{
\item{in_path}{file path (character) for results of habitat type sampling and probability mapping (same as outPath from function multi_Class_Sampling)}
\item{step}{step number (numeric)}
\item{class_name}{name (character) of habitat type for which samples should be selected}
\item{output_format}{format (character) of output; whether shp (default) or geojson}
\item{ref_samples}{list of reference sample points}
\item{ref_switch}{vector with switch values}
\item{dummy_raster}{raster with classified pixels marked as 1}
}
\value{
ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
\enumerate{
\item 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).
}
}
\description{
Saves the reference sample points
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/writeOutSamples.r
% Please edit documentation in R/save_files.r
\name{writeOutSamples}
\alias{writeOutSamples}
\title{Sample Collection for Habitat Types}
......
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