Skip to content

Instantly share code, notes, and snippets.

@kinow
Last active December 23, 2018 08:09
Show Gist options
  • Save kinow/f391be7036b63f8ac0c08b9832e8bfa7 to your computer and use it in GitHub Desktop.
Save kinow/f391be7036b63f8ac0c08b9832e8bfa7 to your computer and use it in GitHub Desktop.
Trying Jena Spatial and Jena Text together
PREFIX : <http://example/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX spatial: <http://jena.apache.org/spatial#>
SELECT ?airport {
?place spatial:nearby (52.45 -1.73 10 'km') .
?place rdfs:label ?airport .
}
@prefix : <http://localhost/jena_example/#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix dbp: <http://dbpedia.org/resource/> .
@prefix airports: <http://airports.dataincubator.org/airports/> .
@prefix airports_sc: <http://airports.dataincubator.org/schema/> .
@prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
@prefix wkt: <http://www.opengis.net/ont/geosparql#> .
airports:EGBB rdf:type airports_sc:LargeAirport ;
geo:lat "52.4539"^^xsd:float ;
geo:long "-1.74803"^^xsd:float ;
rdfs:label "Birmingham International Airport"
.
airports:EGBB__Pair_1 rdf:type airports_sc:LargeAirport ;
:latitude_1 "52.4539"^^xsd:float ;
:longitude_1 "-1.74803"^^xsd:float ;
rdfs:label "Birmingham International Airport (Pair_1)"
.
airports:EGBB_Pair_2 rdf:type airports_sc:LargeAirport ;
:latitude_2 "52.4539"^^xsd:float ;
:longitude_2 "-1.74803"^^xsd:float ;
rdfs:label "Birmingham International Airport (Pair_2)"
.
airports:EGBB_String rdf:type airports_sc:LargeAirport ;
geo:lat "52.4539" ;
geo:long "-1.74803" ;
rdfs:label "Birmingham International Airport (String)"
.
airports:EGFF rdf:type airports_sc:LargeAirport ;
geo:lat "51.3967"^^xsd:float ;
geo:long "-3.34333"^^xsd:float ;
rdfs:label "Cardiff International Airport"
.
airports:EGGD rdf:type airports_sc:LargeAirport ;
geo:lat "51.3827"^^xsd:float ;
geo:long "-2.71909"^^xsd:float ;
rdfs:label "Bristol International Airport"
.
airports:EGKK rdf:type airports_sc:LargeAirport ;
geo:lat "51.1481"^^xsd:float ;
geo:long "-0.190278"^^xsd:float ;
rdfs:label "London Gatwick Airport"
.
airports:EGSS rdf:type airports_sc:LargeAirport ;
geo:lat "51.885"^^xsd:float ;
geo:long "0.235"^^xsd:float ;
rdfs:label "London Stansted Airport"
.
## Example of a TDB dataset and spatial index and text index
@prefix fuseki: <http://jena.apache.org/fuseki#> .
## TDB dataset and spatial index
@prefix : <http://localhost/jena_example/#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix tdb: <http://jena.hpl.hp.com/2008/tdb#> .
@prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> .
@prefix spatial: <http://jena.apache.org/spatial#> .
@prefix text: <http://jena.apache.org/text#> .
# Initialize TDB
[] ja:loadClass "org.apache.jena.tdb.TDB" .
tdb:DatasetTDB rdfs:subClassOf ja:RDFDataset .
tdb:GraphTDB rdfs:subClassOf ja:Model .
# TDB Dataset
<#dataset> rdf:type tdb:DatasetTDB ;
tdb:location "/tmp/TDB" ;
.
# Special dataset
spatial:SpatialDataset rdfs:subClassOf ja:RDFDataset .
# --- start of spatial configuration --- #
[] ja:loadClass "org.apache.jena.query.spatial.SpatialQuery" .
spatial:SpatialIndexLucene rdfs:subClassOf spatial:SpatialIndex .
:spatial_dataset rdf:type spatial:SpatialDataset ;
spatial:dataset <#dataset> ;
spatial:index <#indexLucene> ;
text:dataset <#dataset> ;
text:index <#indexLuceneText> ;
.
<#indexLucene> a spatial:SpatialIndexLucene ;
spatial:directory <file:/tmp/Lucene> ;
spatial:definition <#definition> ;
.
<#definition> a spatial:EntityDefinition ;
spatial:entityField "uri" ;
spatial:geoField "geo"
.
# --- end of spatial configuration --- #
# --- start of text configuration --- #
[] ja:loadClass "org.apache.jena.query.text.TextQuery" .
text:TextIndexLucene rdfs:subClassOf text:TextIndex .
# Text index description
<#indexLuceneText> a text:TextIndexLucene ;
text:directory <file:/tmp/Lucene> ;
text:entityMap <#entMap> ;
text:storeValues true ;
text:analyzer [ a text:StandardAnalyzer ] ;
text:queryAnalyzer [ a text:KeywordAnalyzer ] ;
text:queryParser text:AnalyzingQueryParser ;
# text:defineAnalyzers [ . . . ] ;
text:multilingualSupport true ;
.
<#entMap> a text:EntityMap ;
text:defaultField "label" ;
text:entityField "uri" ;
text:uidField "uid" ;
text:langField "lang" ;
text:graphField "graph" ;
text:map (
[ text:field "label" ;
text:predicate rdfs:label ]
) .
# --- end of text configuration --- #
<#fuseki_tdb> rdf:type fuseki:Service ;
rdfs:label "TDB/spatial-text" ;
fuseki:name "ds" ;
fuseki:serviceQuery "query" ;
fuseki:serviceQuery "sparql" ;
fuseki:serviceUpdate "update" ;
fuseki:serviceUpload "upload" ;
fuseki:serviceReadGraphStore "get" ;
fuseki:serviceReadWriteGraphStore "data" ;
fuseki:dataset :spatial_dataset ;
## Example of a TDB dataset and spatial index
@prefix fuseki: <http://jena.apache.org/fuseki#> .
## TDB dataset and spatial index
@prefix : <http://localhost/jena_example/#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix tdb: <http://jena.hpl.hp.com/2008/tdb#> .
@prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> .
@prefix spatial: <http://jena.apache.org/spatial#> .
@prefix text: <http://jena.apache.org/text#> .
# Initialize TDB
[] ja:loadClass "org.apache.jena.tdb.TDB" .
tdb:DatasetTDB rdfs:subClassOf ja:RDFDataset .
tdb:GraphTDB rdfs:subClassOf ja:Model .
# Initialize Spatial
[] ja:loadClass "org.apache.jena.query.spatial.SpatialQuery" .
spatial:SpatialDataset rdfs:subClassOf ja:RDFDataset .
spatial:SpatialIndexLucene rdfs:subClassOf spatial:SpatialIndex .
## Initialize text query
[] ja:loadClass "org.apache.jena.query.text.TextQuery" .
# A TextDataset is a regular dataset with a text index.
text:TextDataset rdfs:subClassOf ja:RDFDataset .
# Lucene index
text:TextIndexLucene rdfs:subClassOf text:TextIndex .
## ---------------------------------------------------------------
## This URI must be fixed - it's used to assemble the spatial dataset.
:spatial_dataset rdf:type spatial:SpatialDataset ;
spatial:dataset <#dataset> ;
spatial:index <#indexLucene> ;
.
<#dataset> rdf:type tdb:DatasetTDB ;
tdb:location "/tmp/TDB" ;
.
<#indexLucene> a spatial:SpatialIndexLucene ;
spatial:directory <file:/tmp/Lucene> ;
spatial:definition <#definition> ;
.
<#definition> a spatial:EntityDefinition ;
spatial:entityField "uri" ;
spatial:geoField "geo"
.
# Text index description
<#indexLuceneText> a text:TextIndexLucene ;
text:directory <file:/tmp/Lucene> ;
text:entityMap <#entMap> ;
text:storeValues true ;
text:analyzer [ a text:StandardAnalyzer ] ;
text:queryAnalyzer [ a text:KeywordAnalyzer ] ;
text:queryParser text:AnalyzingQueryParser ;
# text:defineAnalyzers [ . . . ] ;
text:multilingualSupport true ;
.
<#entMap> a text:EntityMap ;
text:defaultField "label" ;
text:entityField "uri" ;
text:uidField "uid" ;
text:langField "lang" ;
text:graphField "graph" ;
text:map (
[ text:field "label" ;
text:predicate rdfs:label ]
) .
<#service_spatial_tdb> rdf:type fuseki:Service ;
rdfs:label "TDB/spatial service" ;
fuseki:name "ds" ;
fuseki:serviceQuery "query" ;
fuseki:serviceQuery "sparql" ;
fuseki:serviceUpdate "update" ;
fuseki:serviceUpload "upload" ;
fuseki:serviceReadGraphStore "get" ;
fuseki:serviceReadWriteGraphStore "data" ;
fuseki:dataset :spatial_dataset ;
@kinow
Copy link
Author

