sparql-examples

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

View the Project on GitHub AmsterdamUMC/sparql-examples

NGRC_009

Park density in PC6

Use at


PREFIX osmKey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
SELECT ?postcode4 ?total_area ?parkCount ((?parkCount/?total_area)AS ?park_density)
    WHERE {
    PREFIX osmKey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osmrel: <https://www.openstreetmap.org/relation/>
PREFIX ogc: <http://www.opengis.net/rdf#>
PREFIX osm2rdf: <https://osm2rdf.cs.uni-freiburg.de/rdf#>
SELECT ?postcode6 ?total_area ?parkCount ((?parkCount/?total_area)AS ?park_density)
    WHERE {
    # calculate the area of postcode 4
    {
      SELECT ?postcode6 (SUM(?area) AS ?total_area)
          WHERE {
             osmrel:47811 ogc:sfContains ?way1 .
             ?way1 osmkey:addr:postcode ?p1 .
             FILTER(REGEX(?p1, "^[0-9,A-Z]{6}")) .
             BIND(SUBSTR(?p1,1,6) AS ?postcode6) .
             ?way1 osm2rdf:area ?area .
             }
      GROUP BY ?postcode6
      }
      # calculate the count of the parks in postcode 4
    OPTIONAL {
      SELECT (COUNT(?park_geometry) AS ?parkCount)
          WHERE {
             ?park osmkey:leisure "park" .
             ?park geo:hasGeometry/geo:asWKT ?park_geometry .
          }
      GROUP BY ?postcode6
    }
}
ORDER BY DESC(?park_density)