Project:SPARQL/examples: Difference between revisions

From Beyond Notability
Line 1,544: Line 1,544:
}
}
ORDER BY ?locationLabel
ORDER BY ?locationLabel
</sparql>
=== BN items near a chosen location ===
<sparql tryit="1">
#title:a map of BN items near a chosen starting point in England
## create a thingy for the start point: change the Qxxx ID in the following PREFIX var_start line to chosen location. 
## **MUST** a) be in England and b) have a wikidata item that has coordinate location!
PREFIX var_start: <https://beyond-notability.wikibase.cloud/entity/Q5>  # (Q5=sheffield)
## to change the distance you can edit the filter(?distance < xx) line near the end of the query.
PREFIX bnwd: <https://beyond-notability.wikibase.cloud/entity/>
PREFIX bnwds: <https://beyond-notability.wikibase.cloud/entity/statement/>
PREFIX bnwdv: <https://beyond-notability.wikibase.cloud/value/>
PREFIX bnwdt: <https://beyond-notability.wikibase.cloud/prop/direct/>
PREFIX bnp: <https://beyond-notability.wikibase.cloud/prop/>
PREFIX bnps: <https://beyond-notability.wikibase.cloud/prop/statement/>
PREFIX bnpq: <https://beyond-notability.wikibase.cloud/prop/qualifier/>
## TODO is there a way to get the location itemLabel and use it in the map title...
#defaultView:Map
SELECT distinct  ?item ?itemLabel ?locationLabel ?location ?wikidata ?wd_geo  ?distance #?wd_long ?wd_lat 
WHERE { 
  var_start: bnwdt:P117 ?start_wd_id .
  bind(iri(concat("http://www.wikidata.org/entity/", str(?start_wd_id))) as ?start_wd) .
 
  {?item bnwdt:P29  ?location . } # resided at location
  #union
  #{?location bnwdt:P12 bnwd:Q2147 .} # OR location has instance of locality. is there a way to get this in the same structure?
  union
  {?item bnwdt:P2  ?location . } # item has property P2 location
  # any other possibles ?
 
  ## limit to places in admin territory p33 england q617. (keep numbers down to avoid wikidata timing out)
  ?location bnwdt:P33+ bnwd:Q617.
  ?location bnwdt:P117 ?ws .  # wikidata id 
    bind(iri(concat("http://www.wikidata.org/entity/", str(?ws))) as ?wikidata) .
 
        SERVICE <https://query.wikidata.org/sparql> {
            ?start_wd wdt:P625 ?start. # start_wd coordinates.
            ?wikidata wdt:P625 ?wd_geo .
        } # /wikidata service 
 
    #BIND(geof:longitude(?wd_geo) AS ?wd_long) .
    #BIND(geof:latitude(?wd_geo)  AS ?wd_lat) . 
    bind(geof:distance(?wd_geo, ?start) as ?distance).
    FILTER(?distance < 50). # < xx km from ?start_wd
 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,en-gb". }
}
ORDER BY ?locationLabel
</sparql>
</sparql>


3,427

edits