| |
---|
| | |
---|
| | import java.io.ByteArrayInputStream; |
---|
| | import java.io.ByteArrayOutputStream; |
---|
| | import java.io.File; |
---|
| | import java.io.OutputStream; |
---|
| | import java.io.FileOutputStream; |
---|
| | import java.io.IOException; |
---|
| | import java.io.PipedReader; |
---|
| | import java.io.PipedWriter; |
---|
| | import java.nio.file.Files; |
---|
| | import java.nio.file.Paths; |
---|
| | 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; |
---|
| |
---|
| | ToPostgis type; |
---|
| | String dbname; |
---|
| | |
---|
| | @SuppressWarnings({"UseSpecificCatch", "CallToPrintStackTrace"}) |
---|
| | public static void main(String[] args) { |
---|
| | public static void main(String[] args) throws IOException { |
---|
| | String dbname = "busstop"; |
---|
| | (new UnMapped(dbname)).createKml(35.4341254D,139.408969D, 3.0D); |
---|
| | ByteArrayOutputStream outKmz = (new UnMapped(dbname)).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) { |
---|
| | this.dbname = dbname; |
---|
| | } |
---|
| | |
---|
| | @SuppressWarnings({"CallToPrintStackTrace", "UseSpecificCatch"}) |
---|
| | public void createKml(double lat, double lon, double km) { |
---|
| | public ByteArrayOutputStream createKmz(double lat, double lon, double km) { |
---|
| | try { |
---|
| | final PipedReader read = new PipedReader(); |
---|
| | final PipedWriter write = new PipedWriter(read); |
---|
| | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| | final ByteArrayOutputStream outKml = new ByteArrayOutputStream(); |
---|
| | String title = "'"+ dbname +"' - UnMapped OpenSteetMap"; |
---|
| | |
---|
| | Thread kml = new Thread(new Kml(write, dbname, title, lat,lon, km)); |
---|
| | Thread converter = new Thread(new PipeConverter(read, out)); |
---|
| | Thread converter = new Thread(new PipeConverter(read, outKml)); |
---|
| | |
---|
| | kml.start(); |
---|
| | converter.start(); |
---|
| | try { |
---|
| |
---|
| | catch(InterruptedException ex) { |
---|
| | ex.printStackTrace(); |
---|
| | } |
---|
| | |
---|
| | File kmzFile = new File(dbname +".kmz"); |
---|
| | //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 (OutputStream outKmz = Files.newOutputStream(Paths.get(kmzFile.getName())); |
---|
| | ArchiveOutputStream o = new ZipArchiveOutputStream(outKmz)) |
---|
| | { |
---|
| | try (ArchiveOutputStream o = new ZipArchiveOutputStream(outKmz)) { |
---|
| | ArchiveEntry entry = o.createArchiveEntry(kmlFile, kmlFile.getName()); |
---|
| | o.putArchiveEntry(entry); |
---|
| | try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) { |
---|
| | try (ByteArrayInputStream in = new ByteArrayInputStream(outKml.toByteArray())) { |
---|
| | IOUtils.copy(in, o); |
---|
| | } |
---|
| | o.closeArchiveEntry(); |
---|
| | } |
---|
| | } |
---|
| | finally { |
---|
| | out.flush(); |
---|
| | out.close(); |
---|
| | outKml.flush(); |
---|
| | outKml.close(); |
---|
| | } |
---|
| | return outKmz; |
---|
| | } |
---|
| | catch(Exception e) { |
---|
| | e.printStackTrace(); |
---|
| | return null; |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |