Strabon

changeset 357:28025a51cab6

fixed identification of spatial values in KML
author Babis Nikolaou <charnik@di.uoa.gr>
date Mon Jun 25 20:03:10 2012 +0300 (2012-06-25)
parents 0d7dbda7c52b
children ef3b1a05b95b
files evaluation/src/main/java/org/openrdf/query/algebra/evaluation/iterator/StSPARQLGroupIterator.java generaldb/src/main/java/org/openrdf/sail/generaldb/GeneralDBValueFactory.java generaldb/src/main/java/org/openrdf/sail/generaldb/model/XMLGSDatatypeUtil.java monetdb/src/main/java/org/openrdf/sail/monetdb/iteration/MonetDBBindingIteration.java postgis/src/main/java/org/openrdf/sail/postgis/iteration/PostGISBindingIteration.java resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsKMLWriter.java resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java
line diff
     1.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/iterator/StSPARQLGroupIterator.java	Mon Jun 25 19:47:25 2012 +0300
     1.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/iterator/StSPARQLGroupIterator.java	Mon Jun 25 20:03:10 2012 +0300
     1.3 @@ -357,7 +357,7 @@
     1.4  					if(val instanceof StrabonPolyhedron)
     1.5  					{
     1.6  						String label = val.toString()+";http://www.opengis.net/def/crs/EPSG/0/"+((StrabonPolyhedron)val).getGeometry().getSRID();
     1.7 -						Literal wkt = new LiteralImpl(label,new URIImpl(StrabonPolyhedron.ogcGeometry));
     1.8 +						Literal wkt = new LiteralImpl(label,new URIImpl(StrabonPolyhedron.WKT));
     1.9  						sol.setBinding(name,wkt);
    1.10  					}
    1.11  					else
     2.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/GeneralDBValueFactory.java	Mon Jun 25 19:47:25 2012 +0300
     2.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/GeneralDBValueFactory.java	Mon Jun 25 20:03:10 2012 +0300
     2.3 @@ -239,7 +239,7 @@
     2.4      /****************************************************/
     2.5      public RdbmsLiteral asRdbmsLiteral(GeneralDBPolyhedron polyhedron) {
     2.6              try {
     2.7 -                    URI wkt = new URIImpl(StrabonPolyhedron.ogcGeometry);
     2.8 +                    URI wkt = new URIImpl(StrabonPolyhedron.WKT);
     2.9                      RdbmsLiteral literal = new RdbmsLiteral(polyhedron.getInternalId(), polyhedron.getVersion(),new LiteralImpl(polyhedron.stringValue(), wkt));
    2.10  
    2.11                      if (polyhedron instanceof GeneralDBPolyhedron) {
    2.12 @@ -262,7 +262,7 @@
    2.13      
    2.14      public RdbmsLiteral asRdbmsLiteral(StrabonPolyhedron polyhedron) {
    2.15          try {
    2.16 -                URI wkt = new URIImpl(StrabonPolyhedron.ogcGeometry);
    2.17 +                URI wkt = new URIImpl(StrabonPolyhedron.WKT);
    2.18                  RdbmsLiteral literal = new RdbmsLiteral(new LiteralImpl(polyhedron.stringValue(), wkt));
    2.19  
    2.20                  if (polyhedron instanceof StrabonPolyhedron) {
     3.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/model/XMLGSDatatypeUtil.java	Mon Jun 25 19:47:25 2012 +0300
     3.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/model/XMLGSDatatypeUtil.java	Mon Jun 25 20:03:10 2012 +0300
     3.3 @@ -10,7 +10,9 @@
     3.4  import javax.xml.datatype.XMLGregorianCalendar;
     3.5  import javax.xml.namespace.QName;
     3.6  
     3.7 +import org.openrdf.model.Literal;
     3.8  import org.openrdf.model.URI;
     3.9 +import org.openrdf.model.Value;
    3.10  import org.openrdf.model.datatypes.XMLDateTime;
    3.11  import org.openrdf.model.vocabulary.XMLSchema;
    3.12  
    3.13 @@ -37,6 +39,50 @@
    3.14  	 *-------------------*/
    3.15  
    3.16  	/**
    3.17 +	 * Returns true when the given value is an instance of class @{link GeneralDBPolyhedron} 
    3.18 +	 * or @{link Literal} with datatype @{link StrabonPolyhedron#ogcGeometry} (WKT) or 
    3.19 +	 * @{link StrabonPolyhedron#gml} (GML).  
    3.20 +	 * 
    3.21 +	 * @param value
    3.22 +	 * @return
    3.23 +	 * @author Charalampos Nikolaou <charniK@di.uoa.gr>
    3.24 +	 */
    3.25 +	public static boolean isGeometryValue(Value value) {
    3.26 +		if (value instanceof Literal) {
    3.27 +			if (StrabonPolyhedron.WKT.equals(((Literal) value).getDatatype().stringValue()) || 
    3.28 +				StrabonPolyhedron.GML.equals(((Literal) value).getDatatype().stringValue())) {
    3.29 +				return true;
    3.30 +			}
    3.31 +		} else if (value instanceof GeneralDBPolyhedron) {
    3.32 +			return true;
    3.33 +		}
    3.34 +		
    3.35 +		return false;
    3.36 +	}
    3.37 +	
    3.38 +	/**
    3.39 +	 * Returns true when the given literal has as datatype the WKT URI as it is
    3.40 +	 * in @{link StrabonPolyhedron.ogcGeometry}.
    3.41 +	 * 
    3.42 +	 * @param literal
    3.43 +	 * @return
    3.44 +	 */
    3.45 +	public static boolean isWKTLiteral(Literal literal) {
    3.46 +		return StrabonPolyhedron.WKT.equals(literal.getDatatype().stringValue());
    3.47 +	}
    3.48 +	
    3.49 +	/**
    3.50 +	 * Returns true when the given literal has as datatype the GML URI as it is
    3.51 +	 * in @{link StrabonPolyhedron.gml}.
    3.52 +	 * 
    3.53 +	 * @param literal
    3.54 +	 * @return
    3.55 +	 */
    3.56 +	public static boolean isGMLLiteral(Literal literal) {
    3.57 +		return StrabonPolyhedron.GML.equals(literal.getDatatype().stringValue());
    3.58 +	}
    3.59 +	
    3.60 +	/**
    3.61  	 * FIXME needs retouching
    3.62  	 * My addition!!
    3.63  	 * Checks whether the supplied datatype has a geospatial meaning
     4.1 --- a/monetdb/src/main/java/org/openrdf/sail/monetdb/iteration/MonetDBBindingIteration.java	Mon Jun 25 19:47:25 2012 +0300
     4.2 +++ b/monetdb/src/main/java/org/openrdf/sail/monetdb/iteration/MonetDBBindingIteration.java	Mon Jun 25 20:03:10 2012 +0300
     4.3 @@ -48,7 +48,7 @@
     4.4  			Blob labelBlob = rs.getBlob(index + 1);
     4.5      		byte[] label = labelBlob.getBytes((long)1, (int)labelBlob.length());
     4.6      		int srid = rs.getInt(index + 2);
     4.7 -			return vf.getRdbmsPolyhedron(id, StrabonPolyhedron.ogcGeometry, label, srid);
     4.8 +			return vf.getRdbmsPolyhedron(id, StrabonPolyhedron.WKT, label, srid);
     4.9  
    4.10  		}
    4.11  
    4.12 @@ -64,6 +64,6 @@
    4.13  		Blob labelBlob = rs.getBlob(index + 1); 
    4.14  		byte[] label = labelBlob.getBytes((long)1, (int)labelBlob.length());
    4.15  		int srid = rs.getInt(index + 2);
    4.16 -		return vf.getRdbmsPolyhedron(114, StrabonPolyhedron.ogcGeometry, label, srid);
    4.17 +		return vf.getRdbmsPolyhedron(114, StrabonPolyhedron.WKT, label, srid);
    4.18  	}
    4.19  }
    4.20 \ No newline at end of file
     5.1 --- a/postgis/src/main/java/org/openrdf/sail/postgis/iteration/PostGISBindingIteration.java	Mon Jun 25 19:47:25 2012 +0300
     5.2 +++ b/postgis/src/main/java/org/openrdf/sail/postgis/iteration/PostGISBindingIteration.java	Mon Jun 25 20:03:10 2012 +0300
     5.3 @@ -47,7 +47,7 @@
     5.4  		{
     5.5  			byte[] label = rs.getBytes(index + 1);
     5.6  			int srid = rs.getInt(index + 2);
     5.7 -			return vf.getRdbmsPolyhedron(id, StrabonPolyhedron.ogcGeometry, label, srid);
     5.8 +			return vf.getRdbmsPolyhedron(id, StrabonPolyhedron.WKT, label, srid);
     5.9  
    5.10  		}
    5.11  
    5.12 @@ -62,7 +62,7 @@
    5.13  		//Case of spatial constructs
    5.14  		byte[] label = rs.getBytes(index + 1);
    5.15  		int srid = rs.getInt(index + 2);
    5.16 -		return vf.getRdbmsPolyhedron(114, StrabonPolyhedron.ogcGeometry, label, srid);
    5.17 +		return vf.getRdbmsPolyhedron(114, StrabonPolyhedron.WKT, label, srid);
    5.18  
    5.19  	}
    5.20  }
    5.21 \ No newline at end of file
     6.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsKMLWriter.java	Mon Jun 25 19:47:25 2012 +0300
     6.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsKMLWriter.java	Mon Jun 25 20:03:10 2012 +0300
     6.3 @@ -10,6 +10,7 @@
     6.4  import org.geotools.kml.KML;
     6.5  import org.geotools.kml.KMLConfiguration;
     6.6  import org.geotools.xml.Encoder;
     6.7 +import org.openrdf.model.BNode;
     6.8  import org.openrdf.model.Literal;
     6.9  import org.openrdf.model.Value;
    6.10  import org.openrdf.query.Binding;
    6.11 @@ -21,6 +22,8 @@
    6.12  import org.openrdf.query.resultio.TupleQueryResultFormat;
    6.13  import org.openrdf.query.resultio.TupleQueryResultWriter;
    6.14  import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    6.15 +import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron;
    6.16 +import org.openrdf.sail.generaldb.model.XMLGSDatatypeUtil;
    6.17  import org.slf4j.Logger;
    6.18  import org.slf4j.LoggerFactory;
    6.19  
    6.20 @@ -161,6 +164,10 @@
    6.21  			xmlWriter.endDocument();
    6.22  			
    6.23  			baos.close();
    6.24 +			
    6.25 +			if (nresults > 0 && ngeometries == 0) {
    6.26 +				logger.warn("[Strabon.KMLWriter] No spatial binding found in the result. KML requires that at least one binding maps to a geometry.", nresults);
    6.27 +			}
    6.28  		}
    6.29  		catch (IOException e) {
    6.30  			throw new TupleQueryResultHandlerException(e);
    6.31 @@ -170,8 +177,12 @@
    6.32  	@Override
    6.33  	public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    6.34  		try {
    6.35 +			// true if there are bindings that do not correspond to geometries
    6.36  			boolean hasDesc = false;
    6.37  			
    6.38 +			// increase result size
    6.39 +			nresults++;
    6.40 +			
    6.41  			// create description table and header
    6.42  			indent(descHeader, depth);
    6.43  			descHeader.append(TABLE_DESC_BEGIN);
    6.44 @@ -190,35 +201,20 @@
    6.45  			// parse binding set
    6.46  			for (Binding binding : bindingSet) {
    6.47  				Value value = binding.getValue();
    6.48 -				if (value instanceof Literal) {
    6.49 -					Literal litValue = (Literal) value;
    6.50  
    6.51 -					// it's a spatial literal
    6.52 -					if (litValue.getDatatype().stringValue().equals(StrabonPolyhedron.ogcGeometry)) {
    6.53 -						// TODO Check for GML (when added to StrabonPolyhedron)
    6.54 -						ngeometries++;
    6.55 +				// check for geometry value
    6.56 +				if (XMLGSDatatypeUtil.isGeometryValue(value)) {
    6.57 +					ngeometries++;
    6.58  						
    6.59 -						if (logger.isDebugEnabled()) {
    6.60 -							logger.debug("[Strabon] Found geometry: {}", litValue);
    6.61 -						}
    6.62 -						
    6.63 -						xmlWriter.unescapedText(getKML(value));
    6.64 -						
    6.65 -					} else { // Literal other than geometry
    6.66 -						if (logger.isDebugEnabled()) {
    6.67 -							logger.debug("[Strabon.KMLWriter] Found Literal: {}", value);
    6.68 -						}
    6.69 -						
    6.70 -						// mark that we found sth corresponding to the description
    6.71 -						hasDesc = true;
    6.72 -						
    6.73 -						// write description
    6.74 -						writeDesc(binding);
    6.75 +					if (logger.isDebugEnabled()) {
    6.76 +						logger.debug("[Strabon] Found geometry: {}", value);
    6.77  					}
    6.78  					
    6.79 -				} else { // URI or BlankNode
    6.80 +					xmlWriter.unescapedText(getKML(value));
    6.81 +				
    6.82 +				} else { // URI, BlankNode, or Literal other than spatial literal 
    6.83  					if (logger.isDebugEnabled()) {
    6.84 -						logger.debug("[Strabon.KMLWriter] Found URI/BlankNode: {}", value);
    6.85 +						logger.debug("[Strabon.KMLWriter] Found URI/BlankNode/Literal: {}", value);
    6.86  					}
    6.87  					
    6.88  					// mark that we found sth corresponding to the description
    6.89 @@ -266,9 +262,6 @@
    6.90  
    6.91  			// write the placemark
    6.92  			xmlWriter.endTag(PLACEMARK_TAG);
    6.93 -			
    6.94 -			// increase result size
    6.95 -			nresults++;
    6.96  		}
    6.97  		catch (IOException e) {
    6.98  			throw new TupleQueryResultHandlerException(e);
    6.99 @@ -278,19 +271,42 @@
   6.100  	private String getKML(Value value) {
   6.101  		String kml = "";
   6.102  		QName geometryType = null;
   6.103 +		
   6.104 +		// the underlying geometry in value
   6.105  		Geometry geom = null;
   6.106  		
   6.107 +		// the underlying SRID of the geometry
   6.108 +		int srid = -1;
   6.109 +		
   6.110  		// get the KML encoder
   6.111  		Encoder encoder = null;
   6.112  		
   6.113  		try {
   6.114  			encoder = new Encoder(new KMLConfiguration());
   6.115  			encoder.setIndenting(true);
   6.116 -			geom = jts.WKTread(WKTHelper.getWithoutSRID(value.stringValue()));
   6.117 -			//geom = new WKTReader(gf).read(value.stringValue());
   6.118 +			
   6.119 +			if (value instanceof GeneralDBPolyhedron) {
   6.120 +				GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) value;
   6.121 +				
   6.122 +				geom = dbpolyhedron.getPolyhedron().getGeometry();
   6.123 +				srid = dbpolyhedron.getPolyhedron().getGeometry().getSRID();
   6.124 +				
   6.125 +			} else { // spatial literal
   6.126 +				Literal spatial = (Literal) value;
   6.127 +				
   6.128 +				if (XMLGSDatatypeUtil.isWKTLiteral(spatial)) { // WKT
   6.129 +					String wkt = spatial.stringValue();
   6.130 +					
   6.131 +					geom = jts.WKTread(WKTHelper.getWithoutSRID(wkt));
   6.132 +					srid = WKTHelper.getSRID(wkt);
   6.133 +					
   6.134 +				} else { // GML
   6.135 +					logger.warn("[Strabon.KMLWriter] GML is not supported yet");
   6.136 +				}
   6.137 +			}
   6.138  			
   6.139  			// transform the geometry to 4326
   6.140 -			geom = jts.transform(geom, WKTHelper.getSRID(value.stringValue()), StrabonPolyhedron.defaultSRID);
   6.141 +			geom = jts.transform(geom, srid, StrabonPolyhedron.defaultSRID);
   6.142  			
   6.143  			if (geom instanceof Point) {
   6.144  				geometryType = KML.Point;
   6.145 @@ -350,6 +366,9 @@
   6.146  		descData.append(NEWLINE);
   6.147  		indent(descData, depth + 1);
   6.148  		descData.append(TABLE_DATA_BEGIN);
   6.149 +		if (binding.getValue() instanceof BNode) {
   6.150 +			descData.append("_:");
   6.151 +		}
   6.152  		descData.append(binding.getValue().stringValue());
   6.153  		descData.append(TABLE_DATA_END);
   6.154  		
     7.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java	Mon Jun 25 19:47:25 2012 +0300
     7.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java	Mon Jun 25 20:03:10 2012 +0300
     7.3 @@ -156,7 +156,7 @@
     7.4  		} 
     7.5  		else { // spatial literal
     7.6  			// else if (value instanceof RdbmsPolyhedron)
     7.7 -			URI datatype = new URIImpl(StrabonPolyhedron.ogcGeometry);
     7.8 +			URI datatype = new URIImpl(StrabonPolyhedron.WKT);
     7.9  			Literal literal = new LiteralImpl(value.stringValue(), datatype);
    7.10  			writeLiteral(literal);
    7.11  		}
     8.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java	Mon Jun 25 19:47:25 2012 +0300
     8.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java	Mon Jun 25 20:03:10 2012 +0300
     8.3 @@ -5,12 +5,8 @@
     8.4  
     8.5  import org.openrdf.model.Value;
     8.6  import org.openrdf.model.impl.LiteralImpl;
     8.7 -import org.openrdf.query.algebra.evaluation.function.spatial.StrabonPolyhedron;
     8.8 -import org.openrdf.query.algebra.evaluation.util.JTSWrapper;
     8.9  import org.openrdf.query.resultio.text.tsv.SPARQLResultsTSVWriter;
    8.10  import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron;
    8.11 -import org.slf4j.Logger;
    8.12 -import org.slf4j.LoggerFactory;
    8.13  
    8.14  /**
    8.15   * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    8.16 @@ -18,26 +14,17 @@
    8.17   */
    8.18  public class stSPARQLResultsTSVWriter extends SPARQLResultsTSVWriter {
    8.19  
    8.20 -	private static Logger logger = LoggerFactory.getLogger(org.openrdf.query.resultio.text.stSPARQLResultsTSVWriter.class);
    8.21 -	
    8.22  	public stSPARQLResultsTSVWriter(OutputStream out) {
    8.23  		super(out);
    8.24  	}
    8.25  
    8.26  	@Override
    8.27  	protected void writeValue(Value val) throws IOException {
    8.28 -		// catch the spatial case and create a literal
    8.29 +		// catch the spatial case and create a new literal
    8.30  		// constructing a new literal is the only way if we want to reuse the {@link #writeValue(Value)} method
    8.31  		if (val instanceof GeneralDBPolyhedron) {
    8.32  			GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) val;
    8.33 -			if (StrabonPolyhedron.ogcGeometry.equals(dbpolyhedron.getDatatype().stringValue())) {
    8.34 -				// WKT
    8.35 -				val = new LiteralImpl(JTSWrapper.getInstance().WKTwrite(dbpolyhedron.getPolyhedron().getGeometry()), dbpolyhedron.getDatatype());
    8.36 -				
    8.37 -			} else { // TODO GML
    8.38 -				logger.warn("[Strabon.TSVWriter] GML is not supported yet.");
    8.39 -				
    8.40 -			}
    8.41 +			val = new LiteralImpl(dbpolyhedron.getPolyhedronStringRep(), dbpolyhedron.getDatatype());
    8.42  		} 
    8.43  		
    8.44  		// write value