-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikidataSparqlApi.fs
47 lines (40 loc) · 1.4 KB
/
WikidataSparqlApi.fs
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
namespace Wikidata.Sparql
type Entry =
{ Station: string
Name: string
IBNR: string option
stationCode: string option }
module Api =
open Sparql
let private endpoint = "https://qlever.cs.uni-freiburg.de/api/wikidata"
// railway stations (Q55488) in germany (Q183) from wikidata
let private osmQuery () =
$"""
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?name ?IBNR ?stationCode ?station WHERE {{
?station wdt:P31/wdt:P279* wd:Q55488 .
?station wdt:P17 wd:Q183 .
Optional {{ ?station wdt:P954 ?IBNR . }}
Optional {{
?station wdt:P296|wdt:P8671 ?stationCode .
FILTER regex(?stationCode, "[A-Z]+")
}}
?station rdfs:label ?name .
FILTER (LANG(?name) = "de")
}}
"""
let loadOsmData () : Async<string> =
EraKG.Request.GetAsync endpoint (osmQuery ()) EraKG.Request.applicationSparqlResults
let ToEntries (sparql: QueryResults) : Entry[] =
sparql.results.bindings
|> Array.map (fun b ->
{ Station = b.["station"].value
Name = b.["name"].value
stationCode =
if b.ContainsKey "stationCode" then
Some b.["stationCode"].value
else
None
IBNR = if b.ContainsKey "IBNR" then Some b.["IBNR"].value else None })