diff --git a/src/osm/jp/api/HttpPOST.java b/src/osm/jp/api/HttpPOST.java index c354073..a809e6b 100644 --- a/src/osm/jp/api/HttpPOST.java +++ b/src/osm/jp/api/HttpPOST.java @@ -26,6 +26,10 @@ } public static void getCapabilities(File oFile, String key, String value, double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException { + if (oFile.isFile()) { + oFile.delete(); + } + System.out.println(host + "/api/interpreter"); URL url = new URL(host + "/api/interpreter"); diff --git a/src/osm/jp/coverage/busstop/Busstop.java b/src/osm/jp/coverage/busstop/Busstop.java index 287fbcc..aea9f91 100644 --- a/src/osm/jp/coverage/busstop/Busstop.java +++ b/src/osm/jp/coverage/busstop/Busstop.java @@ -129,15 +129,8 @@ * 既存のOSMバス停を読み込む * --> 'existing.xml' */ - /* - * 既存のOSMデータファイルがなければ、新たにダウンロードする。 - * OSMデータファイルがあるときは、ダウンロードしないでそれを使う。 - */ File existingFile = new File(gmldir, String.format("existing_%02d.xml", areacode)); - if (!existingFile.isFile() || Busstop.update) { - if (existingFile.isFile()) { - existingFile.delete(); - } + if (true) { /** * インポートしたデータの緯度経度範囲を読み取る @@ -178,8 +171,20 @@ * OSM OverPassAPI を使って、既存のOSMバス停のデータを取得して、「existing.xml」に出力する */ System.out.println("AREA: "+ areacode); - HttpPOST.getCapabilities(existingFile, minLat, maxLat, minLon, maxLon); - readExistingFile(con, existingFile); + HttpPOST.getCapabilities(existingFile, "highway", "bus_stop", minLat, maxLat, minLon, maxLon); + readExistingFile(con, existingFile, areacode); + System.out.println("AREA: "+ areacode); + HttpPOST.getCapabilities(existingFile, "highway", "disused:bus_stop", minLat, maxLat, minLon, maxLon); + readExistingFile(con, existingFile, areacode); + System.out.println("AREA: "+ areacode); + HttpPOST.getCapabilities(existingFile, "amenity", "bus_station", minLat, maxLat, minLon, maxLon); + readExistingFile(con, existingFile, areacode); + System.out.println("AREA: "+ areacode); + HttpPOST.getCapabilities(existingFile, "public_transport", "platform", minLat, maxLat, minLon, maxLon); + readExistingFile(con, existingFile, areacode); + System.out.println("AREA: "+ areacode); + HttpPOST.getCapabilities(existingFile, "public_transport", "stop_position", minLat, maxLat, minLon, maxLon); + readExistingFile(con, existingFile, areacode); PreparedStatement ps1; if (Busstop.update) { @@ -289,7 +294,7 @@ return values; } - public static void readExistingFile (Connection con, File existingFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + public static void readExistingFile (Connection con, File existingFile, int areacode) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { int iCounter = 0; DocumentBuilderFactory factory; @@ -304,11 +309,11 @@ factory.setValidating(true); root = builder.parse(existingFile); - iCounter += readExistingNodes(con, root); + iCounter += readExistingNodes(con, root, areacode); System.out.println("既存バス停数["+ iCounter +"]"); } - static int readExistingNodes(Connection con, Node node) throws IOException, SQLException { + static int readExistingNodes(Connection con, Node node, int areacode) throws IOException, SQLException { int iCounter = 0; NodeList nodes = node.getChildNodes(); @@ -316,16 +321,16 @@ Node node2 = nodes.item(i); if (node2.getNodeName().equals("node")) { iCounter++; - importExistingNode(con, node2); + importExistingNode(con, node2, areacode); } else { - iCounter += readExistingNodes(con, node2); + iCounter += readExistingNodes(con, node2, areacode); } } return iCounter; } - static void importExistingNode(Connection con, Node node) throws IOException, SQLException { + static void importExistingNode(Connection con, Node node, int areacode) throws IOException, SQLException { String idrefStr = ""; String latStr = ""; String lonStr = ""; @@ -384,15 +389,33 @@ } // idref と nameStr をデータベースに格納する - System.out.println("import existing_data : "+ idrefStr +" ("+ latStr +","+ lonStr+")["+ Integer.toString(score) +"]"+ nameStr); - PreparedStatement ps5 = con.prepareStatement("INSERT INTO existing_data (idref,lat,lon, name, score) VALUES (?,?,?,?,?)"); - ps5.setString(1, idrefStr); - ps5.setDouble(2, Double.parseDouble(latStr)); - ps5.setDouble(3, Double.parseDouble(lonStr)); - ps5.setString(4, nameStr); - ps5.setInt(5, score); - ps5.executeUpdate(); - ps5.close(); + PreparedStatement ps1 = con.prepareStatement("SELECT name,score FROM existing_data WHERE idref=?"); + ps1.setString(1, idrefStr); + ResultSet rset1 = ps1.executeQuery(); + if (rset1.next()) { + int fixed = rset1.getInt("score"); + if (fixed < score) { + System.out.println("import existing_data : [id:"+ idrefStr +"] score="+ Integer.toString(score) +" "+ nameStr); + PreparedStatement ps5 = con.prepareStatement("UPDATE existing_data SET score=? WHERE idref=?"); + ps5.setInt(1, score); + ps5.setString(2, idrefStr); + ps5.executeUpdate(); + ps5.close(); + } + } + else { + System.out.println("import existing_data : "+ idrefStr +" ("+ latStr +","+ lonStr+")["+ Integer.toString(score) +"]"+ nameStr); + PreparedStatement ps5 = con.prepareStatement("INSERT INTO existing_data (idref,lat,lon, name, score) VALUES (?,?,?,?,?)"); + ps5.setString(1, idrefStr); + ps5.setDouble(2, Double.parseDouble(latStr)); + ps5.setDouble(3, Double.parseDouble(lonStr)); + ps5.setString(4, nameStr); + ps5.setInt(5, score); + ps5.executeUpdate(); + ps5.close(); + } + rset1.close(); + ps1.close(); } } diff --git a/src/osm/jp/postgis/Do_sqlfiles.java b/src/osm/jp/postgis/Do_sqlfiles.java index a1d8d02..7768e6d 100644 --- a/src/osm/jp/postgis/Do_sqlfiles.java +++ b/src/osm/jp/postgis/Do_sqlfiles.java @@ -5,8 +5,11 @@ import java.sql.Statement; import jp.co.areaweb.tools.database.*; +import osm.jp.coverage.busstop.Busstop; public class Do_sqlfiles { + static String sqlFileName = "busstop.sql"; + /** * メイン * @@ -22,6 +25,12 @@ * @throws ParserConfigurationException */ public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException { + for (int i=0; i < args.length; i++) { + if (args[i].equals("-update")) { + sqlFileName = "update.sql"; + } + } + Connection con = DatabaseTool.openDb("postgis"); try { @@ -104,7 +113,7 @@ */ static boolean checkFile(File f) { String name = f.getName(); - if (name.equals("busstop.sql")) { + if (name.equals(sqlFileName)) { return true; } return false;