Skip to content

Commit 230fc51

Browse files
committed
#401 extend sparql queries to add collection uri/label and inherit collection for subject
1 parent 2439a81 commit 230fc51

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

R/geoflow_entity.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,7 @@ geoflow_entity <- R6Class("geoflow_entity",
19661966
target_vocab = vocabs[sapply(vocabs, function(vocab){vocab$id == subject$uri})]
19671967
if(length(target_vocab)>0){
19681968
target_vocab = target_vocab[[1]]
1969-
subject$uri = target_vocab$uri
1969+
subject$uri = target_vocab$uri #default uri
19701970
subject$keywords = lapply(subject$keywords, function(keyword){
19711971
rs = NULL
19721972
if(!is.null(keyword$uri)){
@@ -1982,6 +1982,11 @@ geoflow_entity <- R6Class("geoflow_entity",
19821982
for(lang in unique(rs$lang)){
19831983
attr(keyword$name, paste0("locale#",toupper(lang))) = rs[rs$lang == lang,]$prefLabel[1]
19841984
}
1985+
#overwrite subject uri/name if we find a collection (assuming keywords are from the same collection)
1986+
if(!is.na(rs[1L,]$collection)){
1987+
subject$uri <<- rs[1L,]$collection
1988+
subject$name <<- rs[1L,]$collectionLabel
1989+
}
19851990
}
19861991
return(keyword)
19871992
})

R/geoflow_vocabulary.R

+63-8
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,34 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
107107
#'@description list_collections
108108
#'@param mimetype mimetype
109109
#'@return the response of the SPARQL query
110-
list_collections = function(mimetype = "text/csv"){
110+
list_collections = function(mimetype = "text/csv",
111+
count_sub_collections = TRUE,
112+
count_concepts = TRUE){
111113
str = "
112114
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
113115
114-
SELECT ?collection ?label WHERE {
116+
SELECT ?collection ?label (COUNT(DISTINCT ?subCollection) AS ?count_sub_collections) (COUNT(DISTINCT ?concept) AS ?count_concepts) WHERE {
115117
?collection a skos:Collection .
116118
OPTIONAL { ?collection skos:prefLabel ?label }
117-
}"
118-
self$query(str = str, mimetype = mimetype)
119+
120+
# Count sub-collections
121+
OPTIONAL {
122+
?collection skos:member ?subCollection .
123+
?subCollection a skos:Collection .
124+
}
125+
126+
# Count concepts
127+
OPTIONAL {
128+
?collection skos:member ?concept .
129+
?concept a skos:Concept .
130+
}
131+
}
132+
GROUP BY ?collection ?label
133+
"
134+
out = self$query(str = str, mimetype = mimetype)
135+
if(!count_sub_collections) out$count_sub_collections = NULL
136+
if(!count_concepts) out$count_concepts = NULL
137+
return(out)
119138
},
120139

121140
#'@description query_from_uri
@@ -137,7 +156,21 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
137156
ORDER BY ?lang "
138157
)
139158

140-
self$query(str = str, graphUri = graphUri, mimetype = mimetype)
159+
out = self$query(str = str, graphUri = graphUri, mimetype = mimetype)
160+
if(nrow(out)>0){
161+
out = do.call("rbind", lapply(unique(out$lang), function(lang){
162+
rec= out[out$lang == lang,]
163+
if(any(is.na(rec$collection))){
164+
newrec = rec[!is.na(rec$collection),]
165+
if(nrow(newrec)==0){
166+
rec = rec[1,]
167+
}else{
168+
rec = newrec[1,]
169+
}
170+
}
171+
rec
172+
}))
173+
}
141174
},
142175

143176
#'@description query_from_term
@@ -149,19 +182,41 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
149182

150183
str = paste0(
151184
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
152-
SELECT ?concept ?lang ?prefLabel
185+
SELECT ?concept ?lang ?prefLabel ?collection ?collectionLabel
153186
WHERE {
154187
?concept skos:prefLabel ?searchLabel .
155188
?concept skos:prefLabel ?prefLabel .
156189
FILTER (STR(?searchLabel) = \"", term, "\")
157190
FILTER (LANG(?prefLabel) != \"\")
158191
BIND (LANG(?prefLabel) AS ?lang)
192+
193+
# Optional block to get the collection and its label
194+
OPTIONAL {
195+
?collection skos:member ?concept .
196+
OPTIONAL { ?collection skos:prefLabel ?collectionLabel }
197+
}
198+
159199
}
160-
GROUP BY ?concept ?lang ?prefLabel
200+
GROUP BY ?concept ?lang ?prefLabel ?collection ?collectionLabel
161201
ORDER BY ?lang "
162202
)
163203

164-
self$query(str = str, graphUri = graphUri, mimetype = mimetype)
204+
out = self$query(str = str, graphUri = graphUri, mimetype = mimetype)
205+
if(nrow(out)>0){
206+
out = do.call("rbind", lapply(unique(out$lang), function(lang){
207+
rec= out[out$lang == lang,]
208+
if(any(is.na(rec$collection))){
209+
newrec = rec[!is.na(rec$collection),]
210+
if(nrow(newrec)==0){
211+
rec = rec[1,]
212+
}else{
213+
rec = newrec[1,]
214+
}
215+
}
216+
rec
217+
}))
218+
}
219+
return(out)
165220
}
166221
)
167222
)

0 commit comments

Comments
 (0)