Talk:QLever/Example queries

From OpenStreetMap Wiki
Jump to navigation Jump to search

Restrict query to country

How can I restrict this query to one country only? I tried copying from other queries and with Chat-GPT with no luck. --Ivanbranco (talk) 16:55, 20 May 2025 (UTC)

Hello @Ivanbranco: here at line 8 you have an exemple for # Solo elementi dentro Roma (based on OSM db), I hope it can help.--Una tantum (talk) 10:35, 26 May 2025 (UTC)
Here is a query based on items in Italy (based on Wikidata db).
BUT a better search should include items in Italy in both databases, OSM and Wikidata, but I'm not able to improve the query based on both. CC @Danysan: can you help, please? --Una tantum (talk) 10:54, 26 May 2025 (UTC)
Yes, I confirm, the examples you posted are the correct way to filter to a specific region.
As for the query example original request, here is an updated version limited to Italy and returning in output also coordinates from Wikidata:
PREFIX osm: <https://www.openstreetmap.org/>
PREFIX osm2rdfkey: <https://osm2rdf.cs.uni-freiburg.de/rdf/key#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?qid ?label ?way ?coords WHERE {
  ?qid wdt:P17 wd:Q38; # https://www.wikidata.org/wiki/Q38 = Italy
       wdt:P10689 ?osm .
  OPTIONAL {
    ?qid rdfs:label ?label .
    FILTER(LANG(?label) = "en")
  }
  OPTIONAL {
    ?qid wdt:P625 ?coords.
  }
  BIND(IRI(CONCAT("https://www.openstreetmap.org/way/", ?osm)) AS ?way)
  FILTER NOT EXISTS {
    SERVICE <https://qlever.cs.uni-freiburg.de/api/osm-planet> {
      osmrel:365331 ogc:sfContains ?way . # https://www.openstreetmap.org/relation/365331 = Italy
      ?way rdf:type osm:way .
      ?way osm2rdfkey:wikidata ?qid .
    }
  }
}
Run it (edit query)
--Danysan (talk) 07:15, 27 May 2025 (UTC)