query-963f7eeae12f3d178a45010a1c472ffb

rq turtle/ttl

TODO

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX bd: <http://www.bigdata.com/rdf#>
# Note : this query uses the NamedSubquery feature, see https://wiki.blazegraph.com/wiki/index.php/NamedSubquery

#defaultView:Map{"hide":["?coordinates", "?line", "?rgb"]}
SELECT ?coordinates ?line ?station ?itemLabel ?connectLabel ?image ?opening ?rgb

# subquery that selects all stations of the amsterdam metro

#subquery that selects all the lines of the amsterdam metro

WHERE {
  # using an union of the two result sets in there so that each metro station and each metro line has its own unique result line in the final result set. 
  # (In "metro stations" result set, the "?line" variable will be always unbound, 
  #  and conversely in the "metro lines" result set, only the "?line" variable will have a value)
  {  {
  # There may be several images and several coordinates for a station, so we use a « group by » to ensure we have only one line per station in the results
  SELECT ?station (SAMPLE(?coordinates) AS ?coordinates) (SAMPLE(?image) AS ?image) ?opening WHERE {
    ?station wdt:P31/wdt:P279* wd:Q55488 ; # we get train stations
             wdt:P361 wd:Q260870 ;         # … that are part of the amsterdam metro
             wdt:P625 ?coordinates  .      # … their coordinate
    OPTIONAL { ?station wdt:P1619 ?opening }          # … and their opening date
  } GROUP BY ?station ?opening
}} UNION {  {
  SELECT ?line ?connect ?rgb WHERE {
    # we reuse the stations computed by the other named query
     {
  # There may be several images and several coordinates for a station, so we use a « group by » to ensure we have only one line per station in the results
  SELECT ?station (SAMPLE(?coordinates) AS ?coordinates) (SAMPLE(?image) AS ?image) ?opening WHERE {
    ?station wdt:P31/wdt:P279* wd:Q55488 ; # we get train stations
             wdt:P361 wd:Q260870 ;         # … that are part of the amsterdam metro
             wdt:P625 ?coordinates  .      # … their coordinate
    OPTIONAL { ?station wdt:P1619 ?opening }          # … and their opening date
  } GROUP BY ?station ?opening
}.

    ?station p:P197 [ ps:P197 ?nextstation ;           # … we get all the neighbourstation(s) of one station
                      pq:P81  ?connect ] .  
    ?connect wdt:P361 wd:Q260870 ;                     # … who have a transport line that is a part of the amsterdam metro
             wdt:P465 ?rgb                             # … also get the standard colour for the connecting line

    FILTER(STR(?station) < STR(?nextstation)) . # making sure we draw only one line for each pair of neighbour station, 
                                             # (as P197 (neighboor station) is symmetric, we could have two results for each line :
                                             # 1) ?item = Qaaa and ?nextstation = Qbbb 
                                             # 2) ?item = Qbbb and ?nextstation = Qaaa
                                             # so we choose to keep only (arbitrarily) the first one to draw only one line

    ?station     p:P625 / psv:P625 [ wikibase:geoLatitude ?lat1 ;  wikibase:geoLongitude ?lon1 ].      # the coordinates of one end of the line
    ?nextstation p:P625 / psv:P625 [ wikibase:geoLatitude ?lat2 ; wikibase:geoLongitude ?lon2 ].       #               … of one the other end of the line

    # Building the « LINESTRING » instruction that will ask the view to draw a line frome one station to another, 
    BIND(CONCAT('LINESTRING (', STR(?lon1), ' ', STR(?lat1), ',', STR(?lon2), ' ', STR(?lat2), ')') AS ?str) .
    # with the appropriate datatype for the line to be interpreted as such by the map engine 
    # (thanks to strdt : https://www.w3.org/TR/sparql11-query/#func-strdt )
    BIND(STRDT(?str, geo:wktLiteral) AS ?line)
  }
}} .
  # (more naive approach where lines and stations are mixed-up in the same result set lines make some station dots bigger than others)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v6("?connect") v6("?coordinates"):::projected v6("?image"):::projected v8("?lat1") v10("?lat2") v13("?line"):::projected v9("?lon1") v11("?lon2") v5("?nextstation") v3("?opening"):::projected v7("?rgb"):::projected v1("?station"):::projected v12("?str") a1((" ")) a2((" ")) a3((" ")) a5((" ")) a4((" ")) a7((" ")) a6((" ")) c5(["wd:Q260870"]):::iri c17(["bd:serviceParam"]):::iri c3(["wd:Q55488"]):::iri c19(["#91;AUTO_LANGUAGE#93;,en"]):::literal subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; f0[["str(?station) < str(?nextstation)"]] f0 --> v1 f0 --> v5 v1 --"p:direct/P31"--> a2 a2 --"p:direct/P279"--> c3 v1 --"p:direct/P361"--> c5 v1 --"p:direct/P625"--> v6 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v1 -."p:direct/P1619".-> v3 end bind3[/"sample(?coordinates)"/] v6 --o bind3 bind3 --as--o v6 bind4[/"sample(?image)"/] v6 --o bind4 bind4 --as--o v6 a3 --"p:statement/P197"--> v5 a3 --"p:qualifier/P81"--> v6 v1 --"p:P197"--> a3 v6 --"p:direct/P361"--> c5 v6 --"p:direct/P465"--> v7 a4 --"wikibase:geoLatitude"--> v8 a4 --"wikibase:geoLongitude"--> v9 v1 --"p:P625"--> a5 a5 --"p:statement/value/P625"--> a4 a6 --"wikibase:geoLatitude"--> v10 a6 --"wikibase:geoLongitude"--> v11 v5 --"p:P625"--> a7 a7 --"p:statement/value/P625"--> a6 bind5[/"concat('LINESTRING (',str(?lon1),' ',str(?lat1),',',str(?lon2),' ',str(?lat2),')')"/] v9 --o bind5 v8 --o bind5 v11 --o bind5 v10 --o bind5 bind5 --as--o v12 bind6[/"STRDT(?str,'geo:wktLiteral')"/] v12 --o bind6 bind6 --as--o v13 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v1 --"p:direct/P31"--> a1 a1 --"p:direct/P279"--> c3 v1 --"p:direct/P361"--> c5 v1 --"p:direct/P625"--> v6 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v1 -."p:direct/P1619".-> v3 end bind9[/"sample(?coordinates)"/] v6 --o bind9 bind9 --as--o v6 bind10[/"sample(?image)"/] v6 --o bind10 bind10 --as--o v6 end union0r <== or ==> union0l end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c17 --"wikibase:language"--> c19 end