Mr M
Mr M

Reputation: 79

Unstructured return from sparql-service

I have problems with a SPARQL query I am executing from Apache Jena. The query is passed to LinkedGeoData via sparqlservice. The query looks like that:

Prefix lgdo: <http://linkedgeodata.org/ontology/>
Prefix geom: <http://geovocab.org/geometry#>
Prefix ogc:<http://www.opengis.net/ont/geosparql#>
Select ?s ?l ?g
From <http://linkedgeodata.org> {
  ?s
    a lgdo:Amenity ;
    rdfs:label ?l ;    
    geom:geometry [
      ogc:asWKT ?g
    ] .

    Filter(<bif:st_intersects> (?g, <bif:st_point> (12.372966, 51.310228), 0.1)) .
}

I use <bif:st_intersects> and <bif:st_point>, as the prefix bif is defined by the endpoint http://linkedgeodata.org/sparql but not within my Java code:

String queryString2 =
                        "Prefix lgdo: <http://linkedgeodata.org/ontology/> "+
                        "Prefix geom: <http://geovocab.org/geometry#> "+
                        "Prefix ogc:<http://www.opengis.net/ont/geosparql#> "+
                        "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
                        "Select ?s ?l ?g  From <http://linkedgeodata.org> { ?s  a lgdo:Amenity ;    rdfs:label ?l ;        geom:geometry [      ogc:asWKT ?g    ] .    Filter (<bif:st_intersects> (?g, <bif:st_point> (12.372966, 51.310228), 0.1)) .}";
    QueryExecution qexec2 = QueryExecutionFactory.sparqlService("http://linkedgeodata.org/sparql", query2);

Iterator<Triple> triples2 = qexec2.execConstructTriples();

while(triples2.hasNext())
                {

                    Triple triple = triples2.next();

                    Node s =    triple.getSubject();
                    Node p =    triple.getPredicate();
                    Node o =    triple.getObject();

                    System.out.println(s.toString());
                    System.out.println(p.toString());
                    System.out.println(o.toString());
                    System.out.println("\n");
                }

Here is an excerpt of my output:

b80d5f650b2e98240baf560415cdef40
http://www.w3.org/2005/sparql-results#value
"Kita EinSteinchen"


559c608a13e5bc6e4f98ed9ea24ee97d
http://www.w3.org/2005/sparql-results#binding
b80d5f650b2e98240baf560415cdef40


0759546664f20899fa5455abd7dbcc74
http://www.w3.org/2005/sparql-results#variable
"g"


0759546664f20899fa5455abd7dbcc74
http://www.w3.org/2005/sparql-results#value
"LINESTRING(12.3802344 51.3325989,12.380315 51.3327169,12.3803773 51.3327029,12.3804007 51.3327403,12.3799895 51.3328375,12.3800728 51.332975,12.3800728 51.332975,12.3800992 51.3330203,12.3799822 51.3330486,12.3799632 51.3330555,12.3799248 51.3330256,12.379842 51.3330456,12.3798641 51.3331146,12.3799149 51.3331622,12.3795303 51.333342,12.379223 51.3328586,12.3792961 51.3328139,12.3802344 51.3325989)"^^http://www.openlinksw.com/schemas/virtrdf#Geometry

However, once I run the identical query directly on the SPARQL endpoint (Query on LinkedGeoData SPARQL-service ) I get a well structured solution, i.e., I get the value for every variable (?s ?l ?g). How can I get the same formatting in my Java program? - I do not want to scan that weird return string for the solutions I need.

Thanks in advance!

Upvotes: 0

Views: 92

Answers (1)

durschtnase
durschtnase

Reputation: 166

you can use ResultSet

ResultSet rs = qexec2.execSelect();
ResultSetFormatter.out(rs);

this prints formatted results to console, but you can also use the provided methods of ResultSet to parse the results as you wish

rs.forEachRemaining(r -> System.out.println(r.get("s")));

will print all the results from variable named "s"

see here also

Upvotes: 3

Related Questions