diff --git a/src/osm/jp/api/HttpPOST.java b/src/osm/jp/api/HttpPOST.java index a809e6b..76c91e8 100644 --- a/src/osm/jp/api/HttpPOST.java +++ b/src/osm/jp/api/HttpPOST.java @@ -1,6 +1,9 @@ package osm.jp.api; import java.net.*; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; import java.io.*; /** @@ -25,11 +28,30 @@ getCapabilities(new File("output.xml"), "public_transport", "stop_position", minlat, maxlat, minlon, maxlon); } + public static void getJapanCapabilities(File oFile, String key, String value) throws MalformedURLException, ProtocolException, IOException { + if (oFile.isFile()) { + oFile.delete(); + } + + BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8")); + + for (int i=0; i < all.length; i++) { + getCapabilities(hw, key, value, all[i].minLat, all[i].maxLat, all[i].minLon, all[i].maxLon); + } + hw.close(); + } + 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(); } + BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8")); + getCapabilities(hw, key, value, minLat, maxLat, minLon, maxLon); + hw.close(); + } + + public static void getCapabilities(BufferedWriter hw, String key, String value, 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"); @@ -47,20 +69,33 @@ outputWriter(pw, " "); outputWriter(pw, " "); outputWriter(pw, " "); - outputWriter(pw, " "); + outputWriter(pw, " "); outputWriter(pw, " "); outputWriter(pw, " "); outputWriter(pw, " "); outputWriter(pw, ""); pw.close(); // closeで送信完了 + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + } + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + Map> headers = urlconn.getHeaderFields(); + for (Map.Entry> bar : headers.entrySet()) { + System.out.print("\t" + bar.getKey() +"\t: "); // キーを取得 + List vals = bar.getValue(); // 値を取得 + for(String str : vals) { + System.out.print("["+ str +"],"); + } + System.out.println(); + } + System.out.println("\n---- ボディ ----"); - + BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8")); - - BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8")); while (true) { String line = reader.readLine(); if (line == null) { @@ -69,10 +104,151 @@ hw.write(line); hw.newLine(); } - hw.close(); reader.close(); urlconn.disconnect(); + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException e) { + } } + + public static Japan[] all = new Japan[] { + new Japan(140D, 45D, 1D), + new Japan(141D, 45D, 1D), + new Japan(142D, 45D, 1D), + + new Japan(140D, 44D, 1D), + new Japan(141D, 44D, 1D), + new Japan(142D, 44D, 1D), + new Japan(143D, 44D, 1D), + new Japan(144D, 44D, 1D), + new Japan(145D, 44D, 1D), + + new Japan(140D, 43D, 1D), + new Japan(141D, 43D, 1D), + new Japan(142D, 43D, 1D), + new Japan(143D, 43D, 1D), + new Japan(144.0D, 43.0D, 0.5D), + new Japan(144.5D, 43.0D, 0.5D), + new Japan(144.0D, 43.5D, 0.5D), + new Japan(144.5D, 43.5D, 0.5D), + new Japan(145D, 43D, 1D), + + new Japan(139D, 42D, 1D), + new Japan(140D, 42D, 1D), + new Japan(141D, 42D, 1D), + new Japan(142D, 42D, 1D), + new Japan(143D, 42D, 1D), + new Japan(144D, 42D, 1D), + new Japan(145D, 42D, 1D), + + new Japan(139D, 41D, 1D), + new Japan(140D, 41D, 1D), + new Japan(141D, 41D, 1D), + + new Japan(139D, 40D, 1D), + new Japan(140D, 40D, 1D), + new Japan(141D, 40D, 1D), + + new Japan(139D, 39D, 1D), + new Japan(140D, 39D, 1D), + new Japan(141D, 39D, 1D), + new Japan(142D, 39D, 1D), + + new Japan(138D, 38D, 1D), + new Japan(139D, 38D, 1D), + new Japan(140D, 38D, 1D), + new Japan(141D, 38D, 1D), + + new Japan(136D, 37D, 1D), + new Japan(137D, 37D, 1D), + new Japan(138D, 37D, 1D), + new Japan(139D, 37D, 1D), + new Japan(140D, 37D, 1D), + new Japan(141D, 37D, 1D), + + new Japan(132D, 36D, 1D), + new Japan(133D, 36D, 1D), + new Japan(135D, 36D, 1D), + new Japan(136D, 36D, 1D), + new Japan(137D, 36D, 1D), + new Japan(138D, 36D, 1D), + new Japan(139D, 36D, 1D), + new Japan(140D, 36D, 1D), + + new Japan(132D, 35D, 1D), + new Japan(133D, 35D, 1D), + new Japan(134D, 35D, 1D), + new Japan(135D, 35D, 1D), + new Japan(136D, 35D, 1D), + new Japan(137D, 35D, 1D), + new Japan(138D, 35D, 1D), + new Japan(139D, 35D, 1D), + new Japan(140D, 35D, 1D), + + new Japan(129D, 34D, 1D), + new Japan(130D, 34D, 1D), + new Japan(131D, 34D, 1D), + new Japan(132D, 34D, 1D), + new Japan(133D, 34D, 1D), + new Japan(134D, 34D, 1D), + new Japan(135D, 34D, 1D), + new Japan(136D, 34D, 1D), + new Japan(137D, 34D, 1D), + new Japan(138D, 34D, 1D), + new Japan(139D, 34D, 1D), + + new Japan(129D, 33D, 1D), + new Japan(130D, 33D, 1D), + new Japan(131D, 33D, 1D), + new Japan(132D, 33D, 1D), + new Japan(133D, 33D, 1D), + new Japan(134D, 33D, 1D), + new Japan(135D, 33D, 1D), + new Japan(136D, 33D, 1D), + new Japan(139D, 33D, 1D), + + new Japan(128D, 32D, 1D), + new Japan(129D, 32D, 1D), + new Japan(130D, 32D, 1D), + new Japan(131D, 32D, 1D), + new Japan(132D, 32D, 1D), + new Japan(133D, 32D, 1D), + new Japan(139D, 32D, 1D), + + new Japan(129D, 31D, 1D), + new Japan(130D, 31D, 1D), + new Japan(131D, 31D, 1D), + + new Japan(129D, 30D, 1D), + new Japan(130D, 30D, 1D), + new Japan(131D, 30D, 1D), + + new Japan(128D, 29D, 1D), + new Japan(129D, 29D, 1D), + + new Japan(128D, 28D, 1D), + new Japan(129D, 28D, 1D), + + new Japan(127D, 27D, 1D), + new Japan(128D, 27D, 1D), + new Japan(129D, 27D, 1D), + + new Japan(126D, 26D, 1D), + new Japan(127D, 26D, 1D), + new Japan(128D, 26D, 1D), + new Japan(129D, 26D, 1D), + + new Japan(125D, 25D, 1D), + + new Japan(123D, 24D, 1D), + new Japan(124D, 24D, 1D), + new Japan(125D, 24D, 1D), + + // 小笠原諸島 + new Japan(142D, 27D, 1D), + new Japan(142D, 26D, 1D) + }; public static void outputWriter(PrintWriter pw, String text) { System.out.println("\t" + text); diff --git a/src/osm/jp/api/Japan.java b/src/osm/jp/api/Japan.java new file mode 100644 index 0000000..3127b41 --- /dev/null +++ b/src/osm/jp/api/Japan.java @@ -0,0 +1,22 @@ +package osm.jp.api; + +public class Japan { + public double minLat; + public double maxLat; + public double minLon; + public double maxLon; + + public Japan(double minLat, double maxLat, double minLon, double maxLon) { + this.minLat = minLat; + this.maxLat = maxLat; + this.minLon = minLon; + this.maxLon = maxLon; + } + + public Japan(double minLat, double minLon, double delta) { + this.minLat = minLat; + this.maxLat = minLat + delta; + this.minLon = minLon; + this.maxLon = minLon + delta; + } +} diff --git a/src/osm/jp/coverage/busstop/Busstop.java b/src/osm/jp/coverage/busstop/Busstop.java index aea9f91..caf5900 100644 --- a/src/osm/jp/coverage/busstop/Busstop.java +++ b/src/osm/jp/coverage/busstop/Busstop.java @@ -63,6 +63,23 @@ try { /** + * 既存のOSMバス停を読み込む + * OSM OverPassAPI を使って、既存のOSMバス停のデータを取得して、「existing.xml」に出力する + * --> 'existing.xml' + */ + File existingFile = new File("existing.xml"); + HttpPOST.getJapanCapabilities(existingFile, "highway", "bus_stop"); + readExistingFile(con, existingFile); + HttpPOST.getJapanCapabilities(existingFile, "highway", "disused:bus_stop"); + readExistingFile(con, existingFile); + HttpPOST.getJapanCapabilities(existingFile, "amenity", "bus_station"); + readExistingFile(con, existingFile); + HttpPOST.getJapanCapabilities(existingFile, "public_transport", "platform"); + readExistingFile(con, existingFile); + HttpPOST.getJapanCapabilities(existingFile, "public_transport", "stop_position"); + readExistingFile(con, existingFile); + + /** * 都道府県ごとのGMLディレクトリの処理 */ int fcounter = 0; @@ -112,6 +129,8 @@ conPost = DatabaseTool.openDb("postgis"); } + + File[] files = gmldir.listFiles(); try { for (File iFile : files) { @@ -125,67 +144,8 @@ inputFile(con, iFile, areacode); } - /** - * 既存のOSMバス停を読み込む - * --> 'existing.xml' - */ - File existingFile = new File(gmldir, String.format("existing_%02d.xml", areacode)); if (true) { - /** - * インポートしたデータの緯度経度範囲を読み取る - */ - double maxLat = -90.0D; - double minLat = 90.0D; - double maxLon = -180.0D; - double minLon = 180.0D; - PreparedStatement ps8; - if (Busstop.update) { - ps8 = conPost.prepareStatement("SELECT gid,name,fixed,ST_X(geom) lon, ST_Y(geom) lat FROM t_busstop WHERE area=?"); - ps8.setInt(1, areacode); - } - else { - 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」に出力する - */ - System.out.println("AREA: "+ areacode); - 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) { ps1 = conPost.prepareStatement("SELECT gid,name,ST_Y(geom) lat,ST_X(geom) lon,fixed FROM t_busstop WHERE area=?"); @@ -294,7 +254,7 @@ return values; } - public static void readExistingFile (Connection con, File existingFile, int areacode) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + public static void readExistingFile (Connection con, File existingFile) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { int iCounter = 0; DocumentBuilderFactory factory; @@ -309,11 +269,11 @@ factory.setValidating(true); root = builder.parse(existingFile); - iCounter += readExistingNodes(con, root, areacode); + iCounter += readExistingNodes(con, root); System.out.println("既存バス停数["+ iCounter +"]"); } - static int readExistingNodes(Connection con, Node node, int areacode) throws IOException, SQLException { + static int readExistingNodes(Connection con, Node node) throws IOException, SQLException { int iCounter = 0; NodeList nodes = node.getChildNodes(); @@ -321,16 +281,16 @@ Node node2 = nodes.item(i); if (node2.getNodeName().equals("node")) { iCounter++; - importExistingNode(con, node2, areacode); + importExistingNode(con, node2); } else { - iCounter += readExistingNodes(con, node2, areacode); + iCounter += readExistingNodes(con, node2); } } return iCounter; } - static void importExistingNode(Connection con, Node node, int areacode) throws IOException, SQLException { + static void importExistingNode(Connection con, Node node) throws IOException, SQLException { String idrefStr = ""; String latStr = ""; String lonStr = "";