QLever/Example queries

From OpenStreetMap Wiki
Jump to navigation Jump to search

Basic 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 (using a property path) 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 specific boundary relation

Returns nodes tagged office=newspaper in the relation Philippines. QLever UI can autocomplete the relation ID when you type in its name.

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)


leisure=park centroids

Returns the centroid of every element tagged leisure=park and name=* anywhere in the world, regardless of its element type:

PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?park osmkey:leisure "park" .
  ?park osmkey:name ?name .
  ?park geo:hasCentroid/geo:asWKT ?centroid .
}

Run it (edit query)


man_made=maypole outside two boundaries

Returns the location of every maypole (man_made=maypole) that lies outside of both relation Germany and relation Austria. A MINUS clause allows you to filter the results by a negative criterion. Combining both ogc:sfIntersects expressions into the same MINUS clause would result in the reverse: all the maypoles of Germany and Austria. However, this is usually written as a UNION clause instead.

PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?maypole osmkey:man_made "maypole" .
  ?maypole geo:hasGeometry/geo:asWKT ?geometry .
  MINUS {
    osmrel:51477 ogc:sfIntersects ?maypole .
  }
  MINUS {
    osmrel:16239 ogc:sfIntersects ?maypole .
  }
}

Run it (edit query)


emergency=siren near amenity=fire_station

Returns the location of every outdoor warning siren (emergency=siren) within 30 metres (98 ft) of a fire station (amenity=fire_station):

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT * WHERE {
  ?station osmkey:amenity "fire_station" .
  ?station geo:hasCentroid/geo:asWKT ?station_geometry .
  ?siren osmkey:emergency "siren" .
  ?siren geo:hasCentroid/geo:asWKT ?siren_geometry .
  ?siren_geometry <max-distance-in-meters:30> ?station_geometry .
}

Run it (edit query)


Distance between two points

Returns the distance from node New York City to Null Island in kilometers and miles. By definition, the location of Null Island is at 0 degrees latitude, 0 degrees longitude. As of January 2025, Null Island is node no longer mapped in OSM, so we have to hard-code the coordinates as a well-known text literal.

PREFIX osmnode: <https://www.openstreetmap.org/node/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT * WHERE {
  osmnode:61785451 geo:hasGeometry/geo:asWKT ?from .
  BIND("POINT(0 0)"^^geo:wktLiteral AS ?to)
  BIND(geof:distance(?from, ?to) AS ?kilometers)
  BIND(?kilometers * 0.62137 AS ?miles)
}

Run it (edit query)


Boundaries

Most convoluted municipalities in Oklahoma

Many cities and towns in the U.S. state of relation Oklahoma have extremely complex boundaries. A practice known as fenceline annexation results in many narrow appendages that snake around adjacent unincorporated territory to preempt annexation by competing municipalities. [1] This query ranks Oklahoma's municipal boundaries according to the Polsby–Popper test of compactness. It relies on two subqueries, one for boundary relations and another for old-style boundary areas. In countries where electoral boundaries have been mapped, a similar query could reveal the most gerrymandered electoral districts.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX osm: <https://www.openstreetmap.org/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
PREFIX osm2rdfmember: <https://osm2rdf.cs.uni-freiburg.de/rdf/member#>
SELECT ?boundary ?name ?compactness WHERE {
  {
	# Query for municipal boundaries in Oklahoma that are modeled as relations
	# Sum the lengths of the member ways as the perimeter
    SELECT ?boundary (SUM(?length) AS ?perimeter) WHERE {
	  osmrel:161645 ogc:sfIntersects ?boundary .
	  ?boundary osmkey:boundary "administrative" .
	  ?boundary osmkey:admin_level "8"^^xsd:int .
	  ?boundary rdf:type osm:relation .
	  # Get the members that form the interior and exterior rings of the multipolygon geometry
	  VALUES ?ring { "outer" "inner" }
	  ?boundary osmrel:member [ osm2rdfmember:id ?way ; osm2rdfmember:role ?ring ] .
	  ?way osm2rdf:length ?length .
    }
	GROUP BY ?boundary
  } UNION {
	# Also query for those that are modeled as closed ways
	SELECT ?boundary (?length AS ?perimeter) WHERE {
	  osmrel:161645 ogc:sfIntersects ?boundary .
	  ?boundary osmkey:boundary "administrative" .
	  ?boundary osmkey:admin_level "8"^^xsd:int .
	  ?boundary rdf:type osm:way .
	  ?boundary osm2rdf:length ?length .
	  # Exclude unclosed boundary ways, which are likely overtagged
	  ?boundary osm2rdf:area ?area .
	  # Exclude unnamed boundaries, which are likely overtagged ways
	  ?boundary osmkey:name ?name .
    }
  }
  # Both boundary relations and closed boundary ways have area
  ?boundary osm2rdf:area ?area .
  ?boundary osmkey:name ?name .
  # Polsby–Popper test
  BIND(4 * 3.14159 * ?area / ?perimeter / ?perimeter AS ?compactness)
}
ORDER BY ?compactness

