diff --git a/src/osm/jp/coverage/busstop/ToCartoCSV.java b/src/osm/jp/coverage/busstop/ToCartoCSV.java index 3493309..099f3bc 100644 --- a/src/osm/jp/coverage/busstop/ToCartoCSV.java +++ b/src/osm/jp/coverage/busstop/ToCartoCSV.java @@ -27,89 +27,89 @@ * */ 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"; + public static boolean NAGOYA_MODE = false; + BufferedWriter ow = null; + BufferedWriter ow0 = null; + BufferedWriter ow1 = null; + BufferedWriter ow2 = null; - public static void main (String[] argv) { - try { - ToCartoCSV obj = new ToCartoCSV(new File(".")); - obj.outputDb(DatabaseTool.openDb("postgis")); - } 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 static void main (String[] argv) { + try { + ToCartoCSV obj = new ToCartoCSV(new File(".")); + obj.outputDb(DatabaseTool.openDb("postgis")); + } 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; + 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(); + // 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"); - 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(); - } - } - rset8.close(); - this.ow.flush(); - this.ow0.flush(); - this.ow1.flush(); - this.ow2.flush(); - return counter; - } + 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(); + } + } + } + this.ow.flush(); + this.ow0.flush(); + this.ow1.flush(); + this.ow2.flush(); + return counter; + } /** * postgresql 用に(')をエスケープする @@ -123,9 +123,9 @@ public void close() { try { - this.ow.close(); - } catch (IOException e) { - e.printStackTrace(); - } + this.ow.close(); + } catch (IOException e) { + e.printStackTrace(); + } } }