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. * @param toPropertiy
  57. * @throws Exception
  58. */
  59. public void transport(String toPropertiy) throws Exception {
  60. try (Connection conHsql = DatabaseTool.openDb(Coverage.DB_PORP_LOCALDB);
  61. Connection conPost = DatabaseTool.openDb(toPropertiy))
  62. {
  63. transportNew(conHsql, conPost);
  64. toInsert(conHsql, conPost);
  65. }
  66. }
  67. /**
  68. * HSQLDB: "SELECT idref,area,fixed,lat,lon FROM POSTOFFICE"
  69. * POSTGIS "insert into t_POSTOFFICE(idref,fixed,area,geom) VALUES('n000001',0,0, ST_GeomFromText('POINT(136.9695284611471 35.10300377075564)', 4612));"
  70. *
  71. * @param conHsql
  72. * @param conPost
  73. * @throws java.io.FileNotFoundException
  74. * @throws java.lang.ClassNotFoundException
  75. * @throws java.sql.SQLException
  76. * @throws java.io.IOException
  77. * @throws javax.xml.parsers.ParserConfigurationException
  78. * @throws org.xml.sax.SAXException
  79. */
  80.  
  81. /*
  82. <pre>{@code
  83. CREATE TABLE public.t_TABLENAME
  84. (
  85. gid integer NOT NULL DEFAULT nextval('t_TABLENAME_gid_seq'::regclass),
  86. gmlid text,
  87. idref text,
  88. name text,
  89. fixed integer,
  90. area integer,
  91. code integer,
  92. geom geometry(Point,4612),
  93. CONSTRAINT t_TABLENAME_pkey PRIMARY KEY (gid)
  94. )
  95. WITH (OIDS=FALSE);
  96. ALTER TABLE public.t_TABLENAME
  97. OWNER TO postgres;
  98.  
  99. CREATE INDEX ix_TABLENAME_geom ON public.t_TABLENAME USING gist (geom);
  100. GRANT SELECT ON t_fuel TO gisuser;
  101. }</pre>
  102. */
  103. public void transportNew(Connection conHsql, Connection conPost) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  104. HttpPOST posgre = new HttpPOST(conPost, null);
  105. posgre.sql("DROP TABLE IF EXISTS t_"+ tableName +" CASCADE;");
  106. posgre.sql("CREATE TABLE t_"+ tableName
  107. +" ("
  108. + "gid SERIAL PRIMARY KEY, "
  109. + "gmlid varchar(24), "
  110. + "idref varchar(24), "
  111. + "fixed integer, "
  112. + "area integer, "
  113. + "code integer, "
  114. + "geom GEOMETRY(POINT, 4612)"
  115. + ");");
  116. posgre.sql("CREATE INDEX ix_"+ tableName +"_geom ON t_"+ tableName +" USING GiST (geom);");
  117. posgre.sql("GRANT SELECT ON t_"+ tableName +" TO gisuser;");
  118. }
  119.  
  120. public void toInsert (Connection conHsql, Connection conPost)
  121. throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException
  122. {
  123. String sql = items.getSqlStr(tableName);
  124. try (PreparedStatement ps1 = conHsql.prepareStatement(sql);
  125. ResultSet rset1 = ps1.executeQuery()) {
  126. while (rset1.next()) {
  127. items.setResuit(rset1);
  128.  
  129. String latlon = String.format(
  130. "ST_GeomFromText('POINT(%.7f %.7f)',4612)",
  131. items.lon,
  132. items.lat
  133. );
  134. String sqlStr = "INSERT INTO t_"+ tableName
  135. +" (gmlid,idref,fixed,area,code,geom) "
  136. + "VALUES (?,?,?,?,?, "+ latlon +")";
  137. try (PreparedStatement ps = conPost.prepareStatement(sqlStr)) {
  138. printMark();
  139. ps.setString(1, items.gmlid);
  140. ps.setString(2, items.idref);
  141. ps.setInt(3, items.fixed);
  142. ps.setInt(4, items.area);
  143. ps.setInt(5, items.code);
  144. ps.executeUpdate();
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. *
  151. * @param csvFile
  152. * @param gisdb
  153. * @throws FileNotFoundException
  154. * @throws ClassNotFoundException
  155. * @throws SQLException
  156. * @throws IOException
  157. * @throws ParserConfigurationException
  158. * @throws SAXException
  159. */
  160. public void toCsv (File csvFile, String gisdb)
  161. throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException
  162. {
  163. try (Connection conPost = DatabaseTool.openDb(gisdb);
  164. BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8")))
  165. {
  166. String header = items.getCsvHeader();
  167. ow.write(header);
  168. ow.newLine();
  169. String sql = items.getPostSqlStr(tableName);
  170. PreparedStatement ps8 = conPost.prepareStatement(sql);
  171. try (ResultSet rset8 = ps8.executeQuery()) {
  172. while (rset8.next()) {
  173. items.setPostResuit(rset8);
  174. String osm_node = items.getValue();
  175. System.out.println(osm_node);
  176. ow.write(osm_node);
  177. ow.newLine();
  178. }
  179. }
  180. ow.flush();
  181. }
  182. }
  183.  
  184. public static int outCnt = 0;
  185. public static void printMark() {
  186. outCnt++;
  187. if ((outCnt % 10) == 1) {
  188. System.out.print(".");
  189. if (outCnt >= 800) {
  190. outCnt = 0;
  191. System.out.println();
  192. }
  193. }
  194. }
  195. }