#' Import a shapefile of stream edges without observed or predicted sites as a SpatialStreamNetwork
#'
#'@description
#'
#'Imports a SpatialStreamNetwork without loading the observed and predicted sites.
#'
#'@param filepath a path to a folder containing a stream network. The folder should have the extension .ssn.
#'@param o.write a logical value indicating whether the binaryID.db inside the folder should be overwritten if it already exists. Note that, if no binaryID.db exists, that database will be generated by this function.
#'@return An object of class SpatialStreamNetwork, with nothing in obspoints and predpoints slots.
#'
#'@details
#'
#'The purpose of this function is to allow users to ingest stream shapefiles with no observed or prediction sites into R as a SpatialStreamNetwork. This will then allow them to use their data with functions like \code{generateSites}, which will allow them to explore various designs on their stream network.
#'
#'@export
importStreams <- function(filepath, o.write = FALSE){
old.wd <- getwd()
Path <- dirname(filepath)
ssn.obj <- basename(filepath)
if (Path == ".") {
Path = old.wd
}
setwd(paste(Path, "/", ssn.obj, sep = ""))
on.exit(setwd(old.wd))
edges <- readOGR(".", "edges", verbose = FALSE, stringsAsFactors = FALSE, integer64 = "allow.loss")
rownames(edges@data) <- edges@data[, "rid"]
if (exists("edges") == 0) {
stop("edges.shp is missing from ", Path, " folder")
}
if (getinfo.shape("edges.shp")[[2]] != 3 & getinfo.shape("edges.shp")[[2]] != 13) {
stop("edges.shp does not have polyline geometry")
}
ind1 <- colnames(edges@data) == c("netID")
ind2 <- colnames(edges@data) == c("rid")
ind3 <- colnames(edges@data) == c("upDist")
if (sum(ind1) == 0) {
stop("netID is missing from streams attribute table")
}
if (sum(ind2) == 0) {
stop("rid is missing from streams attribute table")
}
if (sum(ind3) == 0) {
stop("upDist is missing from streams attribute table")
}
if (is.factor(edges@data$netID)) {
edges@data$netID <- anum(edges@data$netID)
}
network.line.coords <- data.frame(edges@data$netID, edges@data[, "rid"], edges@data[, "upDist"])
colnames(network.line.coords) <- c("NetworkID", "SegmentID", "DistanceUpstream")
network.line.coords <- as.data.frame(network.line.coords)
row.names(network.line.coords) <- row.names(edges@data)
network.line.coords[, 1] <- as.factor(network.line.coords[, 1])
network.line.coords[, 2] <- as.factor(network.line.coords[, 2])
rm(ind1, ind2, ind3)
op <- new("SSNPoint", network.point.coords = data.frame(NULL),
point.coords = matrix(nrow = 1, ncol=2), point.data = data.frame(NULL),
points.bbox = matrix(nrow = 2, ncol = 2))
ops <- new("SSNPoints")
ops@SSNPoints[[1]] <- op
ops@ID[[1]] <- "Obs"
ssn <- new(
"SpatialStreamNetwork",
edges, network.line.coords = network.line.coords,
obspoints = ops,
path = paste(Path, "/", ssn.obj, sep = "")
)
ssn@obspoints@SSNPoints[[1]]@point.data$netID <- as.factor(ssn@obspoints@SSNPoints[[1]]@point.data$netID)
ssn@data$netID <- as.factor(ssn@data$netID)
rm(network.line.coords, edges)
SSN:::createBinaryID(ssn, o.write = o.write)
return(ssn)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.