Newer
Older
osmCoverage / src / osm / jp / postgis / CoverageAll.java
  1. package osm.jp.postgis;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.io.OutputStreamWriter;
  11. import java.io.StringReader;
  12. import java.io.UnsupportedEncodingException;
  13. import java.sql.Connection;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.text.DecimalFormat;
  18. import java.time.LocalDate;
  19. import java.time.format.DateTimeFormatter;
  20. import java.util.ArrayList;
  21. import javax.json.Json;
  22. import javax.json.JsonObject;
  23. import javax.json.JsonReader;
  24. import jp.co.areaweb.tools.csv.CsvFile;
  25. import jp.co.areaweb.tools.csv.CsvRecord;
  26. import osm.jp.api.Japan;
  27.  
  28. public class CoverageAll {
  29.  
  30. /*
  31. public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException, TransformerException
  32. {
  33. if (args.length < 3) {
  34. System.out.println("osm.jp.postgis.CoverageAll [outputfile.csv] [outputfile.json] [TABLE_NAME]");
  35. return;
  36. }
  37. File outputFileCsv = new File(args[0]);
  38. File outputFileJson = new File(args[1]);
  39. String tableName = args[2];
  40. CoverageAll obj = new CoverageAll(tableName);
  41. Connection conPost = DatabaseTool.openDb(Coverage.DB_PORP_GISDB);
  42. Connection conHsql = DatabaseTool.openDb(Coverage.DB_PORP_LOCALDB);
  43. try {
  44. obj.load(conPost, conHsql);
  45. obj.outputCSV(outputFileCsv);
  46. obj.outputJson(outputFileJson, "Data source");
  47. }
  48. finally {
  49. DatabaseTool.closeDb(conHsql);
  50. DatabaseTool.closeDb(conPost);
  51. }
  52. }
  53. */
  54.  
  55. public static DecimalFormat df3 = new DecimalFormat("##0.00");
  56. public String tableName;
  57. public File outputFile;
  58. public ArrayList<CoverageData> data;
  59. public String dateStr;
  60. /**
  61. *
  62. * @param pbfDate {"date":"2017-09-22"}
  63. * @throws java.io.FileNotFoundException
  64. */
  65. public CoverageAll(File pbfDate) throws FileNotFoundException, IOException {
  66. if (pbfDate == null) {
  67. dateStr = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
  68. }
  69. else {
  70. String tmp;
  71. try (FileReader fr = new FileReader(pbfDate);
  72. BufferedReader br = new BufferedReader(fr))
  73. {
  74. while((tmp=br.readLine()) != null){
  75. JsonReader jsonReader = Json.createReader(new StringReader(tmp));
  76. JsonObject obj = jsonReader.readObject();
  77. dateStr = obj.getString("date");
  78. System.out.println(dateStr);
  79. return;
  80. }
  81. }
  82. }
  83. }
  84. public void setTablename(String tableName) {
  85. this.tableName = tableName;
  86. }
  87. public void setData(ArrayList<CoverageData> data) {
  88. this.data = data;
  89. }
  90. /**
  91. * useually : load(conPost, conHsql, null);
  92. *
  93. * @param conPost
  94. * @param conHsql
  95. * @throws ClassNotFoundException
  96. * @throws SQLException
  97. * @throws IOException
  98. */
  99. /*
  100. public abstract void load(Connection conPost, Connection conHsql)
  101. throws ClassNotFoundException, SQLException, IOException;
  102. */
  103.  
  104. /**
  105. *
  106. * @param conPost
  107. * @param conHsql
  108. * @param tableName
  109. * @param where
  110. * @throws ClassNotFoundException
  111. * @throws SQLException
  112. * @throws IOException
  113. */
  114. public void load(Connection conPost, Connection conHsql, String tableName, String where)
  115. throws ClassNotFoundException, SQLException, IOException
  116. {
  117. // 都道府県名(name)の設定
  118. for (int i=0; i < Japan.areaArgs.length; i++) {
  119. CoverageData cover = new CoverageData(i, Japan.areaArgs[i], tableName);
  120.  
  121. // 分母(denominator)、分子(molecule)、Lv を記入
  122. // 全国の設定
  123. if (i == 0) {
  124. // 全国の総計
  125. String whereStr = ((where == null) ? "" : String.format("WHERE (%s)", where));
  126. String sql = String.format("SELECT COUNT(*) FROM t_%s %s", tableName, whereStr);
  127. System.out.println(sql);
  128. PreparedStatement ps2 = conPost.prepareStatement(sql);
  129. ResultSet rset2 = ps2.executeQuery();
  130. if (rset2.next()) {
  131. cover.denominator = rset2.getLong(1);
  132. }
  133. rset2.close();
  134.  
  135. whereStr = "WHERE (fixed > 0)" + ((where == null) ? "" : String.format(" and (%s)", where));
  136. sql = String.format("SELECT COUNT(*) FROM t_%s %s", tableName, whereStr);
  137. System.out.println(sql);
  138. ps2 = conPost.prepareStatement(sql);
  139. rset2 = ps2.executeQuery();
  140. if (rset2.next()) {
  141. cover.molecule = rset2.getLong(1);
  142. }
  143. rset2.close();
  144. }
  145. else {
  146. // 都道府県毎の小計
  147. String whereStr = "WHERE (area=?)" + ((where == null) ? "" : String.format(" and (%s)", where));
  148. String sql = String.format("SELECT COUNT(*) FROM t_%s %s", tableName, whereStr);
  149. PreparedStatement ps2 = conPost.prepareStatement(sql);
  150. ps2.setInt(1, i);
  151. System.out.println(String.format("'%s', %d", sql, i));
  152. ResultSet rset2 = ps2.executeQuery();
  153. if (rset2.next()) {
  154. cover.denominator = rset2.getLong(1);
  155. }
  156. rset2.close();
  157.  
  158. whereStr = "WHERE (fixed > 0) and (area=?)" + ((where == null) ? "" : String.format(" and (%s)", where));
  159. sql = String.format("SELECT COUNT(*) FROM t_%s %s", tableName, whereStr);
  160. System.out.println(String.format("'%s', %d", sql, i));
  161. ps2 = conPost.prepareStatement(sql);
  162. ps2.setInt(1, i);
  163. rset2 = ps2.executeQuery();
  164. if (rset2.next()) {
  165. cover.molecule = rset2.getLong(1);
  166. }
  167. rset2.close();
  168. }
  169.  
  170. if (cover.denominator > 0) {
  171. cover.par = (double)cover.molecule / cover.denominator;
  172. cover.par *= 100.0D;
  173. }
  174. else {
  175. cover.par = 100.0D;
  176. }
  177. data.add(cover);
  178. }
  179. }
  180.  
  181. /**
  182. * CSVファイルに出力する
  183. * @param outputFile
  184. * @throws IOException
  185. */
  186. public void outputCSV(File outputFile) throws IOException {
  187. CsvFile csv = new CsvFile(outputFile);
  188. CsvRecord line = new CsvRecord();
  189. line.add("コード");
  190. line.add("都道府県");
  191. line.add("母数");
  192. line.add("入力数");
  193. line.add("率(%)");
  194. csv.add(line);
  195. System.out.println(line.toString());
  196.  
  197. try {
  198. for (CoverageData cover : this.data) {
  199. line = new CsvRecord();
  200. line.add(String.valueOf(cover.areacode));
  201. line.add(cover.name);
  202. line.add(String.valueOf(cover.denominator));
  203. line.add(String.valueOf(cover.molecule));
  204. line.add(String.valueOf(df3.format(cover.par)));
  205. csv.add(line);
  206. System.out.println(line.toString());
  207. }
  208. }
  209. finally {
  210. csv.save();
  211. }
  212. }
  213. public void outputJson(File outputFile, String sourcedata) throws FileNotFoundException, UnsupportedEncodingException, IOException {
  214. try (BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"))) {
  215. int level = 0;
  216. hw.write(space(level++) + "{");
  217. hw.newLine();
  218. hw.write(space(level) + "\"timestamp\": \""+ dateStr +"\",");
  219. hw.newLine();
  220. hw.write(space(level) + "\"sourcedata\": \""+ sourcedata +"\",");
  221. hw.newLine();
  222. hw.write(space(level++) + "\"coverage\": [");
  223. hw.newLine();
  224. boolean head = true;
  225. for (CoverageData cover : this.data) {
  226. if (head == false) {
  227. hw.write(space(level) + ",");
  228. }
  229. else {
  230. head = false;
  231. }
  232. hw.write(space(level++) + "{");
  233. hw.newLine();
  234.  
  235. hw.write(space(level) + "\"code\": "+ String.valueOf(cover.areacode) +",");
  236. hw.newLine();
  237. hw.write(space(level) + "\"name\": \""+ cover.name +"\",");
  238. hw.newLine();
  239. hw.write(space(level) + "\"denominator\": "+ String.valueOf(cover.denominator) +",");
  240. hw.newLine();
  241. hw.write(space(level) + "\"molecule\": "+ String.valueOf(cover.molecule) +",");
  242. hw.newLine();
  243. hw.write(space(level) + "\"par\": "+ String.valueOf(df3.format(cover.par)));
  244. hw.newLine();
  245.  
  246. hw.write(space(--level) + "}");
  247. hw.newLine();
  248. }
  249. hw.write(space(--level) + "]");
  250. hw.newLine();
  251. hw.write(space(--level) + "}");
  252. hw.newLine();
  253. hw.flush();
  254. }
  255. }
  256. static String space(int level) {
  257. return (" ".substring(0,(level > 12 ? 12 : level)));
  258. }
  259. }