Search code examples
sparqlrdfstardoggeosparql

How to calculate distance between two points using geosparql


I'm trying to calculate the distance between two points using geosparql. I have objects like the following image (same properties, different values):

enter image description here

And I'm executing that query in sparql:

PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geosf: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>

SELECT ?distance
WHERE {
    ?wifipoint1 :hasGeometry ?geo1 .
    ?geo1 geos:asWKT ?wpoint1 .
    FILTER sameterm(?wifipoint1, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree103>) 
    ?wifipoint2 :hasGeometry ?geo2 .
    ?geo2 geos:asWKT ?wpoint2 .
    FILTER sameterm(?wifipoint2, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree105>) .
    ?distance geosf:distance(?wpoint1 ?wpoint2 <http://qudt.org/vocab/unit#Kilometer>)
}

Without adding the distance, I'm able to get the following result:

enter image description here

But at the moment I add the distance I get empty rows. Any idea?

Notes:

  • I need to calculate the distance between two wifipoints (NYWifiFree103 and NYWifiFree105) which have each one a point.

  • I'm executing that queries in stardog.

** EDIT **

I simplified the query:

PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>

SELECT (geof:distance(?wpoint1, ?wpoint2, <http://qudt.org/vocab/unit#Kilometer>) as ?distance)
WHERE {
    ?wifipoint1 :hasGeometry ?geo1 .
    ?geo1 geos:asWKT ?wpoint1 .
    FILTER sameterm(?wifipoint1, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree103>) .
    ?wifipoint2 :hasGeometry ?geo2 .
    ?geo2 geos:asWKT ?wpoint2 .
    FILTER sameterm(?wifipoint2, <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#NYWifiFree105>)
}

When I set in geof:distance two harcoded wktLiteral returns me correct distance, but using Points does not return nothing.


Solution

  • The geof:distance function accepts Geometries as its first two parameters. So with your simplified query, using geof:distance(?geo1, ?geo2, unit:Kilometer) should give you the result you desire. The Stardog Blog has a geospatial primer post that you may find useful.