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