Newer
Older
osmCoverage / test / osm / jp / coverage / busstop / DbExistBusstopTest.java
@hayashi hayashi on 3 Nov 2018 9 KB TEST: BUSSTOP.removed
  1. package osm.jp.coverage.busstop;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import javax.json.Json;
  8. import javax.json.JsonArrayBuilder;
  9. import javax.json.JsonBuilderFactory;
  10. import javax.json.JsonObject;
  11. import javax.json.JsonObjectBuilder;
  12. import jp.co.areaweb.tools.database.DatabaseTool;
  13. import static org.hamcrest.CoreMatchers.is;
  14. import org.junit.*;
  15. import static org.junit.Assert.*;
  16. import org.junit.runners.MethodSorters;
  17. import static osm.jp.api.Osmdb.POINT_BUS_NO;
  18. import static osm.jp.api.Osmdb.POINT_FIXME;
  19. import static osm.jp.api.Osmdb.POINT_NO_NAME;
  20. import osm.jp.coverage.DbTest;
  21.  
  22. /**
  23. *
  24. * @author yuu
  25. */
  26. @FixMethodOrder (MethodSorters.NAME_ASCENDING)
  27. public class DbExistBusstopTest {
  28. String databaseName = "database";
  29. String tableName = "BUSSTOP_EXIST";
  30. /**
  31. * コマンド引数チェック
  32. *
  33. * @throws Exception
  34. */
  35. @Test
  36. public void t00_main_null() throws Exception {
  37. try {
  38. String[] args = new String[]{};
  39. DbExistBusstop.main(args);
  40. fail();
  41. }
  42. catch(Exception e) {
  43. System.out.println("OK! --> "+ e.toString());
  44. }
  45. }
  46. @Test
  47. public void t01_main_illiegal() throws Exception {
  48. try {
  49. String[] args = new String[]{"-"};
  50. DbExistBusstop.main(args);
  51. fail();
  52. }
  53. catch(Exception e) {
  54. System.out.println("OK! --> "+ e.toString());
  55. }
  56. }
  57. /**
  58. * (1)テーブルを初期化する
  59. *
  60. * @throws Exception
  61. */
  62. @Test
  63. public void t10_main_init() throws Exception {
  64. try {
  65. String[] args = new String[]{"-INIT"};
  66. DbExistBusstop.main(args);
  67. }
  68. catch(Exception e) {
  69. fail(e.toString());
  70. }
  71. DbTest.checkDatabase(databaseName);
  72. try (Connection hsqldb = DatabaseTool.openDb(databaseName)) {
  73. assertThat(DbTest.isTable(hsqldb, tableName), is(true));
  74. assertThat(DbTest.getRecordCount(hsqldb, tableName), is(0));
  75.  
  76. assertThat(DbTest.isTable(hsqldb, "AREA_NODE"), is(true));
  77. assertThat(DbTest.getRecordCount(hsqldb, "AREA_NODE"), is(0));
  78. }
  79. catch (Exception e) {
  80. fail(e.toString());
  81. }
  82. }
  83. /**
  84. * (2)GMLファイルを読み取る(国土数値情報の読み取り)
  85. *
  86. * @throws Exception
  87. */
  88. @Test
  89. public void t20_main_import() throws Exception {
  90. try {
  91. String[] args = new String[]{"-IMPORT"};
  92. DbExistBusstop.main(args);
  93. }
  94. catch(Exception e) {
  95. fail(e.toString());
  96. }
  97. }
  98. @Test
  99. public void t21_main_import() {
  100. try (Connection hsqldb = DatabaseTool.openDb(databaseName)) {
  101. int cnt = DbTest.getRecordCount(hsqldb, tableName);
  102. System.out.println("'BUSSTOP' table count = " + cnt);
  103. assertThat(cnt > 100, is(true));
  104. cnt = DbTest.getRecordCount(hsqldb, tableName, "REMOVED=TRUE");
  105. System.out.println("'BUSSTOP REMOVED=TRUE' table count = " + cnt);
  106. assertThat(cnt > 0, is(true));
  107.  
  108. String sql = "SELECT * FROM "+ tableName +" WHERE ";
  109. // REMOVED
  110. PreparedStatement ps1 = hsqldb.prepareStatement(sql +"removed=true");
  111. try (ResultSet rset1 = ps1.executeQuery()) {
  112. while (rset1.next()) {
  113. String idref = rset1.getString("idref");
  114. int area = rset1.getInt("area");
  115. int score = rset1.getInt("score");
  116. System.out.println(
  117. String.format("'BUSSTOP REMOVED'{idref='%s', area=%d, score=%d}", idref, area, score)
  118. );
  119. //assertThat(score >= 50, is(true));
  120. }
  121. }
  122.  
  123. // ノード: 八幡前 (3152604023)
  124. // 場所: 33.9808001, 133.3123441
  125. // fixme有り
  126. ps1 = hsqldb.prepareStatement(sql +"idref='3152604023'");
  127. try (ResultSet rset1 = ps1.executeQuery()) {
  128. if (rset1.next()) {
  129. // fixme有り
  130. assertThat(rset1.getInt("score"), is(1));
  131. assertThat(checkRenge(rset1, "33.9808001", "133.3123441"), is(true));
  132. assertThat(rset1.getBoolean("removed"), is(false));
  133. }
  134. else {
  135. fail();
  136. }
  137. }
  138. // ノード: 4940018338
  139. // 場所: 35.5909251, 139.1498642
  140. // highway=bus_stop, name=null, bus=yes,public_transport=platform
  141. ps1 = hsqldb.prepareStatement(sql + "idref='4940018338'");
  142. try (ResultSet rset1 = ps1.executeQuery()) {
  143. if (rset1.next()) {
  144. // nameなし
  145. assertThat(rset1.getInt("score"), is(1));
  146. assertThat(checkRenge(rset1, "35.5909251", "139.1498642"), is(true));
  147. assertThat(rset1.getBoolean("removed"), is(false));
  148. }
  149. else {
  150. fail();
  151. }
  152. }
  153. // ノード: 海老名高校前 (2043102034)
  154. // 場所: 35.4435042, 139.3878934
  155. // disused:highway=bus_stop
  156. // operator=海老名市コミュニティバス
  157. // bench=yes
  158. // ref=11
  159. ps1 = hsqldb.prepareStatement(sql + "idref='2043102034'");
  160. try (ResultSet rset1 = ps1.executeQuery()) {
  161. if (rset1.next()) {
  162. assertThat(rset1.getInt("score"), is(50));
  163. assertThat(checkRenge(rset1, "35.4435042", "139.3878934"), is(true));
  164. assertThat(rset1.getBoolean("removed"), is(true));
  165. }
  166. else {
  167. fail();
  168. }
  169. }
  170. // ノード: 厚木ナイロン (1995040609)
  171. // 場所: 35.4433312, 139.3932098
  172. // public_transport=stop_position,bus=yes
  173. ps1 = hsqldb.prepareStatement(sql + "idref='1995040609'");
  174. try (ResultSet rset1 = ps1.executeQuery()) {
  175. if (rset1.next()) {
  176. assertThat(rset1.getInt("score"), is(50));
  177. assertThat(checkRenge(rset1, "35.4433312", "139.3932098"), is(true));
  178. assertThat(rset1.getBoolean("removed"), is(false));
  179. }
  180. else {
  181. fail();
  182. }
  183. }
  184. // ウェイ: 国分寺台第12 (154659062)
  185. // bus_station
  186. ps1 = hsqldb.prepareStatement(sql + "idref='154659062'");
  187. try (ResultSet rset1 = ps1.executeQuery()) {
  188. if (rset1.next()) {
  189. assertThat(rset1.getInt("score"), is(50));
  190. assertThat(rset1.getBoolean("removed"), is(false));
  191. }
  192. else {
  193. fail();
  194. }
  195. }
  196. }
  197. catch (Exception e) {
  198. fail(e.toString());
  199. }
  200. }
  201. @Test
  202. public void test03DbExistBusstopTest_busstop() {
  203. Connection hsqldb = null;
  204. try {
  205. hsqldb = DatabaseTool.openDb("database");
  206. DbExistBusstop osmExist = new DbExistBusstop(hsqldb);
  207. JsonBuilderFactory factory = Json.createBuilderFactory(null);
  208. JsonArrayBuilder abuilder = factory.createArrayBuilder();
  209. JsonObjectBuilder builder = factory.createObjectBuilder();
  210. builder.add("k","public_transport");
  211. builder.add("v", "platform");
  212. JsonObject tag = builder.build();
  213. abuilder.add(tag);
  214. builder = factory.createObjectBuilder();
  215. builder.add("k","bus");
  216. builder.add("v", "yes");
  217. tag = builder.build();
  218. abuilder.add(tag);
  219.  
  220. osmExist.readExistingSub(abuilder.build(), POINT_BUS_NO | POINT_NO_NAME | POINT_FIXME, false);
  221. } catch (Exception ex) {
  222. fail(ex.toString());
  223. } finally {
  224. if (hsqldb != null) {
  225. DatabaseTool.closeDb(hsqldb);
  226. }
  227. }
  228.  
  229. }
  230. /**
  231. * (3)'table.OSM_EXIST'の内容をCSV形式にして標準出力に出力する
  232. *
  233. * @throws Exception
  234. */
  235. @Test
  236. public void t30_main_expport() throws Exception {
  237. try {
  238. String[] args = new String[]{"-EXPORT"};
  239. DbExistBusstop.main(args);
  240. }
  241. catch(Exception e) {
  242. fail(e.toString());
  243. }
  244. }
  245.  
  246. boolean checkRenge(ResultSet rset, String latStr, String lonStr) throws SQLException {
  247. if (checkRenge(rset.getDouble("lat"), latStr)) {
  248. if (checkRenge(rset.getDouble("lon"), lonStr)) {
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. boolean checkRenge(double d1, String str) throws SQLException {
  255. double base = Double.parseDouble(str);
  256. double up = d1 + 0.00000009D;
  257. double down = d1 - 0.00000009D;
  258. boolean ret = true;
  259. if (Double.compare(base, up) > 0) {
  260. ret = false;
  261. }
  262. if (Double.compare(base, down) < 0) {
  263. ret = false;
  264. }
  265. System.out.println("d1: "+ d1 +" : "+ str +" --> "+ (ret ? "IN" : "out"));
  266. return ret;
  267. }
  268. }