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