Daniel-Mietchen / ideas

A dumping ground for halfbaked ideas, some of which will hopefully be worked on soon
Other
26 stars 6 forks source link

Generalize the SPARQL-based London Underground Map to other networks #1029

Open Daniel-Mietchen opened 5 years ago

Daniel-Mietchen commented 5 years ago

Here is a query that traces the Underground lines using SPARQL:

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

WITH {
  SELECT ?item (SAMPLE(?coordinates) AS ?coordinates) (SAMPLE(?image) AS ?image) (sample(?lat1) as ?lat1) (sample(?lon1) as ?lon1) (sample(?opening) as ?opening) WHERE {
    ?item wdt:P31 wd:Q14562709 .
    ?item wdt:P625 ?coordinates  .
    ?item p:P625 / psv:P625 / wikibase:geoLatitude ?lat1 .
    ?item p:P625 / psv:P625 / wikibase:geoLongitude ?lon1 .
    OPTIONAL { ?item wdt:P18 ?image }.
    OPTIONAL { ?item wdt:P1619 ?opening }.
  } GROUP BY ?item
} AS %stations

WITH {
  SELECT ?nextstation (sample(?lat2) as ?lat2) (sample(?lon2) as ?lon2) WHERE {
    ?nextstation wdt:P31 wd:Q14562709 .
    ?nextstation p:P625 / psv:P625 / wikibase:geoLatitude ?lat2 .
    ?nextstation p:P625 / psv:P625 / wikibase:geoLongitude ?lon2 .
  } GROUP BY ?nextstation
} AS %nextstations

WITH {
  SELECT ?line ?connect ?rgb WHERE {
    INCLUDE %stations .
    INCLUDE %nextstations .
    ?item p:P197 ?nextstationstatement .
    ?nextstationstatement ps:P197 ?nextstation .
    ?nextstationstatement pq:P81 ?connect . ?connect wdt:P361 wd:Q20075  . 
    ?connect wdt:P465 ?rgb .
    FILTER(STR(?item) < STR(?nextstation)) .    
    BIND(CONCAT('LINESTRING (', STR(?lon1), ' ', STR(?lat1), ',', STR(?lon2), ' ', STR(?lat2), ')') AS ?str) .
    BIND(STRDT(?str, geo:wktLiteral) AS ?line) 
  }
} AS %lines 

WHERE {
  { INCLUDE %stations }  UNION { INCLUDE %lines  } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

I'd like to explore adapting this to other network visualizations, perhaps starting with the Berliner U-Bahn (where the data model is slightly different) but also extending to the stages of a Tour de France, or to places where Illuminati sent or received letters.

Daniel-Mietchen commented 5 years ago

I saw the query via https://en.wikipedia.org/w/index.php?title=User_talk:Daniel_Mietchen&oldid=866301939#Facto_Post_%E2%80%93_Issue_17_%E2%80%93_29_October_2018 , which says that it "whizzed past on Twitter".

A search https://twitter.com/search?vertical=default&q=london%20underground%20wikidata yielded this announcing tweet https://twitter.com/Tagishsimon/status/1053808145900953600 as well as a less user-friendly earlier version: https://twitter.com/ZeroOne3010/status/1036367531152474113 .