2 import osm.jp.api.HttpPOST;
\r
4 import javax.xml.parsers.*;
\r
5 import javax.xml.transform.OutputKeys;
\r
6 import javax.xml.transform.Transformer;
\r
7 import javax.xml.transform.TransformerException;
\r
8 import javax.xml.transform.TransformerFactory;
\r
9 import javax.xml.transform.dom.DOMSource;
\r
10 import javax.xml.transform.stream.StreamResult;
\r
12 import org.w3c.dom.*;
\r
13 import org.xml.sax.*;
\r
16 import java.sql.Connection;
\r
17 import java.sql.PreparedStatement;
\r
18 import java.sql.ResultSet;
\r
19 import java.sql.SQLException;
\r
20 import java.sql.Statement;
\r
21 import java.text.SimpleDateFormat;
\r
22 import java.util.ArrayList;
\r
23 import java.util.Calendar;
\r
24 import java.util.Date;
\r
26 import jp.co.areaweb.tools.database.*;
\r
28 public class ConvBusstop {
\r
33 public static final boolean DB_INIT = false;
\r
35 // 近くのバス停を探す範囲(バス停を中心としたNEER×2m四方の領域
\r
36 static final int NEER = 150;
\r
37 static boolean nocheck = false;
\r
42 * java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusstop <option>
\r
43 * OPTION: -nocheck OSMデータ上に既存のバス停が存在するかどうかをチェックしない
\r
44 * OPTION: -check OSMデータ上に既存のバス停が存在するかどうかをチェックする
\r
46 * @throws IOException
\r
47 * @throws SQLException
\r
48 * @throws ClassNotFoundException
\r
49 * @throws FileNotFoundException
\r
50 * @throws TransformerException
\r
51 * @throws SAXException
\r
52 * @throws ParserConfigurationException */
\r
53 public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException, TransformerException
\r
56 if (args.length > index) {
\r
57 if (args[index].equals("-check")) {
\r
58 ConvBusstop.nocheck = false;
\r
61 else if (args[index].equals("-nocheck")) {
\r
62 ConvBusstop.nocheck = true;
\r
68 * アプリケーション [ConvBusstop]
\r
69 * > java -jar ConvBusstop.jar <オプション>
\r
70 * オプション: -exp 実行する直前にデータベースを初期化する(省略可能)
\r
72 File dbdir = new File("database");
\r
73 if (!dbdir.isDirectory()) {
\r
77 Connection con = DatabaseTool.openDb("database");
\r
78 ConvBusstop.initDb(con);
\r
85 File dir = new File(".");
\r
86 File[] files = dir.listFiles();
\r
87 for (File iFile : files) {
\r
88 if (checkFile(iFile)) {
\r
90 ConvBusstop.clearDb(con);
\r
91 inputFile(con, iFile);
\r
93 String iStr = iFile.getName();
\r
94 iStr = iStr.substring(0, iStr.length() - 4);
\r
98 * --> 'existing.xml'
\r
100 if (!ConvBusstop.nocheck && (fcounter > 0)) {
\r
102 * 既存のOSMデータファイルがなければ、新たにダウンロードする。
\r
103 * OSMデータファイルがあるときは、ダウンロードしないでそれを使う。
\r
105 File existingFile = new File(iStr, "existing.xml");
\r
106 if (!existingFile.isFile()) {
\r
108 * インポートしたデータの緯度経度範囲を読み取る
\r
110 double maxLat = -90.0D;
\r
111 double minLat = 90.0D;
\r
112 double maxLon = -180.0D;
\r
113 double minLon = 180.0D;
\r
114 PreparedStatement ps8 = con.prepareStatement("SELECT lat,lon FROM bus_stop");
\r
115 ResultSet rset8 = ps8.executeQuery();
\r
116 while (rset8.next()) {
\r
117 Double lat = rset8.getDouble("lat");
\r
118 Double lon = rset8.getDouble("lon");
\r
120 if (lat > maxLat) {
\r
123 if (lon > maxLon) {
\r
126 if (lat < minLat) {
\r
129 if (lon < minLon) {
\r
136 * OSM OverPassAPI を使って、既存のOSMバス停のデータを取得して、「existing.xml」に出力する
\r
138 HttpPOST.getCapabilities(new File(iStr), minLat, maxLat, minLon, maxLon);
\r
140 readExistingFile(con, existingFile);
\r
142 PreparedStatement ps1 = con.prepareStatement("SELECT idref,name,lat,lon FROM bus_stop");
\r
143 PreparedStatement ps2 = con.prepareStatement("SELECT count(idref) FROM existing_data where (lat > ?) and (lat < ?) and (lon > ?) and (lon < ?)");
\r
144 PreparedStatement ps3 = con.prepareStatement("UPDATE bus_stop SET fixed=? WHERE idref=?");
\r
145 PreparedStatement ps4 = con.prepareStatement("SELECT count(idref) FROM existing_data where (lat > ?) and (lat < ?) and (lon > ?) and (lon < ?) and (name = ?)");
\r
146 ResultSet rset1 = ps1.executeQuery();
\r
147 while (rset1.next()) {
\r
148 String idref = rset1.getString("idref");
\r
149 String name = rset1.getString("name");
\r
150 Double lat = rset1.getDouble("lat");
\r
151 Double lon = rset1.getDouble("lon");
\r
153 // 指定の緯度経度を中心とする半径150x2m四方の矩形領域
\r
154 System.out.print(idref + "("+ name + ") ....");
\r
155 RectArea rect = new RectArea(lat, lon, NEER); // 300m 四方
\r
156 ps2.setDouble(1, rect.minlat);
\r
157 ps2.setDouble(2, rect.maxlat);
\r
158 ps2.setDouble(3, rect.minlon);
\r
159 ps2.setDouble(4, rect.maxlon);
\r
160 ResultSet rset2 = ps2.executeQuery();
\r
161 if (rset2.next()) {
\r
162 int count = rset2.getInt(1);
\r
164 System.out.println("."+ count);
\r
165 ps3.setInt(1, count);
\r
166 ps3.setString(2, idref);
\r
167 ps3.executeUpdate();
\r
170 // 指定の緯度経度を中心とする半径150x4m四方の矩形領域
\r
171 System.out.print("***");
\r
172 rect = new RectArea(lat, lon, NEER*2); // 600m 四方
\r
173 ps4.setDouble(1, rect.minlat);
\r
174 ps4.setDouble(2, rect.maxlat);
\r
175 ps4.setDouble(3, rect.minlon);
\r
176 ps4.setDouble(4, rect.maxlon);
\r
177 ps4.setString(5, name);
\r
178 ResultSet rset4 = ps4.executeQuery();
\r
179 if (rset4.next()) {
\r
180 count = rset4.getInt(1);
\r
181 System.out.println(".."+ count);
\r
182 ps3.setInt(1, count);
\r
183 ps3.setString(2, idref);
\r
184 ps3.executeUpdate();
\r
198 // ローカルデータベース内の情報を出力する
\r
199 outputDb(con, iStr);
\r
201 // ローカルデータベース内の情報を「busstop.xml」に出力する
\r
202 int counter = outputDb(con, new File(iStr));
\r
203 System.out.println("["+ counter +"] 変換完了しました。");
\r
206 System.out.println("["+ fcounter +"]つのファイルをインポートしました。");
\r
209 DatabaseTool.closeDb(con);
\r
213 static String[] shiftArgs(String[] args) {
\r
214 String[] values = new String[args.length - 1];
\r
215 for (int i=1; i < args.length; i++) {
\r
216 values[i - 1] = new String(args[i]);
\r
221 public static void readExistingFile (Connection con, File existingFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
\r
224 DocumentBuilderFactory factory;
\r
225 DocumentBuilder builder;
\r
229 factory = DocumentBuilderFactory.newInstance();
\r
230 builder = factory.newDocumentBuilder();
\r
231 factory.setIgnoringElementContentWhitespace(true);
\r
232 factory.setIgnoringComments(true);
\r
233 factory.setValidating(true);
\r
234 root = builder.parse(existingFile);
\r
236 iCounter += readExistingNodes(con, root);
\r
237 System.out.println("既存バス停数["+ iCounter +"]");
\r
240 static int readExistingNodes(Connection con, Node node) throws IOException, SQLException {
\r
243 NodeList nodes = node.getChildNodes();
\r
244 for (int i = 0; i < nodes.getLength(); i++) {
\r
245 Node node2 = nodes.item(i);
\r
246 if (node2.getNodeName().equals("node")) {
\r
248 importExistingNode(con, node2);
\r
251 iCounter += readExistingNodes(con, node2);
\r
257 static void importExistingNode(Connection con, Node node) throws IOException, SQLException {
\r
258 String idrefStr = "";
\r
259 String latStr = "";
\r
260 String lonStr = "";
\r
261 String nameStr = "";
\r
262 PreparedStatement ps5 = con.prepareStatement("INSERT INTO existing_data (idref,lat,lon, name) VALUES (?,?,?,?)");
\r
264 NamedNodeMap nodeMap = node.getAttributes();
\r
265 if (null != nodeMap) {
\r
266 for (int j=0; j < nodeMap.getLength(); j++) {
\r
267 if (nodeMap.item(j).getNodeName().equals("id")) {
\r
268 idrefStr = nodeMap.item(j).getNodeValue();
\r
270 else if (nodeMap.item(j).getNodeName().equals("lat")) {
\r
271 latStr = nodeMap.item(j).getNodeValue();
\r
273 else if (nodeMap.item(j).getNodeName().equals("lon")) {
\r
274 lonStr = nodeMap.item(j).getNodeValue();
\r
278 NodeList nodes = node.getChildNodes();
\r
279 for (int i = 0; i < nodes.getLength(); i++) {
\r
280 Node node2 = nodes.item(i);
\r
281 if (node2.getNodeName().equals("tag")) {
\r
282 NamedNodeMap nodeMap2 = node2.getAttributes();
\r
283 if (null != nodeMap2) {
\r
285 String value = null;
\r
286 for (int j=0; j < nodeMap2.getLength(); j++) {
\r
287 if (nodeMap2.item(j).getNodeName().equals("k")) {
\r
288 key = nodeMap2.item(j).getNodeValue();
\r
290 else if (nodeMap2.item(j).getNodeName().equals("v")) {
\r
291 value = nodeMap2.item(j).getNodeValue();
\r
295 if ((key != null) && key.toLowerCase().equals("name") && (value != null)) {
\r
303 // idref と nameStr をデータベースに格納する
\r
304 System.out.println("import existing_data : "+ idrefStr +" ("+ latStr +","+ lonStr+")"+ nameStr);
\r
305 ps5.setString(1, idrefStr);
\r
306 ps5.setDouble(2, Double.parseDouble(latStr));
\r
307 ps5.setDouble(3, Double.parseDouble(lonStr));
\r
308 ps5.setString(4, nameStr);
\r
309 ps5.executeUpdate();
\r
315 * ソースファイルを読み取ってローカルベータベースへ記録する
\r
318 * @throws FileNotFoundException
\r
319 * @throws ClassNotFoundException
\r
320 * @throws SQLException
\r
321 * @throws IOException
\r
322 * @throws ParserConfigurationException
\r
323 * @throws SAXException
\r
325 public static void inputFile (Connection con, File iFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
\r
327 String timeStampStr = null;
\r
329 String iStr = iFile.getName();
\r
330 File dir = new File(iStr.substring(0, iStr.length() - 4));
\r
333 DocumentBuilderFactory factory;
\r
334 DocumentBuilder builder;
\r
338 factory = DocumentBuilderFactory.newInstance();
\r
339 builder = factory.newDocumentBuilder();
\r
340 factory.setIgnoringElementContentWhitespace(true);
\r
341 factory.setIgnoringComments(true);
\r
342 factory.setValidating(true);
\r
343 root = builder.parse(iStr);
\r
345 iCounter += showNodes(con, root, iStr.substring(0, iStr.length() - 4), timeStampStr);
\r
346 System.out.println("バス停数["+ iCounter +"]");
\r
349 public static void clearDb(Connection con) throws SQLException {
\r
350 Statement stmt = con.createStatement();
\r
351 long count = stmt.executeUpdate("delete from bus_stop");
\r
352 System.out.println("'Database.bus_stop'から "+ count +" 件のデータを削除しました。");
\r
354 count = stmt.executeUpdate("delete from existing_data");
\r
355 System.out.println("'Database.existing_data'から "+ count +" 件のデータを削除しました。");
\r
357 count = stmt.executeUpdate("delete from bus_course");
\r
358 System.out.println("'Database.bus_course'から "+ count +" 件のデータを削除しました。");
\r
360 count = stmt.executeUpdate("delete from bus_ref");
\r
361 System.out.println("'Database.bus_ref'から "+ count +" 件のデータを削除しました。");
\r
365 public static void initDb(Connection con) throws SQLException {
\r
366 // 'table.BUS_STOP'を新規に作る
\r
367 DbBusstop.create(con);
\r
372 * ローカルデータベース内の情報を出力する
\r
375 * @throws IOException
\r
376 * @throws SQLException
\r
377 * @throws TransformerException
\r
378 * @throws ParserConfigurationException
\r
379 * @throws SAXException
\r
381 public static void outputDb(Connection con, String iCode) throws IOException, SQLException, SAXException, ParserConfigurationException, TransformerException {
\r
382 SimpleDateFormat timeStampFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
\r
383 String timeStampStr = timeStampFmt.format(new Date(Calendar.getInstance().getTimeInMillis()));
\r
384 File dir = new File(iCode);
\r
387 BufferedWriter ow = null;
\r
388 BufferedWriter gw = null;
\r
389 BufferedWriter hw = null;
\r
390 BufferedWriter ww = null;
\r
393 File htmlFile = new File(iCode +".html");
\r
394 hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
\r
395 hw.write("<!DOCTYPE html>");
\r
397 hw.write("<html><head><meta charset=\"utf-8\" /></head>");
\r
399 hw.write("<body><table border='1'>");
\r
402 hw.write("<td>type</td>");
\r
403 hw.write("<td>corp</td>");
\r
404 hw.write("<td>course</td>");
\r
405 hw.write("<td>GPX</td>");
\r
406 hw.write("<td>SAMPLE</td>");
\r
407 hw.write("<td>バス停数</td>");
\r
408 hw.write("<td>未入力</td>");
\r
409 hw.write("<td>既存</td>");
\r
414 File wikiFile = new File(iCode +".wiki.txt");
\r
415 ww = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wikiFile), "UTF-8"));
\r
416 ww.write("= バス路線毎のマッピング状況 =");
\r
420 String maeCorp = "";
\r
421 boolean firstCorp = true;
\r
423 PreparedStatement ps7 = con.prepareStatement("SELECT code,type,corp,course,ifile FROM bus_course WHERE ifile=? ORDER BY type,corp,course");
\r
424 PreparedStatement ps9 = con.prepareStatement("SELECT idref FROM bus_ref WHERE code=?");
\r
425 PreparedStatement ps8 = con.prepareStatement("SELECT name,lat,lon,fixed FROM bus_stop WHERE idref=?");
\r
426 ps7.setString(1, iCode);
\r
427 ResultSet rset7 = ps7.executeQuery();
\r
428 while (rset7.next()) {
\r
429 int code = rset7.getInt(1);
\r
430 int type = rset7.getInt(2);
\r
431 String corp = rset7.getString(3);
\r
432 String course = rset7.getString(4);
\r
435 File osmFile = new File(dir, iCode + String.format("_%1$08d", code) +".osm");
\r
436 File gpxFile = new File(dir, iCode + String.format("_%1$08d", code) +".gpx");
\r
437 File osmSample = new File(dir, iCode + String.format("_s%1$08d", code) +".osm");
\r
439 System.out.println("course = "+ course);
\r
441 int fixedCount = 0;
\r
442 int unfixedCount = 0;
\r
444 // index file header
\r
446 hw.write("<td>"+ type +"</td>");
\r
447 hw.write("<td>"+ corp +"</td>");
\r
448 hw.write("<td><a href='"+ dir.getName() +"/"+ osmFile.getName() +"'>"+ course +"</a></td>");
\r
449 hw.write("<td><a href='"+ dir.getName() +"/"+ gpxFile.getName() +"'>"+ gpxFile.getName() +"</a></td>");
\r
450 hw.write("<td><a href='"+ dir.getName() +"/"+ osmSample.getName() +"'>"+ osmSample.getName() +"</a></td>");
\r
453 //--------------------------------------------
\r
456 if (!maeCorp.equals(corp)) {
\r
457 if (firstCorp == false) {
\r
464 ww.write("=== "+ corp +" ===");
\r
468 ww.write(":{{JA:Tag|network||"+ corp +"}}");
\r
470 ww.write(":{{JA:Tag|operator||"+ corp +"}}");
\r
474 ww.write("{| class=\"wikitable sortable\" style=\"table-layout: fixed; width: 100%\"");
\r
476 ww.write("!style=\"width: 100px\"| ref");
\r
478 ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(マスタ)");
\r
480 ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(往路)");
\r
482 ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(復路)");
\r
484 ww.write("!class=\"unsortable\"| 備考");
\r
487 maeCorp = new String(corp);
\r
493 ww.write("| "+ course +" "); // ref
\r
494 ww.write("|| {{State Route|r=0}} {{relation|0|tools=no}} "); // 編集状況 (マスタ)
\r
495 ww.write("|| {{State Route|r=0|h=0}} {{relation|0|tools=no}} "); // 編集状況 (往路)
\r
496 ww.write("|| {{State Route|r=0|h=0}} {{relation|0|tools=no}} "); // 編集状況 (復路)
\r
497 ww.write("|| "); // 備考
\r
502 ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmFile), "UTF-8"));
\r
503 ow.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
\r
505 ow.write("<osm version=\"0.6\" generator=\"ConvBusstop\">");
\r
509 gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8"));
\r
510 gw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
\r
512 gw.write("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" version=\"1.1\" creator=\"osmtracker-android\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd \">");
\r
515 double maxLat = -180.0;
\r
516 double minLat = 180.0;
\r
517 double maxLon = -180.0;
\r
518 double minLon = 180.0;
\r
520 ps9.setInt(1, code);
\r
521 ResultSet rset9 = ps9.executeQuery();
\r
522 while (rset9.next()) {
\r
523 String idref = rset9.getString(1);
\r
525 ps8.setString(1, idref);
\r
526 ResultSet rset8 = ps8.executeQuery();
\r
527 if (rset8.next()) {
\r
529 String name = rset8.getString(1);
\r
530 Double lat = rset8.getDouble(2);
\r
531 Double lon = rset8.getDouble(3);
\r
532 int fixed = rset8.getInt(4);
\r
534 if (lat > maxLat) {
\r
537 if (lon > maxLon) {
\r
540 if (lat < minLat) {
\r
543 if (lon < minLon) {
\r
547 System.out.println("\tway point = "+ idref +", lat="+ lat +", lon="+ lon +", name="+ name);
\r
548 fixedCount += fixed;
\r
553 int nodeid = Integer.parseInt(idref.substring(1)) * -1;
\r
554 String osm_node = nodeBusstop(nodeid, name, lat, lon, timeStampStr);
\r
555 ow.write(osm_node);
\r
559 //File txtFile = new File(dir, iCode + idref +".txt");
\r
560 //BufferedWriter gw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFile), "UTF-8"));
\r
561 //gw2.write(osm_node);
\r
566 gw.write("<wpt lat=\""+ lat +"\" lon=\""+ lon +"\">\n");
\r
567 gw.write(" <time>"+ timeStampStr +"Z</time>\n");
\r
568 gw.write(" <name><![CDATA["+ name +"]]></name>\n");
\r
569 //gw.write(" <link href=\""+ txtFile.getName() +"\"><text>"+ idref +"</text></link>\n");
\r
570 gw.write("</wpt>\n");
\r
579 hw.write("<td>"+ stopCount +"</td>");
\r
580 hw.write("<td>"+ unfixedCount +"</td>");
\r
581 hw.write("<td>"+ fixedCount +"</td>");
\r
586 BufferedWriter ow2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmSample), "UTF-8"));
\r
587 ow2.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
\r
589 ow2.write("<osm version=\"0.5\" generator=\"ConvBusstop\">");
\r
592 ow2.write(nodeBusstop(code, "SAMPLE", ((maxLat+minLat)/2), ((maxLon+minLon)/2), timeStampStr));
\r
594 ow2.write("</osm>");
\r
599 ow.write("</osm>");
\r
604 gw.write("</gpx>");
\r
613 // index file footer
\r
614 hw.write("</table></body></html>");
\r
619 public static int outputDb(Connection con, File dir) throws IOException, SQLException, SAXException, ParserConfigurationException, TransformerException {
\r
620 File osmFile = new File(dir, "busstop.osm");
\r
623 SimpleDateFormat timeStampFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
\r
624 String timeStampStr = timeStampFmt.format(new Date(Calendar.getInstance().getTimeInMillis()));
\r
625 BufferedWriter ow = null;
\r
628 ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmFile), "UTF-8"));
\r
629 ow.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
\r
631 ow.write("<osm version=\"0.6\" generator=\"ConvBusstop\">");
\r
634 PreparedStatement ps8 = con.prepareStatement("SELECT idref,name,lat,lon,fixed FROM bus_stop");
\r
635 ResultSet rset8 = ps8.executeQuery();
\r
636 while (rset8.next()) {
\r
637 String idref = rset8.getString("idref");
\r
638 String name = rset8.getString("name");
\r
639 Double lat = rset8.getDouble("lat");
\r
640 Double lon = rset8.getDouble("lon");
\r
641 int fixed = rset8.getInt("fixed");
\r
643 if (ConvBusstop.nocheck || (fixed < 1)) {
\r
646 int nodeid = Integer.parseInt(idref.substring(1)) * -1;
\r
647 String osm_node = nodeBusstop(nodeid, name, lat, lon, timeStampStr);
\r
648 System.out.println(osm_node);
\r
649 ow.write(osm_node);
\r
656 ow.write("</osm>");
\r
662 public static Element osmTemplateNode = null;
\r
664 public static String nodeBusstop(int code, String name, Double lat, Double lon, String timeStampStr) throws SAXException, IOException, ParserConfigurationException, TransformerException {
\r
665 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
\r
666 factory.setIgnoringElementContentWhitespace(true);
\r
667 factory.setIgnoringComments(true);
\r
668 factory.setValidating(true);
\r
670 if (osmTemplateNode == null) {
\r
671 Document root = factory.newDocumentBuilder().parse(new File("osmTag.xml"));
\r
672 osmTemplateNode = root.getDocumentElement();
\r
675 Document document = factory.newDocumentBuilder().newDocument(); // Documentの生成
\r
676 Element node = document.createElement("node");
\r
677 document.appendChild(node);
\r
679 node.setAttribute("id", String.valueOf(code));
\r
680 node.setAttribute("timestamp", timeStampStr + "Z");
\r
681 node.setAttribute("lat", String.valueOf(lat));
\r
682 node.setAttribute("lon", String.valueOf(lon));
\r
685 NodeList nodes = osmTemplateNode.getChildNodes();
\r
686 for (int i = 0; i < nodes.getLength(); i++) {
\r
687 Node node2 = nodes.item(i);
\r
689 if (node2.getNodeName().equals("tag")) {
\r
690 NamedNodeMap nodeMap2 = node2.getAttributes();
\r
691 if (null != nodeMap2) {
\r
693 String value = null;
\r
694 for (int j=0; j < nodeMap2.getLength(); j++) {
\r
695 if (nodeMap2.item(j).getNodeName().equals("k")) {
\r
696 key = nodeMap2.item(j).getNodeValue();
\r
698 else if (nodeMap2.item(j).getNodeName().equals("v")) {
\r
699 value = nodeMap2.item(j).getNodeValue();
\r
702 if ((key != null) && (value != null)) {
\r
703 Element tag = document.createElement("tag");
\r
704 tag.setAttribute("k", key);
\r
705 tag.setAttribute("v", value);
\r
706 node.appendChild(tag);
\r
710 else if (node2.getNodeName().equals("ksj")) {
\r
711 NamedNodeMap nodeMap2 = node2.getAttributes();
\r
712 if (null != nodeMap2) {
\r
713 for (int j=0; j < nodeMap2.getLength(); j++) {
\r
714 if (nodeMap2.item(j).getNodeName().equals("busStopName")) {
\r
715 Element tagName = document.createElement("tag");
\r
716 tagName.setAttribute("k", nodeMap2.item(j).getNodeValue());
\r
717 tagName.setAttribute("v", name);
\r
718 node.appendChild(tagName);
\r
720 else if (nodeMap2.item(j).getNodeName().equals("busOperationCompany")) {
\r
722 else if (nodeMap2.item(j).getNodeName().equals("busLineName")) {
\r
729 StringWriter sw = new StringWriter();
\r
730 TransformerFactory tfactory = TransformerFactory.newInstance();
\r
731 Transformer transformer = tfactory.newTransformer();
\r
732 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
\r
733 transformer.transform(new DOMSource(node), new StreamResult(sw));
\r
734 return sw.toString();
\r
741 * @param iFileName // ソースファイル名(拡張子を含まない)
\r
742 * @param timeStampStr
\r
744 * @throws IOException
\r
745 * @throws SQLException
\r
747 public static int showNodes(Connection con, Node node, String iFileName, String timeStampStr) throws IOException, SQLException {
\r
749 NamedNodeMap nodeMap = node.getAttributes();
\r
750 if ( null != nodeMap ) {
\r
751 for ( int j=0; j < nodeMap.getLength(); j++ ) {
\r
752 if (nodeMap.item(j).getNodeName().equals("timeStamp")) {
\r
753 timeStampStr = nodeMap.item(j).getNodeValue();
\r
758 NodeList nodes = node.getChildNodes();
\r
759 for (int i=0; i<nodes.getLength(); i++) {
\r
760 Node node2 = nodes.item(i);
\r
761 if (node2.getNodeName().equals("jps:GM_Point")) {
\r
762 showGmPoint(con, node2);
\r
764 else if (node2.getNodeName().equals("gml:Point")) {
\r
765 showGmlPoint(con, node2);
\r
768 else if (node2.getNodeName().equals("ksj:ED01")) {
\r
770 showED01(con, node2, iFileName);
\r
772 else if (node2.getNodeName().equals("ksj:BusStop")) {
\r
774 showBusStop(con, node2, iFileName);
\r
778 iCounter += showNodes(con, node2, iFileName, timeStampStr);
\r
788 * @param iFileName // ソースファイル名(拡張子を含まない)
\r
789 * @throws IOException
\r
790 * @throws SQLException
\r
792 public static void showED01(Connection con, Node node, String iFileName) throws IOException, SQLException {
\r
793 String idrefStr = "";
\r
794 String nameStr = "";
\r
795 PreparedStatement ps1 = con.prepareStatement("SELECT idref FROM bus_stop WHERE idref=?");
\r
796 PreparedStatement ps2 = con.prepareStatement("INSERT INTO bus_stop (idref,name,ifile) VALUES (?,?,?)");
\r
797 PreparedStatement ps3 = con.prepareStatement("SELECT code FROM bus_course WHERE course=? AND corp=? AND ifile=?");
\r
798 PreparedStatement ps4 = con.prepareStatement("INSERT INTO bus_course (code,type,corp,course,ifile) VALUES (?,?,?,?,?)");
\r
799 PreparedStatement ps5 = con.prepareStatement("INSERT INTO bus_ref (idref,code) VALUES (?,?)");
\r
800 PreparedStatement ps6 = con.prepareStatement("SELECT max(code) FROM bus_course WHERE ifile=?");
\r
802 ArrayList<String[]> bris = new ArrayList<String[]>();
\r
803 NodeList nodes = node.getChildNodes();
\r
804 for (int i=0; i < nodes.getLength(); i++) {
\r
805 Node node2 = nodes.item(i);
\r
806 if (node2.getNodeName().equals("ksj:POS")) {
\r
807 NamedNodeMap nodeMap = node2.getAttributes();
\r
808 if (null != nodeMap) {
\r
809 for ( int j=0; j < nodeMap.getLength(); j++ ) {
\r
810 if (nodeMap.item(j).getNodeName().equals("idref")) {
\r
811 idrefStr = nodeMap.item(j).getNodeValue();
\r
812 System.out.println("found idref='"+ idrefStr +"'");
\r
818 else if (node2.getNodeName().equals("ksj:BSN")) {
\r
819 nameStr = node2.getTextContent();
\r
821 else if (node2.getNodeName().equals("ksj:BRI")) {
\r
822 String[] rtn = anaComm(node2);
\r
829 // idref と nameStr をデータベースに格納する
\r
830 boolean insert = true;
\r
831 ps1.setString(1, idrefStr);
\r
832 ResultSet rset = ps1.executeQuery();
\r
839 ps2.setString(1, idrefStr);
\r
840 ps2.setString(2, nameStr);
\r
841 ps2.setString(3, iFileName);
\r
842 System.out.println("INSERT INTO bus_stop (idref,name,ifile) VALUES ('"+ idrefStr +"','"+ nameStr +"','"+ iFileName +"')");
\r
843 ps2.executeUpdate();
\r
846 for (String[] rtn : bris) {
\r
848 ps3.setString(1, rtn[1]);
\r
849 ps3.setString(2, rtn[2]);
\r
850 ps3.setString(3, iFileName);
\r
851 rset = ps3.executeQuery();
\r
853 code = rset.getInt(1);
\r
858 ps6.setString(1, iFileName);
\r
859 ResultSet rset6 = ps6.executeQuery();
\r
860 if (rset6.next()) {
\r
861 code = rset6.getInt(1);
\r
866 System.out.println("code="+code);
\r
867 ps4.setInt(1, code);
\r
868 ps4.setInt(2, Integer.parseInt(rtn[0]));
\r
869 ps4.setString(3, rtn[2]);
\r
870 ps4.setString(4, rtn[1]);
\r
871 ps4.setString(5, iFileName);
\r
872 ps4.executeUpdate();
\r
875 ps5.setString(1, idrefStr);
\r
876 ps5.setInt(2, code);
\r
877 ps5.executeUpdate();
\r
888 * <ksj:BusStop gml:id="ED01_1">
\r
889 * <ksj:position xlink:href="#n1"/>
\r
890 * <ksj:busStopName>城堀</ksj:busStopName>
\r
891 * <ksj:busRouteInformation>
\r
892 * <ksj:BusRouteInformation>
\r
893 * <ksj:busType>1</ksj:busType>
\r
894 * <ksj:busOperationCompany>箱根登山バス</ksj:busOperationCompany>
\r
895 * <ksj:busLineName>小01</ksj:busLineName>
\r
896 * </ksj:BusRouteInformation>
\r
897 * </ksj:busRouteInformation>
\r
898 * <ksj:busRouteInformation>
\r
899 * <ksj:BusRouteInformation>
\r
900 * <ksj:busType>1</ksj:busType>
\r
901 * <ksj:busOperationCompany>箱根登山バス</ksj:busOperationCompany>
\r
902 * <ksj:busLineName>湯07</ksj:busLineName>
\r
903 * </ksj:BusRouteInformation>
\r
904 * </ksj:busRouteInformation>
\r
905 * <ksj:busRouteInformation>
\r
906 * <ksj:BusRouteInformation>
\r
907 * <ksj:busType>1</ksj:busType>
\r
908 * <ksj:busOperationCompany>箱根登山バス</ksj:busOperationCompany>
\r
909 * <ksj:busLineName>湯11</ksj:busLineName>
\r
910 * </ksj:BusRouteInformation>
\r
911 * </ksj:busRouteInformation>
\r
916 * @param iFileName // ソースファイル名(拡張子を含まない)
\r
917 * @throws IOException
\r
918 * @throws SQLException
\r
920 public static void showBusStop(Connection con, Node node, String iFileName) throws IOException, SQLException {
\r
921 String idrefStr = "";
\r
922 String nameStr = "";
\r
923 PreparedStatement ps2 = con.prepareStatement("UPDATE bus_stop SET name=?,ifile=? WHERE idref=?");
\r
924 PreparedStatement ps3 = con.prepareStatement("SELECT code FROM bus_course WHERE course=? AND corp=? AND ifile=?");
\r
925 PreparedStatement ps4 = con.prepareStatement("INSERT INTO bus_course (code,type,corp,course,ifile) VALUES (?,?,?,?,?)");
\r
926 PreparedStatement ps5 = con.prepareStatement("INSERT INTO bus_ref (idref,code) VALUES (?,?)");
\r
927 PreparedStatement ps6 = con.prepareStatement("SELECT max(code) FROM bus_course WHERE ifile=?");
\r
929 ArrayList<String[]> bris = new ArrayList<String[]>();
\r
930 NodeList nodes = node.getChildNodes();
\r
931 for (int i=0; i < nodes.getLength(); i++) {
\r
932 Node node2 = nodes.item(i);
\r
933 if (node2.getNodeName().equals("ksj:position")) {
\r
934 NamedNodeMap nodeMap = node2.getAttributes();
\r
935 if (null != nodeMap) {
\r
936 for ( int j=0; j < nodeMap.getLength(); j++ ) {
\r
937 if (nodeMap.item(j).getNodeName().equals("xlink:href")) {
\r
938 idrefStr = nodeMap.item(j).getNodeValue();
\r
939 idrefStr = idrefStr.substring(1);
\r
940 System.out.println("found idref='"+ idrefStr +"'");
\r
946 else if (node2.getNodeName().equals("ksj:busStopName")) {
\r
947 nameStr = node2.getTextContent();
\r
949 else if (node2.getNodeName().equals("ksj:busRouteInformation")) {
\r
950 String[] rtn = anaCommJGD(node2);
\r
957 // idref と nameStr をデータベースに格納する
\r
958 ps2.setString(1, nameStr);
\r
959 ps2.setString(2, iFileName);
\r
960 ps2.setString(3, idrefStr);
\r
961 ps2.executeUpdate();
\r
963 for (String[] rtn : bris) {
\r
965 ps3.setString(1, rtn[1]);
\r
966 ps3.setString(2, rtn[2]);
\r
967 ps3.setString(3, iFileName);
\r
968 ResultSet rset = ps3.executeQuery();
\r
970 code = rset.getInt(1);
\r
975 ps6.setString(1, iFileName);
\r
976 ResultSet rset6 = ps6.executeQuery();
\r
977 if (rset6.next()) {
\r
978 code = rset6.getInt(1);
\r
983 System.out.println("course="+ code +" : "+ rtn[0] +" : "+ rtn[1] +" : "+ rtn[2] );
\r
984 ps4.setInt(1, code);
\r
985 ps4.setInt(2, Integer.parseInt(rtn[0]));
\r
986 ps4.setString(3, rtn[2]);
\r
987 ps4.setString(4, rtn[1]);
\r
988 ps4.setString(5, iFileName);
\r
989 ps4.executeUpdate();
\r
992 ps5.setString(1, idrefStr);
\r
993 ps5.setInt(2, code);
\r
994 ps5.executeUpdate();
\r
1003 public static String[] anaComm(Node briNode) {
\r
1004 String[] rtn = new String[3];
\r
1005 rtn[0] = ""; // corp type
\r
1006 rtn[1] = ""; // course name
\r
1007 rtn[2] = ""; // corp name
\r
1009 NodeList nodes = briNode.getChildNodes();
\r
1010 for (int i=0; i < nodes.getLength(); i++) {
\r
1011 Node node2 = nodes.item(i);
\r
1012 if (node2.getNodeName().equals("ksj:BSC")) {
\r
1013 rtn[0] = node2.getTextContent();
\r
1015 else if (node2.getNodeName().equals("ksj:BLN")) {
\r
1016 rtn[1] = node2.getTextContent();
\r
1018 else if (node2.getNodeName().equals("ksj:BOC")) {
\r
1019 rtn[2] = node2.getTextContent();
\r
1027 * <ksj:busRouteInformation>
\r
1028 * <ksj:BusRouteInformation>
\r
1029 * <ksj:busType>1</ksj:busType>
\r
1030 * <ksj:busOperationCompany>箱根登山バス</ksj:busOperationCompany>
\r
1031 * <ksj:busLineName>小01</ksj:busLineName>
\r
1032 * </ksj:BusRouteInformation>
\r
1033 * </ksj:busRouteInformation>
\r
1038 public static String[] anaCommJGD(Node briNode) {
\r
1039 String[] rtn = new String[3];
\r
1042 NodeList nodes2 = briNode.getChildNodes();
\r
1043 for (int i=0; i < nodes2.getLength(); i++) {
\r
1044 Node node2 = nodes2.item(i);
\r
1045 if (node2.getNodeName().equals("ksj:BusRouteInformation")) {
\r
1046 NodeList nodes3 = node2.getChildNodes();
\r
1047 for (int j=0; j < nodes3.getLength(); j++) {
\r
1048 Node node3 = nodes3.item(j);
\r
1049 if (node3.getNodeName().equals("ksj:busType")) {
\r
1050 rtn[0] = new String(node3.getTextContent());
\r
1053 else if (node3.getNodeName().equals("ksj:busLineName")) {
\r
1054 rtn[1] = new String(node3.getTextContent());
\r
1057 else if (node3.getNodeName().equals("ksj:busOperationCompany")) {
\r
1058 rtn[2] = new String(node3.getTextContent());
\r
1071 public static void showGmPoint(Connection con, Node node) throws IOException, SQLException {
\r
1072 String positionStr = "";
\r
1073 String latStr = "";
\r
1074 String lonStr = "";
\r
1075 String idStr = "";
\r
1077 NamedNodeMap nodeMap = node.getAttributes();
\r
1078 if ( null != nodeMap ) {
\r
1079 for ( int j=0; j<nodeMap.getLength(); j++ ) {
\r
1080 if (nodeMap.item(j).getNodeName().equals("id")) {
\r
1081 idStr = nodeMap.item(j).getNodeValue();
\r
1086 NodeList nodes = node.getChildNodes();
\r
1087 for (int i=0; i < nodes.getLength(); i++) {
\r
1088 Node node2 = nodes.item(i);
\r
1089 if (node2.getNodeName().equals("jps:GM_Point.position")) {
\r
1090 NodeList nodes3 = node2.getChildNodes();
\r
1091 for (int j=0; j < nodes3.getLength(); j++) {
\r
1092 Node node3 = nodes3.item(j);
\r
1093 if (node3.getNodeName().equals("jps:DirectPosition")) {
\r
1094 NodeList nodes4 = node3.getChildNodes();
\r
1095 for (int k=0; k < nodes4.getLength(); k++) {
\r
1096 Node node4 = nodes4.item(k);
\r
1097 if (node4.getNodeName().equals("DirectPosition.coordinate")) {
\r
1098 positionStr = node4.getTextContent();
\r
1099 String[] str4Ary = positionStr.split(" ");
\r
1100 latStr = str4Ary[0];
\r
1101 lonStr = str4Ary[1];
\r
1109 PreparedStatement ps6 = con.prepareStatement("UPDATE bus_stop SET lat=?,lon=?,fixed=? WHERE idref=?");
\r
1110 double lat = Double.parseDouble(latStr);
\r
1111 double lon = Double.parseDouble(lonStr);
\r
1112 ps6.setDouble(1, lat);
\r
1113 ps6.setDouble(2, lon);
\r
1114 // ps6.setInt(3, (ConvBusstop.nocheck ? 0 : HttpGET.getMap(lat, lon, NEER)));
\r
1116 ps6.setString(4, idStr);
\r
1117 System.out.println("UPDATE bus_stop("+ idStr +") lat="+ lat +", lon="+ lon +", fixed=0");
\r
1118 ps6.executeUpdate();
\r
1125 * <gml:Point gml:id="n1">
\r
1126 * <gml:pos>35.14591397 139.10569573</gml:pos>
\r
1131 * @throws IOException
\r
1132 * @throws SQLException
\r
1134 public static void showGmlPoint(Connection con, Node node) throws IOException, SQLException {
\r
1135 String positionStr = "";
\r
1136 String latStr = "";
\r
1137 String lonStr = "";
\r
1138 String idStr = "";
\r
1140 NamedNodeMap nodeMap = node.getAttributes();
\r
1141 if ( null != nodeMap ) {
\r
1142 for ( int j=0; j<nodeMap.getLength(); j++ ) {
\r
1143 if (nodeMap.item(j).getNodeName().equals("gml:id")) {
\r
1144 idStr = nodeMap.item(j).getNodeValue();
\r
1149 NodeList nodes = node.getChildNodes();
\r
1150 for (int i=0; i < nodes.getLength(); i++) {
\r
1151 Node node2 = nodes.item(i);
\r
1152 if (node2.getNodeName().equals("gml:pos")) {
\r
1153 positionStr = node2.getTextContent().trim();
\r
1154 String[] str4Ary = positionStr.split(" ");
\r
1155 latStr = str4Ary[0];
\r
1156 lonStr = str4Ary[1];
\r
1158 PreparedStatement ps6 = con.prepareStatement("INSERT INTO bus_stop (lat,lon,fixed,idref) VALUES (?,?,?,?)");
\r
1159 double lat = Double.parseDouble(latStr);
\r
1160 double lon = Double.parseDouble(lonStr);
\r
1161 System.out.println("INSERT INTO bus_stop (lat,lon,fixed,idref) VALUES ('"+ latStr +"','"+ lonStr +"','0','"+ idStr +"')");
\r
1163 ps6.setDouble(1, lat);
\r
1164 ps6.setDouble(2, lon);
\r
1165 //ps6.setInt(3, (ConvBusstop.nocheck ? 0 : HttpGET.getMap(lat, lon, NEER)));
\r
1167 ps6.setString(4, idStr);
\r
1168 ps6.executeUpdate();
\r
1174 static boolean checkFile(File f) {
\r
1175 String name = f.getName();
\r
1176 if (!name.startsWith("P11-")) {
\r
1179 if (!name.toUpperCase().endsWith(".XML")) {
\r