diff --git a/src/osm/jp/coverage/busstop/ToCartoCSV.java b/src/osm/jp/coverage/busstop/ToCartoCSV.java index 099f3bc..5ff07ab 100644 --- a/src/osm/jp/coverage/busstop/ToCartoCSV.java +++ b/src/osm/jp/coverage/busstop/ToCartoCSV.java @@ -2,15 +2,11 @@ import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.IOException; import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.SQLException; import jp.co.areaweb.tools.database.DatabaseTool; @@ -27,88 +23,40 @@ * */ public class ToCartoCSV { - public static final String CSV_FILE_NAME = "busstop"; - public static boolean NAGOYA_MODE = false; - BufferedWriter ow = null; - BufferedWriter ow0 = null; - BufferedWriter ow1 = null; - BufferedWriter ow2 = null; + public static final String CSV_FILE_NAME = "busstop.carto.csv"; public static void main (String[] argv) { try { - ToCartoCSV obj = new ToCartoCSV(new File(".")); - obj.outputDb(DatabaseTool.openDb("postgis")); + outputDb(); } catch (Exception e) { e.printStackTrace(); } } - public ToCartoCSV(File dir) throws UnsupportedEncodingException, FileNotFoundException { - File csvFile = new File(dir, CSV_FILE_NAME+".csv"); - File csv0File = new File(dir, CSV_FILE_NAME+"0.csv"); - File csv1File = new File(dir, CSV_FILE_NAME+"1.csv"); - File csv2File = new File(dir, CSV_FILE_NAME+"2.csv"); - this.ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8")); - this.ow0 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csv0File), "UTF-8")); - this.ow1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csv1File), "UTF-8")); - this.ow2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csv2File), "UTF-8")); - } - - public void setNagoyaMode(boolean mode) { - ToCartoCSV.NAGOYA_MODE = mode; - } - - public boolean isNagoyaMode() { - return ToCartoCSV.NAGOYA_MODE; - } - - public int outputDb(Connection con) throws IOException, SQLException { - int counter = 0; - - // CSV header - System.out.println("name,score,lon,lat"); - this.ow.write("name,score,lon,lat"); - this.ow0.write("name,score,lon,lat"); - this.ow1.write("name,score,lon,lat"); - this.ow2.write("name,score,lon,lat"); - this.ow.newLine(); - this.ow0.newLine(); - this.ow1.newLine(); - this.ow2.newLine(); - - PreparedStatement ps8 = con.prepareStatement("SELECT name,ST_X(ST_TRANSFORM(geom,4612)) as lon, ST_Y(ST_TRANSFORM(geom,4612)) as lat,fixed FROM t_busstop"); - try (ResultSet rset8 = ps8.executeQuery()) { - while (rset8.next()) { - String name = rset8.getString("name"); - Double lat = rset8.getDouble("lat"); - Double lon = rset8.getDouble("lon"); - int score = rset8.getInt("fixed"); - - counter++; - String osm_node; - osm_node = "\""+ escapeStr(name) +"\",\""+ score +"\",\""+ Double.toString(lon) +"\",\""+ Double.toString(lat) +"\""; - System.out.println(osm_node); - this.ow.write(osm_node); - this.ow.newLine(); - if (score == 0) { - this.ow0.write(osm_node); - this.ow0.newLine(); - } - else if (score < 100) { - this.ow1.write(osm_node); - this.ow1.newLine(); - } - else { - this.ow2.write(osm_node); - this.ow2.newLine(); + public static void outputDb() throws Exception { + File csvFile = new File(CSV_FILE_NAME); + try (BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"))) { + Connection con = DatabaseTool.openDb("postgis"); + // CSV header + System.out.println("name,score,lon,lat"); + ow.write("name,score,lon,lat"); + PreparedStatement ps8 = con.prepareStatement("SELECT name,ST_X(ST_TRANSFORM(geom,4612)) as lon, ST_Y(ST_TRANSFORM(geom,4612)) as lat,fixed FROM t_busstop"); + try (ResultSet rset8 = ps8.executeQuery()) { + while (rset8.next()) { + String name = rset8.getString("name"); + Double lat = rset8.getDouble("lat"); + Double lon = rset8.getDouble("lon"); + int score = rset8.getInt("fixed"); + + String osm_node; + osm_node = "\""+ escapeStr(name) +"\",\""+ score +"\",\""+ Double.toString(lon) +"\",\""+ Double.toString(lat) +"\""; + System.out.println(osm_node); + ow.write(osm_node); + ow.newLine(); } } + ow.flush(); } - this.ow.flush(); - this.ow0.flush(); - this.ow1.flush(); - this.ow2.flush(); - return counter; } /** @@ -117,15 +65,7 @@ * @param name * @return */ - public String escapeStr(String name) { + public static String escapeStr(String name) { return ((name == null) ? "" : name.replaceAll("'", "''")); } - - public void close() { - try { - this.ow.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } }