You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Johan Kumps <jo...@telenet.be> on 2021/01/28 16:24:22 UTC

insert data into Jena Fuseki GeoSparql

Hi all,

I'm struggling with inserting triples in a Jena Fuseki Geosparql dataset.
My application has the need to add triples at runtime which will afterwards
will be used to perform geo spatial analysis like within, distance, ...

The server is started as follows:

java -jar jena-fuseki-geosparql-3.17.0-SNAPSHOT.jar -rf
"geosparql_test.rdf" -i --port 35400

I trying to insert triples using this code:

import org.apache.jena.fuseki.geosparql.DatasetException;
import org.apache.jena.geosparql.spatial.SpatialIndexException;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.update.UpdateExecutionFactory;
import org.apache.jena.update.UpdateFactory;
import org.apache.jena.update.UpdateProcessor;
import org.apache.jena.update.UpdateRequest;

public class Case {

    public static void main(String[] args) throws DatasetException,
SpatialIndexException {

        String service = "http://localhost:35400/ds";

        String insertQuery = "PREFIX rdf:
<http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n INSERT DATA {\n" +
                "<http://example.com/test>
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://example.com/Test> .\n" +
                "<http://example.com/test>
<http://www.opengis.net/ont/geosparql#hasGeometry>
<http://example.com/test/geometry> .\n" +
                "<http://example.com/test/geometry>
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://www.opengis.net/ont/sf#Point> .\n" +
                "<http://example.com/test/geometry>
<http://www.opengis.net/ont/geosparql#asWKT>
\"<http://www.opengis.net/def/crs/EPSG/0/27700> POINT(4.4871426
51.9086434)\"^^<http://www.opengis.net/ont/geosparql#wktLiteral> \n" +
                "}";
        UpdateRequest updateRequest = UpdateFactory.create(insertQuery);
        UpdateProcessor updateProcessor =
UpdateExecutionFactory.createRemote(updateRequest, service);
        updateProcessor.execute();

        String query = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n" +
                "\n" +
                "SELECT *\n" +
                "WHERE{\n" +
                "    ?subj ?pred ?obj\n" +
                "}";

        QueryExecution qe = QueryExecutionFactory.sparqlService(service, query);
        ResultSet rs = qe.execSelect();
        ResultSetFormatter.outputAsTSV(rs);

    }
}

What am I doing wrong? Is there a better way to insert triples into Jena
Fuseki Geosparql?

Thanks for you help!

Johan,

Re: insert data into Jena Fuseki GeoSparql

Posted by Johan Kumps <jo...@telenet.be>.
Hi all,

Tha bad request issue is fixed. Apparently the server was not started with
the -u option allowing updates. My bad!
Unfortunately sfWithin queries do not return my inserted triple although
the POINT I now use is the same as one that is in the
sample geosparql_test.rdf namely <http://example.org/Feature#A> which is
contained in <http://example.org/Geometry#PolygonH>.

The insert query:

String insertQuery = "INSERT DATA {\n" +
        "<http://example.org/Feature#test>
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://example.org/Feature#Test> .\n" +
        "<http://example.org/Feature#test>
<http://www.opengis.net/ont/geosparql#hasGeometry>
<http://example.org/Geometry#testPoint> .\n" +
        "<http://example.org/Geometry#testPoint>
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://www.opengis.net/ont/sf#Point> .\n" +
        "<http://example.org/Geometry#testPoint>
<http://www.opengis.net/ont/geosparql#asWKT>
\"<http://www.opengis.net/def/crs/EPSG/0/27700> POINT(60
60)\"^^<http://www.opengis.net/ont/geosparql#wktLiteral> \n" +
        "}";

Please consider following query outputs:

Q1 : PREFIX geo: <http://www.opengis.net/ont/geosparql#>

SELECT *
WHERE{
    ?subj <http://www.opengis.net/ont/geosparql#asWKT> ?obj
    FILTER(STRSTARTS(LCASE(STR(?subj)), "http://example"))
}
?subj ?obj
<http://example.org/Geometry#testPoint> "<
http://www.opengis.net/def/crs/EPSG/0/27700> POINT(60 60)"^^<
http://www.opengis.net/ont/geosparql#wktLiteral>
<http://example.org/Geometry#PolygonH> "<
http://www.opengis.net/def/crs/EPSG/0/27700> POLYGON((30 40, 30 70, 90 70,
90 40, 30 40))"^^<http://www.opengis.net/ont/geosparql#wktLiteral>
<http://example.org/Geometry#PointA> "<
http://www.opengis.net/def/crs/EPSG/0/27700> POINT(60 60)"^^<
http://www.opengis.net/ont/geosparql#wktLiteral>
-----------------------------------------------
Q2 : PREFIX geo: <http://www.opengis.net/ont/geosparql#>

