diff --git a/src/osm/jp/postgis/UnMapped.java b/src/osm/jp/postgis/UnMapped.java index 956d46e..3f5a796 100644 --- a/src/osm/jp/postgis/UnMapped.java +++ b/src/osm/jp/postgis/UnMapped.java @@ -3,11 +3,10 @@ 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; @@ -18,9 +17,14 @@ 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) { @@ -28,15 +32,15 @@ } @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(); @@ -48,27 +52,29 @@ 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; } } } diff --git a/test/busstop.kmz b/test/busstop.kmz deleted file mode 100644 index 30e8330..0000000 --- a/test/busstop.kmz +++ /dev/null Binary files differ diff --git a/test/osm/jp/postgis/UnMappedTest.java b/test/osm/jp/postgis/UnMappedTest.java index 98090de..da4bf8b 100644 --- a/test/osm/jp/postgis/UnMappedTest.java +++ b/test/osm/jp/postgis/UnMappedTest.java @@ -100,7 +100,7 @@ void check(String dbname) { File kmz = null; try { - (new UnMapped(dbname)).createKml(35.4341254D,139.408969D, 5.0D); + (new UnMapped(dbname)).createKmz(35.4341254D,139.408969D, 5.0D); } catch(Exception e) { fail(e.toString());