From: yuuhayashi Date: Sun, 22 Feb 2015 02:36:07 +0000 (+0900) Subject: RELEASE: 2015-02-21 X-Git-Url: http://git.osdn.net/view?p=convbusstop%2Fconvbusstop.git;a=commitdiff_plain;h=1e3a26b520062eb1ddf2aae15ff6ed7b40422046;hp=9e350fc4e6ee34f544faa6d9afaa7331e4ed8e90 RELEASE: 2015-02-21 --- diff --git a/.gitignore b/.gitignore index 68bdc7d..107128c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,26 @@ -*.class -*.jar -*.zip -bin/ -classes/ -sample/ -P11-10_14_GML/ \ No newline at end of file +*.class +*.jar +*.zip +bin/ +classes/ +sample/ +dest/ + +P*-*_GML/ + +# ============ # +# Eclipse # +# ============ # +.metadata +.settings + +# ============ # +# OS generated # +# ============ # +.DS_Store* +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +[Tt]humbs.db diff --git a/ConvBusstop.sh b/ConvBusstop.sh deleted file mode 100644 index b977c22..0000000 --- a/ConvBusstop.sh +++ /dev/null @@ -1,5 +0,0 @@ -rm -r database -java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusroute - -rm -r database -java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusstop -nocheck diff --git a/build.xml b/build.xml index 9fb4284..f9c6677 100644 --- a/build.xml +++ b/build.xml @@ -1,106 +1,106 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dest/ConvBusstop.bat b/dest/ConvBusstop.bat deleted file mode 100644 index 09a806a..0000000 --- a/dest/ConvBusstop.bat +++ /dev/null @@ -1,5 +0,0 @@ -rem java -jar ConvBusstop.jar - -java -jar ConvBusstop.jar -nocheck - -pause \ No newline at end of file diff --git a/dest/database.properties b/dest/database.properties deleted file mode 100644 index b8aa951..0000000 --- a/dest/database.properties +++ /dev/null @@ -1,4 +0,0 @@ -db_driver=org.hsqldb.jdbcDriver -db_url=jdbc:hsqldb:file:./database/db -db_user=sa -db_passwd= diff --git a/expBusstop.bat b/expBusstop.bat new file mode 100644 index 0000000..e7e1035 --- /dev/null +++ b/expBusstop.bat @@ -0,0 +1,3 @@ +java -jar ReadKIBAN.jar DbBusstop + +pause \ No newline at end of file diff --git a/src/ConvBusstop.bat b/src/ConvBusstop.bat index 9a2c2ee..9af83d8 100644 --- a/src/ConvBusstop.bat +++ b/src/ConvBusstop.bat @@ -1,7 +1,7 @@ -del -f database -java -cp .;ConvBusstop.jar;hayashi_0225.jar;hsqldb_2.2.9.jar osm.jp.ConvBusroute - -del -f database -java -cp .;ConvBusstop.jar;hayashi_0225.jar;hsqldb_2.2.9.jar osm.jp.ConvBusstop -nocheck - +rem del -f database +rem java -cp .;ConvBusstop.jar;hayashi_0225.jar;hsqldb_2.2.9.jar osm.jp.ConvBusroute -nocheck + +del -f database +java -cp .;ConvBusstop.jar;hayashi_0225.jar;hsqldb_2.2.9.jar osm.jp.ConvBusstop -check + pause \ No newline at end of file diff --git a/src/ConvBusstop.sh b/src/ConvBusstop.sh old mode 100755 new mode 100644 index b977c22..f52ae0a --- a/src/ConvBusstop.sh +++ b/src/ConvBusstop.sh @@ -1,5 +1,5 @@ -rm -r database -java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusroute - -rm -r database -java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusstop -nocheck +rm -r database +java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusroute + +rm -r database +java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusstop -nocheck diff --git a/src/database.properties b/src/database.properties index b8aa951..e66e66c 100644 --- a/src/database.properties +++ b/src/database.properties @@ -1,4 +1,4 @@ -db_driver=org.hsqldb.jdbcDriver -db_url=jdbc:hsqldb:file:./database/db -db_user=sa -db_passwd= +db_driver=org.hsqldb.jdbcDriver +db_url=jdbc:hsqldb:file:./database/db +db_user=sa +db_passwd= diff --git a/src/expBusstop.bat b/src/expBusstop.bat index 895a323..e7e1035 100644 --- a/src/expBusstop.bat +++ b/src/expBusstop.bat @@ -1,3 +1,3 @@ -java -jar ReadKIBAN.jar DbBusstop - +java -jar ReadKIBAN.jar DbBusstop + pause \ No newline at end of file diff --git a/src/osm/jp/ConvBusroute.java b/src/osm/jp/ConvBusroute.java index c161d69..cb19ba6 100644 --- a/src/osm/jp/ConvBusroute.java +++ b/src/osm/jp/ConvBusroute.java @@ -1,452 +1,452 @@ -package osm.jp; -import hayashi.yuu.tools.logger.LoggerFactory; - -import javax.xml.parsers.*; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.*; -import org.xml.sax.*; - -import java.io.*; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.logging.Logger; - -import jp.co.areaweb.tools.database.*; - -public class ConvBusroute { - - String filter = ""; - String urlStr = ""; - - public static final boolean DB_INIT = false; - - // 近くのバス停を探す範囲(バス停を中心としたNEER×2m四方の領域 - static final int NEER = 75; - static boolean nocheck = true; - static Logger logger = LoggerFactory.getInstance(); - - /** - * メイン - * - * java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusroute - * - * @throws IOException - * @throws SQLException - * @throws ClassNotFoundException - * @throws FileNotFoundException - * @throws TransformerException - * @throws SAXException - * @throws ParserConfigurationException */ - public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException, TransformerException - { - /** - * アプリケーション [ConvBusroute] - * > java -jar ConvBusstop.jar <オプション> - * オプション: -exp 実行する直前にデータベースを初期化する(省略可能) - */ - File dbdir = new File("database"); - if (!dbdir.isDirectory()) { - dbdir.mkdir(); - } - - Connection con = DatabaseTool.openDb("database"); - ConvBusroute.initDb(con); - - try { - /** - * バスルートデータ変換のメイン処理 - */ - int fcounter = 0; - File dir = new File("."); - File[] files = dir.listFiles(); - for (File iFile : files) { - if (checkFile(iFile)) { - fcounter++; - ConvBusroute.clearDb(con); - inputFile(con, iFile); - - // ローカルデータベース内の情報を出力する - String iStr = iFile.getName(); - outputDb(con, iStr.substring(0, iStr.length() - 4)); - } - } - logger.info("["+ fcounter +"]つのファイルをインポートしました。"); - } - finally { - DatabaseTool.closeDb(con); - } - } - - /** - * ソースファイルを読み取ってローカルベータベースへ記録する - * @param con - * @param iFile - * @throws FileNotFoundException - * @throws ClassNotFoundException - * @throws SQLException - * @throws IOException - * @throws ParserConfigurationException - * @throws SAXException - */ - public static void inputFile (Connection con, File iFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - int iCounter = 0; - - String iStr = iFile.getName(); - File dir = new File(iStr.substring(0, iStr.length() - 4)); - dir.mkdir(); - - DocumentBuilderFactory factory; - DocumentBuilder builder; - Node root; - - iCounter = 0; - factory = DocumentBuilderFactory.newInstance(); - builder = factory.newDocumentBuilder(); - factory.setIgnoringElementContentWhitespace(true); - factory.setIgnoringComments(true); - factory.setValidating(true); - root = builder.parse(iStr); - - iCounter += importNodes(con, root, iStr.substring(0, iStr.length() - 4)); - logger.info("バスルート数["+ iCounter +"]"); - } - - public static void clearDb(Connection con) throws SQLException { - Statement stmt = con.createStatement(); - long count = stmt.executeUpdate("delete from bus_route"); - logger.info("'bus_route'から "+ count +" 件のデータを削除しました。"); - - count = stmt.executeUpdate("delete from bus_Curve"); - logger.info("'bus_Curve'から "+ count +" 件のデータを削除しました。"); - - stmt.close(); - } - - public static void initDb(Connection con) throws SQLException { - String createSt; - PreparedStatement ps; - - // 'table.BUS_STOP'を新規に作る - DbBusstop.create(con); - - // 'table.bus_route'を新規に作る - createSt = "CREATE TABLE bus_route (cvid VARCHAR(12), bsc int, boc VARCHAR(128) NOT NULL, bln VARCHAR(512));"; - logger.info(createSt); - ps = con.prepareStatement(createSt); - try { - ps.executeUpdate(); - } - catch (SQLException e) { - if (!(e.toString().startsWith("Table already exists:"))) { - throw e; - } - } - finally { - ps.close(); - } - - // 'table.bus_Curve'を新規に作る - createSt = "CREATE TABLE bus_Curve (idref VARCHAR(12), seq INT, lat DOUBLE, lon DOUBLE, CONSTRAINT bus_curve_pk PRIMARY KEY(idref,seq));"; - logger.info(createSt); - ps = con.prepareStatement(createSt); - try { - ps.executeUpdate(); - } - catch (SQLException e) { - if (!(e.toString().startsWith("Table already exists:"))) { - throw e; - } - } - finally { - ps.close(); - } - } - - - /** - * ローカルデータベース内の情報を出力する - * @param con - * @param iCode - * @throws IOException - * @throws SQLException - */ - public static void outputDb(Connection con, String iCode) throws IOException, SQLException { - File dir = new File(iCode); - dir.mkdir(); - - BufferedWriter gw = null; - BufferedWriter hw = null; - - // HTML header - File htmlFile = new File(iCode +".html"); - hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8")); - hw.write(""); - hw.newLine(); - hw.write(""); - hw.newLine(); - hw.write(""); - hw.newLine(); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.newLine(); - - String maeid = ""; - String maeroute = ""; - File gpxFile = null; - - System.out.println("Database request...."); - 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"); - ResultSet rset7 = ps7.executeQuery(); - while (rset7.next()) { - int bsc = rset7.getInt(1); - String boc = rset7.getString(2); - String bln = rset7.getString(3); - String cvid = rset7.getString(4); - //int seq = rset7.getInt(5); - Double lat = rset7.getDouble(6); - Double lon = rset7.getDouble(7); - - if (!maeroute.equals(boc +" "+ bln)) { - // GPX file - if (!maeroute.equals("")) { - if (!maeid.equals(cvid)) { - // GPX - if (!maeid.equals("")) { - gw.write(""); - gw.newLine(); - maeid = ""; - } - } - - // GPX file footer - gw.write(""); - gw.write(""); - gw.newLine(); - gw.close(); - System.out.println(); - } - maeroute = new String(boc +" "+ bln); - - System.out.print("export course("+ maeroute +") "); - - gpxFile = new File(dir, iCode + cvid +".gpx"); - gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8")); - gw.write(""); - gw.newLine(); - gw.write(""); - gw.newLine(); - - // GPX - gw.write(""); - gw.write(" "+ bsc +" "+ boc +" "+ bln +" "+ cvid +""); - gw.newLine(); - - // INDEX file - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.newLine(); - } - - if (!maeid.equals(cvid)) { - if (!maeid.equals("")) { - // GPX file footer - gw.write(""); - gw.newLine(); - } - System.out.println(); - - // GPX - gw.write(""); - gw.newLine(); - } - maeid = new String(cvid); - - // GPX - System.out.print("."); - gw.write(""); - gw.newLine(); - } - rset7.close(); - - // GPX file footer - gw.write(""); - gw.write(""); - gw.write(""); - gw.newLine(); - gw.close(); - - // index file footer - hw.write("
bocblnGPX
"+ boc +""+ bln +""+ gpxFile.getName() +"
"); - hw.newLine(); - hw.close(); - } - - public static BufferedWriter createGPX(File gpxFile, String cvid) throws IOException { - BufferedWriter gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8")); - gw.write(""); - gw.newLine(); - gw.write(""); - gw.newLine(); - return gw; - } - - - /** - * - * @param con - * @param node - * @param iFileName // ソースファイル名(拡張子を含まない) - * @return - * @throws IOException - * @throws SQLException - */ - public static int importNodes(Connection con, Node node, String iFileName) throws IOException, SQLException { - int iCounter = 0; - - NodeList nodes = node.getChildNodes(); - for (int i=0; i java -jar ConvBusstop.jar <オプション> + * オプション: -exp 実行する直前にデータベースを初期化する(省略可能) + */ + File dbdir = new File("database"); + if (!dbdir.isDirectory()) { + dbdir.mkdir(); + } + + Connection con = DatabaseTool.openDb("database"); + ConvBusroute.initDb(con); + + try { + /** + * バスルートデータ変換のメイン処理 + */ + int fcounter = 0; + File dir = new File("."); + File[] files = dir.listFiles(); + for (File iFile : files) { + if (checkFile(iFile)) { + fcounter++; + ConvBusroute.clearDb(con); + inputFile(con, iFile); + + // ローカルデータベース内の情報を出力する + String iStr = iFile.getName(); + outputDb(con, iStr.substring(0, iStr.length() - 4)); + } + } + logger.info("["+ fcounter +"]つのファイルをインポートしました。"); + } + finally { + DatabaseTool.closeDb(con); + } + } + + /** + * ソースファイルを読み取ってローカルベータベースへ記録する + * @param con + * @param iFile + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws SQLException + * @throws IOException + * @throws ParserConfigurationException + * @throws SAXException + */ + public static void inputFile (Connection con, File iFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + int iCounter = 0; + + String iStr = iFile.getName(); + File dir = new File(iStr.substring(0, iStr.length() - 4)); + dir.mkdir(); + + DocumentBuilderFactory factory; + DocumentBuilder builder; + Node root; + + iCounter = 0; + factory = DocumentBuilderFactory.newInstance(); + builder = factory.newDocumentBuilder(); + factory.setIgnoringElementContentWhitespace(true); + factory.setIgnoringComments(true); + factory.setValidating(true); + root = builder.parse(iStr); + + iCounter += importNodes(con, root, iStr.substring(0, iStr.length() - 4)); + logger.info("バスルート数["+ iCounter +"]"); + } + + public static void clearDb(Connection con) throws SQLException { + Statement stmt = con.createStatement(); + long count = stmt.executeUpdate("delete from bus_route"); + logger.info("'bus_route'から "+ count +" 件のデータを削除しました。"); + + count = stmt.executeUpdate("delete from bus_Curve"); + logger.info("'bus_Curve'から "+ count +" 件のデータを削除しました。"); + + stmt.close(); + } + + public static void initDb(Connection con) throws SQLException { + String createSt; + PreparedStatement ps; + + // 'table.BUS_STOP'を新規に作る + DbBusstop.create(con); + + // 'table.bus_route'を新規に作る + createSt = "CREATE TABLE bus_route (cvid VARCHAR(12), bsc int, boc VARCHAR(128) NOT NULL, bln VARCHAR(512));"; + logger.info(createSt); + ps = con.prepareStatement(createSt); + try { + ps.executeUpdate(); + } + catch (SQLException e) { + if (!(e.toString().startsWith("Table already exists:"))) { + throw e; + } + } + finally { + ps.close(); + } + + // 'table.bus_Curve'を新規に作る + createSt = "CREATE TABLE bus_Curve (idref VARCHAR(12), seq INT, lat DOUBLE, lon DOUBLE, CONSTRAINT bus_curve_pk PRIMARY KEY(idref,seq));"; + logger.info(createSt); + ps = con.prepareStatement(createSt); + try { + ps.executeUpdate(); + } + catch (SQLException e) { + if (!(e.toString().startsWith("Table already exists:"))) { + throw e; + } + } + finally { + ps.close(); + } + } + + + /** + * ローカルデータベース内の情報を出力する + * @param con + * @param iCode + * @throws IOException + * @throws SQLException + */ + public static void outputDb(Connection con, String iCode) throws IOException, SQLException { + File dir = new File(iCode); + dir.mkdir(); + + BufferedWriter gw = null; + BufferedWriter hw = null; + + // HTML header + File htmlFile = new File(iCode +".html"); + hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8")); + hw.write(""); + hw.newLine(); + hw.write(""); + hw.newLine(); + hw.write(""); + hw.newLine(); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.newLine(); + + String maeid = ""; + String maeroute = ""; + File gpxFile = null; + + System.out.println("Database request...."); + 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"); + ResultSet rset7 = ps7.executeQuery(); + while (rset7.next()) { + int bsc = rset7.getInt(1); + String boc = rset7.getString(2); + String bln = rset7.getString(3); + String cvid = rset7.getString(4); + //int seq = rset7.getInt(5); + Double lat = rset7.getDouble(6); + Double lon = rset7.getDouble(7); + + if (!maeroute.equals(boc +" "+ bln)) { + // GPX file + if (!maeroute.equals("")) { + if (!maeid.equals(cvid)) { + // GPX + if (!maeid.equals("")) { + gw.write(""); + gw.newLine(); + maeid = ""; + } + } + + // GPX file footer + gw.write(""); + gw.write(""); + gw.newLine(); + gw.close(); + System.out.println(); + } + maeroute = new String(boc +" "+ bln); + + System.out.print("export course("+ maeroute +") "); + + gpxFile = new File(dir, iCode + cvid +".gpx"); + gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8")); + gw.write(""); + gw.newLine(); + gw.write(""); + gw.newLine(); + + // GPX + gw.write(""); + gw.write(" "+ bsc +" "+ boc +" "+ bln +" "+ cvid +""); + gw.newLine(); + + // INDEX file + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.newLine(); + } + + if (!maeid.equals(cvid)) { + if (!maeid.equals("")) { + // GPX file footer + gw.write(""); + gw.newLine(); + } + System.out.println(); + + // GPX + gw.write(""); + gw.newLine(); + } + maeid = new String(cvid); + + // GPX + System.out.print("."); + gw.write(""); + gw.newLine(); + } + rset7.close(); + + // GPX file footer + gw.write(""); + gw.write(""); + gw.write(""); + gw.newLine(); + gw.close(); + + // index file footer + hw.write("
bocblnGPX
"+ boc +""+ bln +""+ gpxFile.getName() +"
"); + hw.newLine(); + hw.close(); + } + + public static BufferedWriter createGPX(File gpxFile, String cvid) throws IOException { + BufferedWriter gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8")); + gw.write(""); + gw.newLine(); + gw.write(""); + gw.newLine(); + return gw; + } + + + /** + * + * @param con + * @param node + * @param iFileName // ソースファイル名(拡張子を含まない) + * @return + * @throws IOException + * @throws SQLException + */ + public static int importNodes(Connection con, Node node, String iFileName) throws IOException, SQLException { + int iCounter = 0; + + NodeList nodes = node.getChildNodes(); + for (int i=0; i - * OPTION: -nocheck OSMデータ上に既存のバス停が存在するかどうかをチェックしない - * OPTION: -check OSMデータ上に既存のバス停が存在するかどうかをチェックする - * - * @throws IOException - * @throws SQLException - * @throws ClassNotFoundException - * @throws FileNotFoundException - * @throws TransformerException - * @throws SAXException - * @throws ParserConfigurationException */ - public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException, TransformerException - { - int index = 0; - if (args.length > index) { - if (args[index].equals("-check")) { - ConvBusstop.nocheck = false; - index++; - } - if (args[index].equals("-nocheck")) { - ConvBusstop.nocheck = true; - index++; - } - } - - /** - * アプリケーション [ConvBusstop] - * > java -jar ConvBusstop.jar <オプション> - * オプション: -exp 実行する直前にデータベースを初期化する(省略可能) - */ - File dbdir = new File("database"); - if (!dbdir.isDirectory()) { - dbdir.mkdir(); - } - - Connection con = DatabaseTool.openDb("database"); - ConvBusstop.initDb(con); - - try { - /** - * バス停データ変換のメイン処理 - */ - int fcounter = 0; - if (args.length > index) { - File iFile = new File(args[index]); - fcounter++; - - inputFile(con, iFile); - String iStr = iFile.getName(); - outputDb(con, iStr.substring(0, iStr.length() - 4)); - } - else { - File dir = new File("."); - File[] files = dir.listFiles(); - for (File iFile : files) { - if (checkFile(iFile)) { - fcounter++; - ConvBusstop.clearDb(con); - inputFile(con, iFile); - - // ローカルデータベース内の情報を出力する - String iStr = iFile.getName(); - outputDb(con, iStr.substring(0, iStr.length() - 4)); - } - } - } - System.out.println("["+ fcounter +"]つのファイルをインポートしました。"); - - /** - * 既存のOSMバス停を読み込む - * --> 'existing.xml' - */ - if (!ConvBusstop.nocheck) { - /** - * インポートしたデータの緯度経度範囲を読み取る - */ - double maxLat = -90.0D; - double minLat = 90.0D; - double maxLon = -180.0D; - double minLon = 180.0D; - PreparedStatement ps8 = con.prepareStatement("SELECT lat,lon FROM bus_stop"); - ResultSet rset8 = ps8.executeQuery(); - while (rset8.next()) { - Double lat = rset8.getDouble("lat"); - Double lon = rset8.getDouble("lon"); - - if (lat > maxLat) { - maxLat = lat; - } - if (lon > maxLon) { - maxLon = lon; - } - if (lat < minLat) { - minLat = lat; - } - if (lon < minLon) { - minLon = lon; - } - } - rset8.close(); - - readExistingFile(con, new File("existing.xml")); - - PreparedStatement ps1 = con.prepareStatement("SELECT idref,lat,lon FROM bus_stop"); - PreparedStatement ps2 = con.prepareStatement("SELECT count(idref) FROM existing_data where (lat > ?) and (lat < ?) and (lon > ?) and (lon < ?)"); - PreparedStatement ps3 = con.prepareStatement("UPDATE bus_stop SET fixed=? WHERE idref=?"); - ResultSet rset1 = ps1.executeQuery(); - while (rset1.next()) { - String idref = rset1.getString("idref"); - Double lat = rset1.getDouble("lat"); - Double lon = rset1.getDouble("lon"); - - // 指定の緯度経度を中心とする半径75m四方の矩形領域 - RectArea rect = new RectArea(lat, lon, 75); - ps2.setDouble(1, rect.minlat); - ps2.setDouble(2, rect.maxlat); - ps2.setDouble(3, rect.minlon); - ps2.setDouble(4, rect.maxlon); - ResultSet rset2 = ps2.executeQuery(); - if (rset2.next()) { - int count = rset2.getInt(1); - ps3.setInt(1, count); - ps3.setString(1, idref); - ps3.executeQuery(); - } - rset2.close(); - } - rset1.close(); - } - System.out.println("["+ fcounter +"] 変換完了しました。"); - } - finally { - DatabaseTool.closeDb(con); - } - } - - static String[] shiftArgs(String[] args) { - String[] values = new String[args.length - 1]; - for (int i=1; i < args.length; i++) { - values[i - 1] = new String(args[i]); - } - return values; - } - - public static void readExistingFile (Connection con, File existingFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - int iCounter = 0; - - DocumentBuilderFactory factory; - DocumentBuilder builder; - Node root; - - iCounter = 0; - factory = DocumentBuilderFactory.newInstance(); - builder = factory.newDocumentBuilder(); - factory.setIgnoringElementContentWhitespace(true); - factory.setIgnoringComments(true); - factory.setValidating(true); - root = builder.parse(existingFile); - - iCounter += readExistingNodes(con, root); - System.out.println("既存バス停数["+ iCounter +"]"); - } - - static int readExistingNodes(Connection con, Node node) throws IOException, SQLException { - int iCounter = 0; - - NodeList nodes = node.getChildNodes(); - for (int i = 0; i < nodes.getLength(); i++) { - Node node2 = nodes.item(i); - if (node2.getNodeName().equals("node")) { - iCounter++; - importExistingNode(con, node2); - } - else { - iCounter += readExistingNodes(con, node2); - } - } - return iCounter; - } - - static void importExistingNode(Connection con, Node node) throws IOException, SQLException { - String idrefStr = ""; - String latStr = ""; - String lonStr = ""; - PreparedStatement ps5 = con.prepareStatement("INSERT INTO existing_data (idref,lat,lon) VALUES (?,?,?)"); - - NamedNodeMap nodeMap = node.getAttributes(); - if (null != nodeMap) { - for (int j=0; j < nodeMap.getLength(); j++) { - if (nodeMap.item(j).getNodeName().equals("id")) { - idrefStr = nodeMap.item(j).getNodeValue(); - } - if (nodeMap.item(j).getNodeName().equals("lat")) { - latStr = nodeMap.item(j).getNodeValue(); - } - if (nodeMap.item(j).getNodeName().equals("lon")) { - lonStr = nodeMap.item(j).getNodeValue(); - } - } - - // idref と nameStr をデータベースに格納する - ps5.setString(1, idrefStr); - ps5.setDouble(2, Double.parseDouble(latStr)); - ps5.setDouble(3, Double.parseDouble(lonStr)); - ps5.executeUpdate(); - ps5.close(); - } - } - - /** - * ソースファイルを読み取ってローカルベータベースへ記録する - * @param con - * @param iFile - * @throws FileNotFoundException - * @throws ClassNotFoundException - * @throws SQLException - * @throws IOException - * @throws ParserConfigurationException - * @throws SAXException - */ - public static void inputFile (Connection con, File iFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { - int iCounter = 0; - String timeStampStr = null; - - String iStr = iFile.getName(); - File dir = new File(iStr.substring(0, iStr.length() - 4)); - dir.mkdir(); - - DocumentBuilderFactory factory; - DocumentBuilder builder; - Node root; - - iCounter = 0; - factory = DocumentBuilderFactory.newInstance(); - builder = factory.newDocumentBuilder(); - factory.setIgnoringElementContentWhitespace(true); - factory.setIgnoringComments(true); - factory.setValidating(true); - root = builder.parse(iStr); - - iCounter += showNodes(con, root, iStr.substring(0, iStr.length() - 4), timeStampStr); - System.out.println("バス停数["+ iCounter +"]"); - } - - public static void clearDb(Connection con) throws SQLException { - Statement stmt = con.createStatement(); - long count = stmt.executeUpdate("delete from bus_stop"); - System.out.println("'Database.bus_stop'から "+ count +" 件のデータを削除しました。"); - - count = stmt.executeUpdate("delete from existing_data"); - System.out.println("'Database.existing_data'から "+ count +" 件のデータを削除しました。"); - - count = stmt.executeUpdate("delete from bus_course"); - System.out.println("'Database.bus_course'から "+ count +" 件のデータを削除しました。"); - - count = stmt.executeUpdate("delete from bus_ref"); - System.out.println("'Database.bus_ref'から "+ count +" 件のデータを削除しました。"); - stmt.close(); - } - - public static void initDb(Connection con) throws SQLException { - String createSt; - - // 'table.BUS_STOP'を新規に作る - DbBusstop.create(con); - - createSt = "CREATE TABLE existing_data (idref VARCHAR(12) NOT NULL, name VARCHAR(128), lat DOUBLE, lon DOUBLE, CONSTRAINT existing_pk PRIMARY KEY(idref, lat, lon));"; - DbBusstop.create(con, createSt); - - createSt = "CREATE TABLE bus_course (code int, type int, corp VARCHAR(128) NOT NULL, course VARCHAR(512), ifile VARCHAR(128), CONSTRAINT bus_course_pk PRIMARY KEY(code));"; - DbBusstop.create(con, createSt); - - createSt = "CREATE TABLE bus_ref (idref VARCHAR(12), code INT);"; - DbBusstop.create(con, createSt); - } - - - /** - * ローカルデータベース内の情報を出力する - * @param con - * @param iCode - * @throws IOException - * @throws SQLException - */ - public static void outputDb(Connection con, String iCode) throws IOException, SQLException { - SimpleDateFormat timeStampFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - String timeStampStr = timeStampFmt.format(new Date(Calendar.getInstance().getTimeInMillis())); - File dir = new File(iCode); - dir.mkdir(); - - BufferedWriter ow = null; - BufferedWriter gw = null; - BufferedWriter hw = null; - BufferedWriter ww = null; - - // HTML header - File htmlFile = new File(iCode +".html"); - hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8")); - hw.write(""); - hw.newLine(); - hw.write(""); - hw.newLine(); - hw.write(""); - hw.newLine(); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.newLine(); - - // Wiki header - File wikiFile = new File(iCode +".wiki.txt"); - ww = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wikiFile), "UTF-8")); - ww.write("= バス路線毎のマッピング状況 ="); - ww.newLine(); - ww.newLine(); - - String maeCorp = ""; - boolean firstCorp = true; - - PreparedStatement ps7 = con.prepareStatement("SELECT code,type,corp,course,ifile FROM bus_course WHERE ifile=? ORDER BY type,corp,course"); - PreparedStatement ps9 = con.prepareStatement("SELECT idref FROM bus_ref WHERE code=?"); - PreparedStatement ps8 = con.prepareStatement("SELECT name,lat,lon,fixed FROM bus_stop WHERE idref=?"); - ps7.setString(1, iCode); - ResultSet rset7 = ps7.executeQuery(); - while (rset7.next()) { - int code = rset7.getInt(1); - int type = rset7.getInt(2); - String corp = rset7.getString(3); - String course = rset7.getString(4); - - - File osmFile = new File(dir, iCode + String.format("_%1$08d", code) +".osm"); - File gpxFile = new File(dir, iCode + String.format("_%1$08d", code) +".gpx"); - File osmSample = new File(dir, iCode + String.format("_s%1$08d", code) +".osm"); - - System.out.println("course = "+ course); - int stopCount = 0; - int fixedCount = 0; - int unfixedCount = 0; - - // index file header - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.newLine(); - - //-------------------------------------------- - // Wiki見出し2: 運行会社 - //------------ - if (!maeCorp.equals(corp)) { - if (firstCorp == false) { - ww.write("|}"); - ww.newLine(); - ww.newLine(); - } - firstCorp = false; - - ww.write("=== "+ corp +" ==="); - ww.newLine(); - ww.newLine(); - - ww.write(":{{JA:Tag|network||"+ corp +"}}"); - ww.newLine(); - ww.write(":{{JA:Tag|operator||"+ corp +"}}"); - ww.newLine(); - ww.newLine(); - - ww.write("{| class=\"wikitable sortable\" style=\"table-layout: fixed; width: 100%\""); - ww.newLine(); - ww.write("!style=\"width: 100px\"| ref"); - ww.newLine(); - ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(マスタ)"); - ww.newLine(); - ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(往路)"); - ww.newLine(); - ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(復路)"); - ww.newLine(); - ww.write("!class=\"unsortable\"| 備考"); - ww.newLine(); - - maeCorp = new String(corp); - } - - // Wiki - ww.write("|-"); - ww.newLine(); - ww.write("| "+ course +" "); // ref - ww.write("|| {{State Route|r=0}} {{relation|0|tools=no}} "); // 編集状況 (マスタ) - ww.write("|| {{State Route|r=0|h=0}} {{relation|0|tools=no}} "); // 編集状況 (往路) - ww.write("|| {{State Route|r=0|h=0}} {{relation|0|tools=no}} "); // 編集状況 (復路) - ww.write("|| "); // 備考 - ww.newLine(); - - - // OSM file header - ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmFile), "UTF-8")); - ow.write(""); - ow.newLine(); - ow.write(""); - ow.newLine(); - - // GPX file header - gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8")); - gw.write(""); - gw.newLine(); - gw.write(""); - gw.newLine(); - - double maxLat = -180.0; - double minLat = 180.0; - double maxLon = -180.0; - double minLon = 180.0; - - ps9.setInt(1, code); - ResultSet rset9 = ps9.executeQuery(); - while (rset9.next()) { - String idref = rset9.getString(1); - - ps8.setString(1, idref); - ResultSet rset8 = ps8.executeQuery(); - if (rset8.next()) { - stopCount++; - String name = rset8.getString(1); - Double lat = rset8.getDouble(2); - Double lon = rset8.getDouble(3); - int fixed = rset8.getInt(4); - - if (lat > maxLat) { - maxLat = lat; - } - if (lon > maxLon) { - maxLon = lon; - } - if (lat < minLat) { - minLat = lat; - } - if (lon < minLon) { - minLon = lon; - } - - System.out.println("\tway point = "+ idref +", lat="+ lat +", lon="+ lon +", name="+ name); - fixedCount += fixed; - if (fixed == 0) { - unfixedCount++; - - // OSM node - int nodeid = Integer.parseInt(idref.substring(1)) * -1; - String osm_node = nodeBusstop(nodeid, name, lat, lon, timeStampStr); - ow.write(osm_node); - ow.newLine(); - - // TEXT node - //File txtFile = new File(dir, iCode + idref +".txt"); - //BufferedWriter gw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFile), "UTF-8")); - //gw2.write(osm_node); - //gw2.newLine(); - //gw2.close(); - - // GPX waypoint - gw.write("\n"); - gw.write(" \n"); - gw.write(" \n"); - //gw.write(" "+ idref +"\n"); - gw.write("\n"); - gw.newLine(); - } - } - rset8.close(); - } - rset9.close(); - - // INDEX file - hw.write(""); - hw.write(""); - hw.write(""); - hw.write(""); - hw.newLine(); - - // OSM file header - BufferedWriter ow2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmSample), "UTF-8")); - ow2.write(""); - ow2.newLine(); - ow2.write(""); - ow2.newLine(); - - ow2.write(nodeBusstop(code, "SAMPLE", ((maxLat+minLat)/2), ((maxLon+minLon)/2), timeStampStr)); - ow2.newLine(); - ow2.write(""); - ow2.newLine(); - ow2.close(); - - // OSM file footer - ow.write(""); - ow.newLine(); - ow.close(); - - // GPX file footer - gw.write(""); - gw.newLine(); - gw.close(); - } - rset7.close(); - - // Wiki footer - ww.close(); - - // index file footer - hw.write("
typecorpcourseGPXSAMPLEバス停数未入力既存
"+ type +""+ corp +""+ course +""+ gpxFile.getName() +""+ osmSample.getName() +""+ stopCount +""+ unfixedCount +""+ fixedCount +"
"); - hw.newLine(); - hw.close(); - } - - public static String nodeBusstop(int code, String name, Double lat, Double lon, String timeStampStr) { - String osm_node = ("\n"); - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - osm_node += "\n"; - return osm_node; - } - - /** - * - * @param con - * @param node - * @param iFileName // ソースファイル名(拡張子を含まない) - * @param timeStampStr - * @return - * @throws IOException - * @throws SQLException - */ - public static int showNodes(Connection con, Node node, String iFileName, String timeStampStr) throws IOException, SQLException { - int iCounter = 0; - NamedNodeMap nodeMap = node.getAttributes(); - if ( null != nodeMap ) { - for ( int j=0; j < nodeMap.getLength(); j++ ) { - if (nodeMap.item(j).getNodeName().equals("timeStamp")) { - timeStampStr = nodeMap.item(j).getNodeValue(); - } - } - } - - NodeList nodes = node.getChildNodes(); - for (int i=0; i bris = new ArrayList(); - NodeList nodes = node.getChildNodes(); - for (int i=0; i < nodes.getLength(); i++) { - Node node2 = nodes.item(i); - if (node2.getNodeName().equals("ksj:POS")) { - NamedNodeMap nodeMap = node2.getAttributes(); - if (null != nodeMap) { - for ( int j=0; j < nodeMap.getLength(); j++ ) { - if (nodeMap.item(j).getNodeName().equals("idref")) { - idrefStr = nodeMap.item(j).getNodeValue(); - System.out.println("found idref='"+ idrefStr +"'"); - break; - } - } - } - } - else if (node2.getNodeName().equals("ksj:BSN")) { - nameStr = node2.getTextContent(); - } - else if (node2.getNodeName().equals("ksj:BRI")) { - String[] rtn = anaComm(node2); - if (rtn != null) { - bris.add(rtn); - } - } - } - - // idref と nameStr をデータベースに格納する - boolean insert = true; - ps1.setString(1, idrefStr); - ResultSet rset = ps1.executeQuery(); - if (rset.next()) { - insert = false; - } - rset.close(); - - if (insert) { - ps2.setString(1, idrefStr); - ps2.setString(2, nameStr); - ps2.setString(3, iFileName); - System.out.println("INSERT INTO bus_stop (idref,name,ifile) VALUES ('"+ idrefStr +"','"+ nameStr +"','"+ iFileName +"')"); - ps2.executeUpdate(); - } - - for (String[] rtn : bris) { - int code = 0; - ps3.setString(1, rtn[1]); - ps3.setString(2, rtn[2]); - ps3.setString(3, iFileName); - rset = ps3.executeQuery(); - if (rset.next()) { - code = rset.getInt(1); - } - rset.close(); - - if (code == 0) { - ps6.setString(1, iFileName); - ResultSet rset6 = ps6.executeQuery(); - if (rset6.next()) { - code = rset6.getInt(1); - } - rset6.close(); - code++; - - System.out.println("code="+code); - ps4.setInt(1, code); - ps4.setInt(2, Integer.parseInt(rtn[0])); - ps4.setString(3, rtn[2]); - ps4.setString(4, rtn[1]); - ps4.setString(5, iFileName); - ps4.executeUpdate(); - } - - ps5.setString(1, idrefStr); - ps5.setInt(2, code); - ps5.executeUpdate(); - } - - ps1.close(); - ps2.close(); - ps3.close(); - ps4.close(); - ps5.close(); - } - - /** - * - * - * 城堀 - * - * - * 1 - * 箱根登山バス - * 小01 - * - * - * - * - * 1 - * 箱根登山バス - * 湯07 - * - * - * - * - * 1 - * 箱根登山バス - * 湯11 - * - * - * - * - * @param con - * @param node - * @param iFileName // ソースファイル名(拡張子を含まない) - * @throws IOException - * @throws SQLException - */ - public static void showBusStop(Connection con, Node node, String iFileName) throws IOException, SQLException { - String idrefStr = ""; - String nameStr = ""; - PreparedStatement ps2 = con.prepareStatement("UPDATE bus_stop SET name=?,ifile=? WHERE idref=?"); - PreparedStatement ps3 = con.prepareStatement("SELECT code FROM bus_course WHERE course=? AND corp=? AND ifile=?"); - PreparedStatement ps4 = con.prepareStatement("INSERT INTO bus_course (code,type,corp,course,ifile) VALUES (?,?,?,?,?)"); - PreparedStatement ps5 = con.prepareStatement("INSERT INTO bus_ref (idref,code) VALUES (?,?)"); - PreparedStatement ps6 = con.prepareStatement("SELECT max(code) FROM bus_course WHERE ifile=?"); - - ArrayList bris = new ArrayList(); - NodeList nodes = node.getChildNodes(); - for (int i=0; i < nodes.getLength(); i++) { - Node node2 = nodes.item(i); - if (node2.getNodeName().equals("ksj:position")) { - NamedNodeMap nodeMap = node2.getAttributes(); - if (null != nodeMap) { - for ( int j=0; j < nodeMap.getLength(); j++ ) { - if (nodeMap.item(j).getNodeName().equals("xlink:href")) { - idrefStr = nodeMap.item(j).getNodeValue(); - idrefStr = idrefStr.substring(1); - System.out.println("found idref='"+ idrefStr +"'"); - break; - } - } - } - } - else if (node2.getNodeName().equals("ksj:busStopName")) { - nameStr = node2.getTextContent(); - } - else if (node2.getNodeName().equals("ksj:busRouteInformation")) { - String[] rtn = anaCommJGD(node2); - if (rtn != null) { - bris.add(rtn); - } - } - } - - // idref と nameStr をデータベースに格納する - ps2.setString(1, nameStr); - ps2.setString(2, iFileName); - ps2.setString(3, idrefStr); - ps2.executeUpdate(); - - for (String[] rtn : bris) { - int code = 0; - ps3.setString(1, rtn[1]); - ps3.setString(2, rtn[2]); - ps3.setString(3, iFileName); - ResultSet rset = ps3.executeQuery(); - if (rset.next()) { - code = rset.getInt(1); - } - rset.close(); - - if (code == 0) { - ps6.setString(1, iFileName); - ResultSet rset6 = ps6.executeQuery(); - if (rset6.next()) { - code = rset6.getInt(1); - } - rset6.close(); - code++; - - System.out.println("course="+ code +" : "+ rtn[0] +" : "+ rtn[1] +" : "+ rtn[2] ); - ps4.setInt(1, code); - ps4.setInt(2, Integer.parseInt(rtn[0])); - ps4.setString(3, rtn[2]); - ps4.setString(4, rtn[1]); - ps4.setString(5, iFileName); - ps4.executeUpdate(); - } - - ps5.setString(1, idrefStr); - ps5.setInt(2, code); - ps5.executeUpdate(); - } - - ps2.close(); - ps3.close(); - ps4.close(); - ps5.close(); - } - - public static String[] anaComm(Node briNode) { - String[] rtn = new String[3]; - rtn[0] = ""; // corp type - rtn[1] = ""; // course name - rtn[2] = ""; // corp name - - NodeList nodes = briNode.getChildNodes(); - for (int i=0; i < nodes.getLength(); i++) { - Node node2 = nodes.item(i); - if (node2.getNodeName().equals("ksj:BSC")) { - rtn[0] = node2.getTextContent(); - } - else if (node2.getNodeName().equals("ksj:BLN")) { - rtn[1] = node2.getTextContent(); - } - else if (node2.getNodeName().equals("ksj:BOC")) { - rtn[2] = node2.getTextContent(); - } - } - return rtn; - } - - /** - * - * - * - * 1 - * 箱根登山バス - * 小01 - * - * - * - * @param briNode - * @return - */ - public static String[] anaCommJGD(Node briNode) { - String[] rtn = new String[3]; - int vcnt = 0; - - NodeList nodes2 = briNode.getChildNodes(); - for (int i=0; i < nodes2.getLength(); i++) { - Node node2 = nodes2.item(i); - if (node2.getNodeName().equals("ksj:BusRouteInformation")) { - NodeList nodes3 = node2.getChildNodes(); - for (int j=0; j < nodes3.getLength(); j++) { - Node node3 = nodes3.item(j); - if (node3.getNodeName().equals("ksj:busType")) { - rtn[0] = new String(node3.getTextContent()); - vcnt++; - } - else if (node3.getNodeName().equals("ksj:busLineName")) { - rtn[1] = new String(node3.getTextContent()); - vcnt++; - } - else if (node3.getNodeName().equals("ksj:busOperationCompany")) { - rtn[2] = new String(node3.getTextContent()); - vcnt++; - } - } - } - } - - if (vcnt > 0) { - return rtn; - } - return null; - } - - public static void showGmPoint(Connection con, Node node) throws IOException, SQLException { - String positionStr = ""; - String latStr = ""; - String lonStr = ""; - String idStr = ""; - - NamedNodeMap nodeMap = node.getAttributes(); - if ( null != nodeMap ) { - for ( int j=0; j - * 35.14591397 139.10569573 - * - * - * @param con - * @param node - * @throws IOException - * @throws SQLException - */ - public static void showGmlPoint(Connection con, Node node) throws IOException, SQLException { - String positionStr = ""; - String latStr = ""; - String lonStr = ""; - String idStr = ""; - - NamedNodeMap nodeMap = node.getAttributes(); - if ( null != nodeMap ) { - for ( int j=0; j + * OPTION: -nocheck OSMデータ上に既存のバス停が存在するかどうかをチェックしない + * OPTION: -check OSMデータ上に既存のバス停が存在するかどうかをチェックする + * + * @throws IOException + * @throws SQLException + * @throws ClassNotFoundException + * @throws FileNotFoundException + * @throws TransformerException + * @throws SAXException + * @throws ParserConfigurationException */ + public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException, TransformerException + { + int index = 0; + if (args.length > index) { + if (args[index].equals("-check")) { + ConvBusstop.nocheck = false; + index++; + } + else if (args[index].equals("-nocheck")) { + ConvBusstop.nocheck = true; + index++; + } + } + + /** + * アプリケーション [ConvBusstop] + * > java -jar ConvBusstop.jar <オプション> + * オプション: -exp 実行する直前にデータベースを初期化する(省略可能) + */ + File dbdir = new File("database"); + if (!dbdir.isDirectory()) { + dbdir.mkdir(); + } + + Connection con = DatabaseTool.openDb("database"); + ConvBusstop.initDb(con); + + try { + /** + * バス停データ変換のメイン処理 + */ + int fcounter = 0; + if (args.length > index) { + File iFile = new File(args[index]); + fcounter++; + inputFile(con, iFile); + } + else { + File dir = new File("."); + File[] files = dir.listFiles(); + for (File iFile : files) { + if (checkFile(iFile)) { + fcounter++; + ConvBusstop.clearDb(con); + inputFile(con, iFile); + } + } + } + System.out.println("["+ fcounter +"]つのファイルをインポートしました。"); + + /** + * 既存のOSMバス停を読み込む + * --> 'existing.xml' + */ + if (!ConvBusstop.nocheck && (fcounter > 0)) { + /* + * 既存のOSMデータファイルがなければ、新たにダウンロードする。 + * OSMデータファイルがあるときは、ダウンロードしないでそれを使う。 + */ + File existingFile = new File("existing.xml"); + if (!existingFile.isFile()) { + /** + * インポートしたデータの緯度経度範囲を読み取る + */ + double maxLat = -90.0D; + double minLat = 90.0D; + double maxLon = -180.0D; + double minLon = 180.0D; + PreparedStatement ps8 = con.prepareStatement("SELECT lat,lon FROM bus_stop"); + ResultSet rset8 = ps8.executeQuery(); + while (rset8.next()) { + Double lat = rset8.getDouble("lat"); + Double lon = rset8.getDouble("lon"); + + if (lat > maxLat) { + maxLat = lat; + } + if (lon > maxLon) { + maxLon = lon; + } + if (lat < minLat) { + minLat = lat; + } + if (lon < minLon) { + minLon = lon; + } + } + rset8.close(); + + /** + * OSM OverPassAPI を使って、既存のOSMバス停のデータを取得して、「existing.xml」に出力する + */ + HttpPOST.getCapabilities(minLat, maxLat, minLon, maxLon); + } + readExistingFile(con, existingFile); + + PreparedStatement ps1 = con.prepareStatement("SELECT idref,name,lat,lon FROM bus_stop"); + PreparedStatement ps2 = con.prepareStatement("SELECT count(idref) FROM existing_data where (lat > ?) and (lat < ?) and (lon > ?) and (lon < ?)"); + PreparedStatement ps3 = con.prepareStatement("UPDATE bus_stop SET fixed=? WHERE idref=?"); + ResultSet rset1 = ps1.executeQuery(); + while (rset1.next()) { + String idref = rset1.getString("idref"); + String name = rset1.getString("name"); + Double lat = rset1.getDouble("lat"); + Double lon = rset1.getDouble("lon"); + + // 指定の緯度経度を中心とする半径75m四方の矩形領域 + System.out.print(idref + "("+ name + ") ...."); + RectArea rect = new RectArea(lat, lon, NEER); // 100m 四方 + ps2.setDouble(1, rect.minlat); + ps2.setDouble(2, rect.maxlat); + ps2.setDouble(3, rect.minlon); + ps2.setDouble(4, rect.maxlon); + ResultSet rset2 = ps2.executeQuery(); + if (rset2.next()) { + int count = rset2.getInt(1); + System.out.println("...."+ count); + ps3.setInt(1, count); + ps3.setString(2, idref); + ps3.executeUpdate(); + } + rset2.close(); + } + rset1.close(); + ps3.close(); + ps2.close(); + ps1.close(); + } + + // ローカルデータベース内の情報を出力する + if (args.length > index) { + File iFile = new File(args[index]); + String iStr = iFile.getName(); + outputDb(con, iStr.substring(0, iStr.length() - 4)); + } + else { + File dir = new File("."); + File[] files = dir.listFiles(); + for (File iFile : files) { + if (checkFile(iFile)) { + String iStr = iFile.getName(); + outputDb(con, iStr.substring(0, iStr.length() - 4)); + } + } + } + + // ローカルデータベース内の情報を「busstop.xml」に出力する + int counter = outputDb(con); + + System.out.println("["+ counter +"] 変換完了しました。"); + } + finally { + DatabaseTool.closeDb(con); + } + } + + static String[] shiftArgs(String[] args) { + String[] values = new String[args.length - 1]; + for (int i=1; i < args.length; i++) { + values[i - 1] = new String(args[i]); + } + return values; + } + + public static void readExistingFile (Connection con, File existingFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + int iCounter = 0; + + DocumentBuilderFactory factory; + DocumentBuilder builder; + Node root; + + iCounter = 0; + factory = DocumentBuilderFactory.newInstance(); + builder = factory.newDocumentBuilder(); + factory.setIgnoringElementContentWhitespace(true); + factory.setIgnoringComments(true); + factory.setValidating(true); + root = builder.parse(existingFile); + + iCounter += readExistingNodes(con, root); + System.out.println("既存バス停数["+ iCounter +"]"); + } + + static int readExistingNodes(Connection con, Node node) throws IOException, SQLException { + int iCounter = 0; + + NodeList nodes = node.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node2 = nodes.item(i); + if (node2.getNodeName().equals("node")) { + iCounter++; + importExistingNode(con, node2); + } + else { + iCounter += readExistingNodes(con, node2); + } + } + return iCounter; + } + + static void importExistingNode(Connection con, Node node) throws IOException, SQLException { + String idrefStr = ""; + String latStr = ""; + String lonStr = ""; + PreparedStatement ps5 = con.prepareStatement("INSERT INTO existing_data (idref,lat,lon) VALUES (?,?,?)"); + + NamedNodeMap nodeMap = node.getAttributes(); + if (null != nodeMap) { + for (int j=0; j < nodeMap.getLength(); j++) { + if (nodeMap.item(j).getNodeName().equals("id")) { + idrefStr = nodeMap.item(j).getNodeValue(); + } + else if (nodeMap.item(j).getNodeName().equals("lat")) { + latStr = nodeMap.item(j).getNodeValue(); + } + else if (nodeMap.item(j).getNodeName().equals("lon")) { + lonStr = nodeMap.item(j).getNodeValue(); + } + } + + // idref と nameStr をデータベースに格納する + System.out.println("import existing_data : "+ idrefStr +" ("+ latStr +","+ lonStr+")"); + ps5.setString(1, idrefStr); + ps5.setDouble(2, Double.parseDouble(latStr)); + ps5.setDouble(3, Double.parseDouble(lonStr)); + ps5.executeUpdate(); + ps5.close(); + } + } + + /** + * ソースファイルを読み取ってローカルベータベースへ記録する + * @param con + * @param iFile + * @throws FileNotFoundException + * @throws ClassNotFoundException + * @throws SQLException + * @throws IOException + * @throws ParserConfigurationException + * @throws SAXException + */ + public static void inputFile (Connection con, File iFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + int iCounter = 0; + String timeStampStr = null; + + String iStr = iFile.getName(); + File dir = new File(iStr.substring(0, iStr.length() - 4)); + dir.mkdir(); + + DocumentBuilderFactory factory; + DocumentBuilder builder; + Node root; + + iCounter = 0; + factory = DocumentBuilderFactory.newInstance(); + builder = factory.newDocumentBuilder(); + factory.setIgnoringElementContentWhitespace(true); + factory.setIgnoringComments(true); + factory.setValidating(true); + root = builder.parse(iStr); + + iCounter += showNodes(con, root, iStr.substring(0, iStr.length() - 4), timeStampStr); + System.out.println("バス停数["+ iCounter +"]"); + } + + public static void clearDb(Connection con) throws SQLException { + Statement stmt = con.createStatement(); + long count = stmt.executeUpdate("delete from bus_stop"); + System.out.println("'Database.bus_stop'から "+ count +" 件のデータを削除しました。"); + + count = stmt.executeUpdate("delete from existing_data"); + System.out.println("'Database.existing_data'から "+ count +" 件のデータを削除しました。"); + + count = stmt.executeUpdate("delete from bus_course"); + System.out.println("'Database.bus_course'から "+ count +" 件のデータを削除しました。"); + + count = stmt.executeUpdate("delete from bus_ref"); + System.out.println("'Database.bus_ref'から "+ count +" 件のデータを削除しました。"); + stmt.close(); + } + + public static void initDb(Connection con) throws SQLException { + String createSt; + + // 'table.BUS_STOP'を新規に作る + DbBusstop.create(con); + + createSt = "CREATE TABLE existing_data (idref VARCHAR(12) NOT NULL, name VARCHAR(128), lat DOUBLE, lon DOUBLE, CONSTRAINT existing_pk PRIMARY KEY(idref, lat, lon));"; + DbBusstop.create(con, createSt); + + createSt = "CREATE TABLE bus_course (code int, type int, corp VARCHAR(128) NOT NULL, course VARCHAR(512), ifile VARCHAR(128), CONSTRAINT bus_course_pk PRIMARY KEY(code));"; + DbBusstop.create(con, createSt); + + createSt = "CREATE TABLE bus_ref (idref VARCHAR(12), code INT);"; + DbBusstop.create(con, createSt); + } + + + /** + * ローカルデータベース内の情報を出力する + * @param con + * @param iCode + * @throws IOException + * @throws SQLException + */ + public static void outputDb(Connection con, String iCode) throws IOException, SQLException { + SimpleDateFormat timeStampFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + String timeStampStr = timeStampFmt.format(new Date(Calendar.getInstance().getTimeInMillis())); + File dir = new File(iCode); + dir.mkdir(); + + BufferedWriter ow = null; + BufferedWriter gw = null; + BufferedWriter hw = null; + BufferedWriter ww = null; + + // HTML header + File htmlFile = new File(iCode +".html"); + hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8")); + hw.write(""); + hw.newLine(); + hw.write(""); + hw.newLine(); + hw.write(""); + hw.newLine(); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.newLine(); + + // Wiki header + File wikiFile = new File(iCode +".wiki.txt"); + ww = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wikiFile), "UTF-8")); + ww.write("= バス路線毎のマッピング状況 ="); + ww.newLine(); + ww.newLine(); + + String maeCorp = ""; + boolean firstCorp = true; + + PreparedStatement ps7 = con.prepareStatement("SELECT code,type,corp,course,ifile FROM bus_course WHERE ifile=? ORDER BY type,corp,course"); + PreparedStatement ps9 = con.prepareStatement("SELECT idref FROM bus_ref WHERE code=?"); + PreparedStatement ps8 = con.prepareStatement("SELECT name,lat,lon,fixed FROM bus_stop WHERE idref=?"); + ps7.setString(1, iCode); + ResultSet rset7 = ps7.executeQuery(); + while (rset7.next()) { + int code = rset7.getInt(1); + int type = rset7.getInt(2); + String corp = rset7.getString(3); + String course = rset7.getString(4); + + + File osmFile = new File(dir, iCode + String.format("_%1$08d", code) +".osm"); + File gpxFile = new File(dir, iCode + String.format("_%1$08d", code) +".gpx"); + File osmSample = new File(dir, iCode + String.format("_s%1$08d", code) +".osm"); + + System.out.println("course = "+ course); + int stopCount = 0; + int fixedCount = 0; + int unfixedCount = 0; + + // index file header + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.newLine(); + + //-------------------------------------------- + // Wiki見出し2: 運行会社 + //------------ + if (!maeCorp.equals(corp)) { + if (firstCorp == false) { + ww.write("|}"); + ww.newLine(); + ww.newLine(); + } + firstCorp = false; + + ww.write("=== "+ corp +" ==="); + ww.newLine(); + ww.newLine(); + + ww.write(":{{JA:Tag|network||"+ corp +"}}"); + ww.newLine(); + ww.write(":{{JA:Tag|operator||"+ corp +"}}"); + ww.newLine(); + ww.newLine(); + + ww.write("{| class=\"wikitable sortable\" style=\"table-layout: fixed; width: 100%\""); + ww.newLine(); + ww.write("!style=\"width: 100px\"| ref"); + ww.newLine(); + ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(マスタ)"); + ww.newLine(); + ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(往路)"); + ww.newLine(); + ww.write("!class=\"unsortable\" style=\"width: 152px\"| 編集状況(復路)"); + ww.newLine(); + ww.write("!class=\"unsortable\"| 備考"); + ww.newLine(); + + maeCorp = new String(corp); + } + + // Wiki + ww.write("|-"); + ww.newLine(); + ww.write("| "+ course +" "); // ref + ww.write("|| {{State Route|r=0}} {{relation|0|tools=no}} "); // 編集状況 (マスタ) + ww.write("|| {{State Route|r=0|h=0}} {{relation|0|tools=no}} "); // 編集状況 (往路) + ww.write("|| {{State Route|r=0|h=0}} {{relation|0|tools=no}} "); // 編集状況 (復路) + ww.write("|| "); // 備考 + ww.newLine(); + + + // OSM file header + ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmFile), "UTF-8")); + ow.write(""); + ow.newLine(); + ow.write(""); + ow.newLine(); + + // GPX file header + gw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxFile), "UTF-8")); + gw.write(""); + gw.newLine(); + gw.write(""); + gw.newLine(); + + double maxLat = -180.0; + double minLat = 180.0; + double maxLon = -180.0; + double minLon = 180.0; + + ps9.setInt(1, code); + ResultSet rset9 = ps9.executeQuery(); + while (rset9.next()) { + String idref = rset9.getString(1); + + ps8.setString(1, idref); + ResultSet rset8 = ps8.executeQuery(); + if (rset8.next()) { + stopCount++; + String name = rset8.getString(1); + Double lat = rset8.getDouble(2); + Double lon = rset8.getDouble(3); + int fixed = rset8.getInt(4); + + if (lat > maxLat) { + maxLat = lat; + } + if (lon > maxLon) { + maxLon = lon; + } + if (lat < minLat) { + minLat = lat; + } + if (lon < minLon) { + minLon = lon; + } + + System.out.println("\tway point = "+ idref +", lat="+ lat +", lon="+ lon +", name="+ name); + fixedCount += fixed; + if (fixed == 0) { + unfixedCount++; + + // OSM node + int nodeid = Integer.parseInt(idref.substring(1)) * -1; + String osm_node = nodeBusstop(nodeid, name, lat, lon, timeStampStr); + ow.write(osm_node); + ow.newLine(); + + // TEXT node + //File txtFile = new File(dir, iCode + idref +".txt"); + //BufferedWriter gw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFile), "UTF-8")); + //gw2.write(osm_node); + //gw2.newLine(); + //gw2.close(); + + // GPX waypoint + gw.write("\n"); + gw.write(" \n"); + gw.write(" \n"); + //gw.write(" "+ idref +"\n"); + gw.write("\n"); + gw.newLine(); + } + } + rset8.close(); + } + rset9.close(); + + // INDEX file + hw.write(""); + hw.write(""); + hw.write(""); + hw.write(""); + hw.newLine(); + + // OSM file header + BufferedWriter ow2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmSample), "UTF-8")); + ow2.write(""); + ow2.newLine(); + ow2.write(""); + ow2.newLine(); + + ow2.write(nodeBusstop(code, "SAMPLE", ((maxLat+minLat)/2), ((maxLon+minLon)/2), timeStampStr)); + ow2.newLine(); + ow2.write(""); + ow2.newLine(); + ow2.close(); + + // OSM file footer + ow.write(""); + ow.newLine(); + ow.close(); + + // GPX file footer + gw.write(""); + gw.newLine(); + gw.close(); + } + rset7.close(); + + // Wiki footer + ww.close(); + + // index file footer + hw.write("
typecorpcourseGPXSAMPLEバス停数未入力既存
"+ type +""+ corp +""+ course +""+ gpxFile.getName() +""+ osmSample.getName() +""+ stopCount +""+ unfixedCount +""+ fixedCount +"
"); + hw.newLine(); + hw.close(); + } + + public static int outputDb(Connection con) throws IOException, SQLException { + File osmFile = new File("busstop.osm"); + int counter = 0; + + SimpleDateFormat timeStampFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + String timeStampStr = timeStampFmt.format(new Date(Calendar.getInstance().getTimeInMillis())); + BufferedWriter ow = null; + + // OSM file header + ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(osmFile), "UTF-8")); + ow.write(""); + ow.newLine(); + ow.write(""); + ow.newLine(); + + PreparedStatement ps8 = con.prepareStatement("SELECT idref,name,lat,lon,fixed FROM bus_stop"); + ResultSet rset8 = ps8.executeQuery(); + while (rset8.next()) { + String idref = rset8.getString("idref"); + String name = rset8.getString("name"); + Double lat = rset8.getDouble("lat"); + Double lon = rset8.getDouble("lon"); + int fixed = rset8.getInt("fixed"); + + if (ConvBusstop.nocheck || (fixed < 1)) { + // OSM node + counter++; + int nodeid = Integer.parseInt(idref.substring(1)) * -1; + String osm_node = nodeBusstop(nodeid, name, lat, lon, timeStampStr); + System.out.println(osm_node); + ow.write(osm_node); + ow.newLine(); + } + } + rset8.close(); + + // OSM file footer + ow.write(""); + ow.newLine(); + ow.close(); + return counter; + } + + public static String nodeBusstop(int code, String name, Double lat, Double lon, String timeStampStr) { + String osm_node = ("\n"); + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + osm_node += "\n"; + return osm_node; + } + + /** + * + * @param con + * @param node + * @param iFileName // ソースファイル名(拡張子を含まない) + * @param timeStampStr + * @return + * @throws IOException + * @throws SQLException + */ + public static int showNodes(Connection con, Node node, String iFileName, String timeStampStr) throws IOException, SQLException { + int iCounter = 0; + NamedNodeMap nodeMap = node.getAttributes(); + if ( null != nodeMap ) { + for ( int j=0; j < nodeMap.getLength(); j++ ) { + if (nodeMap.item(j).getNodeName().equals("timeStamp")) { + timeStampStr = nodeMap.item(j).getNodeValue(); + } + } + } + + NodeList nodes = node.getChildNodes(); + for (int i=0; i bris = new ArrayList(); + NodeList nodes = node.getChildNodes(); + for (int i=0; i < nodes.getLength(); i++) { + Node node2 = nodes.item(i); + if (node2.getNodeName().equals("ksj:POS")) { + NamedNodeMap nodeMap = node2.getAttributes(); + if (null != nodeMap) { + for ( int j=0; j < nodeMap.getLength(); j++ ) { + if (nodeMap.item(j).getNodeName().equals("idref")) { + idrefStr = nodeMap.item(j).getNodeValue(); + System.out.println("found idref='"+ idrefStr +"'"); + break; + } + } + } + } + else if (node2.getNodeName().equals("ksj:BSN")) { + nameStr = node2.getTextContent(); + } + else if (node2.getNodeName().equals("ksj:BRI")) { + String[] rtn = anaComm(node2); + if (rtn != null) { + bris.add(rtn); + } + } + } + + // idref と nameStr をデータベースに格納する + boolean insert = true; + ps1.setString(1, idrefStr); + ResultSet rset = ps1.executeQuery(); + if (rset.next()) { + insert = false; + } + rset.close(); + + if (insert) { + ps2.setString(1, idrefStr); + ps2.setString(2, nameStr); + ps2.setString(3, iFileName); + System.out.println("INSERT INTO bus_stop (idref,name,ifile) VALUES ('"+ idrefStr +"','"+ nameStr +"','"+ iFileName +"')"); + ps2.executeUpdate(); + } + + for (String[] rtn : bris) { + int code = 0; + ps3.setString(1, rtn[1]); + ps3.setString(2, rtn[2]); + ps3.setString(3, iFileName); + rset = ps3.executeQuery(); + if (rset.next()) { + code = rset.getInt(1); + } + rset.close(); + + if (code == 0) { + ps6.setString(1, iFileName); + ResultSet rset6 = ps6.executeQuery(); + if (rset6.next()) { + code = rset6.getInt(1); + } + rset6.close(); + code++; + + System.out.println("code="+code); + ps4.setInt(1, code); + ps4.setInt(2, Integer.parseInt(rtn[0])); + ps4.setString(3, rtn[2]); + ps4.setString(4, rtn[1]); + ps4.setString(5, iFileName); + ps4.executeUpdate(); + } + + ps5.setString(1, idrefStr); + ps5.setInt(2, code); + ps5.executeUpdate(); + } + + ps1.close(); + ps2.close(); + ps3.close(); + ps4.close(); + ps5.close(); + } + + /** + * + * + * 城堀 + * + * + * 1 + * 箱根登山バス + * 小01 + * + * + * + * + * 1 + * 箱根登山バス + * 湯07 + * + * + * + * + * 1 + * 箱根登山バス + * 湯11 + * + * + * + * + * @param con + * @param node + * @param iFileName // ソースファイル名(拡張子を含まない) + * @throws IOException + * @throws SQLException + */ + public static void showBusStop(Connection con, Node node, String iFileName) throws IOException, SQLException { + String idrefStr = ""; + String nameStr = ""; + PreparedStatement ps2 = con.prepareStatement("UPDATE bus_stop SET name=?,ifile=? WHERE idref=?"); + PreparedStatement ps3 = con.prepareStatement("SELECT code FROM bus_course WHERE course=? AND corp=? AND ifile=?"); + PreparedStatement ps4 = con.prepareStatement("INSERT INTO bus_course (code,type,corp,course,ifile) VALUES (?,?,?,?,?)"); + PreparedStatement ps5 = con.prepareStatement("INSERT INTO bus_ref (idref,code) VALUES (?,?)"); + PreparedStatement ps6 = con.prepareStatement("SELECT max(code) FROM bus_course WHERE ifile=?"); + + ArrayList bris = new ArrayList(); + NodeList nodes = node.getChildNodes(); + for (int i=0; i < nodes.getLength(); i++) { + Node node2 = nodes.item(i); + if (node2.getNodeName().equals("ksj:position")) { + NamedNodeMap nodeMap = node2.getAttributes(); + if (null != nodeMap) { + for ( int j=0; j < nodeMap.getLength(); j++ ) { + if (nodeMap.item(j).getNodeName().equals("xlink:href")) { + idrefStr = nodeMap.item(j).getNodeValue(); + idrefStr = idrefStr.substring(1); + System.out.println("found idref='"+ idrefStr +"'"); + break; + } + } + } + } + else if (node2.getNodeName().equals("ksj:busStopName")) { + nameStr = node2.getTextContent(); + } + else if (node2.getNodeName().equals("ksj:busRouteInformation")) { + String[] rtn = anaCommJGD(node2); + if (rtn != null) { + bris.add(rtn); + } + } + } + + // idref と nameStr をデータベースに格納する + ps2.setString(1, nameStr); + ps2.setString(2, iFileName); + ps2.setString(3, idrefStr); + ps2.executeUpdate(); + + for (String[] rtn : bris) { + int code = 0; + ps3.setString(1, rtn[1]); + ps3.setString(2, rtn[2]); + ps3.setString(3, iFileName); + ResultSet rset = ps3.executeQuery(); + if (rset.next()) { + code = rset.getInt(1); + } + rset.close(); + + if (code == 0) { + ps6.setString(1, iFileName); + ResultSet rset6 = ps6.executeQuery(); + if (rset6.next()) { + code = rset6.getInt(1); + } + rset6.close(); + code++; + + System.out.println("course="+ code +" : "+ rtn[0] +" : "+ rtn[1] +" : "+ rtn[2] ); + ps4.setInt(1, code); + ps4.setInt(2, Integer.parseInt(rtn[0])); + ps4.setString(3, rtn[2]); + ps4.setString(4, rtn[1]); + ps4.setString(5, iFileName); + ps4.executeUpdate(); + } + + ps5.setString(1, idrefStr); + ps5.setInt(2, code); + ps5.executeUpdate(); + } + + ps2.close(); + ps3.close(); + ps4.close(); + ps5.close(); + } + + public static String[] anaComm(Node briNode) { + String[] rtn = new String[3]; + rtn[0] = ""; // corp type + rtn[1] = ""; // course name + rtn[2] = ""; // corp name + + NodeList nodes = briNode.getChildNodes(); + for (int i=0; i < nodes.getLength(); i++) { + Node node2 = nodes.item(i); + if (node2.getNodeName().equals("ksj:BSC")) { + rtn[0] = node2.getTextContent(); + } + else if (node2.getNodeName().equals("ksj:BLN")) { + rtn[1] = node2.getTextContent(); + } + else if (node2.getNodeName().equals("ksj:BOC")) { + rtn[2] = node2.getTextContent(); + } + } + return rtn; + } + + /** + * + * + * + * 1 + * 箱根登山バス + * 小01 + * + * + * + * @param briNode + * @return + */ + public static String[] anaCommJGD(Node briNode) { + String[] rtn = new String[3]; + int vcnt = 0; + + NodeList nodes2 = briNode.getChildNodes(); + for (int i=0; i < nodes2.getLength(); i++) { + Node node2 = nodes2.item(i); + if (node2.getNodeName().equals("ksj:BusRouteInformation")) { + NodeList nodes3 = node2.getChildNodes(); + for (int j=0; j < nodes3.getLength(); j++) { + Node node3 = nodes3.item(j); + if (node3.getNodeName().equals("ksj:busType")) { + rtn[0] = new String(node3.getTextContent()); + vcnt++; + } + else if (node3.getNodeName().equals("ksj:busLineName")) { + rtn[1] = new String(node3.getTextContent()); + vcnt++; + } + else if (node3.getNodeName().equals("ksj:busOperationCompany")) { + rtn[2] = new String(node3.getTextContent()); + vcnt++; + } + } + } + } + + if (vcnt > 0) { + return rtn; + } + return null; + } + + public static void showGmPoint(Connection con, Node node) throws IOException, SQLException { + String positionStr = ""; + String latStr = ""; + String lonStr = ""; + String idStr = ""; + + NamedNodeMap nodeMap = node.getAttributes(); + if ( null != nodeMap ) { + for ( int j=0; j + * 35.14591397 139.10569573 + * + * + * @param con + * @param node + * @throws IOException + * @throws SQLException + */ + public static void showGmlPoint(Connection con, Node node) throws IOException, SQLException { + String positionStr = ""; + String latStr = ""; + String lonStr = ""; + String idStr = ""; + + NamedNodeMap nodeMap = node.getAttributes(); + if ( null != nodeMap ) { + for ( int j=0; j - - - - - - - - - */ - - double minlon = 139.4197591d; - double maxlon = 139.4279939d; - double minlat = 35.4320438d; - double maxlat = 35.4375923d; - HttpGET.getMap(minlon, minlat, maxlon, maxlat); - - HttpGET.getMap(35.4350644157973d, 139.423684433498d, 50); // あやせ荘 - HttpGET.getMap(35.4341675801122d, 139.418362759267d, 50); // 武者奇橋 - HttpGET.getMap(35.4369651010672d, 139.426400070915d, 50); // 綾瀬市役所 - } - - public static void getCapabilities() throws MalformedURLException, ProtocolException, IOException { - System.out.println(host + "/api/capabilities"); - URL url = new URL(host + "/api/capabilities"); - - HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); - urlconn.setRequestMethod("GET"); - urlconn.setInstanceFollowRedirects(false); - urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); - urlconn.connect(); - - System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + - "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); - System.out.println("\n---- ボディ ----"); - - BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); - - while (true){ - String line = reader.readLine(); - if ( line == null ){ - break; - } - System.out.println(line); - } - - reader.close(); - urlconn.disconnect(); - } - - - public static final double BIG_Y = (6378137.0d + 6356752.314d) / 2d; // 地球の平均半径(m) - public static final double LAT1 = Math.PI * 2d * BIG_Y / 360d; // 緯度1度の距離(m) - public static int getMap(double lat, double lon, int m) throws MalformedURLException, ProtocolException, IOException { - double dLat = m / LAT1; // 距離を表す緯度(差分) - double minlat = lat - dLat; // 底辺(緯度) - double maxlat = lat + dLat; // 上辺(緯度) - - double y = Math.cos(lat / 180.0d * Math.PI) * BIG_Y; // 緯線上の地球の半径 - double lon1 = (y * 2.0d * Math.PI) / 360; // 経度1度の距離(m) - double dLon = m / lon1; // 距離を表す経度(差分) - double minlon = lon - dLon; // 左辺 - double maxlon = lon + dLon; // 右辺 - - System.out.println("緯線上の地球の半径= "+ y); - System.out.println("緯度1秒の長さ(m)= "+ LAT1 / 3600); - System.out.println("経度1秒の長さ(m)= "+ lon1 / 3600); - - return getMap(minlon, minlat, maxlon, maxlat); - } - - public static int getMap(RectArea center) throws MalformedURLException, ProtocolException, IOException { - return getMap(center.minlon, center.minlat, center.maxlon, center.maxlat); - } - - public static int getMap(double minlon, double minlat, double maxlon, double maxlat) throws MalformedURLException, ProtocolException, IOException { - String param = host + "/api/0.6/map" + "?bbox="+ Double.toString(minlon) +","+ Double.toString(minlat) +","+ Double.toString(maxlon) +","+ Double.toString(maxlat); - - System.out.println(param); - URL url = new URL(param); - - HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); - urlconn.setRequestMethod("GET"); - urlconn.setInstanceFollowRedirects(false); - urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); - urlconn.connect(); - - System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + - "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); - System.out.println("\n---- ボディ ----"); - - //------------------------------------------ - DocumentBuilderFactory factory; - DocumentBuilder builder; - Node root; - - try { - factory = DocumentBuilderFactory.newInstance(); - builder = factory.newDocumentBuilder(); - factory.setIgnoringElementContentWhitespace(true); - factory.setIgnoringComments(true); - factory.setValidating(true); - root = builder.parse(urlconn.getInputStream()); - - counter = 0; - checkNodes(root); - } catch (ParserConfigurationException e0) { - System.out.println(e0.getMessage()); - } catch (SAXException e1){ - System.out.println(e1.getMessage()); - } catch (IOException e2) { - System.out.println(e2.getMessage()); - } - - System.out.println("バス停数["+ counter +"]"); - urlconn.disconnect(); - - return counter; - } - - public void sendCMD(String api) throws MalformedURLException, ProtocolException, IOException { - System.out.println(host + api); - URL url = new URL(host + api); - - HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); - urlconn.setRequestMethod("GET"); - urlconn.setInstanceFollowRedirects(false); - urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); - urlconn.connect(); - - System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + - "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); - System.out.println("\n---- ボディ ----"); - - BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); - - while (true){ - String line = reader.readLine(); - if ( line == null ){ - break; - } - System.out.println(line); - } - - reader.close(); - urlconn.disconnect(); - } - - public static int counter; - public static void checkNodes(Node node) { - if (isBusstop(node)) { - counter++; - showNode(node); - } - - NodeList nodes = node.getChildNodes(); - for (int i=0; i"+ node.getNodeValue()); - - NodeList nodes = node.getChildNodes(); - for (int i=0; i"); - } - - class RectArea { - public double minlon; - public double maxlon; - public double minlat; - public double maxlat; - - /** - * 矩形領域を中心点と中心点からの距離(メートル)でセットする - * - * @param lat // 中心点の緯度 - * @param lon // 中心点の経度 - * @param m // 距離 領域の一辺の長さの半分 - */ - public RectArea(double lat, double lon, int m) { - double BIG_Y = (40000000.0d / 2.0d / Math.PI); // 地球の半径 - double LAT1 = (10000000.0d / 90.0d); // 緯度1度の距離(m) - double dLat = m / LAT1; // 距離を表す緯度(差分) - minlat = lat - dLat; // 底辺(緯度) - maxlat = lat + dLat; // 上辺(緯度) - double y = Math.sin((90.0d - lat) / 180.0d) * BIG_Y; // 緯線上の地球の半径 - double lon1 = y * 2.0d * Math.PI; // 経度1度の距離(m) - double dLon = m / lon1; // 距離を表す経度(差分) - minlon = lon - dLon; // 左辺 - maxlon = lon + dLon; // 右辺 - } - } -} +package osm.jp.api; + +import java.net.*; +import java.io.*; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * Java HTTP クライアントサンプル - HttpURLConnection 版 - + * + * @author 68user http://X68000.q-e-d.net/~68user/ + */ +public class HttpGET { + //public static String host = "http://api06.dev.openstreetmap.org"; + public static String host = "http://api.openstreetmap.org"; + + public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException { + getCapabilities(); + + /* + + + + + + + + + + */ + + double minlon = 139.4197591d; + double maxlon = 139.4279939d; + double minlat = 35.4320438d; + double maxlat = 35.4375923d; + HttpGET.getMap(minlon, minlat, maxlon, maxlat); + + HttpGET.getMap(35.4350644157973d, 139.423684433498d, 50); // あやせ荘 + HttpGET.getMap(35.4341675801122d, 139.418362759267d, 50); // 武者奇橋 + HttpGET.getMap(35.4369651010672d, 139.426400070915d, 50); // 綾瀬市役所 + } + + public static void getCapabilities() throws MalformedURLException, ProtocolException, IOException { + System.out.println(host + "/api/capabilities"); + URL url = new URL(host + "/api/capabilities"); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("GET"); + urlconn.setInstanceFollowRedirects(false); + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.connect(); + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + System.out.println("\n---- ボディ ----"); + + BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); + + while (true){ + String line = reader.readLine(); + if ( line == null ){ + break; + } + System.out.println(line); + } + + reader.close(); + urlconn.disconnect(); + } + + + public static final double BIG_Y = (6378137.0d + 6356752.314d) / 2d; // 地球の平均半径(m) + public static final double LAT1 = Math.PI * 2d * BIG_Y / 360d; // 緯度1度の距離(m) + public static int getMap(double lat, double lon, int m) throws MalformedURLException, ProtocolException, IOException { + double dLat = m / LAT1; // 距離を表す緯度(差分) + double minlat = lat - dLat; // 底辺(緯度) + double maxlat = lat + dLat; // 上辺(緯度) + + double y = Math.cos(lat / 180.0d * Math.PI) * BIG_Y; // 緯線上の地球の半径 + double lon1 = (y * 2.0d * Math.PI) / 360; // 経度1度の距離(m) + double dLon = m / lon1; // 距離を表す経度(差分) + double minlon = lon - dLon; // 左辺 + double maxlon = lon + dLon; // 右辺 + + System.out.println("緯線上の地球の半径= "+ y); + System.out.println("緯度1秒の長さ(m)= "+ LAT1 / 3600); + System.out.println("経度1秒の長さ(m)= "+ lon1 / 3600); + + return getMap(minlon, minlat, maxlon, maxlat); + } + + public static int getMap(RectArea center) throws MalformedURLException, ProtocolException, IOException { + return getMap(center.minlon, center.minlat, center.maxlon, center.maxlat); + } + + public static int getMap(double minlon, double minlat, double maxlon, double maxlat) throws MalformedURLException, ProtocolException, IOException { + String param = host + "/api/0.6/map" + "?bbox="+ Double.toString(minlon) +","+ Double.toString(minlat) +","+ Double.toString(maxlon) +","+ Double.toString(maxlat); + + System.out.println(param); + URL url = new URL(param); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("GET"); + urlconn.setInstanceFollowRedirects(false); + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.connect(); + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + System.out.println("\n---- ボディ ----"); + + //------------------------------------------ + DocumentBuilderFactory factory; + DocumentBuilder builder; + Node root; + + try { + factory = DocumentBuilderFactory.newInstance(); + builder = factory.newDocumentBuilder(); + factory.setIgnoringElementContentWhitespace(true); + factory.setIgnoringComments(true); + factory.setValidating(true); + root = builder.parse(urlconn.getInputStream()); + + counter = 0; + checkNodes(root); + } catch (ParserConfigurationException e0) { + System.out.println(e0.getMessage()); + } catch (SAXException e1){ + System.out.println(e1.getMessage()); + } catch (IOException e2) { + System.out.println(e2.getMessage()); + } + + System.out.println("バス停数["+ counter +"]"); + urlconn.disconnect(); + + return counter; + } + + public void sendCMD(String api) throws MalformedURLException, ProtocolException, IOException { + System.out.println(host + api); + URL url = new URL(host + api); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("GET"); + urlconn.setInstanceFollowRedirects(false); + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.connect(); + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + System.out.println("\n---- ボディ ----"); + + BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); + + while (true){ + String line = reader.readLine(); + if ( line == null ){ + break; + } + System.out.println(line); + } + + reader.close(); + urlconn.disconnect(); + } + + public static int counter; + public static void checkNodes(Node node) { + if (isBusstop(node)) { + counter++; + showNode(node); + } + + NodeList nodes = node.getChildNodes(); + for (int i=0; i"+ node.getNodeValue()); + + NodeList nodes = node.getChildNodes(); + for (int i=0; i"); + } + + class RectArea { + public double minlon; + public double maxlon; + public double minlat; + public double maxlat; + + /** + * 矩形領域を中心点と中心点からの距離(メートル)でセットする + * + * @param lat // 中心点の緯度 + * @param lon // 中心点の経度 + * @param m // 距離 領域の一辺の長さの半分 + */ + public RectArea(double lat, double lon, int m) { + double BIG_Y = (40000000.0d / 2.0d / Math.PI); // 地球の半径 + double LAT1 = (10000000.0d / 90.0d); // 緯度1度の距離(m) + double dLat = m / LAT1; // 距離を表す緯度(差分) + minlat = lat - dLat; // 底辺(緯度) + maxlat = lat + dLat; // 上辺(緯度) + double y = Math.sin((90.0d - lat) / 180.0d) * BIG_Y; // 緯線上の地球の半径 + double lon1 = y * 2.0d * Math.PI; // 経度1度の距離(m) + double dLon = m / lon1; // 距離を表す経度(差分) + minlon = lon - dLon; // 左辺 + maxlon = lon + dLon; // 右辺 + } + } +} diff --git a/src/osm/jp/api/HttpPOST.java b/src/osm/jp/api/HttpPOST.java index 2b683df..875f0d0 100644 --- a/src/osm/jp/api/HttpPOST.java +++ b/src/osm/jp/api/HttpPOST.java @@ -1,74 +1,74 @@ -package osm.jp.api; - -import java.net.*; -import java.io.*; - -/** - * Java HTTP クライアントサンプル - HttpURLConnection 版 - - * - * @author 68user http://X68000.q-e-d.net/~68user/ - */ -public class HttpPOST { - //public static String host = "http://api06.dev.openstreetmap.org"; - //public static String host = "http://api.openstreetmap.org"; - public static String host = "http://overpass-api.de"; - - public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException { - double minlat = 35.13d; - double maxlat = 35.66d; - double minlon = 138.99d; - double maxlon = 139.79d; - getCapabilities(minlat, maxlat, minlon, maxlon); - } - - public static void getCapabilities(double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException { - System.out.println(host + "/api/interpreter"); - URL url = new URL(host + "/api/interpreter"); - - HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); - urlconn.setRequestMethod("POST"); - urlconn.setDoOutput(true); // POSTのデータを後ろに付ける - urlconn.setInstanceFollowRedirects(false); // 勝手にリダイレクトさせない - urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); - urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8"); - urlconn.connect(); - - // 送信 - PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8"))); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.print(""); - pw.close(); // closeで送信完了 - - System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + - "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); - System.out.println("\n---- ボディ ----"); - - BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); - - File oFile = new File("existing.xml"); - BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8")); - while (true) { - String line = reader.readLine(); - if (line == null) { - break; - } - hw.write(line); - hw.newLine(); - } - hw.close(); - reader.close(); - urlconn.disconnect(); - } -} +package osm.jp.api; + +import java.net.*; +import java.io.*; + +/** + * Java HTTP クライアントサンプル - HttpURLConnection 版 - + * + * @author 68user http://X68000.q-e-d.net/~68user/ + */ +public class HttpPOST { + //public static String host = "http://api06.dev.openstreetmap.org"; + //public static String host = "http://api.openstreetmap.org"; + public static String host = "http://overpass-api.de"; + + public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException { + double minlat = 35.13d; + double maxlat = 35.66d; + double minlon = 138.99d; + double maxlon = 139.79d; + getCapabilities(minlat, maxlat, minlon, maxlon); + } + + public static void getCapabilities(double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException { + System.out.println(host + "/api/interpreter"); + URL url = new URL(host + "/api/interpreter"); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("POST"); + urlconn.setDoOutput(true); // POSTのデータを後ろに付ける + urlconn.setInstanceFollowRedirects(false); // 勝手にリダイレクトさせない + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8"); + urlconn.connect(); + + // 送信 + PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8"))); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.close(); // closeで送信完了 + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + System.out.println("\n---- ボディ ----"); + + BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8")); + + File oFile = new File("existing.xml"); + BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8")); + while (true) { + String line = reader.readLine(); + if (line == null) { + break; + } + hw.write(line); + hw.newLine(); + } + hw.close(); + reader.close(); + urlconn.disconnect(); + } +}