@@ -107,15 +107,34 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
107
107
# '@description list_collections
108
108
# '@param mimetype mimetype
109
109
# '@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 ){
111
113
str = "
112
114
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
113
115
114
- SELECT ?collection ?label WHERE {
116
+ SELECT ?collection ?label (COUNT(DISTINCT ?subCollection) AS ?count_sub_collections) (COUNT(DISTINCT ?concept) AS ?count_concepts) WHERE {
115
117
?collection a skos:Collection .
116
118
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 )
119
138
},
120
139
121
140
# '@description query_from_uri
@@ -137,7 +156,21 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
137
156
ORDER BY ?lang "
138
157
)
139
158
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
+ }
141
174
},
142
175
143
176
# '@description query_from_term
@@ -149,19 +182,41 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
149
182
150
183
str = paste0(
151
184
" PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
152
- SELECT ?concept ?lang ?prefLabel
185
+ SELECT ?concept ?lang ?prefLabel ?collection ?collectionLabel
153
186
WHERE {
154
187
?concept skos:prefLabel ?searchLabel .
155
188
?concept skos:prefLabel ?prefLabel .
156
189
FILTER (STR(?searchLabel) = \" " , term , " \" )
157
190
FILTER (LANG(?prefLabel) != \"\" )
158
191
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
+
159
199
}
160
- GROUP BY ?concept ?lang ?prefLabel
200
+ GROUP BY ?concept ?lang ?prefLabel ?collection ?collectionLabel
161
201
ORDER BY ?lang "
162
202
)
163
203
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 )
165
220
}
166
221
)
167
222
)
0 commit comments