Skip to content

Commit

Permalink
resolve common names among vpus, closes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
jsta committed Mar 27, 2019
1 parent 7e92121 commit a16d99d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 12 additions & 0 deletions R/load.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ nhd_plus_load <- memoise::memoise(function(vpu, component = "NHDSnapshot", dsn,
component = component, dsn = dsn, pretty = pretty,
...)
is_spatial <- unlist(lapply(res, function(x) x$is_spatial))

# resolve common names among vpus (https://github.com/jsta/nhdR/issues/57)
names_template <- which.min(unlist(
lapply(res, function(x) length(names(x$res)))))
names_template <- names(res[[names_template]]$res)

res <- lapply(res, function(x){
names(x$res) <- align_names(names(x$res), names_template)
x$res <- x$res[,names_template]
x
})

res <- do.call("rbind", lapply(res, function(x) x$res))

if(any(is_spatial)){
Expand Down
12 changes: 12 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,15 @@ st_read_custom <- function(x, pretty = FALSE, ...){
# do.call(sf::st_read, c("dsn" = x, arguments))
}
}

align_names <- function(to, from){
res <- rep(NA, length(to))
for(i in seq_along(to)){
if(tolower(to[i]) %in% tolower(from)){
res[i] <- from[which(tolower(to[i]) == tolower(from))]
}else{
res[i] <- to[i]
}
}
res
}
11 changes: 5 additions & 6 deletions tests/testthat/test-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ test_that("nhd_plus_query handles mismatched column names.", {
skip_on_travis()

# https://github.com/jsta/nhdR/issues/57
box <- st_bbox(c(xmin = -105.93003, xmax = -104.91784,
box <- sf::st_bbox(c(xmin = -105.93003, xmax = -104.91784,
ymin = 40.41176, ymax = 41.21014),
crs = st_crs(4326)) %>%
st_as_sfc()

testthat::expect_success(
sf::st_as_sfc()
poudre_flow <- nhd_plus_query(poly = box,
dsn = c('NHDFlowLine'))
)
dsn = c('NHDFlowLine'),
quiet = TRUE)

expect_s3_class(poudre_flow$sp$NHDFlowLine, "sf")
})

0 comments on commit a16d99d

Please sign in to comment.