2 import hayashi.yuu.tools.logger.LoggerFactory;
\r
4 import javax.xml.parsers.*;
\r
5 import javax.xml.transform.TransformerException;
\r
7 import org.w3c.dom.*;
\r
8 import org.xml.sax.*;
\r
11 import java.sql.Connection;
\r
12 import java.sql.PreparedStatement;
\r
13 import java.sql.ResultSet;
\r
14 import java.sql.SQLException;
\r
15 import java.sql.Statement;
\r
16 import java.util.logging.Logger;
\r
18 import jp.co.areaweb.tools.database.*;
\r
20 public class ConvBusroute {
\r
25 public static final boolean DB_INIT = false;
\r
27 // 近くのバス停を探す範囲(バス停を中心としたNEER×2m四方の領域
\r
28 static final int NEER = 75;
\r
29 static boolean nocheck = true;
\r
30 static Logger logger = LoggerFactory.getInstance();
\r
35 * java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusroute
\r
37 * @throws IOException
\r
38 * @throws SQLException
\r
39 * @throws ClassNotFoundException
\r
40 * @throws FileNotFoundException
\r
41 * @throws TransformerException
\r
42 * @throws SAXException
\r
43 * @throws ParserConfigurationException */
\r
44 public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException, TransformerException
\r
47 * アプリケーション [ConvBusroute]
\r
48 * > java -jar ConvBusstop.jar <オプション>
\r
49 * オプション: -exp 実行する直前にデータベースを初期化する(省略可能)
\r
51 File dbdir = new File("database");
\r
52 if (!dbdir.isDirectory()) {
\r
56 Connection con = DatabaseTool.openDb("database");
\r
57 ConvBusroute.initDb(con);
\r
64 File dir = new File(".");
\r
65 File[] files = dir.listFiles();
\r
66 for (File iFile : files) {
\r
67 if (checkFile(iFile)) {
\r
69 ConvBusroute.clearDb(con);
\r
70 inputFile(con, iFile);
\r
72 // ローカルデータベース内の情報を出力する
\r
73 String iStr = iFile.getName();
\r
74 outputDb(con, iStr.substring(0, iStr.length() - 4));
\r
77 logger.info("["+ fcounter +"]つのファイルをインポートしました。");
\r
80 DatabaseTool.closeDb(con);
\r
85 * ソースファイルを読み取ってローカルベータベースへ記録する
\r
88 * @throws FileNotFoundException
\r
89 * @throws ClassNotFoundException
\r
90 * @throws SQLException
\r
91 * @throws IOException
\r
92 * @throws ParserConfigurationException
\r
93 * @throws SAXException
\r
95 public static void inputFile (Connection con, File iFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException {
\r
98 String iStr = iFile.getName();
\r
99 File dir = new File(iStr.substring(0, iStr.length() - 4));
\r
102 DocumentBuilderFactory factory;
\r
103 DocumentBuilder builder;
\r
107 factory = DocumentBuilderFactory.newInstance();
\r
108 builder = factory.newDocumentBuilder();
\r
109 factory.setIgnoringElementContentWhitespace(true);
\r
110 factory.setIgnoringComments(true);
\r
111 factory.setValidating(true);
\r
112 root = builder.parse(iStr);
\r
114 iCounter += importNodes(con, root, iStr.substring(0, iStr.length() - 4));
\r
115 logger.info("バスルート数["+ iCounter +"]");
\r
118 public static void clearDb(Connection con) throws SQLException {
\r
119 Statement stmt = con.createStatement();
\r
120 long count = stmt.executeUpdate("delete from bus_route");
\r
121 logger.info("'bus_route'から "+ count +" 件のデータを削除しました。");
\r
123 count = stmt.executeUpdate("delete from bus_Curve");
\r
124 logger.info("'bus_Curve'から "+ count +" 件のデータを削除しました。");
\r
129 public static void initDb(Connection con) throws SQLException {
\r
131 PreparedStatement ps;
\r
133 // 'table.BUS_STOP'を新規に作る
\r
134 DbBusstop.create(con);
\r
136 // 'table.bus_route'を新規に作る
\r
137 createSt = "CREATE TABLE bus_route (cvid VARCHAR(12), bsc int, boc VARCHAR(128) NOT NULL, bln VARCHAR(512));";
\r
138 logger.info(createSt);
\r
139 ps = con.prepareStatement(createSt);
\r
141 ps.executeUpdate();
\r
143 catch (SQLException e) {
\r
144 if (!(e.toString().startsWith("Table already exists:"))) {
\r
152 // 'table.bus_Curve'を新規に作る
\r
153 createSt = "CREATE TABLE bus_Curve (idref VARCHAR(12), seq INT, lat DOUBLE, lon DOUBLE, CONSTRAINT bus_curve_pk PRIMARY KEY(idref,seq));";
\r
154 logger.info(createSt);
\r
155 ps = con.prepareStatement(createSt);
\r
157 ps.executeUpdate();
\r
159 catch (SQLException e) {
\r
160 if (!(e.toString().startsWith("Table already exists:"))) {
\r
171 * ローカルデータベース内の情報を出力する
\r
174 * @throws IOException
\r
175 * @throws SQLException
\r
177 public static void outputDb(Connection con, String iCode) throws IOException, SQLException {
\r
178 File dir = new File(iCode);
\r
181 BufferedWriter gw = null;
\r
182 BufferedWriter hw = null;
\r
185 File htmlFile = new File(iCode +".html");
\r
186 hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
\r
187 hw.write("<!DOCTYPE html>");
\r
189 hw.write("<html><head><meta charset=\"utf-8\" /></head>");
\r
191 hw.write("<body><table border='1'>");
\r
194 hw.write("<td>boc</td>");
\r
195 hw.write("<td>bln</td>");
\r
196 hw.write("<td>GPX</td>");
\r
201 String maeroute = "";
\r
202 File gpxFile = null;
\r
204 System.out.println("Database request....");
\r
205 PreparedStatement ps7 = con.prepareStatement("SELECT bus_route.bsc, bus_route.boc, bus_route.bln, bus_Curve.idref, bus_Curve.seq, bus_Curve.lat, bus_Curve.lon FROM bus_route INNER JOIN bus_Curve ON bus_route.cvid = bus_Curve.idref ORDER BY bus_route.bsc,bus_route.boc,bus_route.bln, bus_route.cvid,bus_Curve.seq");
\r
206 ResultSet rset7 = ps7.executeQuery();
\r
207 while (rset7.next()) {
\r
208 int bsc = rset7.getInt(1);
\r
209 String boc = rset7.getString(2);
\r
210 String bln = rset7.getString(3);
\r
211 String cvid = rset7.getString(4);
\r
212 //int seq = rset7.getInt(5);
\r
213 Double lat = rset7.getDouble(6);
\r
214 Double lon = rset7.getDouble(7);
\r
216 if (!maeroute.equals(boc +" "+ bln)) {
\r
218 if (!maeroute.equals("")) {
\r
219 if (!maeid.equals(cvid)) {
\r
221 if (!maeid.equals("")) {
\r
222 gw.write("</trkseg>");
\r
229 gw.write("</trk>");
\r
230 gw.write("</gpx>");
\r
233 System.out.println();
\r
235 maeroute = new String(boc +" "+ bln);
\r
237 System.out.print("export course("+ maeroute +") ");
\r
239 gpxFile = new File(dir, iCode + cvid +".gpx");
\r
240 gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8"));
\r
241 gw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
\r
243 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
248 gw.write(" <name>"+ bsc +" "+ boc +" "+ bln +" "+ cvid +"</name>");
\r
253 hw.write("<td>"+ boc +"</td>");
\r
254 hw.write("<td>"+ bln +"</td>");
\r
255 hw.write("<td><a href='"+ dir.getName() +"/"+ gpxFile.getName() +"'>"+ gpxFile.getName() +"</a></td>");
\r
260 if (!maeid.equals(cvid)) {
\r
261 if (!maeid.equals("")) {
\r
263 gw.write("</trkseg>");
\r
266 System.out.println();
\r
269 gw.write("<trkseg>");
\r
272 maeid = new String(cvid);
\r
275 System.out.print(".");
\r
276 gw.write("<trkpt lat=\""+ lat +"\" lon=\""+ lon +"\"></trkpt>");
\r
282 gw.write("</trkseg>");
\r
283 gw.write("</trk>");
\r
284 gw.write("</gpx>");
\r
288 // index file footer
\r
289 hw.write("</table></body></html>");
\r
294 public static BufferedWriter createGPX(File gpxFile, String cvid) throws IOException {
\r
295 BufferedWriter gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8"));
\r
296 gw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
\r
298 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
308 * @param iFileName // ソースファイル名(拡張子を含まない)
\r
310 * @throws IOException
\r
311 * @throws SQLException
\r
313 public static int importNodes(Connection con, Node node, String iFileName) throws IOException, SQLException {
\r
316 NodeList nodes = node.getChildNodes();
\r
317 for (int i=0; i<nodes.getLength(); i++) {
\r
318 Node node2 = nodes.item(i);
\r
319 if (node2.getNodeName().equals("gml:Curve")) {
\r
320 importGmlCurve(con, node2);
\r
322 else if (node2.getNodeName().equals("ksj:BusRoute")) {
\r
324 importBusRoute(con, node2, iFileName);
\r
327 iCounter += importNodes(con, node2, iFileName);
\r
333 public static void importGmlCurve(Connection con, Node node) throws IOException, SQLException {
\r
334 String positionStr = "";
\r
335 String latStr = "";
\r
336 String lonStr = "";
\r
339 NamedNodeMap nodeMap = node.getAttributes();
\r
340 if ( null != nodeMap ) {
\r
341 for ( int j=0; j<nodeMap.getLength(); j++ ) {
\r
342 if (nodeMap.item(j).getNodeName().equals("gml:id")) {
\r
343 idStr = nodeMap.item(j).getNodeValue();
\r
348 NodeList nodes = node.getChildNodes();
\r
349 for (int i=0; i < nodes.getLength(); i++) {
\r
350 Node node2 = nodes.item(i);
\r
351 if (node2.getNodeName().equals("gml:segments")) {
\r
352 NodeList nodes3 = node2.getChildNodes();
\r
353 for (int j=0; j < nodes3.getLength(); j++) {
\r
354 Node node3 = nodes3.item(j);
\r
355 if (node3.getNodeName().equals("gml:LineStringSegment")) {
\r
356 NodeList nodes4 = node3.getChildNodes();
\r
357 for (int k=0; k < nodes4.getLength(); k++) {
\r
358 Node node4 = nodes4.item(k);
\r
359 if (node4.getNodeName().equals("gml:posList")) {
\r
360 BufferedReader bf = new BufferedReader(new StringReader(node4.getTextContent()));
\r
363 while ((line = bf.readLine()) != null) {
\r
365 positionStr = line.trim();
\r
366 if (!positionStr.equals("")) {
\r
367 String[] str4Ary = positionStr.split(" ");
\r
368 latStr = str4Ary[0];
\r
369 lonStr = str4Ary[1];
\r
370 double lat = Double.parseDouble(latStr);
\r
371 double lon = Double.parseDouble(lonStr);
\r
373 System.out.println("import course("+ idStr+","+ seq +","+ lat +","+ lon +")");
\r
375 PreparedStatement ps2 = con.prepareStatement("INSERT INTO bus_Curve (idref,seq,lat,lon) VALUES (?,?,?,?)");
\r
376 ps2.setString(1, idStr);
\r
377 ps2.setInt(2, seq);
\r
378 ps2.setDouble(3, lat);
\r
379 ps2.setDouble(4, lon);
\r
380 ps2.executeUpdate();
\r
392 public static void importBusRoute(Connection con, Node node, String iFileName) throws IOException, SQLException {
\r
398 NodeList nodes = node.getChildNodes();
\r
399 for (int i=0; i < nodes.getLength(); i++) {
\r
400 Node node2 = nodes.item(i);
\r
401 if (node2.getNodeName().equals("ksj:brt")) {
\r
402 NamedNodeMap nodeMap = node2.getAttributes();
\r
403 if (null != nodeMap) {
\r
404 for ( int j=0; j < nodeMap.getLength(); j++ ) {
\r
405 if (nodeMap.item(j).getNodeName().equals("xlink:href")) {
\r
406 cvId = nodeMap.item(j).getNodeValue();
\r
407 cvId = cvId.substring(1);
\r
408 logger.info("found idref='"+ cvId +"'");
\r
414 else if (node2.getNodeName().equals("ksj:bsc")) {
\r
415 String str = node2.getTextContent();
\r
416 bsc = Integer.parseInt(str);
\r
418 else if (node2.getNodeName().equals("ksj:boc")) {
\r
419 boc = node2.getTextContent();
\r
421 else if (node2.getNodeName().equals("ksj:bln")) {
\r
422 bln = node2.getTextContent();
\r
426 PreparedStatement ps2 = con.prepareStatement("INSERT INTO bus_route (cvid,bsc,boc,bln) VALUES (?,?,?,?)");
\r
427 ps2.setString(1, cvId);
\r
428 ps2.setInt(2, bsc);
\r
429 ps2.setString(3, boc);
\r
430 ps2.setString(4, bln);
\r
431 ps2.executeUpdate();
\r
437 * exp: KANAGAWA-ken [N07-11_14.xml]
\r
442 static boolean checkFile(File f) {
\r
443 String name = f.getName();
\r
444 if (!name.toUpperCase().startsWith("N07-")) {
\r
447 if (!name.toLowerCase().endsWith(".xml")) {
\r