Search code examples
sparqlsemantic-websesame

SPARQL trigonometric functions


I am using Sesame server for storing and querying sets of triples. In my repository I have sets of triples that represent locations on Earth defined by longitude and latitude.

Example:

PREFIX ont:<http://example.org/myontology.owl#>

<http://foo.com/location-a> a ont:Location.
<http://foo.com/location-a> ont:hasLatitude "25.91239"^^xsd:double.
<http://foo.com/location-a> ont:hasLongitude "30.3911"^^xsd:double.

<http://foo.com/location-b> a ont:Location.
<http://foo.com/location-b> ont:hasLatitude "15.7778"^^xsd:double.
<http://foo.com/location-b> ont:hasLongitude "13.6755"^^xsd:double.

...

I want to write a query that gives me back all the locations that are on the surface of a circle defined by it's center point (Latidude_Center, Longitude_Center) and a radius (in km or degrees).

To achieve this thing I have to use some math formulas that imply trigonometric functions. As far as I know SPARQL does not support trigonometric functions.

Is there another way for creating this functionality?


Solution

  • You can add custom functions to Sesame's SPARQL engine yourself, programmaticaly. I wrote a tutorial on how to do this some time ago. The gist of it is:

    1. create a class for your function that implements the org.openrdf.query.algebra.evaluation.function.Function interface.

    For example:

    public class SinusFunction implements Function {
    
         /** return the name of the function for use in SPARQL */
         public String getURI() {
              return "http://example.org/function/sin";
         }
    
         public Value evaluate(ValueFactory valueFactory, Value... args)
             throws ValueExprEvaluationException
         {
               // TODO implement computing the function return value
               // based on the input args 
         }
    }
    
    1. create a jar with a Service Provider Interface (SPI) registry configuration for your custom function(s).

    This involves having a file called org.openrdf.query.algebra.evaluation.function.Function in the META-INF/services directory inside your jar file. The contents of this file should be the fully-qualified names of each of your function implementations, one per line.

    1. Drop your jar on Sesame Server's runtime classpath somewhere, and restart.