RDF4J offers an extended algebra for partial GeoSPARQL support. When enabled, this offers additional geospatial functionality as part of the SPARQL engine, on top of any RDF4J repository, using the well-known Spatial4J and JTS libraries for geospatial reasoning.
To enable GeoSPARQL support, all you need to do is include the
rdf4j-queryalgebra-geosparql Maven module in your project:
Adding geospatial data to the Repository
By default, RDF4J only supports GeoSPARQL functions on top of geospatial data represented as so-called Well-Known Text (WKT) strings.
For example, to model the coordinates of landmarks like the Eiffel Tower in Paris or the Tower Bridge in London, you could do something like this:
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix sf: <http://www.opengis.net/ont/sf> .
@prefix ex: <http://example.org/> .
ex:eiffelTower a ex:Landmark ;
ex:coordinates-et a sf:Point;
geo:asWKT "POINT(2.2945 48.8584)"^^geo:wktLiteral .
ex:towerBridge a ex:Landmark ;
ex:coordinates-tb a sf:Point;
geo:asWKT "POINT(-0.0754 51.5055)"^^geo:wktLiteral .
After adding this data to a repository, we can use a GeoSPARQL query to get, for example, the distance between the two landmarks, like so:
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX uom: <http://www.opengis.net/def/uom/OGC/1.0/>
PREFIX ex: <http://example.org/>
?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))
Supported GeoSPARQL functions
In this section we briefly enumerate the GeoSPARQL extension functions supported by RDF4J. For more information on the precise meaning and use of these functions we refer you to to the GeoSPARQL specification.
Non-topological and common query functions
RDF4J supports the following non-topological geospatial functions as defined in the GeoSPARQL specs:
Simple Feature functions
RDF4J supports the following Simple Feature (sf) functions:
RDF4J supports the following Egenhofer (eh) functions:
RDF4J supports the following RCC8 functions:
Improved performance through Lucene
Although RDF4J supports GeoSPARQL querying on any type of store, the Lucene SAIL and its derivates (the SolrSail and the ElasticSearchSail) have built-in optimizations that make geospatial querying on large datasets more efficient. By default, the Lucene SAIL only spatially indexes
http://www.opengis.net/ont/geosparql#asWKT fields. This can be changed using the
Reading and writing WKT Literals
RDF4J 2.4 uses Spatial4J 0.7, which has full support for converting between Shape objects and WKT strings. See the Spatial4J Format documentation for more details.
Here are some useful links: