Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
Jena 3.13.1
-
None
Description
the `spatial:greatCircle` function appears broken - sometimes returning negative numbers. returning negative numbers can never be correct from a distance function - but I've tried to produce something more useful than that as a defect report and repro...
I was trying to "port" some SPARQL queries where I'd written the great circle function (Haversine formula) to use the spatial:greatCircle function - which means I had a pretty good test case to see where the issue arises. I simplified my query down to a repeatable test that demonstrates the issue:
note that in my original query, I was using a custom jena function to compute `radians` - since that function won't be available to you in stock jena to try this out, I've precomputed the radians values and put them in the VALUES block next to lat/lon pairs. This should be runnable on any stock Jena instance with the jena-geosparql functions loaded. Note that for most of the distances computed, the two distances agree quite closely - but for the last two, the jena function returns a negative number, where the hand-computed value is a correct distance between those points
PREFIX m: <http://www.w3.org/2005/xpath-functions/math#> PREFIX spatial: <http://jena.apache.org/function/spatial#> # this is a namespace of custom functions, which won't be available in stock Jena - to reproduce # this, I've pre-computed the radian values and included them in a VALUES block below... # PREFIX f: <http://data.world/function/functions#> SELECT ?lat1 ?lon1 ?lat2 ?lon2 ?φ1 ?φ2 ?Δφ ?Δλ ?d1 ?d2 WHERE { VALUES (?lat1 ?lon1 ?lat2 ?lon2 ?φ1 ?φ2 ?Δφ ?Δλ) { # in my original query, I was computing radians using a custom radians function, which won't be available # in stock Jena, so to make this reproducible I precomputed the radians values needed for the Haversine (41.2572 -95.9656 41.2592 -95.9339 0.7201 0.7201 0.0000 0.0006) (41.2572 -95.9656 41.2482 -96.072 0.7201 0.7199 -0.0002 -0.0019) (41.2572 -95.9656 41.5871 -93.626 0.7201 0.7258 0.0058 0.0408) (41.2572 -95.9656 51.0472 -113.9998 0.7201 0.8909 0.1709 -0.3148) (41.2572 -95.9656 40.7528 -73.9876 0.7201 0.7113 -0.0088 0.3836) (41.2572 -95.9656 49.7237 13.3422 0.7201 0.8678 0.1478 1.9078) (41.2572 -95.9656 -33.9065 18.4175 0.7201 -0.5918 -1.3119 1.9964) (41.2572 -95.9656 -33.8646 151.2099 0.7201 -0.5910 -1.3111 4.3140) } # calculate the "great circle" distance between the two (lat φ, long λ) points, in kilometers. # these are the function calls in my original query, commented out and replaced with VALUES above # BIND (f:radians(?lat1) AS ?φ1) # BIND (f:radians(?lat2) AS ?φ2) # BIND (f:radians(?lat2 - ?lat1) AS ?Δφ) # BIND (f:radians(?lon2 - ?lon1) AS ?Δλ) BIND (m:sin(?Δφ / 2) * m:sin(?Δφ / 2) + m:cos(?φ1) * m:cos(?φ2) * m:sin(?Δλ / 2) * m:sin(?Δλ / 2) AS ?a) BIND (2 * m:atan2(m:sqrt(?a), m:sqrt(1 - ?a)) AS ?c) BIND (6371 AS ?RadiusOfEarthInKm) BIND (?RadiusOfEarthInKm * ?c AS ?d1) # call the Jena function for comparison BIND(spatial:greatCircle(?lat1, ?lon1, ?lat2, ?lon2, <http://www.opengis.net/def/uom/OGC/1.0/kilometre>) AS ?d2) }