sparqlwktrdf4jgeosparql

GeoSparql functions not working as expected in RDF4J's repository


Colleagues and I are working with RDF dataset and RDF4J's in-Memory repository and we are trying to execute GeoSparql queries while using geometry data in WKT format, as shown below:

@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix sf: <http://www.opengis.net/ont/sf> .
@prefix ex: <http://example.org/> .
@prefix geof: <http://www.opengis.net/def/function/geosparql/>.

ex:eiffelTower a ex:Landmark ;
        geo:hasGeometry ex:coordinates-et.
ex:coordinates-et a sf:Point;
        geo:asWKT "POINT(2.2945 48.8584)"^^geo:wktLiteral .

ex:towerBridge a ex:Landmark ;
          geo:hasGeometry ex:coordinates-tb.
ex:coordinates-tb a sf:Point;
        geo:asWKT "POINT(-0.0754 51.5055)"^^geo:wktLiteral .

Scenario-1: So far we managed to successfully use geof:distance function, e.g. of a query and it's result are given below. Intention was to calculate the distance between the coordinates of two landmarks.

SELECT *
WHERE {
  ?lmA a ex:Landmark ;
       geo:hasGeometry [ geo:asWKT ?coord1 ].

  ?lmB a ex:Landmark ;
       geo:hasGeometry [ geo:asWKT ?coord2 ].

  BIND((geof:distance(?coord1, ?coord2, uom:metre)/1000) as ?dist) .
  FILTER (str(?lmA) < str(?lmB))
}

Result of Scenario-1:

lmA => ex:eiffelTower
coord1 => "POINT(2.2945 48.8584)"
lmB =>  ex:towerBridge
coord2 => "POINT(-0.0754 51.5055)"
dist => "339.2412973915987"

Scenario-2: When we tried to use other functions, e.g. geof:sfWithin, geof:sfContains, we ran into some strange behavior. The query with geof:sfWithin example and it's result are shown below. Intention was to get all the points that are positioned inside the given polygon.

SELECT *
WHERE {
  ?lmA a ex:Landmark ;
       geo:hasGeometry ?Geom.

  ?Geom geo:asWKT ?WKT.

  FILTER(geof:sfWithin(?WKT, '''
        <http://www.opengis.net/def/crs/OGC/1.3/CRS84>
            Polygon ((80.0 80.0, -80.0 80.0, -80.0 -80.0, 80.0 -80.0, 80.0 80.0))
        '''^^geo:wktLiteral))
}

Result: -empty table-

Are we doing something wrong in our queries? Our dependencies:


Solution

  • I think the problem is the syntax of your polygon wkt literal. You've got:

    <http://www.opengis.net/def/crs/OGC/1.3/CRS84>
    Polygon ((80.0 80.0, -80.0 80.0, -80.0 -80.0, 80.0 -80.0, 80.0 80.0))
    

    I'm not sure where that URI on the first line comes from (I'm not really a GeoSPARQL expert) but that does not look like a proper WKT literal to me. I tried your query with

    Polygon ((80.0 80.0, -80.0 80.0, -80.0 -80.0, 80.0 -80.0, 80.0 80.0))
    

    instead, and got the following two results:

    lmA=http://example.org/eiffelTower
    WKT="POINT(2.2945 48.8584)"^^<http://www.opengis.net/ont/geosparql#wktLiteral>
    Geom=http://example.org/coordinates-et
    
    lmA=http://example.org/towerBridge
    WKT="POINT(-0.0754 51.5055)"^^<http://www.opengis.net/ont/geosparql#wktLiteral>
    Geom=http://example.org/coordinates-tb
    

    Admittedly, the query silently failing rather than producing some "could not process WKT literal" error or warning is confusing .

    Update:

    Looking a bit further (see also the comments), this is actually a bug in RDF4J. As section 8.5 in the GeoSPARQL spec indicates the prefixing URI for the reference system is not part of the WKT spec itself, but is a GeoSPARQL-specific addition to the definition of a WKT Literal. So RDF4J needs to take care of preprocessing that before handing it off to the underlying WKTReader. Bug ticket created as https://github.com/eclipse/rdf4j/issues/2935.

    As a workaround, you can still get away with removing the URI, as CRS84 is actually the default reference system anyway.