OSDN Git Service

2b683dfa39367fa65e97952a2d8e356a073f8b40
[convbusstop/convbusstop.git] / src / osm / jp / api / HttpPOST.java
1 package osm.jp.api;
2
3 import java.net.*;
4 import java.io.*;
5
6 /**
7  * Java HTTP クライアントサンプル - HttpURLConnection 版 -
8  *
9  * @author 68user http://X68000.q-e-d.net/~68user/
10  */
11 public class HttpPOST {
12         //public static String host = "http://api06.dev.openstreetmap.org";
13         //public static String host = "http://api.openstreetmap.org";
14         public static String host = "http://overpass-api.de";
15         
16         public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException {
17                 double minlat = 35.13d;
18                 double maxlat = 35.66d;
19         double minlon = 138.99d;
20                 double maxlon = 139.79d;
21         getCapabilities(minlat, maxlat, minlon, maxlon);
22     }
23         
24         public static void getCapabilities(double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException {
25                 System.out.println(host + "/api/interpreter");
26         URL url = new URL(host + "/api/interpreter");
27
28         HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
29         urlconn.setRequestMethod("POST");
30         urlconn.setDoOutput(true);                              // POSTのデータを後ろに付ける
31         urlconn.setInstanceFollowRedirects(false);              // 勝手にリダイレクトさせない
32         urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3");
33         urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8");
34         urlconn.connect();
35
36         // 送信
37         PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8")));
38         pw.print("<osm-script timeout=\"900\" element-limit=\"1073741824\">");
39         pw.print("<union>");
40         pw.print("<query type=\"node\">");
41         pw.print("<has-kv k=\"highway\" v=\"bus_stop\"/>");
42         pw.print("<bbox-query s=\"" + minLat + "\" n=\"" + maxLat + "\" w=\"" + minLon + "\" e=\"" + maxLon + "\"/>");
43         pw.print("</query>");
44         pw.print("<query type=\"node\">");
45         pw.print("<has-kv k=\"public_transport\" v=\"platform\"/>");
46         pw.print("<has-kv k=\"bus\" v=\"yes\"/>");
47         pw.print("<bbox-query s=\"" + minLat + "\" n=\"" + maxLat + "\" w=\"" + minLon + "\" e=\"" + maxLon + "\"/>");
48         pw.print("</query>");
49         pw.print("</union>");
50         pw.print("<print/>");
51         pw.print("</osm-script>");
52         pw.close();                     // closeで送信完了
53         
54         System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " +
55                            "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]");
56         System.out.println("\n---- ボディ ----");
57
58         BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
59
60                 File oFile = new File("existing.xml");
61                 BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8"));
62         while (true) {
63             String line = reader.readLine();
64             if (line == null) {
65                 break;
66             }
67                 hw.write(line);
68                 hw.newLine();
69         }
70         hw.close();
71         reader.close();
72         urlconn.disconnect();
73         }
74 }