This repository was archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSPARQLR.Rmd
312 lines (267 loc) · 9 KB
/
SPARQLR.Rmd
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
---
title: "SPARQL with R"
author: "Katharina Kaelin"
knit: (function(input_file, encoding) {
out_dir <- 'docs';
rmarkdown::render(input_file,
encoding=encoding,
output_file=file.path(dirname(input_file), out_dir, 'index.html'))})
output:
html_document:
toc: true
toc_depth: 3
toc_float:
collapsed: true
smooth_scroll: true
theme: lumen
highlight: tango
---
<style type="text/css">
.main-container {
max-width: 1800px;
margin-left: 0;
margin-right: auto;
}
blockquote {
padding: 10px 20px;
margin: 0 0 20px;
font-size: 14px;
border-left: 5px solid #eee;
}
h1.title {
font-size: 38px;
color: #000000;
}
h1 { /* Header 1 */
font-size: 28px;
color: #0033cc;
}
h2 { /* Header 2 */
font-size: 28px;
color: #0099ff
}
h3 { /* Header 3 */
font-size: 14px;
color: #6600cc
}
</style>
# Introduction
**An overview of R libraries to query Wikidata (27.1.2019)** <br>
https://www.lehir.net/how-to-query-wikidata-in-r/ <br>
=> https://www.lehir.net/how-to-query-wikidata-in-r/#summary
**> WikidataR** <br>
https://github.com/Ironholds/WikidataR <br>
https://cran.r-project.org/web/packages/WikidataR/index.html <br>
https://cran.r-project.org/web/packages/WikidataR/WikidataR.pdf
**> WikidataQueryServiceR** <br>
https://github.com/bearloga/WikidataQueryServiceR <br>
https://cran.r-project.org/web/packages/WikidataQueryServiceR/index.html <br>
https://cran.r-project.org/web/packages/WikidataQueryServiceR/WikidataQueryServiceR.pdf
**> SPARQL** <br>
https://cran.r-project.org/web/packages/SPARQL/index.html <br>
https://cran.r-project.org/web/packages/SPARQL/SPARQL.pdf
# R Setup
```{r setup, echo=T, results='hide', message=FALSE, warning=FALSE}
# Import libraries
library(WikidataQueryServiceR) ## This is an R wrapper for the Wikidata Query Service (WDQS) which provides a way for tools toquery Wikidata via SPARQL.
library(SPARQL) ## Load SPARQL SELECT query result tables as a data frame, or UPDATE the triple store by connecting to an end-point over HTTP.
library(tidyverse) ## # collection of R packages designed for data science
library(sf) ## GIS vector library
library(tmap) ## This package offers a flexible, layer-based, and easy to use approach to create thematic maps
library(stringr) ## The stringr package provide a cohesive set of functions designed to make working with strings as easy as possible
library(DT) ## Data objects in R can be rendered as HTML tables using the JavaScript library 'DataTables'
# Number formatting
options(scipen = 1000000)
options(digits = 6)
```
# What else could we be doing today...?

