Commit 6776a82a authored by Blodgett, David L.'s avatar Blodgett, David L.

a couple bug fixes to dates and multilinestrings

parent d9da9efb
......@@ -37,4 +37,5 @@ importFrom(sf,st_polygon)
importFrom(sf,st_set_geometry)
importFrom(sf,st_sf)
importFrom(sf,st_sfc)
importFrom(sf,st_zm)
importFrom(stats,setNames)
......@@ -57,7 +57,7 @@ write_attribute_data <- function(nc_file, att_data, instance_dim_name = "instanc
types <- list(numeric="NC_DOUBLE", integer = "NC_INT", character="NC_CHAR")
# Convert any dates to character. This could be improved later.
i <- sapply(att_data, is, class2 = "Date")
i <- sapply(att_data, is, class2 = "Date") | sapply(att_data, is, class2 = "POSIXt")
att_data[i] <- lapply(att_data[i], as.character)
charDimLen<-0
......
......@@ -89,9 +89,9 @@ write_geometry = function(nc_file, geom_data, instance_dim_name = NULL, variable
#'
#' @importFrom RNetCDF open.nc close.nc create.nc var.put.nc att.put.nc
#' @importFrom stats setNames
#' @importFrom sf st_geometry_type st_crs st_coordinates
#' @importFrom sf st_geometry_type st_crs st_coordinates st_zm
#' @noRd
write_geom_data <- function(geom_data, ...)
write_geom_data <- function(geom_data, ...)
UseMethod("write_geom_data")
#' @noRd
......@@ -159,6 +159,11 @@ write_geom_data.sfc_LINESTRING <- function(geom_data, nc_file, instance_dim_name
#' @name write_geom_data
write_geom_data.sfc_MULTILINESTRING <- function(geom_data, nc_file,
instance_dim_name, variables = c()) {
if(grepl("Z|M", class(st_geometry(geom_data)[[1]])[1])) {
warning("Found more than two dimensions in geometry. Removing Z and M content.")
geom_data <- st_zm(geom_data)
}
crs <- get_crs(geom_data)
geom_data <- st_coordinates(geom_data)
......@@ -186,7 +191,7 @@ write_geom_data.sfc_MULTILINESTRING <- function(geom_data, nc_file,
g_data <- geom_data[geom_data[, 4] == geom, ]
for(g_part in 1:length(unique(g_data[, 3]))) {
for(g_part in unique(g_data[, 3])) {
nc_part <- nc_part + 1
coords <- g_data[g_data[, 3] == g_part, c(1,2)]
......
......@@ -64,3 +64,17 @@ test_that("shapefile line data works", {
}
})
test_that("NHDPlus Multilinestring", {
f <- system.file("extdata/nhdp_flowline_sample.gpkg", package = "ncdfgeom")
test_dat <- sf::read_sf(f)
test_nc <- expect_warning(write_geometry(tempfile(), test_dat), "Found more than two dimensions in geometry. Removing Z and M content.")
test_dat_2 <- read_geometry(test_nc)
expect_equal(class(test_dat_2$FDATE), "character") # coerced to character
expect_equal(class(sf::st_geometry(test_dat_2)[[1]])[1], "XY")
})
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