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. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.SQLSyntaxErrorException;
  13.  
  14. /**
  15. * Java HTTP クライアントサンプル - HttpURLConnection 版 -
  16. *
  17. * @author 68user http://X68000.q-e-d.net/~68user/
  18. */
  19. public class HttpPOST {
  20. public static String host = "http://overpass-api.de";
  21. public static final String EXIST_FILE = "exist.osm.xml";
  22. public String tableName = null;
  23. public Connection hsqldb = null;
  24.  
  25. public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException {
  26. double minlat = 35.00d;
  27. double maxlat = 36.00d;
  28. double minlon = 138.00d;
  29. double maxlon = 140.00d;
  30. //getCapabilities(new File("output.xml"), "highway", "bus_stop", minlat, maxlat, minlon, maxlon);
  31. //getCapabilities(new File("output.xml"), "highway", "disused:bus_stop", minlat, maxlat, minlon, maxlon);
  32. //getCapabilities(new File("output.xml"), "amenity", "bus_station", minlat, maxlat, minlon, maxlon);
  33. //getCapabilities(new File("output.xml"), "public_transport", "platform", minlat, maxlat, minlon, maxlon);
  34. //getCapabilities("public_transport", "stop_position", minlat, maxlat, minlon, maxlon, "node");
  35. getCapabilities("amenity", "fuel", minlat, maxlat, minlon, maxlon, "way");
  36. getCapabilities("amenity", "fuel", minlat, maxlat, minlon, maxlon, "node");
  37. }
  38. public HttpPOST(Connection hsqldb, String tableName) {
  39. this.tableName = tableName;
  40. this.hsqldb = hsqldb;
  41. }
  42. /**
  43. * 'HSQLDB.table.OSM_EXIST'を新規に作る
  44. * 'HSQLDB.table.AREA_NODE'を新規に作る
  45. * 既にテーブルが存在する時にはERROR
  46. *
  47. * @throws SQLException
  48. */
  49. public void create() throws SQLException {
  50. String createSt;
  51. sql("DROP TABLE IF EXISTS "+ this.tableName +" CASCADE");
  52. sql("DROP TABLE IF EXISTS AREA_NODE CASCADE");
  53.  
  54. // 'table.FUEL_EXIST'を新規に作る
  55. createSt = "CREATE TABLE "+ this.tableName +" (idref VARCHAR(12) NOT NULL, name VARCHAR(128), lat DOUBLE, lon DOUBLE, score INT, PRIMARY KEY(idref));";
  56. Db.updateSQL(hsqldb, createSt);
  57. createSt = "CREATE INDEX "+ this.tableName +"_index ON "+ this.tableName +" (lat, lon);";
  58. Db.updateSQL(hsqldb, createSt);
  59.  
  60. // 'table.AREA_NODE'を新規に作る
  61. createSt = "CREATE TABLE AREA_NODE (idref VARCHAR(12) NOT NULL, pid VARCHAR(12), lat DOUBLE, lon DOUBLE);";
  62. Db.updateSQL(hsqldb, createSt);
  63. }
  64. public void sql(String sql) throws SQLException {
  65. System.out.println(sql);
  66. try (PreparedStatement ps = this.hsqldb.prepareStatement(sql)) {
  67. ps.executeUpdate();
  68. }
  69. catch (SQLSyntaxErrorException e) {
  70. System.out.println("107:"+ e.toString());
  71. if (!(e.toString().startsWith("java.sql.SQLSyntaxErrorException: user lacks privilege or object not found:"))) {
  72. throw e;
  73. }
  74. }
  75. }
  76.  
  77. public static void getCapabilities(String key, String value, double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException {
  78. getCapabilities(key, value, minLat, maxLat, minLon, maxLon, "node");
  79. }
  80.  
  81. public static void getCapabilities(String key, String value, double minLat, double maxLat, double minLon, double maxLon, String type) throws MalformedURLException, ProtocolException, IOException {
  82. StringBuilder queryText = new StringBuilder();
  83. queryText.append("<osm-script timeout=\"900\" element-limit=\"1073741824\">");
  84. queryText.append(" <union>");
  85. queryText.append(" <query type=\""+ type +"\">");
  86. queryText.append(" <has-kv k=\""+ key +"\" v=\""+ value +"\"/>");
  87. queryText.append(" <bbox-query s=\"" + minLat + "\" n=\"" + maxLat + "\" w=\"" + minLon + "\" e=\"" + maxLon + "\"/>");
  88. queryText.append(" </query>");
  89. queryText.append(" </union>");
  90. queryText.append(" <print/>");
  91. queryText.append("</osm-script>");
  92. getQuery(queryText.toString());
  93. }
  94.  
  95. /**
  96. *
  97. * @param queryText クエリテキスト(Overpass_API/Overpass_QL)
  98. * @throws MalformedURLException
  99. * @throws ProtocolException
  100. * @throws IOException
  101. */
  102. public static void getQuery(String queryText) throws MalformedURLException, ProtocolException, IOException {
  103. System.out.println(host + "/api/interpreter");
  104. URL url = new URL(host + "/api/interpreter");
  105. int responsecode = 0;
  106.  
  107. do {
  108. HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
  109. try {
  110. urlconn.setRequestMethod("POST");
  111. urlconn.setDoOutput(true); // POSTのデータを後ろに付ける
  112. urlconn.setInstanceFollowRedirects(false); // 勝手にリダイレクトさせない
  113. urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3");
  114. urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8");
  115. urlconn.connect();
  116.  
  117. try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8")))) {
  118. outputWriter(pw, queryText);
  119. pw.flush();
  120. }
  121.  
  122. System.out.println("Sleep 20 sec...");
  123. try {
  124. TimeUnit.SECONDS.sleep(20);
  125. } catch (InterruptedException e) {}
  126.  
  127. responsecode = urlconn.getResponseCode();
  128. System.out.println("レスポンスコード[" + responsecode + "] " +
  129. "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]");
  130. Map<String,List<String>> headers = urlconn.getHeaderFields();
  131. for (Map.Entry<String, List<String>> bar : headers.entrySet()) {
  132. System.out.print("\t" + bar.getKey() +"\t: "); // キーを取得
  133. List<String> vals = bar.getValue(); // 値を取得
  134. for(String str : vals) {
  135. System.out.print("["+ str +"],");
  136. }
  137. System.out.println();
  138. }
  139. if ((responsecode == 429) || (responsecode == 504) || (responsecode == 500)) {
  140. // レスポンスコード[429] レスポンスメッセージ[Too Many Requests]
  141. // レスポンスコード[500] レスポンスメッセージ[Internal server error]
  142. // レスポンスコード[504] レスポンスメッセージ[Gateway Timeout]
  143. System.out.print("Waite 5 minites.");
  144. try {
  145. TimeUnit.MINUTES.sleep(5);
  146. } catch (InterruptedException e) {}
  147. }
  148. else {
  149. System.out.println("\n---- ボディ ----");
  150.  
  151. File oFile = new File(HttpPOST.EXIST_FILE);
  152. oFile.deleteOnExit();
  153. try (
  154. BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8"));
  155. BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"))
  156. ) {
  157. while (true) {
  158. String line = reader.readLine();
  159. if (line == null) {
  160. break;
  161. }
  162. hw.write(line);
  163. hw.newLine();
  164. }
  165. hw.flush();
  166. }
  167. }
  168. }
  169. catch (java.net.ConnectException e) {
  170. // レスポンスコード[600] レスポンスメッセージ[接続がタイムアウトしました (Connection timed out)]
  171. responsecode = 600;
  172. }
  173. finally {
  174. urlconn.disconnect();
  175. }
  176. System.out.print("Waite 5 seconds.");
  177. try {
  178. TimeUnit.SECONDS.sleep(5);
  179. } catch (InterruptedException e) {}
  180. }
  181. while ((responsecode == 429) || (responsecode == 504) || (responsecode == 600));
  182. }
  183. public static void outputWriter(PrintWriter pw, String text) {
  184. System.out.println("\t" + text);
  185. pw.print(text);
  186. }
  187. public static final int POINT_NO = 0; // 評価ポイント無し→ score=50
  188. public static final int POINT_FIXME = 1; // 評価ポイント無し→ score=50
  189. public static final int POINT_BRAND = 2; // 評価ポイント|brand=null → score=1
  190. public static final int POINT_NAME = 4; // 評価ポイント|name=null → score=1
  191. /**
  192. * File(HttpPOST.EXIST_FILE)を読み取って、データベースに反映させる。<br>
  193. * その際に、OSMノードを評価し、scoreを算定する
  194. * param con 反映先のデータベースコネクタ(HSQLDB)
  195. * param point 評価ポイント[POINT_NO|POINT_BRAND|POINT_NAME] 1: 'brand' 2:'name'
  196. */
  197.  
  198.  
  199. /**
  200. *
  201. * @param con
  202. * @param idref
  203. * @return
  204. * @throws SQLException
  205. */
  206. public static Position getNdPosition(Connection con, String idref) throws SQLException {
  207. PreparedStatement ps8 = con.prepareStatement("SELECT lat,lon FROM AREA_NODE where idref=?");
  208. ps8.setString(1, idref);
  209. try (ResultSet rset8 = ps8.executeQuery()) {
  210. while (rset8.next()) {
  211. Double lat = rset8.getDouble(1);
  212. Double lon = rset8.getDouble(2);
  213. return new Position(lat,lon);
  214. }
  215. }
  216. return null;
  217. }
  218.  
  219. }