package osm.jp.postgis; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; public class UnMapped { ToPostgis type; String dbname; String gisdb; @SuppressWarnings({"UseSpecificCatch", "CallToPrintStackTrace"}) public static void main(String[] args) throws IOException { String dbname = "busstop"; if (args.length < 1) { throw new IOException("args[0] <--- 'gisdb'"); } ByteArrayOutputStream outKmz = (new UnMapped(dbname, args[0])).createKmz(35.4341254D,139.408969D, 3.0D); try (ByteArrayInputStream in = new ByteArrayInputStream(outKmz.toByteArray())) { File kmzFile = new File("kmz.kmz"); FileOutputStream o = new FileOutputStream(kmzFile); IOUtils.copy(in, o); } } public UnMapped(String dbname, String gisdb) { this.dbname = dbname; this.gisdb = gisdb; } @SuppressWarnings({"CallToPrintStackTrace", "UseSpecificCatch"}) public ByteArrayOutputStream createKmz(double lat, double lon, double km) { try { final PipedReader read = new PipedReader(); final PipedWriter write = new PipedWriter(read); final ByteArrayOutputStream outKml = new ByteArrayOutputStream(); String title = "'"+ dbname +"' - UnMapped OpenSteetMap"; Thread kml = new Thread(new Kml(write, dbname, title, lat,lon, km, this.gisdb)); Thread converter = new Thread(new PipeConverter(read, outKml)); kml.start(); converter.start(); try { kml.join(); converter.join(); } catch(InterruptedException ex) { ex.printStackTrace(); } //File kmzFile = new File(dbname +".kmz"); //OutputStream outKmz = Files.newOutputStream(Paths.get(dbname +".kmz")); final ByteArrayOutputStream outKmz = new ByteArrayOutputStream(); File kmlFile = new File(dbname +".kml"); try { try (ArchiveOutputStream o = new ZipArchiveOutputStream(outKmz)) { ArchiveEntry entry = o.createArchiveEntry(kmlFile, kmlFile.getName()); o.putArchiveEntry(entry); try (ByteArrayInputStream in = new ByteArrayInputStream(outKml.toByteArray())) { IOUtils.copy(in, o); } o.closeArchiveEntry(); } } finally { outKml.flush(); outKml.close(); } return outKmz; } catch(Exception e) { e.printStackTrace(); return null; } } }