A set of SPARQL examples that are used in different AMC resources
calculated the distance to the nearest park from one point
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 unit: <http://qudt.org/vocab/unit/>
SELECT ?way ?name ?distance ?geom_for_distance
WHERE {
BIND("POINT (4.895168 52.370216)"^^geo:wktLiteral AS ?origin)
?way osmkey:leisure "park" .
?way osmkey:name ?name .
?way geo:hasGeometry/geo:asWKT ?geom .
# 判断 geometry 类型,如果是 Point 用自身,否则用 centroid
BIND(
IF( STRSTARTS(STR(?geom), "POINT"),
?geom,
geof:centroid(?geom)
) AS ?geom_for_distance
)
BIND( geof:distance(?origin, ?geom_for_distance) AS ?distance )
}
ORDER BY ASC(?distance)
LIMIT 5
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v7("?distance"):::projected
v5("?geom")
v6("?geom_for_distance"):::projected
v4("?name"):::projected
v2("?origin")
v3("?way"):::projected
a1((" "))
c2(["park"]):::literal
bind0[/"sPOINT (4.895168 52.370216)^^<http://www.opengis.net/ont/geosparql#wktLiteral>'"/]
bind0 --as--o v2
v3 --https://www.openstreetmap.org/wiki/Key:leisure--> c2
v3 --https://www.openstreetmap.org/wiki/Key:name--> v4
v3 --http://www.opengis.net/ont/geosparql#hasGeometry--> a1
a1 --http://www.opengis.net/ont/geosparql#asWKT--> v5
bind1[/"if(starts-with(str(?geom),'POINT'),?geom,http://www.opengis.net/def/function/geosparql/centroid(?geom))"/]
v5 --o bind1
bind1 --as--o v6
bind2[/"http://www.opengis.net/def/function/geosparql/distance(?origin,?geom_for_distance)"/]
v2 --o bind2
v6 --o bind2
bind2 --as--o v7