Commit bbe542b8 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

read_network(): Consistency of node coordinates.

- Test

- Stop also at different nodes sharing the same coordinates (may be a typo)
parent 042c0817
Package: mapMCDA
Title: Produce an epidemiological risk map by weighting multiple risk
factors
Version: 0.4.49
Version: 0.4.50
Date: 2020-04-29
Authors@R: c( person("Andrea", "Apolloni", email =
"andrea.apolloni@cirad.fr", role = c("ctb"), comment = "Animal
......
......@@ -168,12 +168,33 @@ must be respected:",
v_coord <- v_coord[order(v_coord$Node),]
v_coord$Node <- as.factor(v_coord$Node)
## Check consistency of coordinates
## Check consistency of node coordinates
## Each node has consistent coordinates
if (any((idx <- table(v_coord$Node)) > 1)) {
idx.nodes <- paste(names(idx)[idx > 1], collapse = ", ")
stop("Inconsistent coordinates.\n",
"The file specifies different coordinates in different lines for node(s): ", idx.nodes)
}
## Each coordinates represent a single node
if ( any(idx <- duplicated(v_coord[, -1])) ) {
## Return the nodes in dat (first col) matching a given set of coords x.
matching_coords <- function(x, dat) {
vapply(seq_len(nrow(dat)), function(i) all(dat[i, -1] == x), TRUE)
}
iso_nodes <-
lapply(
which(idx),
function(x) as.character(v_coord[matching_coords(v_coord[x, -1], v_coord), 1])
)
stop("Detected different locations with the same coordinates.\n",
"This can be due to typos in location names.\n",
"Verify the coordinates of nodes ",
paste(lapply(iso_nodes, paste, collapse = ", "), collapse = " and "),
".")
}
ans <- geonetwork::geonetwork(e_list, v_coord, directed = TRUE)
......
......@@ -93,6 +93,50 @@ test_that("Informative errors for incorrectly formatted network data", {
)
})
test_that("Informative errors for inconsistent network data", {
## Same locations with different coordinates
df <- data.frame(
orig = LETTERS[c(1, 1)],
dest = letters[1:2],
olon = rep(1:2),
olat = rep(1:2),
dlon = rep(2:1),
dlat = rep(2:1),
vol = 10*1:2,
stringsAsFactors = FALSE
)
tf <- tempfile(fileext = ".csv")
write.csv(df, file = tf, row.names = FALSE)
expect_error(
read_network(tf),
"Inconsistent coordinates"
)
## Same coordinates with different location names
## (typos in the names?)
df <- data.frame(
orig = LETTERS[1:2],
dest = letters[1:2],
olon = rep(1:2),
olat = rep(1:2),
dlon = rep(2:3),
dlat = rep(2:3),
vol = 10*1:2,
stringsAsFactors = FALSE
)
tf <- tempfile(fileext = ".csv")
write.csv(df, file = tf, row.names = FALSE)
expect_error(
read_network(tf),
"Detected different locations with the same coordinates."
)
})
test_that("read_network aggregates volume for multiple links", {
base_df <- data.frame(
......
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