GraphDB-SE has special support for 2-dimensional geo-spatial data, which uses the WGS84 Geo Positioning RDF vocabulary (World Geodetic System 1984). Special indices can be used for this data, which permit the efficient evaluation of special query forms and extension functions that allow finding locations, which are:
within a certain distance of a point, i.e. within the specified circle on the surface of the sphere (Earth), using the nearby(...) construction;
within rectangles and polygons, where the vertices are defined using spherical polar coordinates, using the within(...) construction.
The following jar files (included with the GraphDB distribution) must be on the classpath, in order for the geo-spatial extensions to function correctly: jsi-1*.jar, log4j-1*.jar, sil-0*.jar, trove4j-2*.jar
Before the geo-spatial extensions can be used, the geo-spatial index must be built. This is achieved by inserting and committing a dummy statement (that is not actually stored), which uses a special predicate:
PREFIX ontogeo: <http://www.ontotext.com/owlim/geo#>
INSERT DATA { _:b1 ontogeo:createIndex _:b2. }
If the indexing is successful, the above update succeeds, otherwise an exception is thrown. Information about the indexing process and any errors can be found in the log. Note that this update will fail if there is no geo-spatial data in the repository, i.e. no statements describing resources with latitude and longitude properties.
Geo-spatial query syntax
The Geo-spatial query syntax is the SPARQL's RDF Collections syntax. This syntax uses round brackets as a shorthand for the statements, connecting a list of values using rdf:first and rdf:rest predicates with terminating rdf:nil. Statement patterns that use one of the special geo-spatial predicates, supported by GraphDB-SE, are treated differently by the query engine. The following special syntax is supported when evaluating SPARQL queries. All descriptions use the namespace:
This statement pattern will evaluate to true if the following constraints hold:
?point geo:lat ?plat .
?point geo:long ?plong .
Shortest great circle distance from (?plat, ?plong) to (?lat, ?long) <= ?distance
Such a construction uses the geo-spatial indices to find bindings for ?point, which lie within the defined circle.
Constants are allowed for any of ?lat ?long ?distance, where latitude and longitude are specified in decimal degrees and distance is specified in either kilometres ('km' suffix) or miles ('mi' suffix). If the units are not specified, then 'km' is assumed.
Restrictions
Latitude is limited to the range -90 (South) to +90 (North)
Longitude is limited to the range -180 (West) to +180 (East)
Examples
Find the names of airports that are within 50 miles of Seoul:
This statement pattern is used to test/find points that lie within the rectangle specified by diagonally opposite corners ?lat1 ?long1 and ?lat2 ?long2. The corners of the rectangle must be either constants or bound values.
It will evaluate to true if the following constraints hold:
?point geo:lat ?plat .
?point geo:long ?plong .
?lat_{1} <= ?plat <= ?lat_{2}
?long_{1} <= ?plong <= ?long_{2}
Note that the corners must be specified most westerly and southerly (first) and most northerly and easterly (second). Proper account is taken for rectangles that cross the +/-180 degree meridian.
Constants are allowed for any of ?lat_{1}?long_{1}?lat_{2}*long_{2}, where latitude and longitude are specified in decimal degrees. If ?point is unbound, then bindings for all points within the rectangle will be produced.
Restrictions
Latitude is limited to the range -90 (South) to +90 (North)
Longitude is limited to the range -180 (West) to +180 (East)
Rectangle vertices must be specified in the order lower-left followed by upper-right
Examples
Find tunnels lying within a rectangle enclosing Tirol, Austria:
This statement pattern is used to test/find points that lie within the polygon whose vertices are specified by three or more latitude/longitude pairs. The values for the vertices must be either constants or bound values.
It will evaluate to true if the following constraints hold:
?point geo:lat ?plat .
?point geo:long ?plong .
the position ?plat ?plong is enclosed by the polygon
The polygon is closed automatically if the first and last vertices do not coincide. The vertices must be constants or bound values. Coordinates are specified in decimal degrees. If ?point is unbound, then bindings for all points within the polygon will be produced.
Restrictions
Latitude is limited to the range -90 (South) to +90 (North)
Longitude is limited to the range -180 (West) to +180 (East)
Examples
Find caves in the sides of cliffs lying within a polygon approximating the shape of England:
This SPARQL extension function computes the distance between two points in kilometres and can be used in FILTER and ORDER BY clauses.
Restrictions
Latitude is limited to the range -90 (South) to +90 (North)
Longitude is limited to the range -180 (West) to +180 (East)
Examples
Find all the airports within 80 miles of Bournemouth and filter out those that are more than 80 kilometres from Brize Norton, order the results with the closest to Brize Norton first:
Knowledge of the implementation's algorithms and assumptions allow users to make the best use of the GraphDB-SE geo-spatial extensions. The following points are significant and can affect the expected behaviour during query answering:
Spherical Earth – the current implementation treats the Earth as a perfect sphere with a radius of 6371.009km;
Only 2-Dimensional points are supported, i.e. there is no special handling of geo:alt (metres above the reference surface of the Earth);
All latitude and longitude values must be specified using decimal degrees, where East and North are positive and -90 <= latitude <= +90 and -180 <= longitude <= +180;
Distances must be in units of kilometres (suffix 'km') or statute miles (suffix 'mi'). If the suffix is omitted, kilometres are assumed;
omgeo:within( rectangle ) construct uses a 'rectangle' whose edges are lines of latitude and longitude, so the north-south distance is constant, and the rectangle described forms a band around the Earth, which starts and stops at the given longitudes;
omgeo:within( polygon ) joins vertices with straight lines on a cylindrical projection of the Earth tangential to the equator. A straight line starting at the point under test and continuing East out of the polygon is examined to see how many polygon edges it intersects. If the number of intersections is even, then the point is outside the polygon. If the number of intersections is odd, the point is inside the polygon. With the current algorithm, the order of vertices is not relevant (clockwise or anticlockwise);
omgeo:within() may not work correctly when the region (polygon or rectangle) spans the +/-180 meridian;
omgeo:nearby() uses the great circle distance between points.