Newer
Older
osmCoverage / src / osm / jp / api / Osmdb.java
  1. package osm.jp.api;
  2.  
  3. import hayashi.yuu.tools.json.JsonTool;
  4. import java.io.*;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.SQLIntegrityConstraintViolationException;
  10. import java.sql.SQLSyntaxErrorException;
  11. import javax.json.Json;
  12. import javax.json.JsonArray;
  13. import javax.json.JsonBuilderFactory;
  14. import javax.json.JsonObject;
  15. import javax.json.JsonObjectBuilder;
  16. import javax.json.JsonValue;
  17. import javax.xml.parsers.ParserConfigurationException;
  18. import jp.co.areaweb.tools.csv.CsvRecord;
  19. import jp.co.areaweb.tools.database.DatabaseTool;
  20. import org.hsqldb.HsqlException;
  21. import org.w3c.dom.NamedNodeMap;
  22. import org.w3c.dom.Node;
  23. import org.w3c.dom.NodeList;
  24. import org.xml.sax.SAXException;
  25. import osm.jp.coverage.busstop.DbBusstop;
  26.  
  27. public abstract class Osmdb {
  28. public String TABLE_NAME = "EXIST_osm";
  29. public String tableName = TABLE_NAME;
  30. public Connection hsqldb = null; // hsqldb DatabaseTool.openDb("database");
  31.  
  32. public Osmdb(Connection hsqldb, String tableName) {
  33. this.hsqldb = hsqldb;
  34. if (tableName != null) {
  35. this.tableName = tableName;
  36. }
  37. }
  38. /**
  39. * 'HSQLDB.table.OSM_EXIST'を新規に作る
  40. * 'HSQLDB.table.AREA_NODE'を新規に作る
  41. * 既にテーブルが存在する時にはERROR
  42. * @throws SQLException
  43. */
  44. public void create() throws SQLException {
  45. String createSt;
  46. sql("DROP TABLE IF EXISTS "+ tableName +" CASCADE");
  47. sql("DROP INDEX IF EXISTS "+ tableName +"_index;");
  48. sql("DROP TABLE IF EXISTS AREA_NODE CASCADE");
  49.  
  50. // 'table.OSM_EXIST'を新規に作る
  51. createSt = "CREATE TABLE "+ tableName
  52. + " ("
  53. + "idref VARCHAR(12) NOT NULL, "
  54. + "name VARCHAR(128), "
  55. + "lat DOUBLE, "
  56. + "lon DOUBLE, "
  57. + "score INT, "
  58. + "gmlid VARCHAR(12), "
  59. + "area INT, "
  60. + "PRIMARY KEY(idref), "
  61. + "removed BOOLEAN DEFAULT FALSE NOT NULL"
  62. + ");";
  63. Db.updateSQL(hsqldb, createSt);
  64. createSt = "CREATE INDEX "+ tableName +"_index ON "+ tableName
  65. + " (lat, lon);";
  66. Db.updateSQL(hsqldb, createSt);
  67.  
  68. // 'table.AREA_NODE'を新規に作る
  69. createSt = "CREATE TABLE AREA_NODE "
  70. + "("
  71. + "idref VARCHAR(12) NOT NULL, "
  72. + "pid VARCHAR(12), "
  73. + "lat DOUBLE, "
  74. + "lon DOUBLE"
  75. + ");";
  76. Db.updateSQL(hsqldb, createSt);
  77. }
  78. void sql(String sql) throws SQLException {
  79. System.out.println(sql);
  80. try (PreparedStatement ps = hsqldb.prepareStatement(sql)) {
  81. ps.executeUpdate();
  82. }
  83. catch (SQLSyntaxErrorException e) {
  84. System.out.println("107:"+ e.toString());
  85. if (!(e.toString().startsWith("java.sql.SQLSyntaxErrorException: user lacks privilege or object not found:"))) {
  86. throw e;
  87. }
  88. }
  89. }
  90.  
  91. public static void outputWriter(PrintWriter pw, String text) {
  92. System.out.println("\t" + text);
  93. pw.print(text);
  94. }
  95. public static final int POINT_NO = 0; // 評価ポイント無し→ score=50
  96. public static final int POINT_FIXME = 1; // 評価ポイント|!(fixme=null) → score=1
  97. public static final int POINT_NO_BRAND = 2; // 評価ポイント|brand=null → score=1
  98. public static final int POINT_NO_NAME = 4; // 評価ポイント|name=null → score=1
  99. public static final int POINT_BUS_NO = 8; // 評価ポイント|!(bus=yes) → score=0
  100. /**
  101. * 'table.OSM_EXIST'の内容をCSV形式にして標準出力に出力する
  102. * @throws java.sql.SQLException
  103. */
  104. public void export() throws SQLException {
  105. String header = "idref,lat,lon,score";
  106. System.out.println("TABLE: "+ tableName);
  107. System.out.println(header);
  108. PreparedStatement ps8 = hsqldb.prepareStatement(
  109. "SELECT idref,lat,lon,score,removed FROM "+ tableName
  110. );
  111. try (ResultSet rset8 = ps8.executeQuery()) {
  112. while (rset8.next()) {
  113. String idcode = rset8.getString(1);
  114. Double lat = rset8.getDouble(2);
  115. Double lon = rset8.getDouble(3);
  116. int score = rset8.getInt(4);
  117. boolean removed = rset8.getBoolean(5);
  118. String out = String.format(
  119. "OSM: %s,%2.5f,%3.5f,%d,%b",
  120. idcode, lat, lon, score, removed
  121. );
  122. System.out.println(out);
  123. }
  124. }
  125. }
  126.  
  127. public int importExistingNode(Connection osmdb, ResultSet rset, int point, boolean removed) throws IOException, SQLException {
  128. String osmidStr = rset.getString("osm_id");
  129. String latStr = rset.getString("lat");
  130. String lonStr = rset.getString("lon");
  131. String brandStr = rset.getString("brand");
  132. String nameStr = rset.getString("name");
  133. String fixmeStr = "";
  134.  
  135. int score = 50;
  136. if (((point & POINT_NO_BRAND) != 0) && isNull(brandStr)) {
  137. score = 1;
  138. }
  139. if (((point & POINT_NO_NAME) != 0) && isNull(nameStr)) {
  140. score = 1;
  141. }
  142. boolean fixme = false;
  143. boolean busYes = false;
  144. if (((point & (POINT_FIXME | POINT_BUS_NO)) != 0)) {
  145. PreparedStatement ps8 = osmdb.prepareStatement(
  146. "SELECT * FROM planet_osm_nodes where id='"+ osmidStr +"'"
  147. );
  148. try (ResultSet rset8 = ps8.executeQuery()) {
  149. while (rset8.next()) {
  150. String tags = rset8.getString("tags");
  151. if (tags != null) {
  152. tags = tags.substring(1);
  153. tags = tags.substring(0, tags.length()-1);
  154. CsvRecord csv = new CsvRecord();
  155. csv.analizeRecord(tags);
  156. boolean key = true;
  157. boolean bus = false;
  158. for (String str : csv) {
  159. if (key) {
  160. if (str.startsWith("fixme")) {
  161. fixme = true;
  162. }
  163. if (str.equals("bus")) {
  164. bus = true;
  165. }
  166. key = false;
  167. }
  168. else {
  169. if (bus) {
  170. if (str.equals("yes")) {
  171. busYes = true;
  172. }
  173. bus = false;
  174. }
  175. key = true;
  176. }
  177. }
  178. //System.out.println(tags);
  179. }
  180. }
  181. }
  182. if (((point & POINT_FIXME) != 0) && fixme) {
  183. score = 1;
  184. }
  185. if (((point & POINT_BUS_NO) != 0) && !busYes) {
  186. score = 0;
  187. }
  188. }
  189. if (score > 0) {
  190. // idref と brandStr をデータベースに格納する
  191. System.out.println(
  192. "INSERT INTO "+ tableName
  193. + " (idref,lat,lon,score,name,removed) VALUES ("
  194. + osmidStr +","
  195. + latStr +","
  196. + lonStr +","
  197. + Integer.toString(score) +","
  198. + "'"+ nameStr +"',"
  199. + "false"
  200. + ")N"
  201. );
  202. try (PreparedStatement ps5 = hsqldb.prepareStatement(
  203. "INSERT INTO "+ tableName +" (idref,lat,lon,score,name,removed) VALUES (?,?,?,?,?,?)"
  204. )) {
  205. ps5.setString(1, osmidStr);
  206. ps5.setDouble(2, Double.parseDouble(latStr));
  207. ps5.setDouble(3, Double.parseDouble(lonStr));
  208. ps5.setInt(4, score);
  209. ps5.setString(5, nameStr);
  210. ps5.setBoolean(6, removed);
  211. ps5.executeUpdate();
  212. return 1;
  213. }
  214. catch (HsqlException | SQLIntegrityConstraintViolationException e) {
  215. // integrity constraint violation: unique constraint or index violation; SYS_PK_10069 table: FUEL_EXIST
  216. // [HsqlException]は、無視する
  217. // integrity constraint violation: unique constraint or index violation; SYS_PK_10069 table: FUEL_EXIST
  218. // [SQLIntegrityConstraintViolationException]は、無視する
  219. }
  220. }
  221.  
  222. return 0;
  223. }
  224.  
  225. /**
  226. * File(HttpPOST.EXIST_FILE)を読み取って、データベースに反映させる。<br>
  227. * その際に、OSMノードを評価し、scoreを算定する
  228. *
  229. * @param sqlWhere POI条件
  230. * @param point 評価ポイント[POINT_NO|POINT_BRAND|POINT_NAME] 1: 'brand' 2:'name'
  231. * @param removed
  232. * @throws Exception エラー
  233. */
  234. public void readExisting(String sqlWhere, int point, boolean removed) throws Exception {
  235. long counter = 0L;
  236. try (Connection osmdb = DatabaseTool.openDb("osmdb")) {
  237. String sqlSelect = "osm_id,"
  238. + "brand,"
  239. + "disused,"
  240. + "name,"
  241. + "ST_Y(ST_Transform(way,4326)) as lat,"
  242. + "ST_X(ST_Transform(way,4326)) as lon";
  243. String sqlNode = String.format(
  244. "SELECT %s FROM planet_osm_point %s",
  245. sqlSelect,
  246. sqlWhere
  247. );
  248. PreparedStatement ps1 = osmdb.prepareStatement(sqlNode);
  249. try (ResultSet rset1 = ps1.executeQuery()) {
  250. while (rset1.next()) {
  251. counter += importExistingNode(osmdb, rset1, point, removed);
  252. }
  253. }
  254. String sqlArea = String.format(
  255. "SELECT %s FROM planet_osm_polygon %s",
  256. "osm_id,brand,disused,name,ST_Y(ST_Transform(ST_Centroid(way),4326)) as lat,ST_X(ST_Transform(ST_Centroid(way),4326)) as lon",
  257. sqlWhere
  258. );
  259. PreparedStatement ps2 = osmdb.prepareStatement(sqlArea);
  260. try (ResultSet rset2 = ps2.executeQuery()) {
  261. while (rset2.next()) {
  262. counter += importExistingNode(osmdb, rset2, point, removed);
  263. }
  264. }
  265.  
  266. System.out.println("Exists Node count = " + counter);
  267. }
  268. }
  269.  
  270. /**
  271. *
  272. * SELECT
  273. * planet_osm_nodes.id,
  274. * planet_osm_nodes.tags
  275. * planet_osm_nodes.way --> lat & lon
  276. * FROM
  277. * public.planet_osm_point,
  278. * public.planet_osm_nodes
  279. * WHERE (
  280. * planet_osm_point.osm_id = planet_osm_nodes.id
  281. * and 'disused:amenity'=ANY(planet_osm_nodes.tags)
  282. * and 'fuel'=ANY(planet_osm_nodes.tags)
  283. * )
  284. *
  285. * @param kStr
  286. * @param vStr
  287. * @param point
  288. * @throws Exception
  289. */
  290. public void readExistingSub(String kStr, String vStr, int point) throws Exception {
  291. readExistingSub(kStr, vStr, point, true);
  292. }
  293. public void readExistingSub(String kStr, String vStr, int point, boolean removed) throws Exception {
  294. JsonBuilderFactory factory = Json.createBuilderFactory(null);
  295. JsonObjectBuilder builder = factory.createObjectBuilder();
  296. builder.add("k",kStr);
  297. builder.add("v", vStr);
  298. JsonObject tag = builder.build();
  299. readExistingSub(tag, point, removed);
  300. }
  301. /**
  302. *
  303. * @param tag {k: string, v: string}
  304. * @param point
  305. * @throws Exception
  306. */
  307. public void readExistingSub(JsonObject tag,int point) throws Exception {
  308. readExistingSub(tag, point, true);
  309. }
  310.  
  311. /**
  312. *
  313. * @param tag {k: string, v: string}
  314. * @param point
  315. * @param removed
  316. * @throws Exception
  317. */
  318. public void readExistingSub(JsonObject tag,int point, boolean removed) throws Exception {
  319. long counter = 0L;
  320. try (Connection osmdb = DatabaseTool.openDb("osmdb")) {
  321. String sqlSelect = "SELECT tags,id,ST_Y(ST_Transform(way,4326)) as lat,ST_X(ST_Transform(way,4326)) as lon";
  322. String sqlWhere = String.format(
  323. "WHERE (osm_id=id AND '%s'=ANY(tags) AND '%s'=ANY(tags))",
  324. tag.getString("k"), tag.getString("v")
  325. );
  326. String sql = String.format(
  327. "%s FROM planet_osm_point,planet_osm_nodes %s", sqlSelect, sqlWhere
  328. );
  329. System.out.println(sql);
  330. PreparedStatement ps1 = osmdb.prepareStatement(sql);
  331. try (ResultSet rset1 = ps1.executeQuery()) {
  332. while (rset1.next()) {
  333. String tags = rset1.getString("tags");
  334. String idref = rset1.getString("id");
  335. String latStr = rset1.getString("lat");
  336. String lonStr = rset1.getString("lon");
  337. double lat = Double.valueOf(latStr);
  338. double lon = Double.valueOf(lonStr);
  339. int score = score(point, tags);
  340. counter += insertExistingNode(idref, lat, lon, score, "", removed);
  341. }
  342. }
  343. System.out.println("Exists Node count = " + counter);
  344. }
  345. }
  346.  
  347. /**
  348. *
  349. * @param array tags: [{k: string, v: string}]
  350. * @param point
  351. * @throws Exception
  352. */
  353. public void readExistingSub(JsonArray array,int point) throws Exception {
  354. readExistingSub(array,point, true);
  355. }
  356.  
  357. /**
  358. *
  359. * @param array tags: [{k: string, v: string}]
  360. * @param point
  361. * @param removed
  362. * @throws Exception
  363. */
  364. public void readExistingSub(JsonArray array,int point, boolean removed) throws Exception {
  365. long counter = 0L;
  366. try (Connection osmdb = DatabaseTool.openDb("osmdb")) {
  367. String sqlSelect = "SELECT "
  368. + "tags,"
  369. + "id,"
  370. + "ST_Y(ST_Transform(way,4326)) as lat,"
  371. + "ST_X(ST_Transform(way,4326)) as lon";
  372. String sqlSS = "";
  373. for (JsonValue v : array) {
  374. JsonObject tag = (JsonObject)v;
  375. sqlSS += String.format(
  376. " AND ('%s'=ANY(tags) AND '%s'=ANY(tags))",
  377. tag.getString("k"), tag.getString("v")
  378. );
  379. }
  380. String sqlWhere = String.format("WHERE (osm_id=id %s)", sqlSS);
  381. String sql = String.format(
  382. "%s FROM planet_osm_point,planet_osm_nodes %s",
  383. sqlSelect, sqlWhere
  384. );
  385. System.out.println(sql);
  386. PreparedStatement ps1 = osmdb.prepareStatement(sql);
  387. try (ResultSet rset1 = ps1.executeQuery()) {
  388. while (rset1.next()) {
  389. String tags = rset1.getString("tags");
  390. String idref = rset1.getString("id");
  391. String latStr = rset1.getString("lat");
  392. String lonStr = rset1.getString("lon");
  393. double lat = Double.valueOf(latStr);
  394. double lon = Double.valueOf(lonStr);
  395. int score = score(point, tags);
  396. counter += insertExistingNode(idref, lat, lon, score, "", removed);
  397. }
  398. }
  399. System.out.println("Exists Node count = " + counter);
  400. }
  401. }
  402.  
  403. int score(int point, String tags) {
  404. int score = 50;
  405. if (tags == null) {
  406. return score;
  407. }
  408. boolean brandYes = false;
  409. boolean busYes = false;
  410. boolean fixmeYes = false;
  411. boolean nameYes = false;
  412. tags = tags.substring(1);
  413. tags = tags.substring(0, tags.length()-1);
  414. CsvRecord csv = new CsvRecord();
  415. csv.analizeRecord(tags);
  416.  
  417. boolean key = true;
  418. boolean bus = false;
  419. for (String str : csv) {
  420. if (key) {
  421. if (str.startsWith("fixme")) {
  422. fixmeYes = true;
  423. }
  424. else if (str.equals("bus")) {
  425. bus = true;
  426. }
  427. else if (str.equals("brand")) {
  428. brandYes = true;
  429. }
  430. else if (str.startsWith("name")) {
  431. nameYes = true;
  432. }
  433. key = false;
  434. }
  435. else {
  436. if (bus) {
  437. if (str.equals("yes")) {
  438. busYes = true;
  439. }
  440. bus = false;
  441. }
  442. key = true;
  443. }
  444. }
  445. if (((point & POINT_NO_BRAND) != 0) && !brandYes) {
  446. score = 1;
  447. }
  448. if (((point & POINT_NO_NAME) != 0) && !nameYes) {
  449. score = 1;
  450. }
  451. if (((point & POINT_FIXME) != 0) && fixmeYes) {
  452. score = 1;
  453. }
  454. if (((point & POINT_BUS_NO) != 0) && !busYes) {
  455. score = 0;
  456. }
  457. return score;
  458. }
  459. /**
  460. *
  461. * @param idref
  462. * @param lat
  463. * @param lon
  464. * @param score
  465. * @param name
  466. * @return
  467. * @throws IOException
  468. * @throws SQLException
  469. */
  470. public int insertExistingNode(String idref, double lat, double lon, int score, String name) throws IOException, SQLException {
  471. return insertExistingNode(idref, lat, lon, score, name, false);
  472. }
  473.  
  474. /**
  475. *
  476. * @param idref
  477. * @param lat
  478. * @param lon
  479. * @param score
  480. * @param name
  481. * @param removed
  482. * @return
  483. * @throws IOException
  484. * @throws SQLException
  485. */
  486. public int insertExistingNode(String idref, double lat, double lon, int score, String name, boolean removed) throws IOException, SQLException {
  487. // idref と brandStr をデータベースに格納する
  488. /*
  489. */
  490. String sql = String.format("DELETE FROM %s WHERE idref=?", tableName);
  491. try (PreparedStatement ps5 = hsqldb.prepareStatement(sql)) {
  492. ps5.setString(1, idref);
  493. ps5.executeUpdate();
  494. }
  495. catch (HsqlException | SQLIntegrityConstraintViolationException e) {
  496. // integrity constraint violation: unique constraint or index violation; SYS_PK_10069 table: FUEL_EXIST
  497. // [HsqlException]は、無視する
  498. // integrity constraint violation: unique constraint or index violation; SYS_PK_10069 table: FUEL_EXIST
  499. // [SQLIntegrityConstraintViolationException]は、無視する
  500. }
  501.  
  502. sql = String.format(
  503. "INSERT INTO %s (idref,lat,lon,score,name,removed) VALUES (%s,%2.7f,%3.7f,%d,%s,%s)",
  504. tableName,
  505. idref, lat, lon, score, name, (removed ? "true":"false")
  506. );
  507. System.out.println(sql);
  508. sql = String.format(
  509. "INSERT INTO %s (idref,lat,lon,score,name,removed) VALUES (?,?,?,?,?,?)",
  510. tableName
  511. );
  512. try (PreparedStatement ps5 = hsqldb.prepareStatement(sql)) {
  513. ps5.setString(1, idref);
  514. ps5.setDouble(2, lat);
  515. ps5.setDouble(3, lon);
  516. ps5.setInt(4, score);
  517. ps5.setString(5, name);
  518. ps5.setBoolean(6, removed);
  519. ps5.executeUpdate();
  520. return 1;
  521. }
  522. catch (HsqlException | SQLIntegrityConstraintViolationException e) {
  523. // integrity constraint violation: unique constraint or index violation; SYS_PK_10069 table: FUEL_EXIST
  524. // [HsqlException]は、無視する
  525. // integrity constraint violation: unique constraint or index violation; SYS_PK_10069 table: FUEL_EXIST
  526. // [SQLIntegrityConstraintViolationException]は、無視する
  527. }
  528. return 0;
  529. }
  530. /**
  531. * REMOVEDファイルからデータを揉み込む
  532. *
  533. * @param revovedFile
  534. * @throws FileNotFoundException
  535. * @throws IOException
  536. * @throws SQLException
  537. */
  538. public void loadRemoved(File revovedFile) throws FileNotFoundException, IOException, SQLException {
  539. if (revovedFile == null) {
  540. throw new FileNotFoundException();
  541. }
  542. if (!revovedFile.exists()) {
  543. throw new FileNotFoundException();
  544. }
  545. if (!revovedFile.isFile()) {
  546. throw new FileNotFoundException();
  547. }
  548. try (PreparedStatement ps = hsqldb.prepareStatement(
  549. "UPDATE "
  550. + tableName
  551. + " SET idref=?,removed=true,fixed1=100"
  552. + " WHERE gmlid=? AND area=?"
  553. )) {
  554. LineNumberReader reader = new LineNumberReader(
  555. new InputStreamReader(new FileInputStream(revovedFile))
  556. );
  557. while (reader.readLine() != null) {
  558. String lineStr = reader.readLine();
  559. if (lineStr == null) {
  560. continue;
  561. }
  562. if (lineStr.trim().length() < 3) {
  563. continue;
  564. }
  565.  
  566. JsonObject json = JsonTool.parse(lineStr);
  567. String gmlid = json.getString("gmlid");
  568. String idref = json.getString("idref");
  569. int area = json.getInt("area");
  570.  
  571. ps.setString(1, idref);
  572. ps.setString(2, gmlid);
  573. ps.setInt(3, area);
  574. ps.executeUpdate();
  575. }
  576. }
  577. }
  578. /**
  579. * removedデータをファイルに出力する
  580. *
  581. * @param removedFile
  582. * @throws SQLException
  583. * @throws java.io.IOException
  584. */
  585. public void outputRemoved(File removedFile) throws SQLException, IOException {
  586. String whereStr = "WHERE (removed=?)";
  587. String fromStr = "FROM "+ DbBusstop.TABLE_NAME;
  588. String sortStr = "ORDER BY area,gmlid";
  589. String sql = String.format("SELECT * %s %s %s", fromStr, whereStr, sortStr);
  590. try (FileWriter fw = new FileWriter(removedFile);
  591. PreparedStatement ps1 = hsqldb.prepareStatement(sql))
  592. {
  593. System.out.println(sql);
  594. ps1.setBoolean(1, true);
  595. try (ResultSet rset1 = ps1.executeQuery()) {
  596. while (rset1.next()) {
  597. String gmlid = rset1.getString("gmlid");
  598. int area = rset1.getInt("area");
  599. String idref = rset1.getString("idref");
  600. JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
  601. jsonBuilder.add("gmlid", gmlid);
  602. jsonBuilder.add("idref", (idref==null ? "" : idref));
  603. jsonBuilder.add("area", area);
  604. JsonObject value = jsonBuilder.build();
  605. String line = value.toString();
  606. fw.write(line);
  607. fw.write("\n");
  608. fw.flush();
  609. }
  610. }
  611. fw.close();
  612. }
  613. }
  614.  
  615. boolean isNull(String vstr) {
  616. if (vstr == null) {
  617. return true;
  618. }
  619. return (vstr.trim().length() < 1);
  620. }
  621.  
  622. int readAreaNodes(Connection con, Node node) throws IOException, SQLException {
  623. int iCounter = 0;
  624.  
  625. NodeList nodes = node.getChildNodes();
  626. for (int i = 0; i < nodes.getLength(); i++) {
  627. Node node2 = nodes.item(i);
  628. switch (node2.getNodeName()) {
  629. case "node":
  630. iCounter++;
  631. importAreaNode(con, node2);
  632. break;
  633. default:
  634. iCounter += readAreaNodes(con, node2);
  635. break;
  636. }
  637. }
  638. return iCounter;
  639. }
  640.  
  641. void importAreaNode(Connection con, Node node) throws IOException, SQLException {
  642. String idrefStr = "";
  643. String latStr = "";
  644. String lonStr = "";
  645.  
  646. NodeList nodes = node.getChildNodes();
  647. if (nodes.getLength() > 0) {
  648. return;
  649. }
  650.  
  651. NamedNodeMap nodeMap = node.getAttributes();
  652. if (nodeMap != null) {
  653. for (int j=0; j < nodeMap.getLength(); j++) {
  654. switch (nodeMap.item(j).getNodeName()) {
  655. case "id":
  656. idrefStr = nodeMap.item(j).getNodeValue();
  657. break;
  658. case "lat":
  659. latStr = nodeMap.item(j).getNodeValue();
  660. break;
  661. case "lon":
  662. lonStr = nodeMap.item(j).getNodeValue();
  663. break;
  664. default:
  665. break;
  666. }
  667. }
  668. // idref と brandStr をデータベースに格納する
  669. System.out.println("insert into AREA_NODE(idref,lat,lon) values("+ idrefStr +","+ latStr +","+ lonStr+")");
  670. try (PreparedStatement ps5 = con.prepareStatement(
  671. "INSERT INTO AREA_NODE (idref,lat,lon) VALUES (?,?,?)"
  672. )) {
  673. ps5.setString(1, idrefStr);
  674. ps5.setDouble(2, Double.parseDouble(latStr));
  675. ps5.setDouble(3, Double.parseDouble(lonStr));
  676. ps5.executeUpdate();
  677. }
  678. }
  679. }
  680. static Position getNdPosition(Connection con, String idref) throws SQLException {
  681. PreparedStatement ps8 = con.prepareStatement(
  682. "SELECT lat,lon FROM AREA_NODE where idref=?"
  683. );
  684. ps8.setString(1, idref);
  685. try (ResultSet rset8 = ps8.executeQuery()) {
  686. while (rset8.next()) {
  687. Double lat = rset8.getDouble(1);
  688. Double lon = rset8.getDouble(2);
  689. return new Position(lat,lon);
  690. }
  691. }
  692. return null;
  693. }
  694. public void dropTable () throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
  695. String sqlStr = "DROP TABLE IF EXISTS "+ tableName;
  696. try (PreparedStatement ps = hsqldb.prepareStatement(sqlStr)) {
  697. System.out.println(sqlStr);
  698. ps.executeUpdate();
  699. }
  700. }
  701. }