Skip to content

Commit

Permalink
Modified ssn_write(), ssn_subset(), and ssn_split_predpts() to remove…
Browse files Browse the repository at this point in the history
… netgeometry column before using st_write. This prevents column names from being abbreviated in the new shapefile
  • Loading branch information
pet221 committed Nov 10, 2023
1 parent 846f8f2 commit 0a41b01
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
12 changes: 9 additions & 3 deletions R/ssn_split_predpts.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ ssn_split_predpts <- function(ssn, predpts, size_predpts, by,
}

## Write shapefile
st_write(sub.data, paste0(id_predpts2, ".shp"), quiet = TRUE)
ind<- colnames(sub.data) == "netgeometry"
st_write(sub.data[,!ind], paste0(id_predpts2, ".shp"), quiet = TRUE)
rm(ind)

## Add to existing ssn
new.index <- length(ssn$preds) + 1
Expand Down Expand Up @@ -264,7 +266,9 @@ ssn_split_predpts <- function(ssn, predpts, size_predpts, by,
}

## write subset of prediction points to file
st_write(sub.data, paste0(id_predpts, ".shp"), quiet = TRUE)
ind<- colnames(sub.data) == "netgeometry"
st_write(sub.data[,!ind], paste0(id_predpts, ".shp"), quiet = TRUE)
rm(ind)

## Add to existing ssn
new.index <- length(ssn$preds) + 1
Expand Down Expand Up @@ -317,7 +321,9 @@ ssn_split_predpts <- function(ssn, predpts, size_predpts, by,
}

## write subset of prediction points to file
st_write(sub.data, paste0(id_predpts, ".shp"), quiet = TRUE)
ind<- colnames(sub.data) == "netgeometry"
st_write(sub.data[,!ind], paste0(id_predpts, ".shp"), quiet = TRUE)
rm(ind)

## Add to existing ssn
new.index <- length(ssn$preds) + 1
Expand Down
11 changes: 11 additions & 0 deletions R/ssn_subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ ssn_subset <- function(ssn, path, subset, clip = FALSE, overwrite = FALSE) {
}

## Write sites shapefile
## REMOVE netgeomtry b/c > 10 character column name
ind <- colnames(ssn.tmp$obs) == "netgeometry"
ssn.tmp$obs<- ssn.tmp$obs[,!ind]
st_write(ssn.tmp$obs, paste0(file, "/sites.shp"), quiet = TRUE)


Expand Down Expand Up @@ -201,6 +204,9 @@ ssn_subset <- function(ssn, path, subset, clip = FALSE, overwrite = FALSE) {
edges.sub <- ssn.tmp$edges[ind.edges, ]

## Save subset of edges
## REMOVE netgeomtry b/c > 10 character column name
ind <- colnames(edges.sub) == "netgeometry"
edges.sub<- edges.sub[,!ind]
st_write(edges.sub, paste0(ssn.tmp$path, "/edges.shp"), quiet = TRUE)


Expand Down Expand Up @@ -243,7 +249,12 @@ ssn_subset <- function(ssn, path, subset, clip = FALSE, overwrite = FALSE) {
rm(ind, ind2)
}
## Subset predictions


preds.sub <- ssn.tmp$preds[[pred.name]][ind.preds, ]

ind <- colnames(preds.sub) == "netgeometry"
preds.sub<- preds.sub[,!ind]
st_write(preds.sub, paste0(ssn.tmp$path, "/", pred.name, ".shp"),
quiet = TRUE
)
Expand Down
10 changes: 10 additions & 0 deletions R/ssn_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ ssn_write <- function(ssn, path, overwrite = FALSE,

## Copy observed sites if they exist
if (class(ssn.tmp$obs)[1] == c("sf")) {
## REMOVE netgeomtry b/c > 10 character column name
ind <- colnames(ssn$obs) == "netgeometry"
ssn$obs<- ssn$obs[,!ind]
st_write(ssn$obs, paste0(ssn.tmp$path, "/sites.shp"), quiet = TRUE)
}

## Copy edges
## REMOVE netgeomtry b/c > 10 character column name
ind <- colnames(ssn$edges) == "netgeometry"
ssn$edges<- ssn$edges[,!ind]
st_write(ssn$edges, paste0(ssn.tmp$path, "/edges.shp"), quiet = TRUE)

## Copy prediction sites
Expand All @@ -106,6 +112,10 @@ ssn_write <- function(ssn, path, overwrite = FALSE,
pred.name.vec <- attributes(ssn.tmp$preds)$names
for (i in seq_len(pred.len)) {
pred.name <- pred.name.vec[i]
## REMOVE netgeomtry b/c > 10 character column name
ind <- colnames(ssn$preds[[pred.name]]) == "netgeometry"
ssn$preds[[pred.name]]<- ssn$preds[[pred.name]][,!ind]

st_write(ssn$preds[[pred.name]], paste0(
ssn.tmp$path, "/",
pred.name, ".shp"
Expand Down

0 comments on commit 0a41b01

Please sign in to comment.