Run it (edit query)


Nature

Straightest rivers

Returns the straightest rivers in relation Alaska, that is, the rivers with the lowest sinuosity index. The channel length is precomputed by osm2rdf (in degrees of arc); the downvalley length is computed by taking the straight-line distance from the source to the mouth (in kilometers). The difference in units makes each index value meaningless on its own but still useful for ranking the rivers. This query only considers individual waterway=river ways. A more rigorous query would consider a type=waterway relation's entire set of Role main_stream members from source to mouth. Order by descending ?sinuosity_index to find meandering rivers and oxbows.

PREFIX osmway: <https://www.openstreetmap.org/way/>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
PREFIX osm2rdfmember: <https://osm2rdf.cs.uni-freiburg.de/rdf/member#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?river ?channel_length ?downvalley_length ?sinuosity_index WHERE {
  {
    SELECT ?river ?source_geometry ?mouth_geometry WHERE {
	  # Compute the last vertex position of each river
	  {
		SELECT ?river (COUNT(?vertex) - 1 as ?last_pos) WHERE {
		  ?river osmkey:waterway "river" .
		  osmrel:1116270 ogc:sfIntersects ?river .
		  ?river osmway:node ?vertex .
		}
		GROUP BY ?river
	  }
	  ?river osmkey:waterway "river" .
	  osmrel:1116270 ogc:sfIntersects ?river .
	  ?river osmway:node [ osmway:node ?first_node ; osm2rdfmember:pos "0"^^xsd:int ] .
	  ?first_node geo:hasGeometry/geo:asWKT ?source_geometry .
	  ?river osmway:node [ osmway:node ?last_node ; osm2rdfmember:pos ?last_pos ] .
	  ?last_node geo:hasGeometry/geo:asWKT ?mouth_geometry .
    }
  }
  ?river osm2rdf:length ?channel_length .
  BIND(geof:distance(?source_geometry, ?mouth_geometry) AS ?downvalley_length)
  BIND(?channel_length / ?downvalley_length AS ?sinuosity_index)
}
ORDER BY ?sinuosity_index

Run it (edit query)


Transportation

Street names in a city

Returns an alphabetical list of the names of all the streets that lie partially or wholly within relation Wellington City, New Zealand, listing each name only once regardless of how many highway=* ways bear the same name:

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX osm: <https://www.openstreetmap.org/>
PREFIX ogc: <http://www.opengis.net/rdf#>
SELECT ?name WHERE {
  # Wellington City
  osmrel:4266321 ogc:sfIntersects ?street .
  VALUES ?street_types {
    # highway=* values that represent streets.
    "motorway" "motorway_link"
	"trunk" "trunk_link"
	"primary" "primary_link"
	"secondary" "secondary_link"
	"tertiary" "tertiary_link"
	"unclassified" "service"
	"residential" "living_street"
	"pedestrian" "busway"
	"proposed" "construction"
  }
  ?street osmkey:highway ?street_types .
  ?street rdf:type osm:way .
  ?street osmkey:name ?name .
}
GROUP BY ?name
ORDER BY ASC(?name)

Run it (edit query)


Street intersections in a city

Returns every street intersection within the city of relation Chicago, respecting topology. For the purpose of this query, a street intersection is where multiple highway=* ways having two or more distinct names are connected at a single node. Name changes are a useful heuristic because a roadway can be split at any point for any reason or none at all. The node does not need to be tagged with highway=* or junction=*; it can have no tags at all. There are multiple "intersections" wherever a dual carriageway crosses another street. This query looks for the intersection nodes in a subquery that is conditionally aggregated, then adds the nodes' geometries. This query performs considerably better than an equivalent OverpassQL query; by replacing the relation ID, you can query for intersections across an entire country. [2]

PREFIX osmway: <https://www.openstreetmap.org/way/>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?intersection ?street_names ?geometry WHERE {
  {
    SELECT ?intersection (GROUP_CONCAT(DISTINCT ?name; SEPARATOR=" at ") AS ?street_names) WHERE {
	  # Chicago
      osmrel:122604 ogc:sfContains ?street .
      ?street osmkey:highway [] .
      ?street osmkey:name ?name .
      ?street osmway:node/osmway:node ?intersection .
    }
    GROUP BY ?intersection
    HAVING (COUNT(DISTINCT ?name) >= 2)
  }
  ?intersection geo:hasGeometry/geo:asWKT ?geometry .
}

Run it (edit query)


Longest straightaways

