sparql-examples

A set of SPARQL examples that are used in different TGX resources

View the Project on GitHub BiGCAT-UM/sparql-examples

102

rq turtle/ttl

Location aspect: nearby locations as topics in works

Use at

PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX target: <http://www.wikidata.org/entity/Q1309>

SELECT
  (ROUND(1000 * ?distance_) / 1000 AS ?distance)
  ?work ?workLabel (CONCAT("/work/", SUBSTR(STR(?work), 32)) AS ?workUrl)
  ?location ?locationLabel (CONCAT("/topic/", SUBSTR(STR(?location), 32)) AS ?locationUrl)
WITH {
  SELECT
    ?work ?location
    (min(?distances) AS ?distance_)
  WHERE {
    ?work wdt:P921 ?location . 

    ?location p:P625 ?other_geo_statement . 

    # Some locations have unknown geocoordinates indicated with novalue
    # psv will remove them. 
    ?other_geo_statement ps:P625 ?other_geo .
    ?other_geo_statement psv:P625 [] .

    # Remove articles from encyclopedia and dictionaries
    # Filter is more expensive when applied here. 
    MINUS { ?work wdt:P31 wd:Q17329259 }
    MINUS { ?work wdt:P31 wd:Q4423781 }
   
    target: wdt:P159* / wdt:P625 ?geo .
    BIND(geof:distance(?other_geo, ?geo) AS ?distances)
  }
  GROUP BY ?work ?location
  ORDER BY ?distance_
  LIMIT 500
} AS %results
WHERE {
  INCLUDE %results
          
  # Label the result
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en" . }
}
ORDER BY ?distance

graph TD