QLever/Example queries

From OpenStreetMap Wiki
Jump to navigation Jump to search

Simple queries

place=*

Returns 50 features tagged place=* anywhere in the world, along with their place=* values. In this query, the place key is the predicate. QLever supports some other kinds of predicates that are not OpenStreetMap keys, so OpenStreetMap keys always begin with the osmkey: prefix. The coordinates are included so you can see the results on a heatmap:

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?element ?name ?placeType ?geometry
WHERE {
  ?element osmkey:place ?placeType ;
           osmkey:name ?name ;
           geo:hasGeometry/geo:asWKT ?geometry .
}
LIMIT 50

Run it (edit query)


office=newspaper nodes

Returns nodes tagged office=newspaper anywhere in the world. You only need to include the rdf:type predicate if you want to display each element's type or restrict the results to elements of a particular type.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osm: <https://www.openstreetmap.org/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?node ?geometry
WHERE {
  ?node osmkey:office "newspaper" ;
        rdf:type osm:node;
        geo:hasGeometry/geo:asWKT ?geometry .
}

Run it (edit query)


office=newspaper within a given country

Returns nodes tagged office=newspaper in the Philippines, which is represented by relation 443174:

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
SELECT ?element ?geometry
WHERE {
  osmrel:443174 ogc:sfContains ?element .
  ?element osmkey:office "newspaper" ;
           geo:hasGeometry/geo:asWKT ?geometry .
}

Run it (edit query)


office=newspaper and name=*

Returns anything that is tagged both office=newspaper and name=* anywhere in the world.

This query is considerably faster than querying for anything tagged name=*, because that tag is extremely common. However, it is only slightly faster than querying for anything tagged office=newspaper. For relatively uncommon tags, it is unnecessary and potentially counterproductive to overspecify the query with many predicates.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT ?element ?name
WHERE {
  ?element osmkey:office "newspaper" ;
        osmkey:name ?name.
}

Run it (edit query)


office=newspaper and optionally name=*

Returns anything that is tagged office=newspaper anywhere in the world. For easier identification, this query displays each feature's name=* if available, but it also includes features without name=*.

To display name=* and name:en=* only when both tags are also present, add a sentence about name:en=* to the OPTIONAL expression below. To display name=* and/or name:en=*, add a separate OPTIONAL expression for name:en=*.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT ?element ?name
WHERE {
  ?element osmkey:office "newspaper".
  OPTIONAL {
    ?element osmkey:name ?name.
  }
}

Run it (edit query)


Quality control

Broken multipolygons

Returns multipolygon relations that don't form closed areas due to a variety of errors:

PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX osm: <https://www.openstreetmap.org/>
SELECT * WHERE {
  ?multipolygon rdf:type osm:relation .
  ?multipolygon osmkey:type "multipolygon" .
  MINUS {
    ?multipolygon osm2rdf:area ?area .
  }
}

Run it (edit query)


Smallest elements tagged landuse=reservoir within a country

This query demonstrates how to identify map elements by their area (by using osm2rdf:area and ORDER BY ASC).

It returns elements with the deprecated tag landuse=reservoir in Portugal, which is represented by relation 295480.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
SELECT ?osm_id ?area WHERE {
  osmrel:295480 ogc:sfContains ?osm_id .
  ?osm_id osmkey:landuse "reservoir" .
  ?osm_id osm2rdf:area ?area.
}
ORDER BY ASC(?area)
LIMIT 100

Run it (edit query)


Not-very-roundabouts

This query ranks junction=roundabout and junction=circular ways by the Polsby–Popper compactness test to find roundabouts that aren't particularly round. Some are variants of the roundabout concept, such as dogbone interchanges, while others are circuits that consist of multiple intersections and shouldn't be tagged as any kind of junction=*. [1]

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  VALUES ?junction_types { "circular" "roundabout" }
  ?roundabout osmkey:junction ?junction_types .
  ?roundabout osm2rdf:area ?area .
  ?roundabout osm2rdf:length ?perimeter .
  ?roundabout geo:hasGeometry/geo:asWKT ?geometry .
  # Polsby–Popper test
  BIND(4 * 3.14159 * ?area / ?perimeter / ?perimeter AS ?compactness)
  OPTIONAL {
    ?roundabout osmkey:name ?name .
  }
}
ORDER BY ASC(?compactness)
LIMIT 100

