rdf4j-postgis  Version 0.1.0.0
TripleStoreConnection Class Reference
Collaboration diagram for TripleStoreConnection:

Public Member Functions

 TripleStoreConnection ()
 

Static Public Member Functions

static String executeQuery (String query, String model) throws JSONException
 
static void main (String[] args) throws JSONException
 

Static Public Attributes

static final TripleStoreConnection INSTANCE = new TripleStoreConnection()
 
static final String prefixCollection
 
static final Map< String, Model > modelmap =new TreeMap<>()
 
static Repository repo = new SailRepository(new MemoryStore())
 

Constructor & Destructor Documentation

◆ TripleStoreConnection()

44  {
45 
46  //modelmap=new TreeMap<>();
47  repo= new SailRepository(new MemoryStore());
48  repo.initialize();
49  // read the file 'example-data-artists.ttl' as an InputStream.
50  //InputStream input = Example08ReadTurtle.class.getResourceAsStream("/" + filename);
51 
52  try {
53  //modelmap.put("testdata.ttl", Rio.parse(new File("testdata.ttl").toURL().openStream(), "", RDFFormat.TURTLE));
54  modelmap.put("testdata2.ttl", Rio.parse(new File("testdata2.ttl").toURL().openStream(), "", RDFFormat.TURTLE));
55  //modelmap.put("rasterexample.ttl", Rio.parse(new File("rasterexample.ttl").toURL().openStream(), "", RDFFormat.TURTLE));
56  } catch (RDFParseException | UnsupportedRDFormatException | IOException e) {
57  // TODO Auto-generated catch block
58  e.printStackTrace();
59  }
60 
61  // modelmap.put("testdata3.ttl", ModelFactory.createOntologyModel());
62  // modelmap.put("testdata4.ttl", ModelFactory.createOntologyModel());
63 
64  }
static final Map< String, Model > modelmap
Definition: TripleStoreConnection.java:41
static Repository repo
Definition: TripleStoreConnection.java:43

References TripleStoreConnection.modelmap, and TripleStoreConnection.repo.

Member Function Documentation

◆ executeQuery()

static String executeQuery ( String  query,
String  model 
) throws JSONException
static
66  {
67  query=prefixCollection+query;
68  System.out.println(query);
69  System.out.println(model);
70  System.out.println(INSTANCE.modelmap.keySet());
71 
72  RepositoryConnection con = repo.getConnection();
73  System.out.println(INSTANCE.modelmap);
74  con.begin();
75  con.add(INSTANCE.modelmap.get(model));
76  con.commit();
77  //System.out.println("Empty? "+con.isEmpty());
78  /*for(Statement statement:modelmap.get(model)) {
79  System.out.println(statement);
80  }*/
81  TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, query);
82  //System.out.println(tupleQuery.toString());
83  TupleQueryResult qresult = tupleQuery.evaluate();
84  //System.out.println(qresult.getBindingNames());
85  //System.out.println(qresult.hasNext());
86  //System.out.println(qresult.);
87  List<JSONArray> allfeatures=new LinkedList<JSONArray>();
88  JSONObject result=new JSONObject();
89  JSONArray geojsonresults=new JSONArray();
90  JSONArray obj=new JSONArray();
91  Boolean first=true;
92  while(qresult.hasNext()) {
93  BindingSet bindingSet = qresult.next();
94  JSONObject jsonobj=new JSONObject();
95  JSONObject properties=new JSONObject();
96  List<JSONObject> geoms=new LinkedList<JSONObject>();
97  int geomvars=0;
98  for(String name:bindingSet.getBindingNames()) {
99  if(name.endsWith("_geom")) {
100  //System.out.println(name+" end with geom");
101  if(first) {
102  JSONObject geojsonresult=new JSONObject();
103  geojsonresult.put("type", "FeatureCollection");
104  geojsonresult.put("name", name);
105  JSONArray features=new JSONArray();
106  allfeatures.add(features);
107 
108  geojsonresults.put(geojsonresult);
109  geojsonresults.getJSONObject(geojsonresults.length()-1).put("features",features);
110  }
111  geomvars++;
112  AsGeoJSON geojson=new AsGeoJSON();
113  Value val=bindingSet.getValue(name);
114  Literal lit=(Literal)val;
115  //System.out.println(LiteralRegistry.INSTANCE.literals);
116  //System.out.println(LiteralRegistry.getLiteral(lit.getDatatype().toString()));
117  if(LiteralRegistry.getLiteral(lit.getDatatype().toString())!=null){
118  LiteralType l=LiteralRegistry.getLiteral(lit.getDatatype().toString());
119  Geometry geom=((VectorLiteral)l).read(bindingSet.getValue(name).stringValue());
120  String geojsonstring=geojson.operation(geom);
121  JSONObject geomobj=new JSONObject(geojsonstring);
122  geoms.add(geomobj);
123  }
124  //System.out.println("Geoms: "+geoms.size());
125  }
126  jsonobj.put(name, bindingSet.getValue(name));
127  properties.put(name, bindingSet.getValue(name));
128  obj.put(jsonobj);
129  }
130  first=false;
131  int geomcounter=0;
132  for(JSONObject geom:geoms) {
133  JSONObject geojsonobj=new JSONObject();
134  geojsonobj.put("type", "Feature");
135  geojsonobj.put("properties", properties);
136  geojsonobj.put("geometry", geom);
137  allfeatures.get(geomcounter%geomvars).put(geojsonobj);
138  geomcounter++;
139  }
140  }
141  result.put("geojson", geojsonresults);
142  result.put("data", obj);
143  //result.put("size", test.size());
144  return result.toString();
145 
146  }
static final TripleStoreConnection INSTANCE
Definition: TripleStoreConnection.java:36
static final String prefixCollection
Definition: TripleStoreConnection.java:38

References LiteralRegistry.getLiteral(), TripleStoreConnection.INSTANCE, TripleStoreConnection.modelmap, AsGeoJSON.operation(), TripleStoreConnection.prefixCollection, and TripleStoreConnection.repo.

Referenced by ExampleWebService.main(), TripleStoreConnection.main(), and ExampleWebService.queryService().

◆ main()

static void main ( String[]  args) throws JSONException
static
150  {
151  String res = TripleStoreConnection.executeQuery(
152  " SELECT ?geom ?wkt_geom WHERE { ?geom geo:asWKT ?wkt_geom . } LIMIT 10",
153  "testdata2.ttl");
154  // System.out.println(res[0]);
155  System.out.println(res);
156  System.out.println(
157  "=====================================================================================================");
158  /*res = TripleStoreConnection.executeQuery(
159  "SELECT ?geom ?wkt WHERE { ?geom geo:asWKT ?wkt . FILTER(geo:ST_Area(?wkt)>10) }", "testdata2.ttl");
160  // System.out.println(res[0]);
161  System.out.println(res);
162  res = TripleStoreConnection.executeQuery(
163  "SELECT ?wkt2 WHERE { ?geom geo:asWKT ?wkt . BIND(geo:ST_YMax(?wkt) AS ?wkt2). FILTER(geo:ST_Area(?wkt)>10) }",
164  "testdata2.ttl");
165  System.out.println(res);
166  System.out.println(res);*/
167  }
TripleStoreConnection()
Definition: TripleStoreConnection.java:44

References TripleStoreConnection.executeQuery().

Member Data Documentation

◆ INSTANCE

◆ modelmap

final Map<String, Model> modelmap =new TreeMap<>()
static

◆ prefixCollection

final String prefixCollection
static
Initial value:
= "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ System.lineSeparator() + "PREFIX geo: <http://www.opengis.net/ont/geosparql#>"

Referenced by TripleStoreConnection.executeQuery().

◆ repo

Repository repo = new SailRepository(new MemoryStore())
static