Newer
Older
osmCoverage / src / osm / jp / postgis / ToPostgis.java
@hayashi hayashi on 26 Feb 2018 5 KB police.ToPostgis.class
  1. package osm.jp.postgis;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.math.BigDecimal;
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import javax.xml.parsers.ParserConfigurationException;
  11. import jp.co.areaweb.tools.database.DatabaseTool;
  12. import org.xml.sax.SAXException;
  13. import osm.jp.api.HttpPOST;
  14.  
  15. /**
  16. * 国土数値情報から読み取ったデータをPostGISへインサートするためのSQLファイルに出力する。
  17. * 出力ファイル名: "t_POSTOFFICE.sql"
  18. * テーブル名: t_POSTOFFICE
  19. * インデックス: ix_t_POSTOFFICE_geom
  20. * gid PostGISの識別ID
  21. * idstr 国土数値情報のノードID
  22. * fixed OSMのNodeが周辺に存在するかどうか、存在しない場合は0,存在する場合はそのScoreの合計。
  23. * area 都道府県コード
  24. * geom PostGIS形式の位置情報(4612:)
  25. * @author yuu
  26. *
  27. */
  28. public class ToPostgis {
  29. protected boolean UPDATE = false;
  30. String tableName = null;
  31. public ToPostgis(String tableName, boolean update) {
  32. this.tableName = tableName;
  33. this.UPDATE = update;
  34. }
  35. public void transport() throws Exception {
  36. Connection conHsql = null;
  37. Connection conPost = null;
  38. try {
  39. // DB.tableを作成
  40. conHsql = DatabaseTool.openDb("database");
  41. conPost = DatabaseTool.openDb("postgis");
  42. transport(conHsql, conPost);
  43. }
  44. finally {
  45. if (conHsql != null) {
  46. DatabaseTool.closeDb(conHsql);
  47. }
  48. if (conPost != null) {
  49. DatabaseTool.closeDb(conPost);
  50. }
  51. }
  52. }
  53. /**
  54. * HSQLDB: "SELECT idref,area,fixed,lat,lon FROM POSTOFFICE"
  55. * POSTGIS "insert into t_POSTOFFICE(idref,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));"
  56. *
  57. * @param conHsql
  58. * @param conPost
  59. * @throws java.io.FileNotFoundException
  60. * @throws java.lang.ClassNotFoundException
  61. * @throws java.sql.SQLException
  62. * @throws java.io.IOException
  63. * @throws javax.xml.parsers.ParserConfigurationException
  64. * @throws org.xml.sax.SAXException
  65. */
  66. void transport (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  67. if (this.UPDATE) {
  68. transportUpdate(conHsql, conPost);
  69. }
  70. else {
  71. transportNew(conHsql, conPost);
  72. }
  73. }
  74.  
  75. public void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  76. HttpPOST.sql(conPost, "DROP TABLE IF EXISTS t_"+ tableName +" CASCADE;");
  77. HttpPOST.sql(conPost, "CREATE TABLE t_"+ tableName +" (gid SERIAL PRIMARY KEY, idref varchar(24), fixed integer,area integer,geom GEOMETRY(POINT, 4612));");
  78. HttpPOST.sql(conPost, "CREATE INDEX ix_"+ tableName +"_geom ON t_"+ tableName +" USING GiST (geom);");
  79. toInsert(conHsql, conPost);
  80. }
  81.  
  82. void transportUpdate (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  83. toInsert(conHsql, conPost);
  84.  
  85. try ( PreparedStatement ps1 = conHsql.prepareStatement("SELECT gid,fixed1 FROM "+ tableName +" where up=1");
  86. PreparedStatement ps2 = conPost.prepareStatement("UPDATE t_"+ tableName +" set fixed=? WHERE gid=?")) {
  87. try (ResultSet rset1 = ps1.executeQuery()) {
  88. while (rset1.next()) {
  89. long gid = rset1.getLong("gid");
  90. int fixed1 = rset1.getInt("fixed1");
  91. ps2.setInt(1, fixed1);
  92. ps2.setLong(2, gid);
  93. System.out.println("UPDATE t_"+ tableName +" set fixed="+ fixed1 +" WHERE gid="+ gid);
  94. ps2.executeUpdate();
  95. }
  96. }
  97. }
  98. }
  99.  
  100. public void toInsert (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  101. String sql = "SELECT idref,area,fixed1,lat,lon FROM "+ tableName;
  102. if (this.UPDATE) {
  103. sql += " where up=2";
  104. }
  105. try (PreparedStatement ps1 = conHsql.prepareStatement(sql)) {
  106. try (ResultSet rset1 = ps1.executeQuery()) {
  107. while (rset1.next()) {
  108. String idref = rset1.getString("idref");
  109. int area = rset1.getInt("area");
  110. int fixed1 = rset1.getInt("fixed1");
  111. double lat = rset1.getDouble("lat");
  112. double lon = rset1.getDouble("lon");
  113. 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))";
  114. try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) {
  115. System.out.println(sqlStr);
  116. ps.executeUpdate();
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }