Strabon
changeset 133:abb14d6fd936
KMZ support in Strabon
author | Manos Karpathiotakis <mk@di.uoa.gr> |
---|---|
date | Tue May 08 18:32:08 2012 +0300 (2012-05-08) |
parents | 6945f58b1619 |
children | ddaa14997077 |
files | runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java |
line diff
1.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java Tue May 08 17:36:10 2012 +0300 1.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java Tue May 08 18:32:08 2012 +0300 1.3 @@ -2,6 +2,7 @@ 1.4 1.5 import java.io.DataOutputStream; 1.6 import java.io.File; 1.7 +import java.io.FileInputStream; 1.8 import java.io.FileNotFoundException; 1.9 import java.io.FileOutputStream; 1.10 import java.io.IOException; 1.11 @@ -16,6 +17,8 @@ 1.12 import java.sql.SQLException; 1.13 import java.util.ArrayList; 1.14 import java.util.Set; 1.15 +import java.util.zip.ZipEntry; 1.16 +import java.util.zip.ZipOutputStream; 1.17 1.18 import javax.xml.namespace.QName; 1.19 1.20 @@ -295,7 +298,7 @@ 1.21 if(val instanceof GeneralDBPolyhedron) 1.22 { 1.23 spatial = true; 1.24 - 1.25 + 1.26 } 1.27 1.28 if(spatial) 1.29 @@ -337,7 +340,7 @@ 1.30 tb[i].add(otherBinding,String.class); 1.31 } 1.32 } 1.33 - 1.34 + 1.35 1.36 int SRID=4326; 1.37 Geometry geom = null; 1.38 @@ -349,7 +352,7 @@ 1.39 SRID = ((GeneralDBPolyhedron) unparsedGeometry).getPolyhedron().getGeometry().getSRID(); 1.40 } 1.41 else //RdbmsLiteral 1.42 - //TODO GML support to be added 1.43 + //TODO GML support to be added 1.44 { 1.45 String unparsedWKT = ((RdbmsLiteral)unparsedGeometry).getLabel(); 1.46 try { 1.47 @@ -373,7 +376,7 @@ 1.48 e.printStackTrace(); 1.49 } 1.50 } 1.51 - 1.52 + 1.53 CoordinateReferenceSystem geomCRS = null; 1.54 try { 1.55 geomCRS = CRS.decode("EPSG:"+SRID); 1.56 @@ -383,11 +386,11 @@ 1.57 } catch (FactoryException e) { 1.58 e.printStackTrace(); 1.59 } 1.60 - 1.61 + 1.62 tb[i].setCRS(geomCRS); 1.63 tb[i].setSRS("EPSG:"+SRID); 1.64 tb[i].add("geometry",Geometry.class); 1.65 - 1.66 + 1.67 SimpleFeatureType featureType = tb[i].buildFeatureType(); 1.68 SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); 1.69 1.70 @@ -398,17 +401,17 @@ 1.71 featureBuilder.add(bindingSet.getValue(otherBinding)); 1.72 } 1.73 } 1.74 - 1.75 + 1.76 featureBuilder.add(geom); 1.77 - 1.78 - 1.79 + 1.80 + 1.81 SimpleFeature feature = featureBuilder.buildFeature(null); 1.82 sfCollection.add(feature); 1.83 1.84 -// FeatureJSON fjson22 = new FeatureJSON(); 1.85 -// fjson22.setEncodeFeatureCRS(true); 1.86 -// fjson22.writeFeatureCollection(sfCollection, dos); 1.87 -// System.out.println(retStream.toString()); 1.88 + // FeatureJSON fjson22 = new FeatureJSON(); 1.89 + // fjson22.setEncodeFeatureCRS(true); 1.90 + // fjson22.writeFeatureCollection(sfCollection, dos); 1.91 + // System.out.println(retStream.toString()); 1.92 } 1.93 1.94 } 1.95 @@ -420,221 +423,7 @@ 1.96 System.out.println(retStream.toString()); 1.97 1.98 } 1.99 - else if ( resultsFormat.equalsIgnoreCase("KMZ") ) { 1.100 - //GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null); 1.101 - GeometryFactory gf = new GeometryFactory(new PrecisionModel(),4326); 1.102 - WKTReader reader = new WKTReader(gf); 1.103 - ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1.104 - 1.105 - DataOutputStream dos = new DataOutputStream(baos); 1.106 - 1.107 - //used to construct the entire kml document 1.108 - StringBuilder sb = new StringBuilder(); 1.109 - 1.110 - TupleQueryResult result = null; 1.111 - try { 1.112 - result = tupleQuery.evaluate(); 1.113 - } catch (QueryEvaluationException e1) { 1.114 - // TODO Auto-generated catch block 1.115 - e1.printStackTrace(); 1.116 - } 1.117 - //System.out.println("-------------------------------------------"); 1.118 - //System.out.println("- RESULTS -"); 1.119 - //System.out.println("-------------------------------------------"); 1.120 - 1.121 - int resultCounter = 0; 1.122 - try { 1.123 - while (result.hasNext()) { 1.124 - BindingSet bindingSet = result.next(); 1.125 - ret.add(bindingSet.toString()); 1.126 - Set<String> bindingNames = bindingSet.getBindingNames(); 1.127 - resultCounter++; 1.128 - int geometryCounter = 0; 1.129 - for(String bindingName : bindingNames) 1.130 - { 1.131 - String unparsed = bindingSet.getBinding(bindingName).getValue().toString(); 1.132 - String corrResult = unparsed.substring(1,unparsed.length()-1); 1.133 - 1.134 - try { 1.135 - Geometry geom = reader.read(corrResult); 1.136 - geom.setSRID(4326); 1.137 - //geom.setSRID(32630); 1.138 - 1.139 - //CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32630"); 1.140 - //CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326"); 1.141 - 1.142 - //MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true); 1.143 - 1.144 - //geom = JTS.transform(geom, transform); 1.145 - //geom.geometryChanged(); 1.146 - geometryCounter++; 1.147 - //Only way to reach this point is if the corrResult is indeed in the form of WKT 1.148 - QName geometryType = null; 1.149 - if(geom instanceof Point) 1.150 - { 1.151 - geometryType = KML.Point; 1.152 - } 1.153 - else if(geom instanceof Polygon) 1.154 - { 1.155 - geometryType = KML.Polygon; 1.156 - } 1.157 - else if(geom instanceof LineString) 1.158 - { 1.159 - geometryType = KML.LineString; 1.160 - } 1.161 - else if(geom instanceof MultiPoint) 1.162 - { 1.163 - geometryType = KML.MultiGeometry; 1.164 - } 1.165 - else if(geom instanceof MultiLineString) 1.166 - { 1.167 - geometryType = KML.MultiGeometry; 1.168 - 1.169 - } 1.170 - else if(geom instanceof MultiPolygon) 1.171 - { 1.172 - geometryType = KML.MultiGeometry; 1.173 - 1.174 - } 1.175 - else if(geom instanceof GeometryCollection) 1.176 - { 1.177 - geometryType = KML.MultiGeometry; 1.178 - 1.179 - } 1.180 - else //TODO exception should be thrown here --> Specialize it 1.181 - { 1.182 - //System.out.println("Wrong Handling--> "+geometryType.toString()); 1.183 - throw new Exception("Wrong Handling--> "+geom.toString()); 1.184 - 1.185 - } 1.186 - 1.187 - //Encoding to KML 1.188 - Encoder encoder = new Encoder(new KMLConfiguration()); 1.189 - encoder.setIndenting(true); 1.190 - //encoder.encode(geom, geometryType, dos); 1.191 - encoder.encode(geom, geometryType, baos); 1.192 - //storing the freshly produced kml element 1.193 - corrResult = baos.toString(); 1.194 - //removing the xml header 1.195 - corrResult = corrResult.substring(38); 1.196 - 1.197 - //Constructing each individual element 1.198 - sb.append("\n<Placemark>"); 1.199 - corrResult = corrResult.replaceAll("xmlns:kml=\"http://earth.google.com/kml/2.1\"","").replaceAll("kml:",""); 1.200 - sb.append("\n<name> Geometry"+resultCounter+"_"+geometryCounter+"</name>"); 1.201 - sb.append("\n<description>"); 1.202 - //Time to fill the description 1.203 - 1.204 - if(bindingNames.size() > 1) 1.205 - { 1.206 - //Creating Row1 --> names 1.207 - sb.append("<![CDATA[<table border=\"1\"> <tr>"); 1.208 - for(String otherBinding: bindingNames) 1.209 - { 1.210 - if(!otherBinding.equals(bindingName)) 1.211 - { 1.212 - sb.append("<td>"); 1.213 - sb.append(otherBinding); 1.214 - sb.append("</td>"); 1.215 - } 1.216 - } 1.217 - sb.append("</tr>"); 1.218 - 1.219 - sb.append("<tr>"); 1.220 - for(String otherBinding: bindingNames) 1.221 - { 1.222 - if(!otherBinding.equals(bindingName)) 1.223 - { 1.224 - sb.append("<td>"); 1.225 - 1.226 - String bindingValue = bindingSet.getBinding(otherBinding).getValue().toString(); 1.227 - sb.append(bindingValue); 1.228 - sb.append("</td>"); 1.229 - } 1.230 - } 1.231 - sb.append("</table>]]>"); 1.232 - } 1.233 - else 1.234 - { 1.235 - sb.append("mantalakia"); 1.236 - } 1.237 - sb.append("</description>"); 1.238 - 1.239 - sb.append(corrResult); 1.240 - sb.append("\n</Placemark>\n"); 1.241 - 1.242 - //emptying the buffer 1.243 - baos.reset(); 1.244 - 1.245 - } catch (ParseException e) { 1.246 - //Den prokeitai gia WKT 1.247 - //System.out.println(bindingSet.toString()); 1.248 - //e.printStackTrace(); 1.249 - } catch (Exception e) { 1.250 - e.printStackTrace(); 1.251 - } 1.252 - 1.253 - //Start populating KML here 1.254 - } 1.255 - // System.out.println(bindingSet.toString()); 1.256 - // out.writeChars(bindingSet.toString()); 1.257 - } 1.258 - } catch (QueryEvaluationException e) { 1.259 - // TODO Auto-generated catch block 1.260 - e.printStackTrace(); 1.261 - } 1.262 - try { 1.263 - dos.close(); 1.264 - } catch (IOException e) { 1.265 - // TODO Auto-generated catch block 1.266 - e.printStackTrace(); 1.267 - } 1.268 - 1.269 - //Finishing the structure of the kml document 1.270 - sb.insert(0,"<?xml version=\"1.0\" encoding=\"UTF-8\"?> <kml xmlns=\"http://www.opengis.net/kml/2.2\"> <Folder>"); 1.271 - sb.append("</Folder></kml>"); 1.272 - 1.273 - //System.out.println(sb.toString()); 1.274 - //System.out.println(baos.toString()); 1.275 - 1.276 - // StringBuilder sb = new StringBuilder(); 1.277 - // sb.append(baos.toString().replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>","")); 1.278 - // sb.insert(0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <kml xmlns=\"http://www.opengis.net/kml/2.2\">" + 1.279 - // "<kml:Placemark xmlns:kml=\"http://www.opengis.net/kml/2.2\">"); 1.280 - // sb.append("</kml:Placemark></kml>"); 1.281 - 1.282 - 1.283 - //System.out.println("*******************************"); 1.284 - //sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 1.285 - //System.out.println(sb.toString()); 1.286 - 1.287 - //System.out.println(sb.toString()); 1.288 - 1.289 - //XXX Probably not needed after all 1.290 - // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 1.291 - // DocumentBuilder builder = factory.newDocumentBuilder(); 1.292 - // StringReader sr = new StringReader(sb.toString()); 1.293 - // Document document = builder.parse(new InputSource(sr)); 1.294 - 1.295 - 1.296 - //System.out.println("-------------------------------------------"); 1.297 - System.out.flush(); 1.298 - 1.299 - try { 1.300 - //String cstr = new String("aa", "UTF8"); 1.301 - String newString = new String(sb.toString().getBytes(), Charset.availableCharsets().get("UTF-8")); 1.302 - writeOut.write(newString); 1.303 - //out.writeBytes(newString); 1.304 - 1.305 - // out.writeBytes(sb.toString()); 1.306 - 1.307 - } catch (IOException e) { 1.308 - // TODO Auto-generated catch block 1.309 - e.printStackTrace(); 1.310 - } 1.311 - 1.312 - } 1.313 - else if ( resultsFormat.equalsIgnoreCase("KML") ) { 1.314 + else if ( resultsFormat.equalsIgnoreCase("KML") || resultsFormat.equalsIgnoreCase("KMZ")) { 1.315 //GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null); 1.316 GeometryFactory gf = new GeometryFactory(new PrecisionModel(),4326); 1.317 WKTReader reader = new WKTReader(gf); 1.318 @@ -839,10 +628,27 @@ 1.319 try { 1.320 //String cstr = new String("aa", "UTF8"); 1.321 String newString = new String(sb.toString().getBytes(), Charset.availableCharsets().get("UTF-8")); 1.322 - writeOut.write(newString); 1.323 - //out.writeBytes(newString); 1.324 1.325 - // out.writeBytes(sb.toString()); 1.326 + if(resultsFormat.equalsIgnoreCase("KML")) 1.327 + { 1.328 + writeOut.write(newString); 1.329 +// System.out.println(newString); 1.330 + } 1.331 + else //KMZ 1.332 + { 1.333 + //compress 1.334 +// FileOutputStream fos = new FileOutputStream(new File("deleteme.kmz")); 1.335 + ZipOutputStream kmzout = new ZipOutputStream(retStream); 1.336 +// ZipOutputStream kmzout = new ZipOutputStream(fos); 1.337 + ZipEntry entry = new ZipEntry("doc.kml"); 1.338 + 1.339 + kmzout.setLevel(9); 1.340 + kmzout.putNextEntry(entry); 1.341 + kmzout.write(newString.getBytes()); 1.342 + kmzout.closeEntry(); 1.343 + kmzout.close(); 1.344 + 1.345 + } 1.346 1.347 } catch (IOException e) { 1.348 // TODO Auto-generated catch block