Run it (edit query)


Tag info

Most prevalent keys in a city

This query returns the most commonly tagged keys in the city of Detroit, Michigan, represented by relation 134591. It excludes non-key predicates that osm2rdf adds, such as metadata about OSM geometries. Geofabrik publishes regional taginfo instances that extend to the state/provincial level, but they don't provide instances for more obscure administrative areas. This query can be adapted for an administrative area at any level or any area feature at all, not necessarily a boundary.

PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT ?p ?count ?percent WHERE {
  {
	SELECT ?p (COUNT(?p) AS ?count) WHERE {
	  ?s ?p ?o .
	  # Detroit
	  osmrel:134591 ogc:sfContains ?s .
	}
	GROUP BY ?p
  }
  FILTER(STRSTARTS(STR(?p), osmkey:))
  BIND(100 * ?count / SUM(?count) AS ?percent)
}
ORDER BY DESC(?count)

Run it (edit query)


Most prevalent keys in a city with distances and areas

This variation on "Most prevalent keys in a city" adds columns to rank the keys by total distance (in ???) or area (in hectares).

PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
SELECT ?p ?count ?count_percent ?length ?length_percent ?area ?area_percent WHERE {
  {
	SELECT ?p (COUNT(?p) AS ?count) (SUM(?l0) AS ?length) (SUM(?a0) AS ?area) WHERE {
	  ?s ?p ?o .
	  osmrel:134591 ogc:sfContains ?s .
	  OPTIONAL {
		?s osm2rdf:length ?l .
      }
	  BIND(COALESCE(?l, 0) AS ?l0)
	  OPTIONAL {
		?s osm2rdf:area ?a .
      }
	  BIND(COALESCE(?a, 0) AS ?a0)
	}
	GROUP BY ?p
  }
  FILTER(STRSTARTS(STR(?p), osmkey:))
  BIND(100 * ?count / SUM(?count) AS ?count_percent)
  BIND(100 * ?length / SUM(?length) AS ?length_percent)
  BIND(100 * ?area / SUM(?area) AS ?area_percent)
}
ORDER BY DESC(?count)

Run it (edit query)


Most prevalent values of highway=* in a city by distance

This variation on "Most prevalent keys in a city" returns values of highway=*, sorting them by total distance.

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
PREFIX osm: <https://www.openstreetmap.org/>
SELECT ?o ?count ?count_percent ?length ?length_percent ?area ?area_percent WHERE {
  {
	SELECT ?o (COUNT(?o) AS ?count) (SUM(?l0) AS ?length) (SUM(?a0) AS ?area) WHERE {
	  ?s osmkey:highway ?o .
	  ?s rdf:type osm:way .
	  osmrel:134591 ogc:sfContains ?s .
	  OPTIONAL {
		?s osm2rdf:length ?l .
      }
	  BIND(COALESCE(?l, 0) AS ?l0)
	  OPTIONAL {
		?s osm2rdf:area ?a .
      }
	  BIND(COALESCE(?a, 0) AS ?a0)
	}
	GROUP BY ?o
  }
  BIND(100 * ?count / SUM(?count) AS ?count_percent)
  BIND(100 * ?length / SUM(?length) AS ?length_percent)
  BIND(100 * ?area / SUM(?area) AS ?area_percent)
}
ORDER BY DESC(?length)

Run it (edit query)


Most heavily tagged elements

This query returns the ten elements with the most tags as recorded by osm2rdf, which does not automatically split values by semicolons. The most heavily tagged features are all countries and their boundary relations, due to their pan-linguistic renown and tendency to have both formal and informal names.

PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
SELECT * WHERE {
  ?element osm2rdf:facts ?facts .
}
ORDER BY DESC(?facts)
LIMIT 10

Run it (edit query)