Returns the most distant pairs of vertices along highway=trunk ways in California. The road segment between each pair is guaranteed to be completely straight, at least in OSM. Unsurprisingly, some of the longest straightaways are in the Mojave Desert. This query may miss some even longer straightaways that are interrupted by extra nodes placed at arbitrary locations for any reason. The query works by getting every consecutive pair of vertices and calculating the distance between them, then ranking the distances. The geof:distance() function accepts two well-known text literals, returning the distance in kilometers. QLever currently only supports passing point geometries into this function.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmway: <https://www.openstreetmap.org/way/>
PREFIX osm2rdfmember: <https://osm2rdf.cs.uni-freiburg.de/rdf/member#>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
SELECT ?highway ?pos1 ?node1 ?pos2 ?node2 ?kilometers ?miles WHERE {
  ?highway osmkey:highway "trunk" .
  osmrel:165475 ogc:sfIntersects ?highway .
  ?highway osmway:node [ osmway:node ?node1 ; osm2rdfmember:pos ?pos1 ] .
  ?highway osmway:node [ osmway:node ?node2 ; osm2rdfmember:pos ?pos2 ] .
  FILTER(?pos1 + 1 = ?pos2)
  ?node1 geo:hasGeometry/geo:asWKT ?geometry1 .
  ?node2 geo:hasGeometry/geo:asWKT ?geometry2 .
  BIND(geof:distance(?geometry1, ?geometry2) AS ?kilometers)
  BIND(?kilometers * 0.62137 AS ?miles)
}
ORDER BY DESC(?kilometers)
LIMIT 100

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=*. [3]

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)


Gaps in historic highway routes

Returns each discontinuity in a route=historic relation. Some gaps may be legitimate due to how the route was originally designated or because segments have become abandoned over time, leaving no trace on the landscape. This query does not consider roles such as Role forward and Role backward, which may also create what appear to be discontinuities.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osm2rdfmember: <https://osm2rdf.cs.uni-freiburg.de/rdf/member#>
SELECT DISTINCT ?route ?pos ?highway ?next_highway WHERE {
  # Compute the last member position of each historic route
  {
    SELECT ?route (MAX(?pos) AS ?last_pos) WHERE {
	  ?route osmkey:route "historic" .
	  ?route osmrel:member [ osm2rdfmember:id ?last_highway ; osm2rdfmember:pos ?pos ] .
	  ?last_highway osmkey:highway [] .
    }
	GROUP BY ?route
  }
  
  # Get each member of a historic route
  ?route osmkey:route "historic" .
  ?route osmrel:member [ osm2rdfmember:id ?highway ; osm2rdfmember:pos ?pos ] .
  # as long as it’s a highway
  ?highway osmkey:highway [] .
  # and it isn’t the route’s last member
  MINUS {
	?route osmkey:route "historic" .
	?route osmrel:member [ osm2rdfmember:id ?highway ; osm2rdfmember:pos ?last_pos ] .
  }
  # Get the route’s next member
  ?route osmrel:member [ osm2rdfmember:id ?next_highway ; osm2rdfmember:pos ?next_pos ] .
  FILTER(?next_pos = ?pos + 1)
  # as long as the two members don’t touch
  MINUS {
	?route osmkey:route "historic" .
	?route osmrel:member/osm2rdfmember:id ?next_highway .
    ?highway ogc:sfTouches ?next_highway .
  }
}
ORDER BY ?route ?pos

Run it (edit query)


Highway routes that double back on themselves

Returns any route=road relation that has two distinct members that overlap each other, a likely mapping error:

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osm2rdfmember: <https://osm2rdf.cs.uni-freiburg.de/rdf/member#>
SELECT * WHERE {
  ?route osmkey:route "road" .
  ?route osmkey:network ?network .
  ?route osmkey:ref ?ref .
  ?route osmrel:member/osm2rdfmember:id ?highway1 .
  ?highway1 osmkey:highway [] .
  ?route osmrel:member/osm2rdfmember:id ?highway2 .
  ?highway2 osmkey:highway [] .
  ?highway1 ogc:sfOverlaps ?highway2 .
  ?highway1 geo:hasGeometry/geo:asWKT ?geometry .
}
ORDER BY ?network ?ref

Run it (edit query)


Demolished buildings already mapped in OpenHistoricalMap

Returns buildings that have been demolished but remain in OpenStreetMap as demolished:building=* areas despite having already been mapped in OpenHistoricalMap for safekeeping. The query matches OHM and OSM buildings based on centroids 10 metres (33 ft) or less apart. See also this similar query based on Wikidata.

