Strabon

changeset 917:7e9173482c03

KMLWriter now checks also for geometries with wktLiteral datatype
author Babis Nikolaou <charnik@di.uoa.gr>
date Sun Mar 24 23:33:47 2013 +0200 (2013-03-24)
parents b58aa113f25e
children 454a685fbf10
files resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java
line diff
     1.1 --- a/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java	Sun Mar 24 23:33:02 2013 +0200
     1.2 +++ b/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java	Sun Mar 24 23:33:47 2013 +0200
     1.3 @@ -25,8 +25,8 @@
     1.4  import org.openrdf.query.Binding;
     1.5  import org.openrdf.query.BindingSet;
     1.6  import org.openrdf.query.TupleQueryResultHandlerException;
     1.7 +import org.openrdf.query.algebra.evaluation.function.spatial.AbstractWKT;
     1.8  import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants;
     1.9 -import org.openrdf.query.algebra.evaluation.function.spatial.WKTHelper;
    1.10  import org.openrdf.query.algebra.evaluation.util.JTSWrapper;
    1.11  import org.openrdf.query.resultio.TupleQueryResultFormat;
    1.12  import org.openrdf.query.resultio.TupleQueryResultWriter;
    1.13 @@ -63,16 +63,11 @@
    1.14  	private static final String PLACEMARK_TAG 		= "Placemark";
    1.15  	private static final String NAME_TAG 			= "name";
    1.16  	private static final String DESC_TAG 			= "description";
    1.17 -	private static final String STYLE_TAG 			= "Style";
    1.18 -	private static final String STYLEMAP_TAG 		= "StyleMap";
    1.19 -	private static final String LINESTYLE_TAG 		= "LineStyle";
    1.20 -	private static final String POLYSTYLE_TAG 		= "PolyStyle";
    1.21  	private static final String EXT_DATA_TAG 		= "ExtendedData";
    1.22  	private static final String DATA_TAG 			= "Data";
    1.23  	private static final String VALUE_TAG			= "value";
    1.24  	private static final String NAME_ATTR			= NAME_TAG;
    1.25  
    1.26 -	private static final String STYLE_ID 			= "resultStyle";
    1.27  	private static final String TABLE_ROW_BEGIN 		= "<TR>";
    1.28  	private static final String TABLE_ROW_END 		= "</TR>";
    1.29  	private static final String TABLE_DATA_BEGIN 		= "<TD>";
    1.30 @@ -84,34 +79,6 @@
    1.31  	private static final String GEOMETRY_NAME 		= "Geometry";
    1.32  	private static final String MULTIGEOMETRY 		= "MultiGeometry";
    1.33  
    1.34 -	/*
    1.35 -	// Styling options
    1.36 -	private static final int numOfStyles = 5;
    1.37 -	private static final String[][] styles = {
    1.38 -			// note that colors are encoded as "aabbggrr" strings where
    1.39 -			// aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff);
    1.40 -			// rr=red
    1.41 -			// (00 to ff).
    1.42 -			// id, line width, line color, polygon fill, mouse over line width,
    1.43 -			// mouse over line color mouse over polygon fill
    1.44 -			// {STYLE_ID + "1", "1.5", "7d0000ff", "ad0000ff", "1.5",
    1.45 -			// "7d0000ff", "ad0000ff"}, {STYLE_ID + "2", "1.5", "7d0000ff",
    1.46 -			// "ad0000ff", "1.5", "7d0000ff", "ad0000ff"}, {STYLE_ID + "3",
    1.47 -			// "1.5", "7d550000", "ad550000", "1.5", "7d0000ff", "ad0000ff"},
    1.48 -			// {STYLE_ID + "4", "1.5", "7d005500", "ad005500", "1.5",
    1.49 -			// "7d0000ff", "ad0000ff"}, {STYLE_ID + "5", "1.5", "7d000055",
    1.50 -			// "ad000055", "1.5", "7d0000ff", "ad0000ff"}};
    1.51 -			{ STYLE_ID + "1", "1.5", "000000ff", "000000ff", "1.5", "000000ff",
    1.52 -					"000000ff" },
    1.53 -			{ STYLE_ID + "2", "1.5", "000000ff", "000000ff", "1.5", "000000ff",
    1.54 -					"000000ff" },
    1.55 -			{ STYLE_ID + "3", "1.5", "7d550000", "ad550000", "1.5", "7d0000ff",
    1.56 -					"ad0000ff" },
    1.57 -			{ STYLE_ID + "4", "1.5", "7d005500", "ad005500", "1.5", "7d0000ff",
    1.58 -					"ad0000ff" },
    1.59 -			{ STYLE_ID + "5", "1.5", "7dff0000", "adff0000", "1.5", "7dff0000",
    1.60 -					"adff0000" } };
    1.61 -	*/
    1.62  	/**
    1.63  	 * The underlying XML formatter.
    1.64  	 */
    1.65 @@ -176,65 +143,13 @@
    1.66  	}
    1.67  
    1.68  	@Override
    1.69 -	public void startQueryResult(List<String> bindingNames)
    1.70 -			throws TupleQueryResultHandlerException {
    1.71 +	public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
    1.72  		try {
    1.73 -
    1.74  			xmlWriter.startDocument();
    1.75  			xmlWriter.setAttribute("xmlns", NAMESPACE);
    1.76  			xmlWriter.startTag(ROOT_TAG);
    1.77  			xmlWriter.startTag(RESULT_SET_TAG);
    1.78  
    1.79 -			/*
    1.80 -			// add default styles
    1.81 -			for (String[] style : styles) {
    1.82 -				String id = style[0];
    1.83 -				String lineWidth = style[1];
    1.84 -				String lineColor = style[2];
    1.85 -				String polygonFill = style[3];
    1.86 -				String mouseOverLineWidth = style[4];
    1.87 -				String mouseOverLineColor = style[5];
    1.88 -				String mouseOverPolygonFill = style[6];
    1.89 -
    1.90 -				// append normal style
    1.91 -				xmlWriter.setAttribute("id", "normal_" + id);
    1.92 -				xmlWriter.startTag(STYLE_TAG);
    1.93 -				xmlWriter.startTag(LINESTYLE_TAG);
    1.94 -				xmlWriter.textElement("width", lineWidth);
    1.95 -				xmlWriter.textElement("color", lineColor);
    1.96 -				xmlWriter.endTag(LINESTYLE_TAG);
    1.97 -				xmlWriter.startTag(POLYSTYLE_TAG);
    1.98 -				xmlWriter.textElement("color", polygonFill);
    1.99 -				xmlWriter.endTag(POLYSTYLE_TAG);
   1.100 -				xmlWriter.endTag(STYLE_TAG);
   1.101 -
   1.102 -				// append highlight style
   1.103 -				xmlWriter.setAttribute("id", "highlight_" + id);
   1.104 -				xmlWriter.startTag(STYLE_TAG);
   1.105 -				xmlWriter.startTag(LINESTYLE_TAG);
   1.106 -				xmlWriter.textElement("width", mouseOverLineWidth);
   1.107 -				xmlWriter.textElement("color", mouseOverLineColor);
   1.108 -				xmlWriter.endTag(LINESTYLE_TAG);
   1.109 -				xmlWriter.startTag(POLYSTYLE_TAG);
   1.110 -				xmlWriter.textElement("color", mouseOverPolygonFill);
   1.111 -				xmlWriter.endTag(POLYSTYLE_TAG);
   1.112 -				xmlWriter.endTag(STYLE_TAG);
   1.113 -
   1.114 -				// define map style combining the above styles
   1.115 -				xmlWriter.setAttribute("id", id);
   1.116 -				xmlWriter.startTag(STYLEMAP_TAG);
   1.117 -				xmlWriter.startTag("Pair");
   1.118 -				xmlWriter.textElement("key", "normal");
   1.119 -				xmlWriter.textElement("styleUrl", "#normal_" + id);
   1.120 -				xmlWriter.endTag("Pair");
   1.121 -				xmlWriter.startTag("Pair");
   1.122 -				xmlWriter.textElement("key", "highlight");
   1.123 -				xmlWriter.textElement("styleUrl", "#highlight_" + id);
   1.124 -				xmlWriter.endTag("Pair");
   1.125 -				xmlWriter.endTag(STYLEMAP_TAG);
   1.126 -			}
   1.127 -			// end of default style definition
   1.128 -			*/
   1.129  		} catch (IOException e) {
   1.130  			throw new TupleQueryResultHandlerException(e);
   1.131  		}
   1.132 @@ -313,7 +228,6 @@
   1.133  				for (String geometry : geometries) {
   1.134  					xmlWriter.startTag(PLACEMARK_TAG);
   1.135  					xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME);
   1.136 -					//xmlWriter.textElement("styleUrl", "#"+ styles[geometries.indexOf(geometry) % (numOfStyles - 2)][0]);
   1.137  					xmlWriter.startTag(MULTIGEOMETRY);
   1.138  					xmlWriter.unescapedText(geometry);
   1.139  					xmlWriter.endTag(MULTIGEOMETRY);
   1.140 @@ -324,7 +238,6 @@
   1.141  			// also write them in the same placemarks
   1.142  			xmlWriter.startTag(PLACEMARK_TAG);
   1.143  			xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME);
   1.144 -			//xmlWriter.textElement("styleUrl", "#" + styles[(numOfStyles - 1)][0]);
   1.145  			xmlWriter.startTag(MULTIGEOMETRY);
   1.146  			
   1.147  			for (String geometry : geometries) {
   1.148 @@ -384,48 +297,69 @@
   1.149  
   1.150  	private String getGeometry(Value value) {
   1.151  		String geometry = "";
   1.152 +		
   1.153  		QName geometryType = null;
   1.154 +		
   1.155  		// the underlying geometry in value
   1.156  		Geometry geom = null;
   1.157 +		
   1.158  		// the underlying SRID of the geometry
   1.159  		int srid = -1;
   1.160 +		
   1.161  		// get the KML encoder
   1.162  		Encoder encoder = null;
   1.163 +		
   1.164  		try {
   1.165  			encoder = new Encoder(new KMLConfiguration());
   1.166  			encoder.setIndenting(true);
   1.167 +			
   1.168  			if (value instanceof GeneralDBPolyhedron) {
   1.169  				GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) value;
   1.170  				geom = dbpolyhedron.getPolyhedron().getGeometry();
   1.171  				srid = dbpolyhedron.getPolyhedron().getGeometry().getSRID();
   1.172 +				
   1.173  			} else { // spatial literal
   1.174  				Literal spatial = (Literal) value;
   1.175  				String geomRep = spatial.stringValue();
   1.176 +				
   1.177  				if (XMLGSDatatypeUtil.isWKTLiteral(spatial)) { // WKT
   1.178 -					geom = jts.WKTread(WKTHelper.getWithoutSRID(geomRep));
   1.179 -					srid = WKTHelper.getSRID(geomRep);
   1.180 +					AbstractWKT awkt = new AbstractWKT(geomRep, spatial.getDatatype().stringValue());
   1.181 +					
   1.182 +					geom = jts.WKTread(awkt.getWKT());
   1.183 +					srid = awkt.getSRID();
   1.184 +					
   1.185  				} else { // GML
   1.186  					geom = jts.GMLread(geomRep);
   1.187  					srid = geom.getSRID();
   1.188  				}
   1.189  			}
   1.190 +			
   1.191  			// transform the geometry to {@link GeoConstants#defaultSRID}
   1.192  			geom = jts.transform(geom, srid, GeoConstants.defaultSRID);
   1.193 +			
   1.194  			if (geom instanceof Point) {
   1.195  				geometryType = KML.Point;
   1.196 +				
   1.197  			} else if (geom instanceof Polygon) {
   1.198  				geometryType = KML.Polygon;
   1.199 +				
   1.200  			} else if (geom instanceof LineString) {
   1.201  				geometryType = KML.LineString;
   1.202 +				
   1.203  			} else if (geom instanceof MultiPoint) {
   1.204  				geometryType = KML.MultiGeometry;
   1.205 +				
   1.206  			} else if (geom instanceof MultiLineString) {
   1.207  				geometryType = KML.MultiGeometry;
   1.208 +				
   1.209  			} else if (geom instanceof MultiPolygon) {
   1.210  				geometryType = KML.MultiGeometry;
   1.211 +				
   1.212  			} else if (geom instanceof GeometryCollection) {
   1.213  				geometryType = KML.MultiGeometry;
   1.214 +				
   1.215  			}
   1.216 +			
   1.217  			if (geometryType == null) {
   1.218  				logger.warn("[Strabon.KMLWriter] Found unknown geometry type.");
   1.219