SELECT ?obj ?x
WHERE{
    ?obj geo:sfWithin ?x
} ORDER by ?obj
?obj ?x
<http://example.org/Feature#A> <http://example.org/Feature#A>
<http://example.org/Feature#A> <http://example.org/Feature#H>
<http://example.org/Feature#A> <http://example.org/Geometry#PointA>
<http://example.org/Feature#A> <http://example.org/Geometry#PolygonH>
<http://example.org/Feature#H> <http://example.org/Feature#H>
<http://example.org/Feature#H> <http://example.org/Geometry#PolygonH>
<http://example.org/Geometry#PointA> <http://example.org/Feature#A>
<http://example.org/Geometry#PointA> <http://example.org/Feature#H>
<http://example.org/Geometry#PointA> <http://example.org/Geometry#PointA>
<http://example.org/Geometry#PointA> <http://example.org/Geometry#PolygonH>
<http://example.org/Geometry#PolygonH> <http://example.org/Feature#H>
<http://example.org/Geometry#PolygonH> <http://example.org/Geometry#PolygonH
>

As you can see the asWKT value is the same for  <
http://example.org/Geometry#PointA> and  <
http://example.org/Geometry#testPoint>. But  <
http://example.org/Geometry#testPoint> is not considered to be within  <
http://example.org/Geometry#PolygonH>.
What can be wrong here?

Johan,

Op do 28 jan. 2021 om 17:24 schreef Johan Kumps <jo...@telenet.be>:

> Hi all,
>
> I'm struggling with inserting triples in a Jena Fuseki Geosparql dataset.
> My application has the need to add triples at runtime which will afterwards
> will be used to perform geo spatial analysis like within, distance, ...
>
> The server is started as follows:
>
> java -jar jena-fuseki-geosparql-3.17.0-SNAPSHOT.jar -rf
> "geosparql_test.rdf" -i --port 35400
>
> I trying to insert triples using this code:
>
> import org.apache.jena.fuseki.geosparql.DatasetException;
> import org.apache.jena.geosparql.spatial.SpatialIndexException;
> import org.apache.jena.query.QueryExecution;
> import org.apache.jena.query.QueryExecutionFactory;
> import org.apache.jena.query.ResultSet;
> import org.apache.jena.query.ResultSetFormatter;
> import org.apache.jena.update.UpdateExecutionFactory;
> import org.apache.jena.update.UpdateFactory;
> import org.apache.jena.update.UpdateProcessor;
> import org.apache.jena.update.UpdateRequest;
>
> public class Case {
>
>     public static void main(String[] args) throws DatasetException, SpatialIndexException {
>
>         String service = "http://localhost:35400/ds";
>
>         String insertQuery = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n INSERT DATA {\n" +
>                 "<http://example.com/test> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Test> .\n" +
>                 "<http://example.com/test> <http://www.opengis.net/ont/geosparql#hasGeometry> <http://example.com/test/geometry> .\n" +
>                 "<http://example.com/test/geometry> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.opengis.net/ont/sf#Point> .\n" +
>                 "<http://example.com/test/geometry> <http://www.opengis.net/ont/geosparql#asWKT> \"<http://www.opengis.net/def/crs/EPSG/0/27700> POINT(4.4871426 51.9086434)\"^^<http://www.opengis.net/ont/geosparql#wktLiteral> \n" +
>                 "}";
>         UpdateRequest updateRequest = UpdateFactory.create(insertQuery);
>         UpdateProcessor updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, service);
>         updateProcessor.execute();
>
>         String query = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n" +
>                 "\n" +
>                 "SELECT *\n" +
>                 "WHERE{\n" +
>                 "    ?subj ?pred ?obj\n" +
>                 "}";
>
>         QueryExecution qe = QueryExecutionFactory.sparqlService(service, query);
>         ResultSet rs = qe.execSelect();
>         ResultSetFormatter.outputAsTSV(rs);
>
>     }
> }
>
> What am I doing wrong? Is there a better way to insert triples into Jena
> Fuseki Geosparql?
>
> Thanks for you help!
>
> Johan,
>
>
>
>
>
>
>
>