Newer
Older
osmCoverage / src / osm / jp / api / HttpPOST.java
@hayashi hayashi on 25 Sep 2017 6 KB Caverage_fuel 完成
  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 final String EXIST_FILE = "exist.osm.xml";
  19.  
  20. public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException {
  21. double minlat = 35.13d;
  22. double maxlat = 35.66d;
  23. double minlon = 138.99d;
  24. double maxlon = 139.79d;
  25. //getCapabilities(new File("output.xml"), "highway", "bus_stop", minlat, maxlat, minlon, maxlon);
  26. //getCapabilities(new File("output.xml"), "highway", "disused:bus_stop", minlat, maxlat, minlon, maxlon);
  27. //getCapabilities(new File("output.xml"), "amenity", "bus_station", minlat, maxlat, minlon, maxlon);
  28. //getCapabilities(new File("output.xml"), "public_transport", "platform", minlat, maxlat, minlon, maxlon);
  29. getCapabilities("public_transport", "stop_position", minlat, maxlat, minlon, maxlon, "node");
  30. getCapabilities("amenity", "fuel", minlat, maxlat, minlon, maxlon, "way");
  31. }
  32. public static void getCapabilities(String key, String value, double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException {
  33. getCapabilities(key, value, minLat, maxLat, minLon, maxLon, "node");
  34. }
  35.  
  36. public static void getCapabilities(String key, String value, double minLat, double maxLat, double minLon, double maxLon, String type) throws MalformedURLException, ProtocolException, IOException {
  37. StringBuilder queryText = new StringBuilder();
  38. queryText.append("<osm-script timeout=\"900\" element-limit=\"1073741824\">");
  39. queryText.append(" <union>");
  40. queryText.append(" <query type=\""+ type +"\">");
  41. queryText.append(" <has-kv k=\""+ key +"\" v=\""+ value +"\"/>");
  42. queryText.append(" <bbox-query s=\"" + minLat + "\" n=\"" + maxLat + "\" w=\"" + minLon + "\" e=\"" + maxLon + "\"/>");
  43. queryText.append(" </query>");
  44. queryText.append(" </union>");
  45. queryText.append(" <print/>");
  46. queryText.append("</osm-script>");
  47. getQuery(queryText.toString());
  48. }
  49.  
  50. /**
  51. *
  52. * @param queryText クエリテキスト(Overpass_API/Overpass_QL)
  53. * @throws MalformedURLException
  54. * @throws ProtocolException
  55. * @throws IOException
  56. */
  57. public static void getQuery(String queryText) throws MalformedURLException, ProtocolException, IOException {
  58. System.out.println(host + "/api/interpreter");
  59. URL url = new URL(host + "/api/interpreter");
  60. int responsecode = 0;
  61.  
  62. do {
  63. HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
  64. urlconn.setRequestMethod("POST");
  65. urlconn.setDoOutput(true); // POSTのデータを後ろに付ける
  66. urlconn.setInstanceFollowRedirects(false); // 勝手にリダイレクトさせない
  67. urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3");
  68. urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8");
  69. urlconn.connect();
  70.  
  71. try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8")))) {
  72. outputWriter(pw, queryText);
  73. pw.flush();
  74. }
  75.  
  76. try {
  77. TimeUnit.SECONDS.sleep(1);
  78. } catch (InterruptedException e) {}
  79.  
  80. responsecode = urlconn.getResponseCode();
  81. System.out.println("レスポンスコード[" + responsecode + "] " +
  82. "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]");
  83. Map<String,List<String>> headers = urlconn.getHeaderFields();
  84. for (Map.Entry<String, List<String>> bar : headers.entrySet()) {
  85. System.out.print("\t" + bar.getKey() +"\t: "); // キーを取得
  86. List<String> vals = bar.getValue(); // 値を取得
  87. for(String str : vals) {
  88. System.out.print("["+ str +"],");
  89. }
  90. System.out.println();
  91. }
  92. if ((responsecode == 429) || (responsecode == 504) || (responsecode == 500)) {
  93. // レスポンスコード[429] レスポンスメッセージ[Too Many Requests]
  94. // レスポンスコード[500] レスポンスメッセージ[Internal server error]
  95. // レスポンスコード[504] レスポンスメッセージ[Gateway Timeout]
  96. try {
  97. TimeUnit.MINUTES.sleep(5);
  98. } catch (InterruptedException e) {}
  99. }
  100. else {
  101. System.out.println("\n---- ボディ ----");
  102. File oFile = new File(HttpPOST.EXIST_FILE);
  103. oFile.deleteOnExit();
  104. try (BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8"))) {
  105. try (BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"))) {
  106. while (true) {
  107. String line = reader.readLine();
  108. if (line == null) {
  109. break;
  110. }
  111. hw.write(line);
  112. hw.newLine();
  113. }
  114. hw.flush();
  115. }
  116. }
  117. }
  118. urlconn.disconnect();
  119. try {
  120. TimeUnit.SECONDS.sleep(5);
  121. } catch (InterruptedException e) {}
  122. }
  123. while ((responsecode == 429) || (responsecode == 504));
  124. }
  125. public static void outputWriter(PrintWriter pw, String text) {
  126. System.out.println("\t" + text);
  127. pw.print(text);
  128. }
  129. }