forked from rsbivand/rgrass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_helpers.R
50 lines (49 loc) · 1.35 KB
/
read_helpers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
sanitize_layername <- function(name, type, mapsets, ignore.stderr) {
stopifnot(
is.character(type),
length(type) == 1L,
type %in% c("raster", "vector")
)
stopifnot(
is.character(name),
length(name) == 1L
)
vca <- unlist(strsplit(name, "@"))
if (length(vca) == 1L) {
exsts <- execGRASS("g.list",
type = type, pattern = vca[1],
intern = TRUE, ignore.stderr = ignore.stderr
)
if (length(exsts) > 1L) {
stop(
"multiple layers named ", vca[1],
" found in in mapsets in search path: ",
paste(mapsets, collapse = ", "),
" ; use full path with @ to choose the required raster"
)
}
if (length(exsts) == 0L || exsts != vca[1]) {
stop(
name, " not found in mapsets in search path: ",
paste(mapsets, collapse = ", ")
)
}
} else if (length(vca) == 2L) {
exsts <- execGRASS("g.list",
type = type, pattern = vca[1],
mapset = vca[2], intern = TRUE, ignore.stderr = ignore.stderr
)
if (length(exsts) == 0L || exsts != vca[1]) {
stop(name, " not found in mapset: ", vca[2])
}
} else {
stop(name, " incorrectly formatted")
}
vca
}
get_mapsets <- function() {
unlist(strsplit(
execGRASS("g.mapsets", flags = "p", intern = TRUE),
" "
))
}