Newer
Older
osmCoverage / src / osm / jp / postgis / Postgis.java
@hayashi hayashi on 5 Aug 2018 6 KB parameter
  1. package osm.jp.postgis;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.LineNumberReader;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLException;
  11. import java.sql.SQLSyntaxErrorException;
  12. import java.util.ArrayList;
  13. import jp.co.areaweb.tools.csv.CsvRecord;
  14. import jp.co.areaweb.tools.database.DatabaseTool;
  15. import static osm.jp.postgis.ToPostgis.printMark;
  16.  
  17. /**
  18. * CSVファイルの内容をPostGIS.db.tableに書き戻す
  19. *
  20. * @author yuu
  21. *
  22. */
  23. public class Postgis implements AutoCloseable
  24. {
  25. /**
  26. *
  27. * $ java -cp .:osmCoverage.jar:hayashi.jar:postgresql.jar \
  28. * osm.jp.postgis.Postgis ./share
  29. * @param args
  30. * @throws Exception
  31. */
  32. public static void main(String[] args) throws Exception {
  33. File dir = new File(args[0]);
  34. try (Postgis db = new Postgis(dir)) {
  35. db.initTableAll();
  36. db.importCsvAll();
  37. }
  38. }
  39. TableInfo[] tables;
  40. Connection con = null;
  41. File workspaceDir;
  42. class TableInfo {
  43. String tableName;
  44. String csvPath;
  45. TableInfo(String tableName, String csvPath) {
  46. this.tableName = tableName;
  47. this.csvPath = csvPath;
  48. }
  49. }
  50. public Postgis(File workspace) throws ClassNotFoundException, SQLException, IOException {
  51. if (workspace == null) {
  52. this.workspaceDir = new File(".");
  53. }
  54. else {
  55. this.workspaceDir = workspace;
  56. }
  57. ArrayList<TableInfo> list = new ArrayList<>();
  58. list.add(new TableInfo("t_busstop", "GML_BUSSTOP/t_busstop.csv"));
  59. list.add(new TableInfo("t_fuel", "GML_FUEL/t_fuel.csv"));
  60. list.add(new TableInfo("t_police", "GML_POLICE/t_police.csv"));
  61. list.add(new TableInfo("t_postoffice", "GML_POSTOFFICE/t_postoffice.csv"));
  62. this.tables = list.toArray(new TableInfo[list.size()]);
  63. con = DatabaseTool.openDb("gisdb");
  64. }
  65.  
  66. @Override
  67. public void close() throws Exception {
  68. if (con != null) {
  69. DatabaseTool.closeDb(con);
  70. }
  71. }
  72. public void initTableAll() throws Exception {
  73. for (TableInfo tableInfo : tables) {
  74. initTable(tableInfo.tableName);
  75. }
  76. }
  77.  
  78. public void initTable(String tableName) throws Exception {
  79. sql("DROP TABLE IF EXISTS "+ tableName +" CASCADE;");
  80. sql("CREATE TABLE "+ tableName
  81. +" ("
  82. + "gid SERIAL PRIMARY KEY, "
  83. + "gmlid varchar(24), "
  84. + "idref varchar(24), "
  85. + "fixed integer, "
  86. + "area integer, "
  87. + "code integer, "
  88. + "geom GEOMETRY(POINT, 4612)"
  89. + ");");
  90. sql("CREATE INDEX ix_"+ tableName +"_geom ON "+ tableName +" USING GiST (geom);");
  91. }
  92. public void importCsvAll() throws Exception {
  93. for (TableInfo info : this.tables) {
  94. importCsv(info);
  95. }
  96. }
  97. public void importCsv(TableInfo info) throws Exception {
  98. String charsetName = "UTF-8";
  99. File csvFile = new File(this.workspaceDir, info.csvPath);
  100. try (LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(csvFile), charsetName))) {
  101. String line;
  102. ArrayList<String> header = new ArrayList<>();
  103. while ((line = reader.readLine()) != null) {
  104. if (line.trim().length() == 0) {
  105. continue;
  106. }
  107. CsvRecord record = new CsvRecord();
  108. record.analizeRecord(line);
  109. if (header.isEmpty()) {
  110. for (String str : record) {
  111. header.add(str);
  112. }
  113. }
  114. else {
  115. toInsert(info.tableName, header, record);
  116. }
  117. }
  118. }
  119. }
  120.  
  121. public void toInsert(String tableName, ArrayList<String> header, CsvRecord record)
  122. throws Exception
  123. {
  124. String gmlidStr = getStr(header, record, "gmlid");
  125. String idrefStr = getStr(header, record, "idref");
  126. int area = getInt(header, record, "area");
  127. int fixed = getInt(header, record, "fixed");
  128. int code = getInt(header, record, "code");
  129. String latStr = record.get(header.indexOf("lat"));
  130. String lonStr = record.get(header.indexOf("lon"));
  131. String latlon = String.format(
  132. "ST_GeomFromText('POINT(%.7f %.7f)',4612)",
  133. Double.parseDouble(lonStr),
  134. Double.parseDouble(latStr)
  135. );
  136. String sqlStr = "INSERT INTO "+ tableName
  137. +" (gmlid,idref,area,fixed,code,geom) "
  138. + "VALUES (?,?,?,?,?, "+ latlon +")";
  139. try (PreparedStatement ps = this.con.prepareStatement(sqlStr)) {
  140. printMark();
  141. ps.setString(1, gmlidStr); // gmlid
  142. ps.setString(2, idrefStr); // idref
  143. ps.setInt(3, area); // area
  144. ps.setInt(4, fixed); // fixed
  145. ps.setInt(5, code); // code
  146. ps.executeUpdate();
  147. }
  148. catch (SQLSyntaxErrorException e) {
  149. System.out.println("107:"+ e.toString());
  150. if (!(e.toString().startsWith("java.sql.SQLSyntaxErrorException: user lacks privilege or object not found:"))) {
  151. throw e;
  152. }
  153. }
  154. }
  155. String getStr(ArrayList<String> header, CsvRecord record, String key) {
  156. int i = header.indexOf(key);
  157. if (i < 0) {
  158. return "";
  159. }
  160. else {
  161. return record.get(i);
  162. }
  163. }
  164. int getInt(ArrayList<String> header, CsvRecord record, String key) {
  165. int i = header.indexOf(key);
  166. if (i < 0) {
  167. return 0;
  168. }
  169. else {
  170. return Integer.parseInt(record.get(i));
  171. }
  172. }
  173.  
  174. public void sql(String sql) throws SQLException {
  175. System.out.println(sql);
  176. try (PreparedStatement ps = this.con.prepareStatement(sql)) {
  177. ps.executeUpdate();
  178. }
  179. catch (SQLSyntaxErrorException e) {
  180. System.out.println("107:"+ e.toString());
  181. if (!(e.toString().startsWith("java.sql.SQLSyntaxErrorException: user lacks privilege or object not found:"))) {
  182. throw e;
  183. }
  184. }
  185. }
  186. }