diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 1e28d55..d30546b 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -903,6 +903,14 @@ + + + + + + + + @@ -1388,6 +1396,14 @@ + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 6869014..e74ced1 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -nbbuild.xml.data.CRC32=354430f8 +nbbuild.xml.data.CRC32=7469a291 nbbuild.xml.script.CRC32=e7ea1557 nbbuild.xml.stylesheet.CRC32=8064a381@1.80.1.48 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=354430f8 -nbproject/build-impl.xml.script.CRC32=b6dace46 +nbproject/build-impl.xml.data.CRC32=7469a291 +nbproject/build-impl.xml.script.CRC32=9170c4d3 nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/project.properties b/nbproject/project.properties index f621fe8..99b9997 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -31,7 +31,6 @@ dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= -file.reference.hayashi_0225.jar=lib/hayashi_0225.jar file.reference.hsqldb_2.2.9.jar=lib\\hsqldb_2.2.9.jar file.reference.javax.json.jar=lib/javax.json.jar file.reference.osmCoverage-src=src @@ -44,7 +43,7 @@ ${file.reference.hsqldb_2.2.9.jar}:\ ${file.reference.postgresql-9.4.1212.jar}:\ ${file.reference.javax.json.jar}:\ - ${file.reference.hayashi_0225.jar} + ${reference.hayashi.jar} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false @@ -93,6 +92,8 @@ meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false platform.active=default_platform +project.hayashi=../hayashi +reference.hayashi.jar=${project.hayashi}/dist/hayashi.jar run.classpath=\ ${javac.classpath}:\ ${build.classes.dir} diff --git a/nbproject/project.xml b/nbproject/project.xml index 935a364..24c2e39 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -11,6 +11,15 @@ - + + + hayashi + jar + + jar + clean + jar + + diff --git a/src/osm/jp/coverage/busstop/DbExistBusstop.java b/src/osm/jp/coverage/busstop/DbExistBusstop.java index 9cb09ca..ffe9da7 100644 --- a/src/osm/jp/coverage/busstop/DbExistBusstop.java +++ b/src/osm/jp/coverage/busstop/DbExistBusstop.java @@ -39,10 +39,8 @@ * @throws ParserConfigurationException */ public static void main(String[] args) throws Exception { - Connection hsqldb = null; - try { - hsqldb = DatabaseTool.openDb("database"); - + try (Connection hsqldb = DatabaseTool.openDb("database")) + { /** * 既存のOSMバス停を読み込む * OSM OverPassAPI を使って、既存のOSMバス停のデータを取得して、 @@ -52,11 +50,6 @@ osmExist.create(); osmExist.getJapanCapabilities(); } - finally { - if (hsqldb != null) { - DatabaseTool.closeDb(hsqldb); - } - } } public DbExistBusstop(Connection hsqldb) { @@ -119,11 +112,8 @@ */ @Override public void readExisting(String sqlWhere, int point) throws Exception { - Connection osmdb = null; long counter = 0L; - try { - osmdb = DatabaseTool.openDb("osmdb"); - + try (Connection osmdb = DatabaseTool.openDb("osmdb")) { String sql = "select osm_id,brand,disused,name"; StringBuilder whereText0 = new StringBuilder(); @@ -170,8 +160,5 @@ System.out.println("Exists Node count = " + counter); } - finally { - DatabaseTool.closeDb(osmdb); - } } } \ No newline at end of file diff --git a/src/osm/jp/coverage/busstop/NagoyaBusstop.java b/src/osm/jp/coverage/busstop/NagoyaBusstop.java index a1f1859..2b1ae54 100644 --- a/src/osm/jp/coverage/busstop/NagoyaBusstop.java +++ b/src/osm/jp/coverage/busstop/NagoyaBusstop.java @@ -2,7 +2,6 @@ import osm.jp.api.RectArea; import osm.jp.api.HttpPOST; import osm.jp.api.KatakanaToHiragana; -import osm.jp.postgis.Do_sqlfiles; import javax.xml.parsers.*; import javax.xml.transform.OutputKeys; @@ -82,10 +81,10 @@ Connection conPost = null; try { conPost = DatabaseTool.openDb("postgis"); - Do_sqlfiles.sqlExecute(conPost, "DROP TABLE nagoya_busstop;"); - Do_sqlfiles.sqlExecute(conPost, "CREATE TABLE nagoya_busstop (gid SERIAL PRIMARY KEY, name text, kana text, fixed integer, area integer, geom GEOMETRY(POINT, 4612));"); - Do_sqlfiles.sqlExecute(conPost, "CREATE INDEX ix_nagoya_busstop_geom ON nagoya_busstop USING GiST (geom);"); - Do_sqlfiles.sqlExecute(conPost, "commit;"); + DoSQL.sqlExecute(conPost, "DROP TABLE nagoya_busstop;"); + DoSQL.sqlExecute(conPost, "CREATE TABLE nagoya_busstop (gid SERIAL PRIMARY KEY, name text, kana text, fixed integer, area integer, geom GEOMETRY(POINT, 4612));"); + DoSQL.sqlExecute(conPost, "CREATE INDEX ix_nagoya_busstop_geom ON nagoya_busstop USING GiST (geom);"); + DoSQL.sqlExecute(conPost, "commit;"); //Do_sqlfiles.sqlExecute(conPost, new File(ToPostgis.SQL_FILE_NAME)); outputDb2Osm(conPost, new File("Nagoya_busstop.osm")); diff --git a/src/osm/jp/coverage/busstop/ToPostgis.java b/src/osm/jp/coverage/busstop/ToPostgis.java index 069b472..3c6e0ac 100644 --- a/src/osm/jp/coverage/busstop/ToPostgis.java +++ b/src/osm/jp/coverage/busstop/ToPostgis.java @@ -1,20 +1,11 @@ package osm.jp.coverage.busstop; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import javax.xml.parsers.ParserConfigurationException; -import jp.co.areaweb.tools.database.DatabaseTool; -import org.xml.sax.SAXException; -import osm.jp.api.HttpPOST; +import osm.jp.postgis.PostgisItem; +import osm.jp.postgis.PostgisItems; /** * HSQLDB.BUS_STOPの結果をPOSTGIS.t_busstopへ反映させる - * テーブル名: t_busstop + * テーブル名: t_busstop * インデックス: ix_busstop_geom * gid PostGISの識別ID * gmlid 国土数値情報のID(areaと合わせてKEYとなる) @@ -24,91 +15,19 @@ * @author yuu * */ -public class ToPostgis { +public class ToPostgis extends osm.jp.postgis.ToPostgis { public static void main(String[] argv) throws Exception { - Connection conHsql = null; - Connection conPost = null; - try { - // DB.tableを作成 - conHsql = DatabaseTool.openDb("database"); - conPost = DatabaseTool.openDb("postgis"); - - transportNew(conHsql, conPost); - - } - finally { - if (conHsql != null) { - DatabaseTool.closeDb(conHsql); - } - if (conPost != null) { - DatabaseTool.closeDb(conPost); - } - } + ToPostgis model = new ToPostgis(); + model.transport(); } - /** - * HSQLDB: "SELECT idref,area,fixed,lat,lon FROM BUS_STOP" - * POSTGIS "insert into t_busstop(idref,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));" - * - * @param conHsql - * @param conPost - * @throws java.io.FileNotFoundException - * @throws java.lang.ClassNotFoundException - * @throws java.sql.SQLException - * @throws java.io.IOException - * @throws javax.xml.parsers.ParserConfigurationException - * @throws org.xml.sax.SAXException - */ - public static void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - HttpPOST httpPOST = new HttpPOST(conPost, null); - httpPOST.sql("DROP TABLE IF EXISTS t_busstop CASCADE;"); - httpPOST.sql("CREATE TABLE t_busstop (" - + "gid SERIAL PRIMARY KEY, " - + "gmlid text, " - + "idref text, " - + "name text, " - + "fixed integer, " - + "area integer, " - + "geom GEOMETRY(POINT, 4612)" - + ");"); - httpPOST.sql("CREATE INDEX ix_busstop_geom ON t_busstop USING GiST (geom);"); - toInsert(conHsql, conPost); - } - - public static void toInsert (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - String sql = "SELECT gmlid,name,lat,lon,fixed1,area FROM "+ DbBusstop.TABLE_NAME; - try (PreparedStatement ps1 = conHsql.prepareStatement(sql)) { - try (ResultSet rset1 = ps1.executeQuery()) { - while (rset1.next()) { - String gmlid = rset1.getString("gmlid"); - int area = rset1.getInt("area"); - int fixed1 = rset1.getInt("fixed1"); - double lat = rset1.getDouble("lat"); - double lon = rset1.getDouble("lon"); - String name = rset1.getString("name"); - - int fixed = 0; - if (fixed1 >= 100) { - fixed = 3; - } - else if (fixed1 >= 50) { - fixed = 2; - } - else if (fixed1 > 0) { - fixed = 1; - } - String geom = "ST_GeomFromText('POINT("+ BigDecimal.valueOf(lon).toPlainString() +" "+ BigDecimal.valueOf(lat).toPlainString() +")', 4612)"; - String sqlStr = "INSERT INTO t_busstop (gmlid,fixed,name,area,geom) VALUES (?,?,?,?,"+ geom +")"; - System.out.println(sqlStr +" ["+ gmlid +", "+ fixed +", '"+ name +"', "+ area +"]"); - try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) { - ps.setString(1, gmlid); - ps.setInt(2, fixed); - ps.setString(3, name); - ps.setInt(4, area); - ps.executeUpdate(); - } - } - } - } + public ToPostgis() { + super(DbBusstop.TABLE_NAME); + + this.items = new PostgisItems(); + items.add(new PostgisItem("gmlid", "gmlid")); + items.add(new PostgisItem("idref", "idref")); + items.add(new PostgisItem("area", "area")); + items.add(new PostgisItem("fixed", "fixed1")); } } diff --git a/src/osm/jp/coverage/fuel/ToPostgis.java b/src/osm/jp/coverage/fuel/ToPostgis.java index 3884ee6..4464e76 100644 --- a/src/osm/jp/coverage/fuel/ToPostgis.java +++ b/src/osm/jp/coverage/fuel/ToPostgis.java @@ -1,17 +1,7 @@ package osm.jp.coverage.fuel; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import javax.xml.parsers.ParserConfigurationException; -import jp.co.areaweb.tools.database.DatabaseTool; -import org.xml.sax.SAXException; -import osm.jp.api.HttpPOST; -import static osm.jp.coverage.busstop.ToPostgis.toInsert; +import osm.jp.postgis.PostgisItem; +import osm.jp.postgis.PostgisItems; /** * 国土数値情報から読み取ったデータをPostGISへインサートするためのSQLファイルに出力する。 @@ -26,122 +16,21 @@ * @author yuu * */ -public class ToPostgis { - public static boolean UPDATE = false; +public class ToPostgis extends osm.jp.postgis.ToPostgis { public static void main(String[] argv) throws Exception { - for (String arg : argv) { - if (arg.toUpperCase().equals("-UPDATE")) { - ToPostgis.UPDATE = true; - } - } + ToPostgis model = new ToPostgis(); + model.transport(); + } + + public ToPostgis() { + super(DbFuel.TABLE_NAME); - Connection conHsql = null; - Connection conPost = null; - try { - // DB.tableを作成 - conHsql = DatabaseTool.openDb("database"); - conPost = DatabaseTool.openDb("postgis"); - - if (!ToPostgis.UPDATE) { - } - transport(conHsql, conPost); - - } - finally { - if (conHsql != null) { - DatabaseTool.closeDb(conHsql); - } - if (conPost != null) { - DatabaseTool.closeDb(conPost); - } - } + this.items = new PostgisItems(); + items.add(new PostgisItem("gmlid", "gmlid")); + items.add(new PostgisItem("idref", "idref")); + items.add(new PostgisItem("area", "area")); + items.add(new PostgisItem("fixed", "fixed")); } - /** - * HSQLDB: "SELECT gmlid,area,fixed,lat,lon FROM FUEL" - * POSTGIS "insert into t_FUEL(gmlid,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));" - * - * @param conHsql - * @param conPost - * @throws java.io.FileNotFoundException - * @throws java.lang.ClassNotFoundException - * @throws java.sql.SQLException - * @throws java.io.IOException - * @throws javax.xml.parsers.ParserConfigurationException - * @throws org.xml.sax.SAXException - */ - public static void transport (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - if (ToPostgis.UPDATE) { - transportUpdate(conHsql, conPost); - } - else { - transportNew(conHsql, conPost); - } - } - - public static void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - HttpPOST httpPOST = new HttpPOST(conPost, null); - httpPOST.sql("DROP TABLE IF EXISTS t_FUEL CASCADE;"); - httpPOST.sql("CREATE TABLE t_FUEL (" - + "gid SERIAL PRIMARY KEY," - + "idref text, " - + "gmlid text, " - + "name text, " - + "fixed integer," - + "area integer," - + "geom GEOMETRY(POINT, 4612));"); - httpPOST.sql("CREATE INDEX ix_fuel_geom ON t_FUEL USING GiST (geom);"); - toInsert(conHsql, conPost); - } - - public static void transportUpdate (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - toInsert(conHsql, conPost); - - try ( PreparedStatement ps1 = conHsql.prepareStatement("SELECT gmlid,area,fixed1 FROM FUEL where up=1"); - PreparedStatement ps2 = conPost.prepareStatement("UPDATE t_FUEL set fixed=? WHERE gmlid=? AND area=?")) { - try (ResultSet rset1 = ps1.executeQuery()) { - while (rset1.next()) { - String gmlid = rset1.getString("gmlid"); - int area = rset1.getInt("area"); - int fixed1 = rset1.getInt("fixed1"); - ps2.setInt(1, fixed1); - ps2.setString(2, gmlid); - ps2.setInt(3, area); - System.out.println("UPDATE t_FUEL set fixed="+ fixed1 +" WHERE gmlid="+ gmlid +" AND area="+ area); - ps2.executeUpdate(); - } - } - } - } - - public static void toInsert (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - String sql = "SELECT gmlid,area,fixed1,lat,lon,idref FROM "+ DbFuel.TABLE_NAME; - if (ToPostgis.UPDATE) { - sql += " where up=2"; - } - try (PreparedStatement ps1 = conHsql.prepareStatement(sql)) { - try (ResultSet rset1 = ps1.executeQuery()) { - while (rset1.next()) { - String gmlid = rset1.getString("gmlid"); - int area = rset1.getInt("area"); - int fixed1 = rset1.getInt("fixed1"); - double lat = rset1.getDouble("lat"); - double lon = rset1.getDouble("lon"); - String idref = rset1.getString("idref"); - - String sqlStr = "INSERT INTO t_FUEL (gmlid,fixed,area,idref,geom) VALUES ('"+ gmlid +"',"+ fixed1 +","+ area +","+ idref +",idref,ST_GeomFromText('POINT("+ BigDecimal.valueOf(lon).toPlainString() +" "+ BigDecimal.valueOf(lat).toPlainString() +")', 4612))"; - System.out.println(sqlStr); - sqlStr = "INSERT INTO t_FUEL (gmlid,fixed,area,idref,geom) VALUES (?,?,?,?,ST_GeomFromText('POINT("+ BigDecimal.valueOf(lon).toPlainString() +" "+ BigDecimal.valueOf(lat).toPlainString() +")', 4612))"; - try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) { - ps.setString(1, gmlid); - ps.setInt(2, fixed1); - ps.setInt(3, area); - ps.setString(4, idref); - ps.executeUpdate(); - } - } - } - } - } } diff --git a/src/osm/jp/coverage/police/ToPostgis.java b/src/osm/jp/coverage/police/ToPostgis.java index ea35c67..10e51b2 100644 --- a/src/osm/jp/coverage/police/ToPostgis.java +++ b/src/osm/jp/coverage/police/ToPostgis.java @@ -1,15 +1,7 @@ package osm.jp.coverage.police; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import javax.xml.parsers.ParserConfigurationException; -import org.xml.sax.SAXException; -import osm.jp.api.HttpPOST; +import osm.jp.postgis.PostgisItem; +import osm.jp.postgis.PostgisItems; /** * 国土数値情報から読み取ったデータをPostGISへインサートするためのSQLファイルに出力する。 @@ -27,57 +19,17 @@ public class ToPostgis extends osm.jp.postgis.ToPostgis { public static void main(String[] argv) throws Exception { - boolean update = false; - if (argv != null) { - for (String arg : argv) { - if (arg.toUpperCase().equals("-UPDATE")) { - update = true; - } - } - } - - ToPostgis model = new ToPostgis(update); + ToPostgis model = new ToPostgis(); model.transport(); } - public ToPostgis(boolean update) { - super(DbPolice.TABLE_NAME, update); + public ToPostgis() { + super(DbPolice.TABLE_NAME); + + this.items = new PostgisItems(); + items.add(new PostgisItem("gmlid", "idref")); + items.add(new PostgisItem("area", "area")); + items.add(new PostgisItem("fixed", "fixed1")); + items.add(new PostgisItem("code", "code")); } - - @Override - public void transportNew(Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - HttpPOST httpPOST = new HttpPOST(conPost, null); - httpPOST.sql("DROP TABLE IF EXISTS t_"+ DbPolice.TABLE_NAME +" CASCADE;"); - httpPOST.sql("CREATE TABLE t_"+ DbPolice.TABLE_NAME +" (gid SERIAL PRIMARY KEY, idref varchar(24), fixed integer,area integer, code integer,geom GEOMETRY(POINT, 4612));"); - httpPOST.sql("CREATE INDEX ix_"+ DbPolice.TABLE_NAME +"_geom ON t_"+ DbPolice.TABLE_NAME +" USING GiST (geom);"); - toInsert(conHsql, conPost); - } - - @Override - public void toInsert(Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - String sql = "SELECT idref,area,code,fixed1,lat,lon FROM "+ DbPolice.TABLE_NAME; - if (this.UPDATE) { - sql += " where up=2"; - } - try (PreparedStatement ps1 = conHsql.prepareStatement(sql)) { - try (ResultSet rset1 = ps1.executeQuery()) { - while (rset1.next()) { - String idref = rset1.getString("idref"); - int area = rset1.getInt("area"); - int fixed1 = rset1.getInt("fixed1"); - double lat = rset1.getDouble("lat"); - double lon = rset1.getDouble("lon"); - int code = rset1.getInt("code"); - - String sqlStr = "INSERT INTO t_"+ DbPolice.TABLE_NAME +" (idref,fixed,area,code,geom) VALUES ('"+ idref +"',"+ fixed1 +","+ area +","+ code +",ST_GeomFromText('POINT("+ BigDecimal.valueOf(lon).toPlainString() +" "+ BigDecimal.valueOf(lat).toPlainString() +")', 4612))"; - try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) { - System.out.println(sqlStr); - ps.executeUpdate(); - } - } - } - } - } - - } diff --git a/src/osm/jp/coverage/postoffice/Postoffice.java b/src/osm/jp/coverage/postoffice/Postoffice.java index 3bc4d9c..b3efeed 100644 --- a/src/osm/jp/coverage/postoffice/Postoffice.java +++ b/src/osm/jp/coverage/postoffice/Postoffice.java @@ -14,7 +14,6 @@ import java.sql.SQLException; import java.text.SimpleDateFormat; import jp.co.areaweb.tools.database.*; -import osm.jp.api.HttpPOST; import osm.jp.api.Japan; public class Postoffice { diff --git a/src/osm/jp/coverage/postoffice/ToPostgis.java b/src/osm/jp/coverage/postoffice/ToPostgis.java index df1b403..e07f00f 100644 --- a/src/osm/jp/coverage/postoffice/ToPostgis.java +++ b/src/osm/jp/coverage/postoffice/ToPostgis.java @@ -1,5 +1,8 @@ package osm.jp.coverage.postoffice; +import osm.jp.postgis.PostgisItem; +import osm.jp.postgis.PostgisItems; + /** * 国土数値情報から読み取ったデータをPostGISへインサートするためのSQLファイルに出力する。 * 出力ファイル名: "t_POSTOFFICE.sql" @@ -16,19 +19,17 @@ public class ToPostgis extends osm.jp.postgis.ToPostgis { public static void main(String[] argv) throws Exception { - boolean update = false; - for (String arg : argv) { - if (arg.toUpperCase().equals("-UPDATE")) { - update = true; - } - } - - ToPostgis model = new ToPostgis(update); + ToPostgis model = new ToPostgis(); model.transport(); } - public ToPostgis(boolean update) { - super(DbPostoffice.TABLE_NAME, update); + public ToPostgis() { + super(DbPostoffice.TABLE_NAME); + + this.items = new PostgisItems(); + items.add(new PostgisItem("gmlid", "idref")); + items.add(new PostgisItem("area", "area")); + items.add(new PostgisItem("fixed", "fixed1")); } } diff --git a/src/osm/jp/postgis/Do_sqlfiles.java b/src/osm/jp/postgis/Do_sqlfiles.java deleted file mode 100644 index fdeef36..0000000 --- a/src/osm/jp/postgis/Do_sqlfiles.java +++ /dev/null @@ -1,130 +0,0 @@ -package osm.jp.postgis; -import java.io.*; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; - -import jp.co.areaweb.tools.database.*; - -public class Do_sqlfiles { - static String sqlFileName = "busstop.sql"; - - /** - * メイン - * - * java -cp .:SelectBusstop.jar:hayashi_0225.jar:postgresql-9.4.1212.jar osm.jp.SelectBusstop - * - * - * @throws IOException - * @throws SQLException - * @throws ClassNotFoundException - * @throws FileNotFoundException - * @throws TransformerException - * @throws SAXException - * @throws ParserConfigurationException */ - public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException - { - for (int i=0; i < args.length; i++) { - if (args[i].equals("-update")) { - sqlFileName = "update.sql"; - } - } - - Connection con = DatabaseTool.openDb("postgis"); - - try { - /** - * 都道府県ごとのGMLディレクトリの処理 - */ - File dir = new File("."); - File[] dirs = dir.listFiles(); - for (File iDir : dirs) { - if (checkGMLdir(iDir)) { - System.out.println(iDir.getName()); - - File[] files = iDir.listFiles(); - for (File iFile : files) { - if (checkFile(iFile)) { - System.out.println(iFile.getAbsoluteFile()); - Do_sqlfiles.sqlExecute(con, iFile); - } - } - } - } - } - finally { - DatabaseTool.closeDb(con); - } - } - - /** - * sqlFileを実行する - * @param conn データベースコネクション - * @param sqlFile 実行するSQLファイル - * @throws SQLException SQL実行エラー - * @throws IOException - */ - public static void sqlExecute(Connection conn, File sqlFile) throws SQLException, IOException { - // ローカルデータベース内の情報をPostGIS用の「busstop.sql」に出力する - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sqlFile), "UTF-8")); - - // 既存のreadLine()では\rまたは\n、および\r\nにて改行とみなしている。 - String strLine = null; - while ((strLine = br.readLine()) != null) { // null=ファイルの最後 - sqlExecute(conn, strLine); - } - sqlExecute(conn, "commit;"); - br.close(); - } - - /** - * sqlStrを実行する - * @param conn データベースコネクション - * @param sqlStr 実行するSQL文 - * @throws SQLException SQL実行エラー - */ - public static void sqlExecute(Connection conn, String sqlStr) throws SQLException { - if ((sqlStr != null) && (!sqlStr.trim().equals(""))) { - System.out.println(sqlStr); - Statement stat = conn.createStatement(); - stat.execute(sqlStr); - stat.close(); - } - } - - /** - * 数値地図情報のデータファイルかどうかを見極める - * @param f - * @return - */ - static boolean checkGMLdir(File f) { - if (!f.isDirectory()) { - return false; - } - String name = f.getName(); - if (!name.startsWith(GML_DIR_PREFIX)) { - return false; - } - if (!name.toUpperCase().endsWith(GML_DIR_PRIFIX)) { - return false; - } - return true; - } - - public static final String GML_DIR_PREFIX = "P11-10_"; - public static final String GML_DIR_PRIFIX = "_GML"; - - /** - * SQLファイルかどうかを見極める - * @param f - * @return - */ - static boolean checkFile(File f) { - String name = f.getName(); - if (name.equals(sqlFileName)) { - return true; - } - return false; - } - -} \ No newline at end of file diff --git a/src/osm/jp/postgis/ExportCSV.java b/src/osm/jp/postgis/ExportCSV.java new file mode 100644 index 0000000..eb93f91 --- /dev/null +++ b/src/osm/jp/postgis/ExportCSV.java @@ -0,0 +1,52 @@ +package osm.jp.postgis; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import jp.co.areaweb.tools.database.DatabaseTool; + +public class ExportCSV { + public static String tableName; + public static String CSV_FILE_NAME = "t_busstop.csv"; + + public static void main (String[] argv) { + tableName = "t_FUEL"; + CSV_FILE_NAME = tableName +".csv"; + try { + outputDb(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + 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("gid,geom,score"); + ow.write("gid,geom,score"); + ow.newLine(); + PreparedStatement ps8 = con.prepareStatement("SELECT gid,idref,fixed,area,geom FROM t_busstop"); + try (ResultSet rset8 = ps8.executeQuery()) { + while (rset8.next()) { + String gid = rset8.getString("gid"); + String geom = rset8.getString("geom"); + int score = rset8.getInt("fixed"); + + String osm_node; + osm_node = ""+ gid +","+ geom +","+ score +""; + System.out.println(osm_node); + ow.write(osm_node); + ow.newLine(); + } + } + ow.flush(); + } + } +} diff --git a/src/osm/jp/postgis/PostgisItem.java b/src/osm/jp/postgis/PostgisItem.java new file mode 100644 index 0000000..aedc9e3 --- /dev/null +++ b/src/osm/jp/postgis/PostgisItem.java @@ -0,0 +1,19 @@ +package osm.jp.postgis; + +public class PostgisItem { + public static final int TYPE_STRING = 1; + public static final int TYPE_INT = 2; + public String postName = ""; + public String localName = ""; + public int type = 0; + + public PostgisItem(String postName, String localName, int type) { + this.postName = localName; + this.localName = localName; + this.type = type; + } + + public PostgisItem(String postName, String localName) { + this(postName, localName, 0); + } +} diff --git a/src/osm/jp/postgis/PostgisItems.java b/src/osm/jp/postgis/PostgisItems.java new file mode 100644 index 0000000..f8910fc --- /dev/null +++ b/src/osm/jp/postgis/PostgisItems.java @@ -0,0 +1,74 @@ +package osm.jp.postgis; + +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class PostgisItems extends ArrayList { + String gmlid = ""; + String idref = ""; + int area = 0; + int fixed = 0; + int code = 0; + double lat = 0.0D; + double lon = 0.0D; + + public static void main() { + PostgisItems items = new PostgisItems(); + items.add(new PostgisItem("gmlid", "gmlid")); + items.add(new PostgisItem("idref", "idref")); + items.add(new PostgisItem("area", "area")); + items.add(new PostgisItem("fixed", "fixed1")); + items.add(new PostgisItem("code", "code")); + } + + public PostgisItems() { + super(); + } + + public void setResuit(ResultSet rset) throws SQLException { + for (PostgisItem item : this) { + switch (item.postName) { + case "gmlid": + gmlid = rset.getString(item.localName); + break; + case "idref": + idref = rset.getString(item.localName); + break; + case "area": + area = rset.getInt(item.localName); + break; + case "fixed": + fixed = rset.getInt(item.localName); + break; + case "code": + code = rset.getInt(item.localName); + break; + default: + break; + } + } + lat = rset.getDouble("lat"); + lon = rset.getDouble("lon"); + } + + public String getSqlStr(String tableName) { + StringBuilder sb = new StringBuilder(); + sb.append("SELECT "); + for (PostgisItem item : this) { + sb.append(item.localName); + sb.append(","); + } + sb.append("lat,lon FROM "+ tableName); + return sb.toString(); + } + + public String getGeomStr() { + return String.format( + "ST_GeomFromText('POINT(%s %s)', 4612)", + BigDecimal.valueOf(lon).toPlainString(), + BigDecimal.valueOf(lat).toPlainString() + ); + } +} diff --git a/src/osm/jp/postgis/ToGeoJSON.java b/src/osm/jp/postgis/ToGeoJSON.java index 90f50d6..eb1989f 100644 --- a/src/osm/jp/postgis/ToGeoJSON.java +++ b/src/osm/jp/postgis/ToGeoJSON.java @@ -47,14 +47,14 @@ public String tableName; public String sqlForm1 = "SELECT row_to_json(feature) FROM (" - + "select 'Feature' As type, " - + "ST_AsGeoJSON(ST_Transform(t_%s.geom,4326))::json As geometry, " - + "row_to_json((" - + "SELECT p FROM (SELECT t_%s.idref, t_%s.fixed) AS p" - + ")) AS properties From t_%s %s order by area,idref) As feature"; + + "select 'Feature' As type, " + + "ST_AsGeoJSON(ST_Transform(t_%s.geom,4326))::json As geometry, " + + "row_to_json((" + + "SELECT p FROM (SELECT t_%s.idref, t_%s.fixed) AS p" + + ")) AS properties From t_%s %s order by area,idref) As feature"; public String sqlForm2 = "SELECT row_to_json(feature) FROM (" - + "select 'Feature' As type, " - + "ST_AsGeoJSON(ST_Transform(t_%s.geom,4326))::json As geometry From t_%s %s order by area,idref) As feature"; + + "select 'Feature' As type, " + + "ST_AsGeoJSON(ST_Transform(t_%s.geom,4326))::json As geometry From t_%s %s order by area,idref) As feature"; public ToGeoJSON(String tableName) { this.tableName = tableName; @@ -88,7 +88,7 @@ ow.write(line); ow.newLine(); } - line = rset8.getString(1); + line = String.format("\"%s\"", rset8.getString(1)); System.out.print(line); ow.write(line); } diff --git a/src/osm/jp/postgis/ToPostgis.java b/src/osm/jp/postgis/ToPostgis.java index a1d45ae..42af81f 100644 --- a/src/osm/jp/postgis/ToPostgis.java +++ b/src/osm/jp/postgis/ToPostgis.java @@ -26,30 +26,30 @@ * */ public class ToPostgis { - protected boolean UPDATE = false; String tableName = null; + public PostgisItems items = null; - public ToPostgis(String tableName, boolean update) { + public ToPostgis(String tableName) { this.tableName = tableName; - this.UPDATE = update; + this.items = new PostgisItems(); + items.add(new PostgisItem("gmlid", "gmlid")); + items.add(new PostgisItem("idref", "idref")); + items.add(new PostgisItem("area", "area")); + items.add(new PostgisItem("fixed", "fixed1")); + items.add(new PostgisItem("code", "code")); + } + + public ToPostgis(String tableName, PostgisItems items) { + this(tableName); + this.items = items; } public void transport() throws Exception { - Connection conHsql = null; - Connection conPost = null; - try { - // DB.tableを作成 - conHsql = DatabaseTool.openDb("database"); - conPost = DatabaseTool.openDb("postgis"); - transport(conHsql, conPost); - } - finally { - if (conHsql != null) { - DatabaseTool.closeDb(conHsql); - } - if (conPost != null) { - DatabaseTool.closeDb(conPost); - } + try (Connection conHsql = DatabaseTool.openDb("database"); + Connection conPost = DatabaseTool.openDb("postgis")) + { + transportNew(conHsql, conPost); + toInsert(conHsql, conPost); } } @@ -66,62 +66,79 @@ * @throws javax.xml.parsers.ParserConfigurationException * @throws org.xml.sax.SAXException */ - void transport (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - if (this.UPDATE) { - transportUpdate(conHsql, conPost); - } - else { - transportNew(conHsql, conPost); - } - } - public void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + /* +
{@code
+        CREATE TABLE public.t_TABLENAME
+        (
+          gid integer NOT NULL DEFAULT nextval('t_TABLENAME_gid_seq'::regclass),
+          gmlid text,
+          idref text,
+          name text,
+          fixed integer,
+          area integer,
+          code integer,
+          geom geometry(Point,4612),
+          CONSTRAINT t_TABLENAME_pkey PRIMARY KEY (gid)
+        )
+        WITH (OIDS=FALSE);
+        ALTER TABLE public.t_TABLENAME
+        OWNER TO postgres;
+
+        CREATE INDEX ix_TABLENAME_geom ON public.t_TABLENAME USING gist (geom);
+    }
+ */ + public void transportNew(Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { HttpPOST posgre = new HttpPOST(conPost, null); posgre.sql("DROP TABLE IF EXISTS t_"+ tableName +" CASCADE;"); - posgre.sql("CREATE TABLE t_"+ tableName +" (gid SERIAL PRIMARY KEY, idref varchar(24), fixed integer,area integer,geom GEOMETRY(POINT, 4612));"); + posgre.sql("CREATE TABLE t_"+ tableName + +" (" + + "gid SERIAL PRIMARY KEY, " + + "gmlid varchar(24), " + + "idref varchar(24), " + + "fixed integer, " + + "area integer, " + + "code integer, " + + "geom GEOMETRY(POINT, 4612)" + + ");"); posgre.sql("CREATE INDEX ix_"+ tableName +"_geom ON t_"+ tableName +" USING GiST (geom);"); - toInsert(conHsql, conPost); } - void transportUpdate (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - toInsert(conHsql, conPost); - - try ( PreparedStatement ps1 = conHsql.prepareStatement("SELECT gid,fixed1 FROM "+ tableName +" where up=1"); - PreparedStatement ps2 = conPost.prepareStatement("UPDATE t_"+ tableName +" set fixed=? WHERE gid=?")) { - try (ResultSet rset1 = ps1.executeQuery()) { - while (rset1.next()) { - long gid = rset1.getLong("gid"); - int fixed1 = rset1.getInt("fixed1"); - ps2.setInt(1, fixed1); - ps2.setLong(2, gid); - System.out.println("UPDATE t_"+ tableName +" set fixed="+ fixed1 +" WHERE gid="+ gid); - ps2.executeUpdate(); - } - } - } - } - - public void toInsert (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - String sql = "SELECT idref,area,fixed1,lat,lon FROM "+ tableName; - if (this.UPDATE) { - sql += " where up=2"; - } + public void toInsert (Connection conHsql, Connection conPost) + throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException + { + String sql = items.getSqlStr(tableName); try (PreparedStatement ps1 = conHsql.prepareStatement(sql)) { try (ResultSet rset1 = ps1.executeQuery()) { while (rset1.next()) { - String idref = rset1.getString("idref"); - int area = rset1.getInt("area"); - int fixed1 = rset1.getInt("fixed1"); - double lat = rset1.getDouble("lat"); - double lon = rset1.getDouble("lon"); + items.setResuit(rset1); - String sqlStr = "INSERT INTO t_"+ tableName +" (idref,fixed,area,geom) VALUES ('"+ idref +"',"+ fixed1 +","+ area +",ST_GeomFromText('POINT("+ BigDecimal.valueOf(lon).toPlainString() +" "+ BigDecimal.valueOf(lat).toPlainString() +")', 4612))"; + String sqlStr = "INSERT INTO t_"+ tableName + +" (gmlid,idref,fixed,area,code,geom) " + + "VALUES (?,?,?,?,?, ST_GeomFromText('POINT(? ?)',4612))"; try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) { - System.out.println(sqlStr); + printMark(); + ps.setString(1, items.gmlid); + ps.setString(2, items.idref); + ps.setInt(3, items.fixed); + ps.setInt(4, items.area); + ps.setInt(5, items.code); + ps.setDouble(6, items.lon); + ps.setDouble(7, items.lat); ps.executeUpdate(); } } } } } + + public static int outCnt = 0; + public static void printMark() { + System.out.print("."); + outCnt++; + if (outCnt >= 100) { + outCnt = 0; + System.out.println(); + } + } }