Skip to content

Commit 1054197

Browse files
committed
#401 get_concepts_hierarchy
1 parent a2cb65f commit 1054197

5 files changed

+107
-0
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(add_config_utils)
4+
export(build_hierarchical_list)
45
export(check_packages)
56
export(closeWorkflow)
67
export(create_geoflow_data_from_dbi)

R/geoflow_utils.R

+24
Original file line numberDiff line numberDiff line change
@@ -916,4 +916,28 @@ describeOGCRelation <- function(entity, data_object, service, download = FALSE,
916916
#'@export
917917
create_object_identification_id = function(prefix, str){
918918
paste(prefix, digest::digest(object = str, algo = "crc32", serialize = FALSE), sep = "_")
919+
}
920+
921+
#'@name build_hierarchical_list
922+
#'@aliases build_hierarchical_list
923+
#'@title build_hierarchical_list
924+
#'
925+
#'@usage build_hierarchical_list(data, parent)
926+
#'
927+
#'@param data data
928+
#'@param parent parent
929+
#'@return a hierarchical list
930+
#'@export
931+
build_hierarchical_list <- function(data, parent, parent_key, child_key) {
932+
children <- data[data[,parent_key] == parent, ]
933+
children <- children[order(children[,child_key]),]
934+
if (nrow(children) == 0) {
935+
return(NULL)
936+
} else {
937+
nested_list <- lapply(1:nrow(children), function(i) {
938+
build_vocabulary_hierarchical_list(data, children[i, child_key])
939+
})
940+
names(nested_list) <- children[,child_key]
941+
return(nested_list)
942+
}
919943
}

R/geoflow_vocabulary.R

+34
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,40 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
139139
return(out)
140140
},
141141

142+
#'@description list_concepts
143+
#'@param lang lang
144+
#'@param mimetype mimetype
145+
#'@param out_format output format (data.frame or list). Default is "data.frame"
146+
#'@return the response of the SPARQL query
147+
get_concepts_hierarchy = function(lang = "en", mimetype = "text/csv",
148+
out_format = c("data.frame","list")){
149+
out_format = match.arg(out_format)
150+
str = paste0("
151+
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
152+
153+
SELECT ?broaderConcept ?broaderPrefLabel ?concept ?prefLabel WHERE {
154+
?concept a skos:Concept .
155+
OPTIONAL {
156+
?concept skos:prefLabel ?prefLabel .
157+
FILTER (LANG(?prefLabel) = \"",lang,"\")
158+
}
159+
OPTIONAL {
160+
?concept skos:broader ?broaderConcept .
161+
OPTIONAL {
162+
?broaderConcept skos:prefLabel ?broaderPrefLabel .
163+
FILTER (LANG(?broaderPrefLabel) = \"",lang,"\")
164+
}
165+
}
166+
}
167+
ORDER BY ?concept
168+
")
169+
out = self$query(str = str, mimetype = mimetype)
170+
if(out_format == "list"){
171+
out = build_hierarchical_list(out, parent = "root", parent_key = "broaderPrefLabel", child_key = "prefLabel")
172+
}
173+
return(out)
174+
},
175+
142176
#'@description list_concepts
143177
#'@param lang lang
144178
#'@param mimetype mimetype

man/build_hierarchical_list.Rd

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geoflow_skos_vocabulary.Rd

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)