PREFIX osm2rdfkey: <https://osm2rdf.cs.uni-freiburg.de/rdf/key#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?osm_building ?ohm_building ?end_date ?ohm_geometry WHERE {
  ?osm_building osmkey:demolished:building [] .
  ?osm_building geo:hasCentroid/geo:asWKT ?osm_centroid .
  ?osm_centroid <max-distance-in-meters:10> ?ohm_centroid .
  SERVICE <https://qlever.cs.uni-freiburg.de/api/ohm-planet> {
	?ohm_building osmkey:building [] .
	?ohm_building geo:hasCentroid/geo:asWKT ?ohm_centroid .
	?ohm_building geo:hasGeometry/geo:asWKT ?ohm_geometry .
	?ohm_building osm2rdfkey:end_date ?end_date .
  }
}
ORDER BY ?end_date

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)


Most prevalent cuisines

This query returns the 100 most prevalent cuisines. The cuisine=* key accepts a list of keywords separated by semicolons. osm2rdf does not automatically split value lists by semicolons, but you can split them yourself using a regular expression:

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT (SAMPLE(?value) AS ?cuisine) (COUNT(*) AS ?count) WHERE {
  ?osm osmkey:cuisine ?list .
  BIND(REPLACE(?list, ".*?([^;]+).*", "\\1") AS ?value)
}
GROUP BY ?value
ORDER BY DESC(?count)
LIMIT 100

Run it (edit query)


Things that mimic chimneys

For each common key or tag, taginfo reports statistics about which tags or keys are used in combination with it; however, it does not track combinations of relatively rare keys or tags. The following query returns what taginfo's Combinations tab would report for mimics=chimney if it were a much more common tag, so you can find out what structures typically mimic chimneys and what else we know about these structures. It requires running two subqueries about tags and unioning the results to two subqueries about keys.

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT ?key ?value ?count (100 * ?count / ?count_all AS ?percent) WHERE {
  {
    # Count occurrences of tags on elements that also have the input tag
	{
	  SELECT ?key ?value (COUNT(?key) AS ?count) WHERE {
		?element osmkey:mimics "chimney" .
		?element ?key ?value .
	  }
	  GROUP BY ?key ?value
	}
    # Count elements that have the input tag
	{
	  SELECT (COUNT(DISTINCT ?element) AS ?count_all) WHERE {
		?element osmkey:mimics "chimney" .
		?element ?key ?value .
	  }
	}
  } UNION {
    # Count occurrences of keys on elements that also have the input tag
	{
	  SELECT ?key (COUNT(?key) AS ?count) WHERE {
		?element osmkey:mimics "chimney" .
		?element ?key ?value .
	  }
	  GROUP BY ?key
	}
    # Count elements that have the input tag
	{
	  SELECT (COUNT(DISTINCT ?element) AS ?count_all) WHERE {
		?element osmkey:mimics "chimney" .
		?element ?key ?value .
	  }
	}
  }
  # Filter out metadata predicates (e.g., rdf, osm2rdf, ogc prefixes)
  FILTER(STRSTARTS(STR(?key), "https://www.openstreetmap.org/wiki/"))
}
ORDER BY DESC(?count)

Run it (edit query)


Nodes that mimic chimneys in Portugal

This query returns the things that mimic chimneys but only considers nodes that fall inside the Portugal boundary relation:

PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX osm: <https://www.openstreetmap.org/>
SELECT ?key ?value ?count (100 * ?count / ?count_all AS ?percent) WHERE {
  {
    # Count occurrences of tags on elements that also have the input tag
	{
	  SELECT ?key ?value (COUNT(?key) AS ?count) WHERE {
		?element osmkey:mimics "chimney" .
		?element rdf:type osm:node .
		osmrel:295480 ogc:sfIntersects ?element .
		?element ?key ?value .
	  }
	  GROUP BY ?key ?value
	}
    # Count elements that have the input tag
	{
	  SELECT (COUNT(DISTINCT ?element) AS ?count_all) WHERE {
		?element osmkey:mimics "chimney" .
		?element rdf:type osm:node .
		osmrel:295480 ogc:sfIntersects ?element .
		?element ?key ?value .
	  }
	}
  } UNION {
    # Count occurrences of keys on elements that also have the input tag
	{
	  SELECT ?key (COUNT(?key) AS ?count) WHERE {
		?element osmkey:mimics "chimney" .
		?element rdf:type osm:node .
		osmrel:295480 ogc:sfIntersects ?element .
		?element ?key ?value .
	  }
	  GROUP BY ?key
	}
    # Count elements that have the input tag
	{
	  SELECT (COUNT(DISTINCT ?element) AS ?count_all) WHERE {
		?element osmkey:mimics "chimney" .
		?element rdf:type osm:node .
		osmrel:295480 ogc:sfIntersects ?element .
		?element ?key ?value .
	  }
	}
  }
  # Filter out metadata predicates (e.g., rdf, osm2rdf, ogc prefixes)
  FILTER(STRSTARTS(STR(?key), "https://www.openstreetmap.org/wiki/"))
}
ORDER BY DESC(?count)

Run it (edit query)


See also