Commit 7edde661 authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

Merge branch 'master' into carsten_optimization

parents 90f2ab88 5487f73a
......@@ -9,8 +9,9 @@ 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)
export(write_Out_Samples)
export(writeOutSamples)
exportClasses(Habitat)
#' 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 inPath 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 className 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: RefHaSa_step_classname.shp/RefHaSa_step_classname.geojson
#' 1) Point Shape represents pixel that belong to selected habitat type and can be used as reference for further model building
#'
#'
#' @export
###write out selected samples
write_Out_Samples <- function (inPath, step, className, output_format = c("shp", "geojson")) {
paste(inPath, "step_", step, "_", className, ".tif", sep = "")
run1 <- get(load(paste(inPath, "Run", step, sep = "")))
load(paste(inPath, "threshold_step_", step, sep = ""))
dummy_sample <-
raster::raster(paste(inPath, "step_", step, "_", className, ".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") {
crs_dummy <- sp::proj4string(dummy_sample)
crs(res) <- crs_dummy
res <- sp::spTransform(res, CRS("+proj=longlat +datum=WGS84 +init=epsg:4326"))
rgdal::writeOGR(
res,
layer = paste("RefHaSa_step_", step, "_", className, sep = ""),
dsn = paste(inPath, "RefHaSa_step_", step, "_", className, ".geojson", sep = ""),
driver = "GeoJSON",
check_exists = TRUE,
overwrite_layer = TRUE
)
} else {
crs_dummy <- sp::proj4string(dummy_sample)
crs(res) <- crs_dummy
res <- sp::spTransform(res, CRS("+proj=longlat +datum=WGS84 +init=epsg:4326"))
rgdal::writeOGR(
res,
layer = paste("RefHaSa_step_", step, "_", className, sep = ""),
dsn = paste(inPath, "RefHaSa_", className, "_", step, ".shp", sep = ""),
driver = "ESRI Shapefile",
check_exists = TRUE,
overwrite_layer = TRUE
)
}
}
......@@ -267,7 +267,6 @@ sample_nb <- function(raster,
flush(stdout())
switch <- switch[ch, index]
points <- points_list[ch]
remove(points_list)
dif <- dif[2,]
##############################################################################
###Vohersage
......@@ -315,8 +314,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,
......
......@@ -28,7 +28,7 @@
#' @param plot_on_browser plot on the browser or inline in a notebook (default TRUE)
#'
#' @return 4 files per step:
#' 1) Habitat type probability map as geocoded *.kml layer and *.tif raster files and *.png image output
#' 1) Habitat type probability map as geocoded *.kmz file (with a *.kml layer and *.png image output), and *.tif raster file
#' 2) A Habitat object (only if save_runs is set to TRUE) consisting of 7 slots: \cr
#' run1@models - list of selcted classifiers \cr
#' run1@ref_samples - list of SpatialPointsDataFrames with same length as run1@models holding reference labels [1,2] for each selected model \cr
......@@ -491,9 +491,7 @@ multi_Class_Sampling <- function(in.raster,
if ( save_runs == TRUE) {
run1 <- maFo_rf
save_run(outPath,
ni,
run1)
save_run(outPath = outPath, step = ni, run1 = run1)
remove(run1)
}
......
......@@ -12,6 +12,23 @@
#'
#' @export
plot_results <- function(inPath, color = NULL) {
num_files <- length(list.files(
inPath,
pattern = ".kmz$",
all.files = FALSE,
include.dirs = TRUE,
no.. = TRUE
))
if (num_files == 0) {
num_files <- length(list.files(
inPath,
pattern = ".kml$",
all.files = FALSE,
include.dirs = TRUE,
no.. = TRUE
))
}
# Compares the number of .png to that of step_*.tif. If step_*.tif files are missing the
# user's attention is drawn.
......@@ -21,14 +38,8 @@ plot_results <- function(inPath, color = NULL) {
all.files = FALSE,
include.dirs = TRUE,
no.. = TRUE
)) != length(list.files(
inPath,
pattern = ".png$",
all.files = FALSE,
include.dirs = TRUE,
no.. = TRUE
))) {
message("Make sure the number of the step_*.tif files fits to the number of the .png files and that
)) != num_files) {
message("Make sure the number of the step_*.tif files fits to the number of the step_*.km(l|z) files and that
there are no other files from previous runs.
If you have resumed a run, then the data of the aborted and the continued run needs
to be in the Results directory. You need them for plotting the classification map.")
......
......@@ -4,11 +4,11 @@
#'
#' @param outPath output path
#' @param step step number
#' @param run the object to be saved
#' @param run1 the object to be saved
#'
#' @export
save_run <- function(outPath, step, run) {
save(run, file = paste(outPath, paste("Run", step, sep = ""), sep = ""))
save_run <- function(outPath, step, run1) {
save(run1, file = paste(outPath, paste("Run", step, sep = ""), sep = ""))
}
#' Save Tif
......@@ -52,5 +52,175 @@ save_kml <- function(outPath, step, raster, overwrite) {
crs = "+proj=longlat +datum=WGS84",
method = 'ngb')
raster::KML(kml, paste(outPath, paste("step_", step, sep = ""), sep = ""), overwrite = overwrite, zip = FALSE)
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
......@@ -83,7 +83,7 @@ multi_Class_Sampling(
\value{
4 files per step:
\enumerate{
\item Habitat type probability map as geocoded *.kml layer and *.tif raster files and *.png image output
\item Habitat type probability map as geocoded *.kmz file (with a *.kml layer and *.png image output), and *.tif raster file
\item A Habitat object (only if save_runs is set to TRUE) consisting of 7 slots: \cr
run1@models - list of selcted classifiers \cr
run1@ref_samples - list of SpatialPointsDataFrames with same length as run1@models holding reference labels \link{1,2} for each selected model \cr
......
% 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
}
......@@ -4,14 +4,14 @@
\alias{save_run}
\title{Save a run}
\usage{
save_run(outPath, step, run)
save_run(outPath, step, run1)
}
\arguments{
\item{outPath}{output path}
\item{step}{step number}
\item{run}{the object to be saved}
\item{run1}{the object to be saved}
}
\description{
Saves the run object for a step
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/save_files.r
\name{writeOutSamples}
\alias{writeOutSamples}
\title{Selected Sample Collection for Habitat Types}
\usage{
writeOutSamples(in_path, step, class_name, output_format = c("shp", "geojson"))
}
\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}
}
\value{
ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
\enumerate{
\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. Only those points are selected
which lay in the habitat type selected by the user's input threshold.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/WriteOutSamples.r
\name{write_Out_Samples}
\alias{write_Out_Samples}
\title{Sample Collection for Habitat Types}
\usage{
write_Out_Samples(inPath, step, className, output_format = c("shp", "geojson"))
}
\arguments{
\item{inPath}{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{className}{name (character) of habitat type for which samples should be selected}
\item{output_format}{format (character) of output; whether shp (default) or geojson}
}
\value{
ESRI shapefiles/GeoJSON with name: RefHaSa_step_classname.shp/RefHaSa_step_classname.geojson
\enumerate{
\item Point Shape represents pixel that belong to selected habitat type and can be used as reference for further model building
}
}
\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.
}
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