Commit 5487f73a authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

Merge branch 'saveSamplePoints' into 'master'

Save sample points

See merge request !21
parents 45ef975f 291dce37
Pipeline #25451 passed with stages
in 10 minutes and 21 seconds
......@@ -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)
......
......@@ -270,7 +270,6 @@ sample_nb <- function(raster,
flush(stdout())
switch <- switch[ch, index]
points <- points_list[ch]
remove(points_list)
dif <- dif[2,]
##############################################################################
###Vohersage
......@@ -318,8 +317,8 @@ sample_nb <- function(raster,
obj <- new(
"Habitat",
models = list(),
ref_samples = list(),
switch = vector(),
ref_samples = points,
switch = switch,
layer = layer,
mod_all = list(),
class_ind = 0,
......
......@@ -54,3 +54,173 @@ 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 num_models number of models used for the classification of a habitat
#' @param dummy_raster raster with probabilities for each pixel
#'
#' @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.
#' 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,
num_models,
dummy_raster) {
collect <- list()
j <- 0
dummy_raster[dummy_raster == num_models] <- NA
dummy_raster[dummy_raster > num_models] <- 1
###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) {
result <- result[-which(is.na(res)), ]
}
raster::crs(result) <- raster::crs(dummy_raster)
output_format <- match.arg(output_format)
if (output_format == "geojson") {
# Only transform is there points to be saved.
if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
}
rgdal::writeOGR(
obj = result,
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 = result,
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
)
}
}
#' Selected 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. Only those points are selected
#' which lay in the habitat type selected by the user's input threshold.
#'
#' @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 the pixels which 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) {
result <- result[-which(is.na(res)), ]
}
raster::crs(result) <- raster::crs(dummy_sample)
output_format <- match.arg(output_format)
if (output_format == "geojson") {
# Only transform is there points to be saved.
if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
}
rgdal::writeOGR(
obj = result,
layer = paste("sel_SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "sel_SamplePoints_step_", step, "_", class_name, ".geojson", sep = ""),
driver = "GeoJSON",
check_exists = TRUE,
overwrite_layer = TRUE
)
} else {
rgdal::writeOGR(
obj = result,
layer = paste("sel_SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "sel_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,
num_models,
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{num_models}{number of models used for the classification of a habitat}
\item{dummy_raster}{raster with probabilities for each pixel}
}
\value{
ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
\enumerate{
\item Point Shape/GeoJSON represents the pixels which were used to train the models for that habitat.
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}
\title{Selected Sample Collection for Habitat Types}
\usage{
writeOutSamples(in_path, step, class_name, output_format = c("shp", "geojson"))
}
......@@ -18,10 +18,12 @@ writeOutSamples(in_path, step, class_name, output_format = c("shp", "geojson"))
\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.
\item Point Shape/GeoJSON represents the pixels which 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{
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.
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. Only those points are selected
which lay in the habitat type selected by the user's input threshold.
}
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