Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

Added support for GEO visualization #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/cz/cvut/kbss/pubby/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cz.cvut.kbss.pubby;

/**
* Petr Křemen, 2013
* [email protected]
*/
public class Point {
double lng, lat;

public Point(double lng, double lat) {
this.lng = lng;
this.lat = lat;
}

public double getLng() {
return lng;
}

public double getLat() {
return lat;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cz.cvut.kbss.pubby.servlets;

import com.hp.hpl.jena.rdf.model.Resource;
import de.fuberlin.wiwiss.pubby.ResourceDescription;

/**
* Petr Křemen, 2013
* [email protected]
*/
public interface ResourceDescriptionProvider {
public Resource get(String url);
}
35 changes: 35 additions & 0 deletions src/main/java/cz/cvut/kbss/pubby/vocab/GEO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cz.cvut.kbss.pubby.vocab;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

/**
* Petr Křemen 2013
* [email protected]
*/
public class GEO {

/** <p>The RDF model that holds the vocabulary terms</p> */
private static Model m_model = ModelFactory.createDefaultModel();

/** <p>The namespace of the vocabulary as a string</p> */
public static final String NS = "http://www.w3.org/2003/01/geo/wgs84_pos#";

/** <p>The namespace of the vocabulary as a string</p>
* @see #NS */
public static String getURI() {return NS;}

/** <p>The namespace of the vocabulary as a resource</p> */
public static final Resource NAMESPACE = m_model.createResource( NS );

/** <p>Longitude property.</p> */
public static final Property P_LONG = m_model.createProperty( NS + "long" );

/** <p>Latitude property.</p> */
public static final Property P_LAT = m_model.createProperty( NS + "lat" );

/** <p>Latitude property.</p> */
public static final Property P_LOCATION = m_model.createProperty( NS + "location" );
}
49 changes: 46 additions & 3 deletions src/main/java/de/fuberlin/wiwiss/pubby/ResourceDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;

import de.fuberlin.wiwiss.pubby.Configuration;
import cz.cvut.kbss.pubby.Point;
import cz.cvut.kbss.pubby.servlets.ResourceDescriptionProvider;
import cz.cvut.kbss.pubby.vocab.GEO;

/**
* A convenient interface to an RDF description of a resource.
Expand All @@ -36,8 +38,9 @@ public class ResourceDescription {
private final Configuration config;
private PrefixMapping prefixes = null;
private List<ResourceProperty> properties = null;

public ResourceDescription(HypermediaResource resource, Model model,
private List<Point> points;

public ResourceDescription(HypermediaResource resource, Model model,
Configuration config) {
this.hypermediaResource = resource;
this.model = model;
Expand Down Expand Up @@ -316,4 +319,44 @@ public int compareTo(Value other) {
return getNode().getLiteralLexicalForm().compareTo(otherValue.getNode().getLiteralLexicalForm());
}
}

private void addPoint(final Resource r) {
final Statement lngS = r.getProperty(GEO.P_LONG);
final Statement latS = r.getProperty(GEO.P_LAT);

if ( lngS != null || latS != null ) {
points.add(new Point(lngS.getObject().asLiteral().getDouble(),latS.getObject().asLiteral().getDouble()));
}
}

/**
* Adds retrieves all points connected by geo:location property to this resource.
*
* @param p provider for data of the location resources
*/
private void addAllPoints(ResourceDescriptionProvider p) {
final StmtIterator it= resource.listProperties(GEO.P_LOCATION);
while(it.hasNext()) {
Statement s = it.nextStatement();
if ( !s.getObject().isAnon() ) {
addPoint(p.get(s.getObject().asResource().getURI()));
}
}
addPoint(resource);
}

/**
* Gets all points that are relevant to this resource. This includes
* 1) the point extracted from the geo:lat and geo:long properties of this resource
* 2) all the points connected to this resource by geo:location property.
* @param p provider for retrieving resource descriptions
* @return
*/
public List<Point> getPoints(ResourceDescriptionProvider p) {
if (points == null) {
points = new ArrayList<Point>();
addAllPoints(p);
}
return points;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cz.cvut.kbss.pubby.servlets.ResourceDescriptionProvider;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;

Expand All @@ -32,9 +33,9 @@ public class PageURLServlet extends BaseURLServlet {

public boolean doGet(HypermediaResource controller,
Collection<MappedResource> resources,
HttpServletRequest request,
final HttpServletRequest request,
HttpServletResponse response,
Configuration config) throws ServletException, IOException {
final Configuration config) throws ServletException, IOException {

Model description = getResourceDescription(resources);

Expand Down Expand Up @@ -66,8 +67,15 @@ public boolean doGet(HypermediaResource controller,
context.put("title", resourceDescription.getLabel());
context.put("comment", resourceDescription.getComment());
context.put("image", resourceDescription.getImageURL());
context.put("properties", resourceDescription.getProperties());

context.put("properties", resourceDescription.getProperties());
context.put("geoPoints", resourceDescription.getPoints(new ResourceDescriptionProvider() {
@Override
public Resource get(String url) {
return getResourceDescription(config.getMappedResourcesFromRelativeWebURI(
url.substring(config.getWebApplicationBaseURI().length()), true)).getResource(url);
}
}));

try {
Model metadata = ModelFactory.createDefaultModel();
for (MappedResource resource: resources) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/webapp/WEB-INF/templates/page.vm
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,34 @@
<a href="#meta" onclick="showAllMetadata('metadata-tables')">expand all</a>
#end

#if (!$geoPoints.isEmpty())
<a name="geo"></a>
<h2>Geographic location</h2>
<div id="map">
</div>
<script src="${server_base}static/jquery-1.9.1.min.js"></script>
<script src="${server_base}static/OpenLayers.js"></script>
<script type="text/javascript">
map = new OpenLayers.Map('map',
{
controls: [new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.Attribution(),
new OpenLayers.Control.PanZoom(), new OpenLayers.Control.Permalink(), new OpenLayers.Control.Navigation() ],
numZoomLevels: 22//,
}
);
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
markers=new OpenLayers.Layer.Markers( "Markers" );
map.addLayers([new OpenLayers.Layer.OSM(),markers]);
#set($x = 0)
#foreach($i in $geoPoints)
#if($x == 0)
map.setCenter(new OpenLayers.LonLat(${i.lng},${i.lat}).transform( fromProjection, toProjection ),10);
#end
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(${i.lng},${i.lat}).transform( fromProjection, toProjection)));
#set($x = $x+1)
#end
</script>
#end

#parse("footer.vm")
Loading