Newer
Older
osmCoverage / test / osm / jp / coverage / ToPostgis.java
  1. package osm.jp.coverage;
  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. * HSQLDB.TESTの結果をPOSTGIS.t_testへ反映させる
  17. * テーブル名: t_test
  18. * インデックス: ix_test_geom
  19. * gid PostGISの識別ID
  20. * name 名称
  21. * fixed OSMのバス停が周辺に存在するかどうか、存在しない場合は0,存在する場合はその数。
  22. * geom PostGIS形式の位置情報(4612:)
  23. * @author yuu
  24. *
  25. */
  26. public class ToPostgis {
  27. public static void main(String[] argv) throws Exception {
  28. Connection conHsql = null;
  29. Connection conPost = null;
  30. try {
  31. // DB.tableを作成
  32. conHsql = DatabaseTool.openDb("database");
  33. conPost = DatabaseTool.openDb("postgis");
  34. transportNew(conHsql, conPost);
  35. }
  36. finally {
  37. if (conHsql != null) {
  38. DatabaseTool.closeDb(conHsql);
  39. }
  40. if (conPost != null) {
  41. DatabaseTool.closeDb(conPost);
  42. }
  43. }
  44. }
  45.  
  46. /**
  47. * HSQLDB: "SELECT idref,area,fixed,lat,lon FROM BUS_STOP"
  48. * POSTGIS "insert into t_busstop(idref,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));"
  49. *
  50. * @param conHsql
  51. * @param conPost
  52. * @throws java.io.FileNotFoundException
  53. * @throws java.lang.ClassNotFoundException
  54. * @throws java.sql.SQLException
  55. * @throws java.io.IOException
  56. * @throws javax.xml.parsers.ParserConfigurationException
  57. * @throws org.xml.sax.SAXException
  58. */
  59. public static void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  60. HttpPOST postgres = new HttpPOST(conPost, null);
  61. postgres.sql("DROP TABLE IF EXISTS t_test CASCADE;");
  62. postgres.sql("CREATE TABLE t_test (gid SERIAL PRIMARY KEY,gmlid text,name text,fixed integer,area integer,geom GEOMETRY(POINT, 4612));");
  63. postgres.sql("CREATE INDEX ix_test_geom ON t_test USING GiST (geom);");
  64. toInsert(conHsql, conPost);
  65. }
  66.  
  67. public static void toInsert (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  68. String sql = "SELECT gmlid,lat,lon,fixed1,area FROM "+ DbTest.TABLE_NAME;
  69. try (PreparedStatement ps1 = conHsql.prepareStatement(sql)) {
  70. try (ResultSet rset1 = ps1.executeQuery()) {
  71. while (rset1.next()) {
  72. String gmlid = rset1.getString("gmlid");
  73. int area = rset1.getInt("area");
  74. int fixed1 = rset1.getInt("fixed1");
  75. double lat = rset1.getDouble("lat");
  76. double lon = rset1.getDouble("lon");
  77. int fixed = 0;
  78. if (fixed1 > 0) {
  79. fixed = 1;
  80. }
  81. String geom = "ST_GeomFromText('POINT("+ BigDecimal.valueOf(lon).toPlainString() +" "+ BigDecimal.valueOf(lat).toPlainString() +")', 4612)";
  82. String sqlStr = "INSERT INTO t_test (gmlid,fixed,area,geom) VALUES (?,?,?,"+ geom +")";
  83. System.out.println(sqlStr +" ["+ gmlid +", "+ fixed +", "+ area +"]");
  84. try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) {
  85. ps.setString(1, gmlid);
  86. ps.setInt(2, fixed);
  87. ps.setInt(3, area);
  88. ps.executeUpdate();
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }