Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
select graphs from UI
Browse files Browse the repository at this point in the history
  • Loading branch information
luigiselmi committed Feb 24, 2015
1 parent 9eb5486 commit 0da8c64
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 41 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@
<version>1.3</version>
<configuration>
<mainClass>${main.class}</mainClass>
<arguments>
<!-- arguments>
<argument>http://sandbox.fusepool.info:8181/ldp/wr-ldpc/Trentino-Events-1/eventi-xml-xml-transformed</argument>
</arguments>
</arguments-->
</configuration>
</plugin>
</plugins>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/eu/fusepool/p3/spatial/demo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
public class Main {

public static void main(String[] args) throws Exception {
/*
String graph = null;
if (args != null) {
graph = args[0];
}
*/
Server server = new Server(Integer.valueOf(8080));
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setAttribute("graph", graph);
//webapp.setAttribute("graph", graph);
webapp.setResourceBase("src/main/webapp");
server.setHandler(webapp);
server.start();
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/eu/fusepool/p3/spatial/demo/SpatialDataEnhancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,31 @@ public TripleCollection enhance(String dataSetUrl, TripleCollection dataToEnhanc
* @throws ParseException
* @throws Exception
*/
public JSONObject enhanceJson(String dataSetUrl, TripleCollection dataToEnhance) throws JSONException {
public JSONObject enhanceJson(List<String> dataSetUrlList, TripleCollection dataToEnhance) throws JSONException {
JSONObject resultJson = new JSONObject();
if( dataToEnhance != null ){
if( ! dataToEnhance.isEmpty() ) {

//look for the knowledge base name in the triple store before fetching the data from the url.
if( ! isCachedGraph(spatialDataset, dataSetUrl) ){
loadKnowledgeBase(spatialDataset, dataSetUrl, dataSetUrl);
}
else {
log.info("Rdf data set " + dataSetUrl + " already in the triple store.");
}
WGS84Point point = getPoint(dataToEnhance);
double radius = getCircle(dataToEnhance).radius;
if(point.getStartDate() != null || point.getEndDate() != null){
resultJson = queryEventsNearbyJson(point, dataSetUrl, radius);
}
else {
resultJson = queryNearbyJson(point, dataSetUrl, radius);
if (dataSetUrlList != null) {
Iterator<String> dataSetIter = dataSetUrlList.iterator();
while (dataSetIter.hasNext()) {
String dataSetUrl = dataSetIter.next();
if( ! isCachedGraph(spatialDataset, dataSetUrl) ){
loadKnowledgeBase(spatialDataset, dataSetUrl, dataSetUrl);
}
else {
log.info("Rdf data set " + dataSetUrl + " already in the triple store.");
}
WGS84Point point = getPoint(dataToEnhance);
double radius = getCircle(dataToEnhance).radius;
if(point.getStartDate() != null || point.getEndDate() != null){
resultJson = queryEventsNearbyJson(point, dataSetUrl, radius);
}
else {
resultJson = queryNearbyJson(point, dataSetUrl, radius);
}
}
}
}
else {
Expand Down Expand Up @@ -378,7 +384,7 @@ public JSONObject queryEventsNearbyJson(WGS84Point point, String graphName, doub
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>");

String qs = StrUtils.strjoinNL("SELECT * ",
"FROM NAMED <" + graphName + ">",
//"FROM NAMED <" + graphName + ">",
"WHERE { ",
"GRAPH <" + graphName + "> ",
" { ?location spatial:nearby (" + point.getLat() + " " + point.getLong() + " " + radius + " 'm') .",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package eu.fusepool.p3.spatial.demo;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.clerezza.rdf.core.Resource;
import org.apache.clerezza.rdf.core.Triple;
import org.apache.clerezza.rdf.core.TripleCollection;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
import org.apache.clerezza.rdf.core.serializedform.Parser;
import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -31,21 +40,26 @@ public class SpatialSearchServlet extends HttpServlet{
private static final UriRef schema_circle = new UriRef("http://schema.org/circle");
private static final UriRef schema_containedIn = new UriRef("http://schema.org/containedIn");
private static final UriRef schema_geo = new UriRef("http://schema.org/geo");
private static final UriRef ldp_rdfsource = new UriRef("http://www.w3.org/ns/ldp#RDFSource");
private static final UriRef ldp_contains = new UriRef("http://www.w3.org/ns/ldp#contains");
private static final UriRef rdf_type = new UriRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");


String graph = null;
SpatialDataEnhancer enhancer = null;

public void init() throws ServletException {
/*
if (getServletContext().getAttribute("graph") != null) {
graph = getServletContext().getAttribute("graph").toString();
}
else {
throw new ServletException("The data graph must be provided at the start of the application");
}

*/
try {
enhancer = new SpatialDataEnhancer();
enhancer.loadKnowledgeBase(enhancer.getDataset(), graph, graph);
//enhancer.loadKnowledgeBase(enhancer.getDataset(), graph, graph);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand All @@ -54,19 +68,20 @@ public void init() throws ServletException {
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String container = request.getParameter("container");
String coordinates = request.getParameter("coordinates");
double radius = Double.parseDouble(request.getParameter("radius"));
String date = request.getParameter("date");

System.out.println("Coordinates: " + coordinates + ", radius (m): " + radius + ", date: " + date);
System.out.println("Container: " + container + ", coordinates: " + coordinates + ", radius (m): " + radius + ", date: " + date);


WGS84Point position = getPosition(getCoordinates(coordinates), date);
//WGS84Point position = getPosition(getCoordinates(coordinates), date);
TripleCollection rdfQuery = getRdfQuery(getCoordinates(coordinates), radius, date);

JSONObject pois = null;
try {
pois = enhancer.enhanceJson(graph, rdfQuery);
pois = enhancer.enhanceJson(getGraphs(container), rdfQuery);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down Expand Up @@ -134,4 +149,27 @@ private TripleCollection getRdfQuery(double [] coordinates, double radius, Strin
double [] coordinates = {Double.parseDouble(coordsStr[0]), Double.parseDouble(coordsStr[1])};
return coordinates;
}

private List<String> getGraphs(String container) throws MalformedURLException, IOException {
ArrayList<String> containerChild = new ArrayList<String>();
String [] graphs = null;
URLConnection connection = new URL(container).openConnection();
connection.addRequestProperty("Accept", "text/turtle");
String contentType = connection.getContentType();
InputStream is = connection.getInputStream();
Parser parser = Parser.getInstance();
TripleCollection containerGraph = parser.parse(is, SupportedFormat.TURTLE);
// is it a LDP container ?
Iterator<Triple> childIter = containerGraph.filter(null, ldp_contains, null);
while (childIter.hasNext()) {
UriRef childRef = (UriRef) childIter.next().getObject();
containerChild.add(childRef.getUnicodeString());
}

// is it a RDF graph ?
if (containerChild.size() == 0 ) {
containerChild.add(container);
}
return containerChild;
}
}
23 changes: 5 additions & 18 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<div id="wrapper">
<div id="mainColumn">
<h1>FusepoolP3 Spatial Search Demo</h1>
<p>Search events or point of interest. Move the marker to select a position, chose a radius and optionally a start date for events.</p>

<p>Search events or point of interest. Move the marker to select a position, choose a radius and optionally a start date for events.</p>
LDP Container: <input id="container" type="text" size="20"/><br>
Coordinates:
<input id="coordinates" type="text" />
Radius (m):
Expand Down Expand Up @@ -84,10 +84,11 @@ <h1>FusepoolP3 Spatial Search Demo</h1>
}

function getJson(){
var container = document.getElementById('container').value;
var coords = document.getElementById('coordinates').value;
var date = document.getElementById('date').value;
var radius = document.getElementById('radius').value;
$.getJSON("./search?coordinates="+coords+"&date="+date+"&radius="+radius,function(data){
$.getJSON("./search?coordinates="+coords+"&date="+date+"&radius="+radius+"&container="+container,function(data){
geojsonLayer = L.geoJson(data,{
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.name + "<br>" +
Expand All @@ -99,21 +100,7 @@ <h1>FusepoolP3 Spatial Search Demo</h1>
});
}

function getData(){
var coords = document.getElementById('coordinates').value;
var eventsLayer = new L.geoJson();
eventsLayer.addTo(map);
$.ajax({
dataType: "json",
url: "./geocode?streetname="+coords,
success: function(data){
$(data.features).each(function(key,data){
eventsLayer.addData(data);
});
}
}).error(function(){});

}


function removeMarkers() {
map.removeLayer(geojsonLayer);
Expand Down

0 comments on commit 0da8c64

Please sign in to comment.