Strabon
changeset 325:90c0265f8020
delegate KMZ encoding to KMLWriter
author | Babis Nikolaou <charnik@di.uoa.gr> |
---|---|
date | Fri Jun 22 13:45:05 2012 +0300 (2012-06-22) |
parents | fe02c447ee46 |
children | b29f0bd444a1 |
files | resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsKMLWriter.java runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java |
line diff
1.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsKMLWriter.java Fri Jun 22 12:49:26 2012 +0300 1.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsKMLWriter.java Fri Jun 22 13:45:05 2012 +0300 1.3 @@ -26,16 +26,13 @@ 1.4 1.5 import com.vividsolutions.jts.geom.Geometry; 1.6 import com.vividsolutions.jts.geom.GeometryCollection; 1.7 -import com.vividsolutions.jts.geom.GeometryFactory; 1.8 import com.vividsolutions.jts.geom.LineString; 1.9 import com.vividsolutions.jts.geom.MultiLineString; 1.10 import com.vividsolutions.jts.geom.MultiPoint; 1.11 import com.vividsolutions.jts.geom.MultiPolygon; 1.12 import com.vividsolutions.jts.geom.Point; 1.13 import com.vividsolutions.jts.geom.Polygon; 1.14 -import com.vividsolutions.jts.geom.PrecisionModel; 1.15 import com.vividsolutions.jts.io.ParseException; 1.16 -import com.vividsolutions.jts.io.WKTReader; 1.17 1.18 /** 1.19 * 1.20 @@ -115,9 +112,7 @@ 1.21 1.22 /** 1.23 * Creates an stSPARQLResultsKMLWriter that encodes the SPARQL 1.24 - * results in KML/KMZ. 1.25 - * 1.26 - * TODO: KMZ 1.27 + * results in KML. 1.28 * 1.29 * @param out 1.30 * @throws UnsupportedEncodingException
2.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java Fri Jun 22 12:49:26 2012 +0300 2.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java Fri Jun 22 13:45:05 2012 +0300 2.3 @@ -18,18 +18,13 @@ 2.4 import java.util.zip.ZipEntry; 2.5 import java.util.zip.ZipOutputStream; 2.6 2.7 -import javax.xml.namespace.QName; 2.8 - 2.9 import org.apache.commons.io.output.ByteArrayOutputStream; 2.10 import org.geotools.data.simple.SimpleFeatureCollection; 2.11 import org.geotools.feature.FeatureCollections; 2.12 import org.geotools.feature.simple.SimpleFeatureBuilder; 2.13 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; 2.14 import org.geotools.geojson.feature.FeatureJSON; 2.15 -import org.geotools.kml.KML; 2.16 -import org.geotools.kml.KMLConfiguration; 2.17 import org.geotools.referencing.CRS; 2.18 -import org.geotools.xml.Encoder; 2.19 import org.opengis.feature.simple.SimpleFeature; 2.20 import org.opengis.feature.simple.SimpleFeatureType; 2.21 import org.opengis.referencing.FactoryException; 2.22 @@ -69,15 +64,6 @@ 2.23 import org.slf4j.LoggerFactory; 2.24 2.25 import com.vividsolutions.jts.geom.Geometry; 2.26 -import com.vividsolutions.jts.geom.GeometryCollection; 2.27 -import com.vividsolutions.jts.geom.GeometryFactory; 2.28 -import com.vividsolutions.jts.geom.LineString; 2.29 -import com.vividsolutions.jts.geom.MultiLineString; 2.30 -import com.vividsolutions.jts.geom.MultiPoint; 2.31 -import com.vividsolutions.jts.geom.MultiPolygon; 2.32 -import com.vividsolutions.jts.geom.Point; 2.33 -import com.vividsolutions.jts.geom.Polygon; 2.34 -import com.vividsolutions.jts.geom.PrecisionModel; 2.35 import com.vividsolutions.jts.io.ParseException; 2.36 import com.vividsolutions.jts.io.WKTReader; 2.37 2.38 @@ -436,214 +422,35 @@ 2.39 System.out.println(retStream.toString()); 2.40 2.41 } 2.42 - else if (resultsFormat.equalsIgnoreCase("KML")) { 2.43 + else if (resultsFormat.equalsIgnoreCase("KML") || resultsFormat.equalsIgnoreCase("KMZ")) { 2.44 if (logger.isDebugEnabled()) { 2.45 - logger.debug("Serializing results (KML)"); 2.46 + logger.debug("Serializing results (KML/KMZ)"); 2.47 } 2.48 2.49 - tupleQuery.evaluate(new stSPARQLResultsKMLWriter(retStream)); 2.50 - System.out.println(retStream.toString()); 2.51 + if (resultsFormat.equalsIgnoreCase("KML")) { 2.52 + tupleQuery.evaluate(new stSPARQLResultsKMLWriter(System.out)); 2.53 + 2.54 + } else { 2.55 + // create a zip entry 2.56 + ZipEntry entry = new ZipEntry("sparql_results.kml"); 2.57 + 2.58 + // create a zip stream on standard output 2.59 + ZipOutputStream kmzout = new ZipOutputStream(System.out); 2.60 + 2.61 + // add the zip entry in it 2.62 + kmzout.putNextEntry(entry); 2.63 + 2.64 + // pass the zip stream for evaluation 2.65 + tupleQuery.evaluate(new stSPARQLResultsKMLWriter(kmzout)); 2.66 + 2.67 + // close the zip entry 2.68 + kmzout.closeEntry(); 2.69 + 2.70 + // close the zip stream 2.71 + kmzout.close(); 2.72 + } 2.73 2.74 - } else if (resultsFormat.equalsIgnoreCase("KMZ")) { 2.75 - //GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null); 2.76 - GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); 2.77 - WKTReader reader = new WKTReader(gf); 2.78 - ByteArrayOutputStream baos = new ByteArrayOutputStream(); 2.79 - 2.80 - DataOutputStream dos = new DataOutputStream(baos); 2.81 - 2.82 - //used to construct the entire kml document 2.83 - StringBuilder sb = new StringBuilder(); 2.84 - 2.85 - TupleQueryResult result = null; 2.86 - try { 2.87 - result = tupleQuery.evaluate(); 2.88 - } catch (QueryEvaluationException e1) { 2.89 - logger.error("[Strabon.query] Error in query evaluation.", e1); 2.90 - } 2.91 - 2.92 - int resultCounter = 0; 2.93 - try { 2.94 - while (result.hasNext()) { 2.95 - BindingSet bindingSet = result.next(); 2.96 - ret.add(bindingSet.toString()); 2.97 - Set<String> bindingNames = bindingSet.getBindingNames(); 2.98 - resultCounter++; 2.99 - int geometryCounter = 0; 2.100 - for(String bindingName : bindingNames) 2.101 - { 2.102 - String unparsed = bindingSet.getBinding(bindingName).getValue().toString(); 2.103 - String corrResult = unparsed.substring(1,unparsed.length()-1); 2.104 - 2.105 - try { 2.106 - Geometry geom = reader.read(corrResult); 2.107 - //Integer s = new Integer(4326); 2.108 - //geom.setSRID(4326); 2.109 - 2.110 - //geom.setSRID(32630); 2.111 - 2.112 - //CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32630"); 2.113 - //CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326"); 2.114 - 2.115 - //MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true); 2.116 - 2.117 - //geom = JTS.transform(geom, transform); 2.118 - //geom.geometryChanged(); 2.119 - geometryCounter++; 2.120 - //Only way to reach this point is if the corrResult is indeed in the form of WKT 2.121 - QName geometryType = null; 2.122 - if(geom instanceof Point) 2.123 - { 2.124 - geometryType = KML.Point; 2.125 - } 2.126 - else if(geom instanceof Polygon) 2.127 - { 2.128 - geometryType = KML.Polygon; 2.129 - } 2.130 - else if(geom instanceof LineString) 2.131 - { 2.132 - geometryType = KML.LineString; 2.133 - } 2.134 - else if(geom instanceof MultiPoint) 2.135 - { 2.136 - geometryType = KML.MultiGeometry; 2.137 - } 2.138 - else if(geom instanceof MultiLineString) 2.139 - { 2.140 - geometryType = KML.MultiGeometry; 2.141 - 2.142 - } 2.143 - else if(geom instanceof MultiPolygon) 2.144 - { 2.145 - geometryType = KML.MultiGeometry; 2.146 - 2.147 - } 2.148 - else if(geom instanceof GeometryCollection) 2.149 - { 2.150 - geometryType = KML.MultiGeometry; 2.151 - 2.152 - } 2.153 - else //TODO exception should be thrown here --> Specialize it 2.154 - { 2.155 - //System.out.println("Wrong Handling--> "+geometryType.toString()); 2.156 - throw new Exception("Wrong Handling--> "+geom.toString()); 2.157 - 2.158 - } 2.159 - 2.160 - //Encoding to KML 2.161 - Encoder encoder = new Encoder(new KMLConfiguration()); 2.162 - encoder.setIndenting(true); 2.163 - //encoder.encode(geom, geometryType, dos); 2.164 - encoder.encode(geom, geometryType, baos); 2.165 - //storing the freshly produced kml element 2.166 - corrResult = baos.toString(); 2.167 - //removing the xml header 2.168 - corrResult = corrResult.substring(38); 2.169 - 2.170 - //Constructing each individual element 2.171 - sb.append("\n<Placemark>"); 2.172 - corrResult = corrResult.replaceAll("xmlns:kml=\"http://earth.google.com/kml/2.1\"","").replaceAll("kml:",""); 2.173 - sb.append("\n<name> Geometry"+resultCounter+"_"+geometryCounter+"</name>"); 2.174 - sb.append("\n<description>"); 2.175 - //Time to fill the description 2.176 - 2.177 - if(bindingNames.size() > 1) 2.178 - { 2.179 - //Creating Row1 --> names 2.180 - sb.append("<![CDATA[<table border=\"1\"> <tr>"); 2.181 - for(String otherBinding: bindingNames) 2.182 - { 2.183 - if(!otherBinding.equals(bindingName)) 2.184 - { 2.185 - sb.append("<td>"); 2.186 - sb.append(otherBinding); 2.187 - sb.append("</td>"); 2.188 - } 2.189 - } 2.190 - sb.append("</tr>"); 2.191 - 2.192 - sb.append("<tr>"); 2.193 - for(String otherBinding: bindingNames) 2.194 - { 2.195 - if(!otherBinding.equals(bindingName)) 2.196 - { 2.197 - sb.append("<td>"); 2.198 - 2.199 - String bindingValue = bindingSet.getBinding(otherBinding).getValue().toString(); 2.200 - sb.append(bindingValue); 2.201 - sb.append("</td>"); 2.202 - } 2.203 - } 2.204 - sb.append("</tr></table>]]>"); 2.205 - } 2.206 - else 2.207 - { 2.208 - sb.append("mantalakia"); 2.209 - } 2.210 - sb.append("</description>"); 2.211 - 2.212 - sb.append(corrResult); 2.213 - sb.append("\n</Placemark>\n"); 2.214 - 2.215 - //emptying the buffer 2.216 - baos.reset(); 2.217 - 2.218 - } catch (ParseException e) { 2.219 - // this is not WKT 2.220 - // this exception is not error! pass by thematic values and keep searching for geometries 2.221 - } catch (Exception e) { 2.222 - logger.error("[Strabon.query] Received exception during KML construction.", e); 2.223 - } 2.224 - 2.225 - //Start populating KML here 2.226 - } 2.227 - // System.out.println(bindingSet.toString()); 2.228 - // out.writeChars(bindingSet.toString()); 2.229 - } 2.230 - } catch (QueryEvaluationException e) { 2.231 - logger.error("[Strabon.query] Error during query evaluation.", e); 2.232 - } 2.233 - try { 2.234 - dos.close(); 2.235 - } catch (IOException e) { 2.236 - logger.error("[Strabon.query] Error during querying.", e); 2.237 - } 2.238 - 2.239 - //Finishing the structure of the kml document 2.240 - sb.insert(0,"<?xml version=\"1.0\" encoding=\"UTF-8\"?> <kml xmlns=\"http://www.opengis.net/kml/2.2\"> <Folder>"); 2.241 - sb.append("</Folder></kml>"); 2.242 - 2.243 - try { 2.244 - //String cstr = new String("aa", "UTF8"); 2.245 - // ggarbis: For too large strings (e.g., 44MB) it returns empty string. 2.246 - // String newString = new String(sb.toString().getBytes(), Charset.availableCharsets().get("UTF-8")); 2.247 - 2.248 - if(resultsFormat.equalsIgnoreCase("KML")) 2.249 - { 2.250 - // writeOut.write(newString); 2.251 - writeOut.write(sb.toString()); 2.252 - // System.out.println(newString); 2.253 - } 2.254 - else //KMZ 2.255 - { 2.256 - //compress 2.257 - ZipOutputStream kmzout = new ZipOutputStream(retStream); 2.258 - ZipEntry entry = new ZipEntry("doc.kml"); 2.259 - 2.260 - //kmzout.setLevel(6); 2.261 - kmzout.putNextEntry(entry); 2.262 - // kmzout.write(newString.getBytes()); 2.263 - kmzout.write(sb.toString().getBytes()); 2.264 - kmzout.closeEntry(); 2.265 - kmzout.close(); 2.266 - 2.267 - } 2.268 - 2.269 - } catch (IOException e) { 2.270 - logger.error("[Strabon.query]", e); 2.271 - } 2.272 - } 2.273 - else if(resultsFormat.equalsIgnoreCase("HTML")) { 2.274 + } else if(resultsFormat.equalsIgnoreCase("HTML")) { 2.275 //System.out.println("Placemark1!!"); 2.276 TupleQueryResult result = tupleQuery.evaluate(); 2.277 // List<String> bindingNames = result.getBindingNames();