Newer
Older
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#Functions to read in data from a .gml file to an OGS6 object
#'read_in_gml
#'@description Wrapper function to read in a whole .gml file
#'@param ogs6_obj A OGS6 class object
#'@param gml_path The path to the geometry file that should be read in
#'@export
read_in_gml <- function(ogs6_obj, gml_path) {
assertthat::assert_that(class(ogs6_obj) == "OGS6")
xml_doc <- validate_read_in_xml(gml_path)
name <- xml2::xml_text(xml2::xml_find_first(xml_doc, "//name"))
points <- read_in_points(xml_doc)
polylines <- read_in_polylines(xml_doc)
surfaces <- read_in_surfaces(xml_doc)
ogs6_obj$add_gml(r2ogs6_gml(name, points, polylines, surfaces))
}
#'read_in_points
#'@description Reads points from a .gml file
#'@param xml_doc A parsed XML document (of class 'xml2::xml_document')
read_in_points <- function(xml_doc) {
points_tibble <- tibble::tibble(x = c(),
y = c(),
z = c(),
name = c())
points_nodeset <- xml2::xml_find_all(xml_doc, "//points/*")
for(i in seq_len(length(points_nodeset))){
attrs <- xml2::xml_attrs(points_nodeset[[i]])
point_name <- ""
if(length(attrs) == 5){
point_name <- attrs[[5]]
}
points_tibble <- tibble::add_row(points_tibble,
x = as.double(attrs[[2]]),
y = as.double(attrs[[3]]),
z = as.double(attrs[[4]]),
name = point_name,
)
}
return(invisible(points_tibble))
}
#'read_in_polylines
#'@description Reads polylines from a .gml file
#'@param xml_doc A parsed XML document (of class 'xml2::xml_document')
read_in_polylines <- function(xml_doc) {
polylines_list <- list()
polylines_nodeset <- xml2::xml_find_all(xml_doc, "//polylines/*")
if(class(polylines_nodeset) == "xml_missing"){
return(invisible(NULL))
}
for(i in seq_len(length(polylines_nodeset))){
attrs <- xml2::xml_attrs(polylines_nodeset[[i]])
pnt_nodeset <- xml2::xml_children(polylines_nodeset[[i]])
pnt_vector <- c()
for(j in seq_len(length(pnt_nodeset))){
pnt_vector <- c(pnt_vector, xml2::xml_double(pnt_nodeset[[j]]))
}
polyline <- list(name = attrs[[2]], pnt_vector)
polylines_list <- c(polylines_list, list(polyline))
}
return(invisible(polylines_list))
}
#'read_in_surfaces
#'@description Reads surfaces from a .gml file
#'@param xml_doc A parsed XML document (of class 'xml2::xml_document')
read_in_surfaces <- function(xml_doc) {
surfaces_list <- list()
surfaces_nodeset <- xml2::xml_find_all(xml_doc, "//surfaces/*")
if(class(surfaces_nodeset) == "xml_missing"){
return(invisible(NULL))
}
for(i in seq_len(length(surfaces_nodeset))){
attrs <- xml2::xml_attrs(surfaces_nodeset[[i]])
element_nodeset <- xml2::xml_children(surfaces_nodeset[[i]])
element_1 <- as.double(xml2::xml_attrs(element_nodeset[[1]]))
element_2 <- as.double(xml2::xml_attrs(element_nodeset[[2]]))
surface <- list(name = attrs[[2]], element_1, element_2)
surfaces_list <- c(surfaces_list, list(surface))
}
return(invisible(surfaces_list))
}