Newer
Older
osmCoverage / src / osm / jp / postgis / ToPostgis.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へインサートする。
  20. * 出力db名: "postgis.properties"
  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. /**
  35. * コンストラクタ
  36. * @param tableName 出力先データベーステーブル
  37. */
  38. public ToPostgis(String tableName) {
  39. this.tableName = tableName;
  40. this.items = new PostgisItems();
  41. items.add(new PostgisItem("gmlid", "gmlid"));
  42. items.add(new PostgisItem("idref", "idref"));
  43. items.add(new PostgisItem("area", "area"));
  44. items.add(new PostgisItem("fixed", "fixed1"));
  45. items.add(new PostgisItem("code", "code"));
  46. }
  47.  
  48. public ToPostgis(String tableName, PostgisItems items) {
  49. this(tableName);
  50. this.items = items;
  51. }
  52. /**
  53. * HSQLDBからPOSTGISへ
  54. *
  55. * @throws Exception
  56. */
  57. public void transport() throws Exception {
  58. transport("postgis");
  59. }
  60. public void transport(String toPropertiy) throws Exception {
  61. try (Connection conHsql = DatabaseTool.openDb("database");
  62. Connection conPost = DatabaseTool.openDb(toPropertiy))
  63. {
  64. transportNew(conHsql, conPost);
  65. toInsert(conHsql, conPost);
  66. }
  67. }
  68. /**
  69. * HSQLDB: "SELECT idref,area,fixed,lat,lon FROM POSTOFFICE"
  70. * POSTGIS "insert into t_POSTOFFICE(idref,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));"
  71. *
  72. * @param conHsql
  73. * @param conPost
  74. * @throws java.io.FileNotFoundException
  75. * @throws java.lang.ClassNotFoundException
  76. * @throws java.sql.SQLException
  77. * @throws java.io.IOException
  78. * @throws javax.xml.parsers.ParserConfigurationException
  79. * @throws org.xml.sax.SAXException
  80. */
  81.  
  82. /*
  83. <pre>{@code
  84. CREATE TABLE public.t_TABLENAME
  85. (
  86. gid integer NOT NULL DEFAULT nextval('t_TABLENAME_gid_seq'::regclass),
  87. gmlid text,
  88. idref text,
  89. name text,
  90. fixed integer,
  91. area integer,
  92. code integer,
  93. geom geometry(Point,4612),
  94. CONSTRAINT t_TABLENAME_pkey PRIMARY KEY (gid)
  95. )
  96. WITH (OIDS=FALSE);
  97. ALTER TABLE public.t_TABLENAME
  98. OWNER TO postgres;
  99.  
  100. CREATE INDEX ix_TABLENAME_geom ON public.t_TABLENAME USING gist (geom);
  101. GRANT SELECT ON t_fuel TO gisuser;
  102. }</pre>
  103. */
  104. public void transportNew(Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  105. HttpPOST posgre = new HttpPOST(conPost, null);
  106. posgre.sql("DROP TABLE IF EXISTS t_"+ tableName +" CASCADE;");
  107. posgre.sql("CREATE TABLE t_"+ tableName
  108. +" ("
  109. + "gid SERIAL PRIMARY KEY, "
  110. + "gmlid varchar(24), "
  111. + "idref varchar(24), "
  112. + "fixed integer, "
  113. + "area integer, "
  114. + "code integer, "
  115. + "geom GEOMETRY(POINT, 4612)"
  116. + ");");
  117. posgre.sql("CREATE INDEX ix_"+ tableName +"_geom ON t_"+ tableName +" USING GiST (geom);");
  118. posgre.sql("GRANT SELECT ON t_"+ tableName +" TO gisuser;");
  119. }
  120.  
  121. public void toInsert (Connection conHsql, Connection conPost)
  122. throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException
  123. {
  124. String sql = items.getSqlStr(tableName);
  125. try (PreparedStatement ps1 = conHsql.prepareStatement(sql);
  126. ResultSet rset1 = ps1.executeQuery()) {
  127. while (rset1.next()) {
  128. items.setResuit(rset1);
  129.  
  130. String latlon = String.format(
  131. "ST_GeomFromText('POINT(%.7f %.7f)',4612)",
  132. items.lon,
  133. items.lat
  134. );
  135. String sqlStr = "INSERT INTO t_"+ tableName
  136. +" (gmlid,idref,fixed,area,code,geom) "
  137. + "VALUES (?,?,?,?,?, "+ latlon +")";
  138. try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) {
  139. printMark();
  140. ps.setString(1, items.gmlid);
  141. ps.setString(2, items.idref);
  142. ps.setInt(3, items.fixed);
  143. ps.setInt(4, items.area);
  144. ps.setInt(5, items.code);
  145. ps.executeUpdate();
  146. }
  147. }
  148. }
  149. }
  150. public void toCsv (File csvFile)
  151. throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException
  152. {
  153. try (Connection conPost = DatabaseTool.openDb("postgis");
  154. BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8")))
  155. {
  156. String header = items.getCsvHeader();
  157. ow.write(header);
  158. ow.newLine();
  159. String sql = items.getPostSqlStr(tableName);
  160. PreparedStatement ps8 = conPost.prepareStatement(sql);
  161. try (ResultSet rset8 = ps8.executeQuery()) {
  162. while (rset8.next()) {
  163. items.setPostResuit(rset8);
  164. String osm_node = items.getValue();
  165. System.out.println(osm_node);
  166. ow.write(osm_node);
  167. ow.newLine();
  168. }
  169. }
  170. ow.flush();
  171. }
  172. }
  173.  
  174. public static int outCnt = 0;
  175. public static void printMark() {
  176. System.out.print(".");
  177. outCnt++;
  178. if (outCnt >= 100) {
  179. outCnt = 0;
  180. System.out.println();
  181. }
  182. }
  183. }