# Museum in Canton Zürich
https://w.wiki/AwP
```{r, collapse=TRUE, warning=FALSE, message=FALSE}
## WikidataQueryServiceR
start.time <- Sys.time()
museum1_df <- WikidataQueryServiceR::query_wikidata('SELECT DISTINCT ?item ?name ?coord ?lat ?lon
WHERE
{
hint:Query hint:optimizer "None" .
?item wdt:P131* wd:Q11943 .
?item wdt:P31/wdt:P279* wd:Q33506 .
?item wdt:P625 ?coord .
?item p:P625 ?coordinate .
?coordinate psv:P625 ?coordinate_node .
?coordinate_node wikibase:geoLatitude ?lat .
?coordinate_node wikibase:geoLongitude ?lon .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "de" .
?item rdfs:label ?name
}
}
ORDER BY ASC (?name)')
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
DT::datatable(museum1_df)
## SPARQL
start.time <- Sys.time()
endpoint <- "https://query.wikidata.org/sparql"
query <- 'SELECT DISTINCT ?item ?name ?coord ?lat ?lon
WHERE
{
hint:Query hint:optimizer "None" .
?item wdt:P131* wd:Q11943 .
?item wdt:P31/wdt:P279* wd:Q33506 .
?item wdt:P625 ?coord .
?item p:P625 ?coordinate .
?coordinate psv:P625 ?coordinate_node .
?coordinate_node wikibase:geoLatitude ?lat .
?coordinate_node wikibase:geoLongitude ?lon .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "de" .
?item rdfs:label ?name
}
}
ORDER BY ASC (?name) '
museum2 <- SPARQL::SPARQL(endpoint,query,curl_args=list(useragent=R.version.string))
museum2_df <- museum2$results
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
DT::datatable(museum2_df)
```
# Municipalities of Canton Zürich
http://yasgui.org/short/fg7fNak6G
```{r, collapse=TRUE, warning=FALSE, message=FALSE}
## SPARQL
endpoint <- "https://ld.geo.admin.ch/query"
query <- 'PREFIX schema: <http://schema.org/>
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX st: <https://ld.geo.admin.ch/def/>
select ?Municipality ?Name ?Population ?bfs ?WKT
where{
?Municipality gn:featureCode gn:A.ADM3 .
?Municipality schema:name ?Name .
?Municipality gn:population?Population .
?Municipality st:bfsNumber ?bfs .
?Municipality dct:issued ?Date .
?Municipality gn:parentADM1 ?InCanton .
?InCanton schema:name ?CantonName .
?Municipality geo:hasGeometry ?Geometry .
?Geometry geo:asWKT ?WKT .
FILTER (?Date = "2019-01-01"^^xsd:date)
FILTER (?CantonName = "Zürich")
}'
municipality <- SPARQL::SPARQL(endpoint,query)
municipality_df <- municipality$results
DT::datatable(municipality_df)
```
# Convert df to spatial df
```{r, collapse=TRUE, warning=FALSE, message=FALSE}
## museum
museum_sf <- sf::st_as_sf(x = museum2_df, coords = c("lon", "lat"), crs= 4326) %>% st_transform(2056)
museum_sf
## municipality
municipality_df$WKT[1]
municipality_df$WKT_corrected <-str_match(municipality_df$WKT, '\"(.*?)\"')[,2]
municipality_df$WKT_corrected[1]
municipality_sfc = st_as_sfc(municipality_df$WKT_corrected, crs=4326)
municipality_sf = st_sf(municipality_df %>% select(-WKT, -WKT_corrected), geometry =municipality_sfc) %>% st_transform(2056)
municipality_sf
# Plot result: R base plot
plot(st_geometry(municipality_sf))
plot(st_geometry(museum_sf), pch = 19, col="blue", cex = 0.5, add = TRUE)
plot(st_geometry(municipality_sf), add = TRUE)
legend(x=2708000,y=1287500,
c("Museum","Muncipality"),
lty=c(NA,1),
pch=c(19,NA),
cex=.8,
col=c("blue","black"),
bty='n'
)
```
# Calculate density of museums per municipality
```{r, collapse=TRUE, warning=FALSE, message=FALSE}
# Spatial Join: instead of joining dataframes via an equal ID we join data- frames based on an equal location.
spjoin_sf <- sf::st_join(museum_sf, municipality_sf)
spjoin_sf
# Density calculation
# > 1. Count points per polygon
pts_count <- spjoin_sf %>%
dplyr::group_by(bfs) %>%
dplyr::summarise(count=n())
municipality_sf <- municipality_sf %>%
dplyr::left_join(pts_count %>% st_set_geometry(NULL) , by = c("bfs" ))
# > 2. Calculate area of polygon
municipality_sf <- municipality_sf %>%
dplyr::mutate(mun_area_m2 =as.vector(sf::st_area(.)))
# > 3. Calculate density: count/area
municipality_sf$density <- municipality_sf$count / municipality_sf$mun_area_m2 * 1000000
# Plot result: tmap
# > tmap static
tmap::tm_shape(municipality_sf) +
tmap::tm_fill("density",
title="Number of Museums per km2 \n(Classification Method: Quantile)",
style="quantile",
n = 4, # preferred number of classes
palette="YlGnBu",
colorNA = "grey90",
textNA = "No Museum",
legend.hist = TRUE,
) +
tmap::tm_borders() +
tmap::tm_legend(outside = TRUE, hist.width =3) +
tmap::tm_layout(main.title = "Density of Museum",
frame = FALSE,
legend.position = c("right", "top"),
legend.outside = TRUE)
# Export Data as shp
st_write(museum_sf, "./museum.shp", delete_layer = TRUE)
st_write(municipality_sf, "./municipality.shp", delete_layer = TRUE)
```
# Wikidata query that lists all datasets that use “statistik.zh.ch” as a source
Thank you @[csarasuagar](https://twitter.com/csarasuagar)! **=D**
```{r}
# municipality
# https://w.wiki/BA8
start.time <- Sys.time()
endpoint <- "https://query.wikidata.org/sparql"
query <- 'SELECT *
{
?ch wdt:P31 wd:Q70208 .
OPTIONAL {?ch wdt:P17 wd:Q39.}
?ch ?prop ?statement .
?statement prov:wasDerivedFrom ?refnode.
?refnode pr:P854 ?ref. #pr:P248 #pr:P854
FILTER (CONTAINS(str(?ref),"statistik.zh.ch"))
}
order by ?ch
'
source_municipality <- SPARQL::SPARQL(endpoint,query,curl_args=list(useragent=R.version.string))
source_municipality_df <-source_municipality$results
DT::datatable(source_municipality_df )
# city
# https://w.wiki/BA7
start.time <- Sys.time()
endpoint <- "https://query.wikidata.org/sparql"
query <- 'SELECT *
WHERE
{
?ch wdt:P31 wd:Q54935504 .
OPTIONAL {?ch wdt:P17 wd:Q39.}
?ch ?prop ?statement .
?statement prov:wasDerivedFrom ?refnode.
?refnode pr:P854 ?ref. #pr:P248 #pr:P854
FILTER (CONTAINS(str(?ref),"statistik.zh.ch"))
}
order by ?ch
'
source_city <- SPARQL::SPARQL(endpoint,query,curl_args=list(useragent=R.version.string))
source_city_df <-source_city$results
DT::datatable(source_city_df )
```