Strabon

changeset 351:95b1f7a9580e

added support for tab separated results format
author Babis Nikolaou <charnik@di.uoa.gr>
date Mon Jun 25 17:57:51 2012 +0300 (2012-06-25)
parents 7316bec474c1
children 146feca5586f
files resultio/pom.xml resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java
line diff
     1.1 --- a/resultio/pom.xml	Mon Jun 25 15:50:16 2012 +0300
     1.2 +++ b/resultio/pom.xml	Mon Jun 25 17:57:51 2012 +0300
     1.3 @@ -23,6 +23,11 @@
     1.4  		
     1.5  		<dependency>
     1.6  			<groupId>org.openrdf.sesame</groupId>
     1.7 +			<artifactId>sesame-sail-generaldb</artifactId>
     1.8 +		</dependency>
     1.9 +		
    1.10 +		<dependency>
    1.11 +			<groupId>org.openrdf.sesame</groupId>
    1.12  			<artifactId>sesame-queryresultio-sparqlxml</artifactId>
    1.13  			<version>${sesame.version}</version>
    1.14  		</dependency>
     2.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java	Mon Jun 25 15:50:16 2012 +0300
     2.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java	Mon Jun 25 17:57:51 2012 +0300
     2.3 @@ -1,17 +1,47 @@
     2.4  package org.openrdf.query.resultio.text;
     2.5  
     2.6 +import java.io.IOException;
     2.7  import java.io.OutputStream;
     2.8  
     2.9 +import org.openrdf.model.Value;
    2.10 +import org.openrdf.model.impl.LiteralImpl;
    2.11 +import org.openrdf.query.algebra.evaluation.function.spatial.StrabonPolyhedron;
    2.12 +import org.openrdf.query.algebra.evaluation.util.JTSWrapper;
    2.13  import org.openrdf.query.resultio.text.tsv.SPARQLResultsTSVWriter;
    2.14 +import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron;
    2.15 +import org.slf4j.Logger;
    2.16 +import org.slf4j.LoggerFactory;
    2.17  
    2.18  /**
    2.19   * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    2.20 - *
    2.21 + * 
    2.22   */
    2.23  public class stSPARQLResultsTSVWriter extends SPARQLResultsTSVWriter {
    2.24  
    2.25 +	private static Logger logger = LoggerFactory.getLogger(org.openrdf.query.resultio.text.stSPARQLResultsTSVWriter.class);
    2.26 +	
    2.27  	public stSPARQLResultsTSVWriter(OutputStream out) {
    2.28  		super(out);
    2.29  	}
    2.30  
    2.31 +	@Override
    2.32 +	protected void writeValue(Value val) throws IOException {
    2.33 +		// catch the spatial case and create a literal
    2.34 +		// constructing a new literal is the only way if we want to reuse the {@link #writeValue(Value)} method
    2.35 +		if (val instanceof GeneralDBPolyhedron) {
    2.36 +			GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) val;
    2.37 +			if (StrabonPolyhedron.ogcGeometry.equals(dbpolyhedron.getDatatype().stringValue())) {
    2.38 +				// WKT
    2.39 +				val = new LiteralImpl(JTSWrapper.getInstance().WKTwrite(dbpolyhedron.getPolyhedron().getGeometry()), dbpolyhedron.getDatatype());
    2.40 +				
    2.41 +			} else { // TODO GML
    2.42 +				logger.warn("[Strabon.TSVWriter] GML is not supported yet.");
    2.43 +				
    2.44 +			}
    2.45 +		} 
    2.46 +		
    2.47 +		// write value
    2.48 +		super.writeValue(val);
    2.49 +		
    2.50 +	}
    2.51  }
     3.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Mon Jun 25 15:50:16 2012 +0300
     3.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Mon Jun 25 17:57:51 2012 +0300
     3.3 @@ -1,6 +1,5 @@
     3.4  package eu.earthobservatory.runtime.generaldb;
     3.5  
     3.6 -import java.io.DataOutputStream;
     3.7  import java.io.File;
     3.8  import java.io.FileNotFoundException;
     3.9  import java.io.FileOutputStream;
    3.10 @@ -18,17 +17,6 @@
    3.11  import java.util.zip.ZipEntry;
    3.12  import java.util.zip.ZipOutputStream;
    3.13  
    3.14 -import org.geotools.data.simple.SimpleFeatureCollection;
    3.15 -import org.geotools.feature.FeatureCollections;
    3.16 -import org.geotools.feature.simple.SimpleFeatureBuilder;
    3.17 -import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
    3.18 -import org.geotools.geojson.feature.FeatureJSON;
    3.19 -import org.geotools.referencing.CRS;
    3.20 -import org.opengis.feature.simple.SimpleFeature;
    3.21 -import org.opengis.feature.simple.SimpleFeatureType;
    3.22 -import org.opengis.referencing.FactoryException;
    3.23 -import org.opengis.referencing.NoSuchAuthorityCodeException;
    3.24 -import org.opengis.referencing.crs.CoordinateReferenceSystem;
    3.25  import org.openrdf.model.URI;
    3.26  import org.openrdf.model.Value;
    3.27  import org.openrdf.model.ValueFactory;
    3.28 @@ -43,11 +31,11 @@
    3.29  import org.openrdf.query.TupleQueryResultHandlerException;
    3.30  import org.openrdf.query.Update;
    3.31  import org.openrdf.query.UpdateExecutionException;
    3.32 -import org.openrdf.query.algebra.evaluation.function.spatial.StrabonPolyhedron;
    3.33 -import org.openrdf.query.resultio.sparqljson.stSPARQLResultsGeoJSONWriter;
    3.34 +import org.openrdf.query.resultio.sparqljson.stSPARQLResultsGeoJSONWriterFactory;
    3.35  import org.openrdf.query.resultio.sparqlxml.Format;
    3.36 -import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsKMLWriter;
    3.37 -import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsXMLWriter;
    3.38 +import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsKMLWriterFactory;
    3.39 +import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsXMLWriterFactory;
    3.40 +import org.openrdf.query.resultio.text.stSPARQLResultsTSVWriterFactory;
    3.41  import org.openrdf.repository.RepositoryException;
    3.42  import org.openrdf.repository.sail.SailRepository;
    3.43  import org.openrdf.repository.sail.SailRepositoryConnection;
    3.44 @@ -58,16 +46,10 @@
    3.45  import org.openrdf.rio.RDFParser;
    3.46  import org.openrdf.rio.Rio;
    3.47  import org.openrdf.rio.ntriples.NTriplesWriter;
    3.48 -import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron;
    3.49  import org.openrdf.sail.helpers.SailBase;
    3.50 -import org.openrdf.sail.rdbms.model.RdbmsLiteral;
    3.51  import org.slf4j.Logger;
    3.52  import org.slf4j.LoggerFactory;
    3.53  
    3.54 -import com.vividsolutions.jts.geom.Geometry;
    3.55 -import com.vividsolutions.jts.io.ParseException;
    3.56 -import com.vividsolutions.jts.io.WKTReader;
    3.57 -
    3.58  public abstract class Strabon {
    3.59  
    3.60  	private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.runtime.generaldb.Strabon.class);
    3.61 @@ -241,19 +223,14 @@
    3.62  			status = false;
    3.63  		}
    3.64  		
    3.65 +		if (logger.isDebugEnabled()) {
    3.66 +			logger.debug("Serializing results ({})", resultsFormat.name());
    3.67 +		}
    3.68 +		
    3.69  		TupleQueryResult result = null;
    3.70 -		
    3.71  		switch (resultsFormat) {
    3.72  			case DEFAULT:
    3.73 -				
    3.74 -				result = tupleQuery.evaluate();
    3.75 -				while (result.hasNext()) {
    3.76 -					BindingSet bindingSet = result.next();
    3.77 -					
    3.78 -					writeString(out, bindingSet.toString());
    3.79 -					writeString(out, NEWLINE);
    3.80 -				}
    3.81 -
    3.82 +				tupleQuery.evaluate(new stSPARQLResultsTSVWriterFactory().getWriter(out));
    3.83  				break;
    3.84  				
    3.85  			case EXP:
    3.86 @@ -273,22 +250,18 @@
    3.87  				break;
    3.88  				
    3.89  			case XML:
    3.90 -				if (logger.isDebugEnabled()) {
    3.91 -					logger.debug("Serializing results (XML)");
    3.92 -				}
    3.93 -				
    3.94 -				tupleQuery.evaluate(new stSPARQLResultsXMLWriter(out));
    3.95 +				tupleQuery.evaluate(new stSPARQLResultsXMLWriterFactory().getWriter(out));
    3.96  				break;
    3.97  				
    3.98  			case KML:
    3.99 -				if (logger.isDebugEnabled()) {
   3.100 -					logger.debug("Serializing results (KML)");
   3.101 -				}
   3.102 -
   3.103 -				tupleQuery.evaluate(new stSPARQLResultsKMLWriter(out));
   3.104 +				tupleQuery.evaluate(new stSPARQLResultsKMLWriterFactory().getWriter(out));
   3.105 +				break;
   3.106  				
   3.107 +			case GEOJSON:
   3.108 +				tupleQuery.evaluate(new stSPARQLResultsGeoJSONWriterFactory().getWriter(out));
   3.109  				break;
   3.110  				
   3.111 +				
   3.112  			case KMZ:
   3.113  				// create a zip entry
   3.114  				ZipEntry entry = new ZipEntry("sparql_results.kml");
   3.115 @@ -300,7 +273,7 @@
   3.116  				kmzout.putNextEntry(entry);
   3.117  				
   3.118  				// pass the zip stream for evaluation
   3.119 -				tupleQuery.evaluate(new stSPARQLResultsKMLWriter(kmzout));
   3.120 +				tupleQuery.evaluate(new stSPARQLResultsKMLWriterFactory().getWriter(kmzout));
   3.121  
   3.122  				// close the zip entry
   3.123  				kmzout.closeEntry();
   3.124 @@ -309,16 +282,7 @@
   3.125  				kmzout.close();
   3.126  				break;
   3.127  				
   3.128 -			case GEOJSON:
   3.129 -				if (logger.isDebugEnabled()) {
   3.130 -					logger.debug("Serializing results (GEOJSON)");
   3.131 -				}
   3.132 -
   3.133 -				tupleQuery.evaluate(new stSPARQLResultsGeoJSONWriter(out));
   3.134 -				break;
   3.135 -				
   3.136  			case HTML:
   3.137 -		
   3.138  				result = tupleQuery.evaluate();
   3.139  				
   3.140  				if (result.hasNext()) {
   3.141 @@ -391,12 +355,12 @@
   3.142  		}
   3.143  	}
   3.144  
   3.145 -	public void storeInRepo(String src, String format) throws RDFParseException, RepositoryException, IOException,InvalidDatasetFormatFault, RDFHandlerException
   3.146 +	public void storeInRepo(String src, String format) throws RDFParseException, RepositoryException, IOException, RDFHandlerException, InvalidDatasetFormatFault
   3.147  	{
   3.148  		storeInRepo(src, null, null, format);
   3.149  	}
   3.150  
   3.151 -	public void storeInRepo(String src, String baseURI, String context, String format) throws RDFParseException, RepositoryException, IOException,InvalidDatasetFormatFault, RDFHandlerException
   3.152 +	public void storeInRepo(String src, String baseURI, String context, String format) throws RDFParseException, RepositoryException, IOException, RDFHandlerException, InvalidDatasetFormatFault
   3.153  	{
   3.154  		RDFFormat realFormat = null;
   3.155