Newer
Older
osmCoverage / src / osm / jp / postgis / ToPostgis.java
@hayashi hayashi on 7 Oct 2017 4 KB 正確な距離計算
  1. package osm.jp.postgis;
  2.  
  3. import osm.jp.coverage.busstop.*;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.math.BigDecimal;
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import javax.xml.parsers.ParserConfigurationException;
  12. import jp.co.areaweb.tools.database.DatabaseTool;
  13. import org.xml.sax.SAXException;
  14. import osm.jp.api.HttpPOST;
  15.  
  16. /**
  17. * 1000mメッシュをPOSTGIS.t_testへ反映させる
  18. * ノード: 綾西 (368434484) 35.4342443, 139.4092180
  19. * から
  20. * ノード: 庚申供養 (1679559681) 35.4342940, 139.4207050
  21. * ノード: 内藤橋 (1742708189) : 35.4432806, 139.4104114
  22. * までが 1,000m となるはず
  23. *
  24. * テーブル名: t_test
  25. * インデックス: ix_test_geom
  26. * gid PostGISの識別ID
  27. * fixed
  28. * geom PostGIS形式の位置情報(4612:)
  29. * @author yuu
  30. *
  31. */
  32. public class ToPostgis {
  33. public static void main(String[] argv) throws Exception {
  34. Connection conHsql = null;
  35. Connection conPost = null;
  36. try {
  37. // DB.tableを作成
  38. conHsql = DatabaseTool.openDb("database");
  39. conPost = DatabaseTool.openDb("postgis");
  40. transportNew(conHsql, conPost);
  41. }
  42. finally {
  43. if (conHsql != null) {
  44. DatabaseTool.closeDb(conHsql);
  45. }
  46. if (conPost != null) {
  47. DatabaseTool.closeDb(conPost);
  48. }
  49. }
  50. }
  51.  
  52. /**
  53. * POSTGIS "insert into t_test(idref,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));"
  54. *
  55. * @param conHsql
  56. * @param conPost
  57. * @throws java.io.FileNotFoundException
  58. * @throws java.lang.ClassNotFoundException
  59. * @throws java.sql.SQLException
  60. * @throws java.io.IOException
  61. * @throws javax.xml.parsers.ParserConfigurationException
  62. * @throws org.xml.sax.SAXException
  63. */
  64. public static void transportNew (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  65. HttpPOST.sql(conPost, "DROP TABLE IF EXISTS t_test CASCADE;");
  66. HttpPOST.sql(conPost, "CREATE TABLE t_test (gid SERIAL PRIMARY KEY,fixed integer,area integer,geom GEOMETRY(POINT, 4612));");
  67. HttpPOST.sql(conPost, "CREATE INDEX ix_test_geom ON t_test USING GiST (geom);");
  68.  
  69. toInsert(conHsql, conPost);
  70. }
  71.  
  72. public static void toInsert (Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  73. String sql = "SELECT gmlid,name,lat,lon,fixed1,area FROM "+ DbBusstop.TABLE_NAME;
  74. try (PreparedStatement ps1 = conHsql.prepareStatement(sql)) {
  75. try (ResultSet rset1 = ps1.executeQuery()) {
  76. while (rset1.next()) {
  77. String gmlid = rset1.getString("gmlid");
  78. int area = rset1.getInt("area");
  79. int fixed1 = rset1.getInt("fixed1");
  80. double lat = rset1.getDouble("lat");
  81. double lon = rset1.getDouble("lon");
  82. String name = rset1.getString("name");
  83. int fixed = 0;
  84. if (fixed1 >= 100) {
  85. fixed = 3;
  86. }
  87. else if (fixed1 >= 50) {
  88. fixed = 2;
  89. }
  90. else if (fixed1 > 0) {
  91. fixed = 1;
  92. }
  93. String geom = "ST_GeomFromText('POINT("+ BigDecimal.valueOf(lon).toPlainString() +" "+ BigDecimal.valueOf(lat).toPlainString() +")', 4612)";
  94. String sqlStr = "INSERT INTO t_busstop (gmlid,name,fixed,area,geom) VALUES ('"+ gmlid +"','"+ name +"',"+ fixed +","+ area +","+ geom +")";
  95. System.out.println(sqlStr);
  96. sqlStr = "INSERT INTO t_busstop (gmlid,name,fixed,area,geom) VALUES (?,?,?,?,"+ geom +")";
  97. try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) {
  98. ps.setString(1, gmlid);
  99. ps.setString(2, name);
  100. ps.setInt(3, fixed);
  101. ps.setInt(4, area);
  102. ps.executeUpdate();
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }