Newer
Older
osmCoverage / src / osm / jp / postgis / UnMapped.java
package osm.jp.postgis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
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;

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