Newer
Older
osmCoverage / src / osm / jp / postgis / UnMapped.java
  1. package osm.jp.postgis;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.PipedReader;
  9. import java.io.PipedWriter;
  10. import org.apache.commons.compress.archivers.ArchiveEntry;
  11. import org.apache.commons.compress.archivers.ArchiveOutputStream;
  12. import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
  13. import org.apache.commons.compress.utils.IOUtils;
  14.  
  15. public class UnMapped {
  16. ToPostgis type;
  17. String dbname;
  18. @SuppressWarnings({"UseSpecificCatch", "CallToPrintStackTrace"})
  19. public static void main(String[] args) throws IOException {
  20. String dbname = "busstop";
  21. ByteArrayOutputStream outKmz = (new UnMapped(dbname)).createKmz(35.4341254D,139.408969D, 3.0D);
  22. try (ByteArrayInputStream in = new ByteArrayInputStream(outKmz.toByteArray())) {
  23. File kmzFile = new File("kmz.kmz");
  24. FileOutputStream o = new FileOutputStream(kmzFile);
  25. IOUtils.copy(in, o);
  26. }
  27. }
  28. public UnMapped(String dbname) {
  29. this.dbname = dbname;
  30. }
  31. @SuppressWarnings({"CallToPrintStackTrace", "UseSpecificCatch"})
  32. public ByteArrayOutputStream createKmz(double lat, double lon, double km) {
  33. try {
  34. final PipedReader read = new PipedReader();
  35. final PipedWriter write = new PipedWriter(read);
  36. final ByteArrayOutputStream outKml = new ByteArrayOutputStream();
  37. String title = "'"+ dbname +"' - UnMapped OpenSteetMap";
  38. Thread kml = new Thread(new Kml(write, dbname, title, lat,lon, km));
  39. Thread converter = new Thread(new PipeConverter(read, outKml));
  40. kml.start();
  41. converter.start();
  42. try {
  43. kml.join();
  44. converter.join();
  45. }
  46. catch(InterruptedException ex) {
  47. ex.printStackTrace();
  48. }
  49. //File kmzFile = new File(dbname +".kmz");
  50. //OutputStream outKmz = Files.newOutputStream(Paths.get(dbname +".kmz"));
  51. final ByteArrayOutputStream outKmz = new ByteArrayOutputStream();
  52. File kmlFile = new File(dbname +".kml");
  53. try {
  54. try (ArchiveOutputStream o = new ZipArchiveOutputStream(outKmz)) {
  55. ArchiveEntry entry = o.createArchiveEntry(kmlFile, kmlFile.getName());
  56. o.putArchiveEntry(entry);
  57. try (ByteArrayInputStream in = new ByteArrayInputStream(outKml.toByteArray())) {
  58. IOUtils.copy(in, o);
  59. }
  60. o.closeArchiveEntry();
  61. }
  62. }
  63. finally {
  64. outKml.flush();
  65. outKml.close();
  66. }
  67. return outKmz;
  68. }
  69. catch(Exception e) {
  70. e.printStackTrace();
  71. return null;
  72. }
  73. }
  74. }