sparql-examples

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

View the Project on GitHub AmsterdamUMC/sparql-examples

NGRC_007

Park density in the radius around person's regular visiting places add optional in order to include all values, even if there is no parks, or ?start is no existing.

Use at



#https://qlever.dev/osm-planet/MUADuu
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:>

SELECT ?zipcode_start
    (COUNT(?park) AS ?parkCount)
    (IF(COUNT(?park)=0, 0, COUNT(?park)/(3.14*500*500)) AS ?parkDensity)
WHERE {
    # 确保每个邮编都有行
    VALUES ?zipcode_start {'1033SC' '1095MS' '1012AB'}

    # 可选的起点节点
    OPTIONAL { ?start osmKey:addr:postcode ?zipcode_start ;
                   osmKey:name ?name_start ;
                   geo:hasGeometry ?geom_start .
               ?geom_start geo:asWKT ?wkt_start .

               # 可选的公园节点
               OPTIONAL { ?park osmkey:leisure "park" ;
                                geo:hasGeometry/geo:asWKT ?park_geometry .
                          FILTER(geof:distance(?wkt_start, ?park_geometry) < 500)
              }
             }
      }
GROUP BY ?zipcode_start
ORDER BY ?zipcode_start

graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v6("?geom_start")
  v5("?name_start")
  v7("?park"):::projected 
  v8("?parkCount")
  v9("?parkDensity")
  v3("?park_geometry")
  v4("?start")
  v2("?wkt_start")
  v2("?zipcode_start"):::projected 
  a1((" "))
  c7(["park"]):::literal 
  bind0[/VALUES ?zipcode_start/]
  bind0-->v2
  bind00(["1033SC"])
  bind00 --> bind0
  bind01(["1095MS"])
  bind01 --> bind0
  bind02(["1012AB"])
  bind02 --> bind0
  subgraph optional0["(optional)"]
  style optional0 fill:#bbf,stroke-dasharray: 5 5;
    v4 -.https://www.openstreetmap.org/wiki/Key:addr:postcode.->  v2
    v4 --https://www.openstreetmap.org/wiki/Key:name-->  v5
    v4 --http://www.opengis.net/ont/geosparql#hasGeometry-->  v6
    v6 --http://www.opengis.net/ont/geosparql#asWKT-->  v2
    subgraph optional1["(optional)"]
    style optional1 fill:#bbf,stroke-dasharray: 5 5;
      v7 -.https://www.openstreetmap.org/wiki/Key:leisure.->  c7
      v7 --http://www.opengis.net/ont/geosparql#hasGeometry-->  a1
      a1 --http://www.opengis.net/ont/geosparql#asWKT-->  v3
    end
  end
  bind4[/"count(?park)"/]
  v7 --o bind4
  bind4 --as--o v8
  bind5[/"if( = '0^^xsd:integer','0^^xsd:integer', / '3.14^^xsd:decimal' * '500^^xsd:integer' * '500^^xsd:integer')"/]
  null --o bind5
  null --o bind5
  bind5 --as--o v9