diff --git a/src/osm/jp/coverage/police/ToPostgis.java b/src/osm/jp/coverage/police/ToPostgis.java index 28b00a0..75027c8 100644 --- a/src/osm/jp/coverage/police/ToPostgis.java +++ b/src/osm/jp/coverage/police/ToPostgis.java @@ -1,5 +1,16 @@ 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; + /** * 国土数値情報から読み取ったデータをPostGISへインサートするためのSQLファイルに出力する。 * 出力ファイル名: "t_POLICE.sql" @@ -30,5 +41,40 @@ public ToPostgis(boolean update) { super(DbPolice.TABLE_NAME, update); } + + @Override + public void transportNew(Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + HttpPOST.sql(conPost, "DROP TABLE IF EXISTS t_"+ DbPolice.TABLE_NAME +" CASCADE;"); + HttpPOST.sql(conPost, "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(conPost, "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/postgis/ToPostgis.java b/src/osm/jp/postgis/ToPostgis.java index c06b10b..9d57c49 100644 --- a/src/osm/jp/postgis/ToPostgis.java +++ b/src/osm/jp/postgis/ToPostgis.java @@ -26,7 +26,7 @@ * */ public class ToPostgis { - boolean UPDATE = false; + protected boolean UPDATE = false; String tableName = null; public ToPostgis(String tableName, boolean update) { @@ -75,7 +75,7 @@ } } - void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + public void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { HttpPOST.sql(conPost, "DROP TABLE IF EXISTS t_"+ tableName +" CASCADE;"); HttpPOST.sql(conPost, "CREATE TABLE t_"+ tableName +" (gid SERIAL PRIMARY KEY, idref varchar(24), fixed integer,area integer,geom GEOMETRY(POINT, 4612));"); HttpPOST.sql(conPost, "CREATE INDEX ix_"+ tableName +"_geom ON t_"+ tableName +" USING GiST (geom);"); @@ -100,7 +100,7 @@ } } - void toInsert (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + 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";