kinow commented Dec 23, 2018

Query response:

{
  "head": {
    "vars": [ "airport" ]
  } ,
  "results": {
    "bindings": [
      {
        "airport": { "type": "literal" , "value": "Birmingham International Airport (String)" }
      } ,
      {
        "airport": { "type": "literal" , "value": "Birmingham International Airport" }
      }
    ]
  }
}

Originally from Jena Spatial tests. Had to remove entries using wkt as it needed JTS.

@kinow
Copy link
Author

kinow commented Dec 23, 2018

Also did a few modifications in the source code for logging and to get dependencies in runtime (downloaded jts' ZIP from sourceforge).

diff --git a/jena-fuseki2/jena-fuseki-webapp/src/main/resources/org/apache/jena/fuseki/log4j.properties b/jena-fuseki2/jena-fuseki-webapp/src/main/resources/org/apache/jena/fuseki/log4j.properties
index e84e60e179..909861119d 100644
--- a/jena-fuseki2/jena-fuseki-webapp/src/main/resources/org/apache/jena/fuseki/log4j.properties
+++ b/jena-fuseki2/jena-fuseki-webapp/src/main/resources/org/apache/jena/fuseki/log4j.properties
@@ -14,7 +14,7 @@ log4j.appender.fuseki.plain.target=System.out
 log4j.appender.fuseki.plain.layout=org.apache.log4j.PatternLayout
 log4j.appender.fuseki.plain.layout.ConversionPattern=%m%n
 
-log4j.rootLogger=INFO, jena.plainstdout
+log4j.rootLogger=ALL, jena.plainstdout
 log4j.logger.org.apache.jena=WARN
 log4j.logger.org.apache.jena.fuseki=INFO
 
diff --git a/jena-spatial/pom.xml b/jena-spatial/pom.xml
index d81dbffd1f..723f2e45ac 100644
--- a/jena-spatial/pom.xml
+++ b/jena-spatial/pom.xml
@@ -93,6 +93,51 @@
       <groupId>org.locationtech.spatial4j</groupId>
       <artifactId>spatial4j</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>jts</groupId>
+      <artifactId>jts</artifactId>
+      <version>0.1</version>
+      <scope>system</scope>
+      <systemPath>/home/kinow/Downloads/jts-1.14.jar</systemPath>
+    </dependency>
+
+    <dependency>
+      <groupId>jts</groupId>
+      <artifactId>jdom</artifactId>
+      <version>0.1</version>
+      <scope>system</scope>
+      <systemPath>/home/kinow/Downloads/jdom.jar</systemPath>
+    </dependency>
+
+    <dependency>
+      <groupId>jts</groupId>
+      <artifactId>jtsio</artifactId>
+      <version>0.1</version>
+      <scope>system</scope>
+      <systemPath>/home/kinow/Downloads/jtsio-1.14.jar</systemPath>
+    </dependency>
+
+    <dependency>
+      <groupId>jts</groupId>
+      <artifactId>xerces</artifactId>
+      <version>0.1</version>
+      <scope>system</scope>
+      <systemPath>/home/kinow/Downloads/xerces.jar</systemPath>
+    </dependency>
+
+    <dependency>
+        <groupId>org.locationtech.spatial4j</groupId>
+        <artifactId>spatial4j</artifactId>
+        <version>0.7</version>
+        <type>bundle</type>
+    </dependency>
+
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <optional>true</optional>
+    </dependency>
     
   </dependencies>

Running in Eclipse, project jena-fuseki-webapp, class org.apache.jena.fuseki.cmd.FusekiCmd, program arguments --config=/home/kinow/Development/java/jena/assembler-text-spatial.ttl --verbose --debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment