Newer
Older
osmCoverage / src / osm / jp / postgis / ToPostgis.java
@hayashi hayashi on 5 Aug 2018 6 KB new class: Postgis.java
  1. package osm.jp.postgis;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStreamWriter;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import javax.xml.parsers.ParserConfigurationException;
  14. import jp.co.areaweb.tools.database.DatabaseTool;
  15. import org.xml.sax.SAXException;
  16. import osm.jp.api.HttpPOST;
  17.  
  18. /**
  19. * 国土数値情報から読み取ったデータをPostGISへインサートするためのSQLファイルに出力する。
  20. * 出力ファイル名: "t_POSTOFFICE.sql"
  21. * テーブル名: t_POSTOFFICE
  22. * インデックス: ix_t_POSTOFFICE_geom
  23. * gid PostGISの識別ID
  24. * idstr 国土数値情報のノードID
  25. * fixed OSMのNodeが周辺に存在するかどうか、存在しない場合は0,存在する場合はそのScoreの合計。
  26. * area 都道府県コード
  27. * geom PostGIS形式の位置情報(4612:)
  28. * @author yuu
  29. *
  30. */
  31. public class ToPostgis {
  32. String tableName = null;
  33. public PostgisItems items = null;
  34. public ToPostgis(String tableName) {
  35. this.tableName = tableName;
  36. this.items = new PostgisItems();
  37. items.add(new PostgisItem("gmlid", "gmlid"));
  38. items.add(new PostgisItem("idref", "idref"));
  39. items.add(new PostgisItem("area", "area"));
  40. items.add(new PostgisItem("fixed", "fixed1"));
  41. items.add(new PostgisItem("code", "code"));
  42. }
  43.  
  44. public ToPostgis(String tableName, PostgisItems items) {
  45. this(tableName);
  46. this.items = items;
  47. }
  48. /**
  49. * HSQLDBからPOSTGISへ
  50. *
  51. * @throws Exception
  52. */
  53. public void transport() throws Exception {
  54. try (Connection conHsql = DatabaseTool.openDb("database");
  55. Connection conPost = DatabaseTool.openDb("postgis"))
  56. {
  57. transportNew(conHsql, conPost);
  58. toInsert(conHsql, conPost);
  59. }
  60. }
  61. /**
  62. * HSQLDB: "SELECT idref,area,fixed,lat,lon FROM POSTOFFICE"
  63. * POSTGIS "insert into t_POSTOFFICE(idref,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));"
  64. *
  65. * @param conHsql
  66. * @param conPost
  67. * @throws java.io.FileNotFoundException
  68. * @throws java.lang.ClassNotFoundException
  69. * @throws java.sql.SQLException
  70. * @throws java.io.IOException
  71. * @throws javax.xml.parsers.ParserConfigurationException
  72. * @throws org.xml.sax.SAXException
  73. */
  74.  
  75. /*
  76. <pre>{@code
  77. CREATE TABLE public.t_TABLENAME
  78. (
  79. gid integer NOT NULL DEFAULT nextval('t_TABLENAME_gid_seq'::regclass),
  80. gmlid text,
  81. idref text,
  82. name text,
  83. fixed integer,
  84. area integer,
  85. code integer,
  86. geom geometry(Point,4612),
  87. CONSTRAINT t_TABLENAME_pkey PRIMARY KEY (gid)
  88. )
  89. WITH (OIDS=FALSE);
  90. ALTER TABLE public.t_TABLENAME
  91. OWNER TO postgres;
  92.  
  93. CREATE INDEX ix_TABLENAME_geom ON public.t_TABLENAME USING gist (geom);
  94. }</pre>
  95. */
  96. public void transportNew(Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  97. HttpPOST posgre = new HttpPOST(conPost, null);
  98. posgre.sql("DROP TABLE IF EXISTS t_"+ tableName +" CASCADE;");
  99. posgre.sql("CREATE TABLE t_"+ tableName
  100. +" ("
  101. + "gid SERIAL PRIMARY KEY, "
  102. + "gmlid varchar(24), "
  103. + "idref varchar(24), "
  104. + "fixed integer, "
  105. + "area integer, "
  106. + "code integer, "
  107. + "geom GEOMETRY(POINT, 4612)"
  108. + ");");
  109. posgre.sql("CREATE INDEX ix_"+ tableName +"_geom ON t_"+ tableName +" USING GiST (geom);");
  110. }
  111.  
  112. public void toInsert (Connection conHsql, Connection conPost)
  113. throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException
  114. {
  115. String sql = items.getSqlStr(tableName);
  116. try (PreparedStatement ps1 = conHsql.prepareStatement(sql);
  117. ResultSet rset1 = ps1.executeQuery()) {
  118. while (rset1.next()) {
  119. items.setResuit(rset1);
  120.  
  121. String latlon = String.format(
  122. "ST_GeomFromText('POINT(%.7f %.7f)',4612)",
  123. items.lon,
  124. items.lat
  125. );
  126. String sqlStr = "INSERT INTO t_"+ tableName
  127. +" (gmlid,idref,fixed,area,code,geom) "
  128. + "VALUES (?,?,?,?,?, "+ latlon +")";
  129. try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) {
  130. printMark();
  131. ps.setString(1, items.gmlid);
  132. ps.setString(2, items.idref);
  133. ps.setInt(3, items.fixed);
  134. ps.setInt(4, items.area);
  135. ps.setInt(5, items.code);
  136. ps.executeUpdate();
  137. }
  138. }
  139. }
  140. }
  141. public void toCsv (File csvFile)
  142. throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException
  143. {
  144. try (Connection conPost = DatabaseTool.openDb("postgis");
  145. BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8")))
  146. {
  147. String header = items.getCsvHeader();
  148. ow.write(header);
  149. ow.newLine();
  150. String sql = items.getPostSqlStr(tableName);
  151. PreparedStatement ps8 = conPost.prepareStatement(sql);
  152. try (ResultSet rset8 = ps8.executeQuery()) {
  153. while (rset8.next()) {
  154. items.setPostResuit(rset8);
  155. String osm_node = items.getValue();
  156. System.out.println(osm_node);
  157. ow.write(osm_node);
  158. ow.newLine();
  159. }
  160. }
  161. ow.flush();
  162. }
  163. }
  164.  
  165. public static int outCnt = 0;
  166. public static void printMark() {
  167. System.out.print(".");
  168. outCnt++;
  169. if (outCnt >= 100) {
  170. outCnt = 0;
  171. System.out.println();
  172. }
  173. }
  174. }