Newer
Older
osmCoverage / src / osm / jp / api / HttpPOST.java
  1. package osm.jp.api;
  2.  
  3. import java.net.*;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.TimeUnit;
  7. import java.io.*;
  8.  
  9. /**
  10. * Java HTTP クライアントサンプル - HttpURLConnection 版 -
  11. *
  12. * @author 68user http://X68000.q-e-d.net/~68user/
  13. */
  14. public class HttpPOST {
  15. //public static String host = "http://api06.dev.openstreetmap.org";
  16. //public static String host = "http://api.openstreetmap.org";
  17. public static String host = "http://overpass-api.de";
  18. public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException {
  19. double minlat = 35.13d;
  20. double maxlat = 35.66d;
  21. double minlon = 138.99d;
  22. double maxlon = 139.79d;
  23. getCapabilities(new File("output.xml"), "highway", "bus_stop", minlat, maxlat, minlon, maxlon);
  24. getCapabilities(new File("output.xml"), "highway", "disused:bus_stop", minlat, maxlat, minlon, maxlon);
  25. getCapabilities(new File("output.xml"), "amenity", "bus_station", minlat, maxlat, minlon, maxlon);
  26. getCapabilities(new File("output.xml"), "public_transport", "platform", minlat, maxlat, minlon, maxlon);
  27. getCapabilities(new File("output.xml"), "public_transport", "stop_position", minlat, maxlat, minlon, maxlon);
  28. }
  29. public static void getCapabilities(File oFile, String key, String value, double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException {
  30. if (oFile.isFile()) {
  31. oFile.delete();
  32. }
  33.  
  34. BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8"));
  35. getCapabilities(hw, key, value, minLat, maxLat, minLon, maxLon);
  36. hw.close();
  37. }
  38. public static void getCapabilities(BufferedWriter hw, String key, String value, double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException {
  39. System.out.println(host + "/api/interpreter");
  40. URL url = new URL(host + "/api/interpreter");
  41. int responsecode = 0;
  42.  
  43. do {
  44. HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
  45. urlconn.setRequestMethod("POST");
  46. urlconn.setDoOutput(true); // POSTのデータを後ろに付ける
  47. urlconn.setInstanceFollowRedirects(false); // 勝手にリダイレクトさせない
  48. urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3");
  49. urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8");
  50. urlconn.connect();
  51.  
  52. // 送信
  53. PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8")));
  54. outputWriter(pw, "<osm-script timeout=\"900\" element-limit=\"1073741824\">");
  55. outputWriter(pw, " <union>");
  56. outputWriter(pw, " <query type=\"node\">");
  57. outputWriter(pw, " <has-kv k=\""+ key +"\" v=\""+ value +"\"/>");
  58. outputWriter(pw, " <bbox-query s=\"" + minLon + "\" n=\"" + maxLon + "\" w=\"" + minLat + "\" e=\"" + maxLat + "\"/>");
  59. outputWriter(pw, " </query>");
  60. outputWriter(pw, " </union>");
  61. outputWriter(pw, " <print/>");
  62. outputWriter(pw, "</osm-script>");
  63. pw.close(); // closeで送信完了
  64. try {
  65. TimeUnit.SECONDS.sleep(1);
  66. } catch (InterruptedException e) {}
  67.  
  68. responsecode = urlconn.getResponseCode();
  69. System.out.println("レスポンスコード[" + responsecode + "] " +
  70. "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]");
  71. Map<String,List<String>> headers = urlconn.getHeaderFields();
  72. for (Map.Entry<String, List<String>> bar : headers.entrySet()) {
  73. System.out.print("\t" + bar.getKey() +"\t: "); // キーを取得
  74. List<String> vals = bar.getValue(); // 値を取得
  75. for(String str : vals) {
  76. System.out.print("["+ str +"],");
  77. }
  78. System.out.println();
  79. }
  80. if ((responsecode == 429) || (responsecode == 504) || (responsecode == 500)) {
  81. // レスポンスコード[429] レスポンスメッセージ[Too Many Requests]
  82. // レスポンスコード[500] レスポンスメッセージ[Internal server error]
  83. // レスポンスコード[504] レスポンスメッセージ[Gateway Timeout]
  84. try {
  85. TimeUnit.MINUTES.sleep(5);
  86. } catch (InterruptedException e) {}
  87. }
  88. else {
  89. System.out.println("\n---- ボディ ----");
  90. BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
  91. while (true) {
  92. String line = reader.readLine();
  93. if (line == null) {
  94. break;
  95. }
  96. hw.write(line);
  97. hw.newLine();
  98. }
  99. reader.close();
  100. }
  101. urlconn.disconnect();
  102. try {
  103. TimeUnit.SECONDS.sleep(5);
  104. } catch (InterruptedException e) {}
  105. }
  106. while ((responsecode == 429) || (responsecode == 504));
  107. }
  108.  
  109. public static void outputWriter(PrintWriter pw, String text) {
  110. System.out.println("\t" + text);
  111. pw.print(text);
  112. }
  113. }