I want to extract items that are located within 20 km of the given latitude and longitude. Below I provide my query. The only issue that it has is related to ?lat
and ?long
. They have the format like this "41.3823035"^^xsd:decimal
. In the ontology file, latitude and longitude have the format xsd:float
.
When I run the query, I get the error:
Error 500: Infinite or NaN
Fuseki - version 2.4.1 (Build date: 2016-11-04T18:59:20+0000)
If I substitute ?lat
by 41.38, and ?long
by 2.22
, then it works.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.test.com/my-ontology.owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX oo: <http://www.w3.org/2002/07/owl#>
PREFIX oa: <http://www.w3.org/ns/oa#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX lfn: <http://www.dotnetrdf.org/leviathan#>
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>
SELECT DISTINCT ?id ?kwds ?lat WHERE {
?p owl:id 123 .
?p rdf:type ?service .
?service rdfs:subClassOf ?family .
?otherService rdfs:subClassOf ?family .
?item rdf:type ?otherService .
?item owl:id ?id .
?item owl:latitude ?lat .
?item owl:longitude ?long .
BIND ((lfn:degrees-to-radians(?lat)) AS ?lat1) .
BIND ((lfn:degrees-to-radians(?long)) AS ?lon1) .
BIND ((lfn:degrees-to-radians(41.384697)) AS ?lat2) .
BIND ((lfn:degrees-to-radians(2.150849)) AS ?lon2) .
BIND ((?lon2-?lon1) AS ?dlon) .
BIND ((?lat2-?lat1) AS ?dlat) .
BIND ((lfn:sq(lfn:sin(?dlat/2))+lfn:cos(?lat1)*lfn:cos(?lat2)*lfn:sin(?dlon/2) ) AS ?a) .
BIND ((2*lfn:sin-1(lfn:sqrt(?a))) AS ?c) .
BIND ((xsd:float(fn:round((6371*?c)))) AS ?dist) .
FILTER ( ?dist < 25 ) .
}
UPDATE
The problem is caused by this line BIND ((2*lfn:sin-1(lfn:sqrt(?a))) AS ?c) .
. For some values of lat
and lon
(0.0) it returns NaN
.
I solved this issue by adding IF
to the BIND
statement:
BIND (IF(?c != "NaN"^^xsd:double,xsd:float(fn:round((6371*?c))),999999) AS ?dist)