Commit 865d2f66 authored by Johannes Knoch's avatar Johannes Knoch
Browse files

Merge branch 'save_samplepoints' into 'master'

Save samplepoints

See merge request !18
parents d77b335b 4415836b
Pipeline #24950 passed with stages
in 10 minutes and 46 seconds
#' Sample Collection for Habitat Types #' Sample Collection for Habitat Types
#' #'
#'Writes out a set of samples (SpatialPointsDataFrame) into an ESRI shapefile 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.
#' #'
#' @param inPath file path (character) for results of habitat type sampling and probability mapping (same as outPath from function multi_Class_Sampling) #' @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 step step number (numeric)
#' @param className name (character) of habitat type for which samples should be selected #' @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 shapefile with name: RefHaSa_className_step.shp #' @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 #' 1) Point Shape represents pixel that belong to selected habitat type and can be used as reference for further model building
#' #'
#' #'
#' @export #' @export
###write out selected samples for step = 6 using ###write out selected samples
write_Out_Samples <- function (inPath, step, className) { write_Out_Samples <- function (inPath, step, className, output_format = c("shp", "geojson")) {
paste(inPath, "step_", step, "_", className, ".tif", sep = "") paste(inPath, "step_", step, "_", className, ".tif", sep = "")
run1 <- get(load(paste(inPath, "Run", step, sep = ""))) run1 <- get(load(paste(inPath, "Run", step, sep = "")))
load(paste(inPath, "threshold_step_", step, sep = "")) load(paste(inPath, "threshold_step_", step, sep = ""))
dummy <- dummy_sample <-
raster::raster(paste(inPath, "step_", step, "_", className, ".tif", sep = raster::raster(paste(inPath, "step_", step, "_", className, ".tif", sep =
"")) ""))
thres <- threshold[6]
dummy[dummy < thres] <- NA length_threshold <- length(threshold)
dummy[dummy >= thres] <- 1 thres <- threshold[length_threshold]
dummy_sample[dummy_sample < thres] <- NA
dummy_sample[dummy_sample >= thres] <- 1
collect <- list() collect <- list()
j <- 0 j <- 0
...@@ -43,17 +47,41 @@ write_Out_Samples <- function (inPath, step, className) { ...@@ -43,17 +47,41 @@ write_Out_Samples <- function (inPath, step, className) {
} }
} }
} }
result <- do.call(rbind, collect) result <- do.call(rbind, collect)
res <- raster::extract(dummy, result) res <- raster::extract(dummy_sample, result)
if (length(which(is.na(res))) > 0) { if (length(which(is.na(res))) > 0) {
res <- result[-which(is.na(res)), ] 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( rgdal::writeOGR(
res, res,
layer = "result", 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 = ""), dsn = paste(inPath, "RefHaSa_", className, "_", step, ".shp", sep = ""),
driver = "ESRI Shapefile" driver = "ESRI Shapefile",
check_exists = TRUE,
overwrite_layer = TRUE
) )
}
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
\alias{write_Out_Samples} \alias{write_Out_Samples}
\title{Sample Collection for Habitat Types} \title{Sample Collection for Habitat Types}
\usage{ \usage{
write_Out_Samples(inPath, step, className) write_Out_Samples(inPath, step, className, output_format = c("shp", "geojson"))
} }
\arguments{ \arguments{
\item{inPath}{file path (character) for results of habitat type sampling and probability mapping (same as outPath from function multi_Class_Sampling)} \item{inPath}{file path (character) for results of habitat type sampling and probability mapping (same as outPath from function multi_Class_Sampling)}
...@@ -12,13 +12,15 @@ write_Out_Samples(inPath, step, className) ...@@ -12,13 +12,15 @@ write_Out_Samples(inPath, step, className)
\item{step}{step number (numeric)} \item{step}{step number (numeric)}
\item{className}{name (character) of habitat type for which samples should be selected} \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{ \value{
ESRI shapefile with name: RefHaSa_className_step.shp ESRI shapefiles/GeoJSON with name: RefHaSa_step_classname.shp/RefHaSa_step_classname.geojson
\enumerate{ \enumerate{
\item Point Shape represents pixel that belong to selected habitat type and can be used as reference for further model building \item Point Shape represents pixel that belong to selected habitat type and can be used as reference for further model building
} }
} }
\description{ \description{
Writes out a set of samples (SpatialPointsDataFrame) into an ESRI shapefile 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.
} }
Supports Markdown
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