Hi Osma,
Many thanks! The link of the spacialindexer you sent me was useful...
It turns out that I needed to manually add the data to the spacial index.
Here I provide the minimalist source code which works for me and successfully
retrieves triples with geo:lat and geo:long coordinates within given radius:
String queryStr =
"PREFIX spatial: <http://jena.apache.org/spatial#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> " +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
"PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#> " +
"SELECT ?loc ?lat " +
"WHERE {?loc spatial:nearby(35.5 32 1000 'km' ) . ?loc geo:lat
?lat}";
Model m = SemanticSensorModel.getIntance().getJenaModel();
EntityDefinition entDef = new EntityDefinition("1", "2");
Dataset baseDataset = DatasetFactory.create(m);
try {
Directory dir = FSDirectory.open( new
File("/home/martin/spatial_index"));
Dataset spatialDataset =
SpatialDatasetFactory.createLucene(baseDataset, dir, entDef);
DatasetGraphSpatial dataset = (DatasetGraphSpatial)
(spatialDataset.asDatasetGraph());
SpatialIndex spatialIndex = dataset.getSpatialIndex();
SpatialIndexContext context = new SpatialIndexContext(spatialIndex);
spatialIndex.startIndexing();
Iterator<Quad> quadIter = dataset.find(Node.ANY, Node.ANY, Node.ANY,
Node.ANY);
for (; quadIter.hasNext();) {
Quad quad = quadIter.next();
context.index(quad.getGraph(), quad.getSubject(),
quad.getPredicate(), quad.getObject());
}
spatialIndex.finishIndexing();
Query query = QueryFactory.create(queryStr) ;
QueryExecution qexec = QueryExecutionFactory.create(query,
spatialDataset);
ResultSet results = qexec.execSelect() ;
for ( ; results.hasNext() ; )
{
QuerySolution soln = results.nextSolution() ;
System.out.println(soln.get("loc").toString() + " " +
soln.get("lat").toString());
}
System.out.println("Done printing results");
} catch (IOException e) {
e.printStackTrace();
}
One should note the use of the
spatialIndex.startIndexing()/context.index()/spatialIndex.finishIndexing()-
these are the calls that do the manual spatial indexing.
I have also posted the solution to stackoverflow:
http://stackoverflow.com/questions/35801520/geospatial-queries-with-apache-jena/36185016#36185016
Also after these calls are performed, the '/home/martin/spatial_index' grows
Best Regards
Martin
________________________________________
From: Osma Suominen <[email protected]>
Sent: Tuesday, March 22, 2016 12:10 PM
To: [email protected]
Subject: Re: Geospatial SPARQL queries with Jena
Hi Martin!
I think the problem is not with creating the spatial index - you've done
that already - but with loading the data into the index. Like Andy said
earlier in the thread, just associating the spatial dataset with the
base dataset (and a directory) doesn't cause the preexisting data to be
indexed.
I haven't really used jena-spatial (I'm more familiar with jena-text
which works in a similar way) but maybe you could check out what the
spatialindexer.java [1] command line tool does.
-Osma
[1]
https://github.com/apache/jena/blob/master/jena-spatial/src/main/java/jena/spatialindexer.java
On 21/03/16 16:48, Martin Vachovski wrote:
> Hi Osma,
>
> Many thanks for the quick reply.
> Seems that you're right- no matter how I construct the EntityDefinition
> object,
> I always get the same files in the index directory (binary equivalent).
> So either I don't provide a valid EntityDefinition or I am missing something
> else.
> Do you know if I need to explicitly create a Lucene index?
>
> Best Regards
> Martin
> ________________________________________
> From: Osma Suominen <[email protected]>
> Sent: Monday, March 21, 2016 11:12 AM
> To: [email protected]
> Subject: Re: Geospatial SPARQL queries with Jena
>
> Hi Martin,
>
> The files you show look like an empty Lucene index. With a grand total
> of 53+36=90 bytes, there is hardly room for even a single entry. So it
> seems that your data is not being indexed at all.
>
> -Osma
>
> 21.03.2016, 12:44, Martin Vachovski kirjoitti:
>> Hi Andy,
>>
>> Sorry for the late reply.
>>
>>>>> The code has several things that are not from jena. Where is the data
>>>>> being loaded into the spatial index? Check the Lucene directory has
>>>>> actually grown.
>>
>> After I execute the code I get three files into the
>> '/home/martin/spatial_index' folder:
>> -rw-r--r-- 1 martin martin 53 Mar 21 10:40 segments_1
>> -rw-r--r-- 1 martin martin 36 Mar 21 10:40 segments.gen
>> -rw-r--r-- 1 martin martin 0 Mar 21 10:40 write.lock
>>
>> The files are small, but the data that I have contains not too many triples-
>> as it is set up just for the testing of this feature.
>>
>> Best Regards
>> Martin
>>
>> ________________________________________
>> From: Andy Seaborne <[email protected]>
>> Sent: Sunday, March 13, 2016 7:37 PM
>> To: [email protected]
>> Subject: Re: Geospatial SPARQL queries with Jena
>>
>> Hi there,
>>
>> The code has several things that are not from jena. Where is the data
>> being loaded into the spatial index? Check the Lucene directory has
>> actually grown.
>>
>>
>> > Dataset baseDataset = DatasetFactory.create(m);
>> > try {
>> > Directory dir = FSDirectory.open( new
>> File("/home/martin/spatial_index"));
>> > Dataset spatialDataset =
>> SpatialDatasetFactory.createLucene(baseDataset, dir, entDef);
>>
>> just associating the baseDataset with a Directory does not cause
>> automatic indexing - loading into spatialDataset should cause The lucene
>> directory to change.
>>
>> Andy
>>
>> On 11/03/16 14:11, Martin Vachovski wrote:
>>> Hi All,
>>>
>>>
>>> I am new to this group.
>>>
>>> I've being trying to start with geospatial SPARQL queries with Jena, but so
>>> far no luck.
>>>
>>>
>>> I have a simple Jena model which among other data contains the following:
>>>
>>>
>>> <rdf:RDF
>>> xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>>> xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
>>> xmlns:ssn="http://purl.oclc.org/NET/ssnx/ssn#"
>>> xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
>>>
>>>
>>> <ssn:ObservationValue
>>> rdf:about="http://af3.info/ObservationValues/GasCO/1450439142">
>>> <geo:location>
>>> <geo:Point
>>> rdf:about="http://af3.info/locations/GasCO/1450439142">
>>> <geo:lat
>>> rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>>> >35.4</geo:lat>
>>> <geo:long
>>> rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>>> >32</geo:long>
>>> </geo:Point>
>>> </geo:location>
>>> <!-- some more data here -->
>>> </ssn:ObservationValue>
>>>
>>>
>>> </rdf:RDF>
>>>
>>>
>>> So I am trying to extract the geo:Point resource using the following Jena
>>> code:
>>>
>>>
>>>
>>> private static void geoQuery()
>>> {
>>> InitJenaSpatial jenaSpatial = new InitJenaSpatial();
>>> jenaSpatial.start();
>>> try {
>>> String queryStr =
>>> "PREFIX spatial: <http://jena.apache.org/spatial#> " +
>>> "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
>>> "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> " +
>>> "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
>>> "PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>" +
>>> "SELECT ?loc " +
>>> "WHERE {?loc spatial:nearby(35.4 32 1000 'km' )}";
>>>
>>> // the object m contains the Jena model with the data
>>> Model m = SemanticSensorModel.getIntance().getJenaModel();
>>> EntityDefinition entDef = new EntityDefinition("ssn:ObservationValue",
>>> "geo:location");
>>> // EntityDefinition entDef = new
>>> EntityDefinition("<http://af3.info/ObservationValues/GasCO/1450439142>",
>>> // "<http://af3.info/locations/GasCO/1450439142>");
>>> // EntityDefinition entDef = new
>>> EntityDefinition("<http://purl.oclc.org/NET/ssnx/ssn#ObservationValue>",
>>> // "<http://www.w3.org/2003/01/geo/wgs84_pos#location>");
>>> Dataset baseDataset = DatasetFactory.create(m);
>>> try {
>>> Directory dir = FSDirectory.open( new File("/home/martin/spatial_index"));
>>> Dataset spatialDataset = SpatialDatasetFactory.createLucene(baseDataset,
>>> dir, entDef);
>>> Query query = QueryFactory.create(queryStr) ;
>>> QueryExecution qexec = QueryExecutionFactory.create(query, spatialDataset);
>>> ResultSet results = qexec.execSelect() ;
>>> for ( ; results.hasNext() ; )
>>> {
>>> QuerySolution soln = results.nextSolution() ;
>>> System.out.println(soln.get("loc").toString());
>>> }
>>> } catch (IOException e) { e.printStackTrace(); }
>>> }
>>> catch(Exception e ) { System.out.println(e.getMessage()); }
>>> jenaSpatial.stop();
>>> }
>>> }
>>>
>>> The above code doesn't throw any exceptions or errors, but the result
>>> object is always empty.
>>> I suspect that my problem is with the construction of EntityDefinition
>>> entDef but I haven't
>>> been able to find anything relevant in the Apache Jena's online
>>> documentation- it contains plain Javadocs.
>>>
>>> Sorry for the long source code- I've tried to keep it as short as possible.
>>> Any help would be much appreciated
>>>
>>> Best Regards
>>> Martin
>>>
>>>
>
>
> --
> Osma Suominen
> D.Sc. (Tech), Information Systems Specialist
> National Library of Finland
> P.O. Box 26 (Kaikukatu 4)
> 00014 HELSINGIN YLIOPISTO
> Tel. +358 50 3199529
> [email protected]
> http://www.nationallibrary.fi
>
--
Osma Suominen
D.Sc. (Tech), Information Systems Specialist
National Library of Finland
P.O. Box 26 (Kaikukatu 4)
00014 HELSINGIN YLIOPISTO
Tel. +358 50 3199529
[email protected]
http://www.nationallibrary.fi