Newer
Older
osmCoverage / src / osm / jp / postgis / Do_sqlfiles.java
@yuu yuu on 20 Mar 2017 3 KB Nagoya busstop to OSM file.
  1. package osm.jp.postgis;
  2. import java.io.*;
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. import jp.co.areaweb.tools.database.*;
  8.  
  9. public class Do_sqlfiles {
  10. static String sqlFileName = "busstop.sql";
  11. /**
  12. * メイン
  13. *
  14. * java -cp .:SelectBusstop.jar:hayashi_0225.jar:postgresql-9.4.1212.jar osm.jp.SelectBusstop
  15. *
  16. *
  17. * @throws IOException
  18. * @throws SQLException
  19. * @throws ClassNotFoundException
  20. * @throws FileNotFoundException
  21. * @throws TransformerException
  22. * @throws SAXException
  23. * @throws ParserConfigurationException */
  24. public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException
  25. {
  26. for (int i=0; i < args.length; i++) {
  27. if (args[i].equals("-update")) {
  28. sqlFileName = "update.sql";
  29. }
  30. }
  31.  
  32. Connection con = DatabaseTool.openDb("postgis");
  33. try {
  34. /**
  35. * 都道府県ごとのGMLディレクトリの処理
  36. */
  37. File dir = new File(".");
  38. File[] dirs = dir.listFiles();
  39. for (File iDir : dirs) {
  40. if (checkGMLdir(iDir)) {
  41. System.out.println(iDir.getName());
  42. File[] files = iDir.listFiles();
  43. for (File iFile : files) {
  44. if (checkFile(iFile)) {
  45. System.out.println(iFile.getAbsoluteFile());
  46. Do_sqlfiles.sqlExecute(con, iFile);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. finally {
  53. DatabaseTool.closeDb(con);
  54. }
  55. }
  56. /**
  57. * sqlFileを実行する
  58. * @param conn データベースコネクション
  59. * @param sqlFile 実行するSQLファイル
  60. * @throws SQLException SQL実行エラー
  61. * @throws IOException
  62. */
  63. public static void sqlExecute(Connection conn, File sqlFile) throws SQLException, IOException {
  64. // ローカルデータベース内の情報をPostGIS用の「busstop.sql」に出力する
  65. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sqlFile), "UTF-8"));
  66.  
  67. // 既存のreadLine()では\rまたは\n、および\r\nにて改行とみなしている。
  68. String strLine = null;
  69. while ((strLine = br.readLine()) != null) { // null=ファイルの最後
  70. sqlExecute(conn, strLine);
  71. }
  72. sqlExecute(conn, "commit;");
  73. br.close();
  74. }
  75. /**
  76. * sqlStrを実行する
  77. * @param conn データベースコネクション
  78. * @param sqlStr 実行するSQL文
  79. * @throws SQLException SQL実行エラー
  80. */
  81. public static void sqlExecute(Connection conn, String sqlStr) throws SQLException {
  82. if ((sqlStr != null) && (!sqlStr.trim().equals(""))) {
  83. System.out.println(sqlStr);
  84. Statement stat = conn.createStatement();
  85. stat.execute(sqlStr);
  86. stat.close();
  87. }
  88. }
  89. /**
  90. * 数値地図情報のデータファイルかどうかを見極める
  91. * @param f
  92. * @return
  93. */
  94. static boolean checkGMLdir(File f) {
  95. if (!f.isDirectory()) {
  96. return false;
  97. }
  98. String name = f.getName();
  99. if (!name.startsWith(GML_DIR_PREFIX)) {
  100. return false;
  101. }
  102. if (!name.toUpperCase().endsWith(GML_DIR_PRIFIX)) {
  103. return false;
  104. }
  105. return true;
  106. }
  107.  
  108. public static final String GML_DIR_PREFIX = "P11-10_";
  109. public static final String GML_DIR_PRIFIX = "_GML";
  110.  
  111. /**
  112. * SQLファイルかどうかを見極める
  113. * @param f
  114. * @return
  115. */
  116. static boolean checkFile(File f) {
  117. String name = f.getName();
  118. if (name.equals(sqlFileName)) {
  119. return true;
  120. }
  121. return false;
  122. }
  123.  
  124. }