diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14076aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +*.class +*.zip +bin/ +classes/ +sample/ +dest/ +**/ConvBusstop.jar + +P*-*_GML/ + +# ============ # +# Eclipse # +# ============ # +.externalToolBuilders/ +.metadata +.settings +.project +.classpath + +# ============ # +# OS generated # +# ============ # +.DS_Store* +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +[Tt]humbs.db diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..42a8bb9 --- /dev/null +++ b/build.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/doc/sql.txt b/doc/sql.txt new file mode 100644 index 0000000..4514cb2 --- /dev/null +++ b/doc/sql.txt @@ -0,0 +1,22 @@ +CREATE TABLE t_busstop ( + gid SERIAL PRIMARY KEY, + osm_id bigint, + name text, + geom GEOMETRY(POINT, 4612) +); +CREATE INDEX ix_busstop_geom ON t_busstop USING GiST (geom); + +insert into t_busstop(osm_id,name,geom) select osm_id, st_transform(way,4612) FROM planet_osm_point WHERE (highway='bus_stop'); +insert into t_busstop(osm_id,name,geom) VALUES(884, 'なまえ', ST_GeomFromText('POINT(139.4110836 35.4341053)', 4612)); + +---- +CREATE TABLE t_test ( + gid SERIAL PRIMARY KEY, + osm_id bigint, + name text, + geom GEOMETRY(POINT, 4612) +); + +CREATE INDEX ix_test_geom ON t_test USING GiST (geom); + +insert into t_test(osm_id,name,geom) VALUES(884, 'なまえ', ST_GeomFromText('POINT(139.4110836 35.4341053)', 4612)); diff --git a/git.sh b/git.sh new file mode 100755 index 0000000..09a2b5b --- /dev/null +++ b/git.sh @@ -0,0 +1,2 @@ +cd ~/workspace/convbusstop +gitk diff --git a/lib/hayashi_0225.jar b/lib/hayashi_0225.jar new file mode 100644 index 0000000..00ad366 --- /dev/null +++ b/lib/hayashi_0225.jar Binary files differ diff --git a/lib/hsqldb_2.2.9.jar b/lib/hsqldb_2.2.9.jar new file mode 100644 index 0000000..b51f653 --- /dev/null +++ b/lib/hsqldb_2.2.9.jar Binary files differ diff --git a/lib/postgresql-9.4.1212.jar b/lib/postgresql-9.4.1212.jar new file mode 100644 index 0000000..b0de752 --- /dev/null +++ b/lib/postgresql-9.4.1212.jar Binary files differ diff --git a/src/Demo.java b/src/Demo.java new file mode 100644 index 0000000..688d674 --- /dev/null +++ b/src/Demo.java @@ -0,0 +1,37 @@ +import java.io.*; +import java.sql.Connection; +import java.sql.SQLException; +import jp.co.areaweb.tools.database.*; + +public class Demo { + /** + * メイン + * + * java -cp .:SelectBusstop.jar:hayashi_0225.jar:postgresql-9.4.1212.jar osm.jp.SelectBusstop + * + * @throws IOException + * @throws SQLException + * @throws ClassNotFoundException + * @throws FileNotFoundException + * @throws TransformerException + * @throws SAXException + * @throws ParserConfigurationException */ + public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException + { + File dbdir = new File("database"); + if (!dbdir.isDirectory()) { + dbdir.mkdir(); + } + + Connection con = DatabaseTool.openDb("database"); + //Demo.initDb(con); + + try { + System.out.println("Connection OK!"); + } + finally { + DatabaseTool.closeDb(con); + } + } + +} \ No newline at end of file diff --git a/src/Demo.sh b/src/Demo.sh new file mode 100755 index 0000000..9398633 --- /dev/null +++ b/src/Demo.sh @@ -0,0 +1 @@ +java -cp .:postgis.jar:hayashi_0225.jar:postgresql-9.4.1212.jar Demo diff --git a/src/Do_sqlfiles.sh b/src/Do_sqlfiles.sh new file mode 100755 index 0000000..3240223 --- /dev/null +++ b/src/Do_sqlfiles.sh @@ -0,0 +1 @@ +java -cp .:postgis.jar:hayashi_0225.jar:postgresql-9.4.1212.jar osm.jp.postgis.Do_sqlfiles \ No newline at end of file diff --git a/src/create_busstop.sql b/src/create_busstop.sql new file mode 100644 index 0000000..786ce85 --- /dev/null +++ b/src/create_busstop.sql @@ -0,0 +1,26 @@ +-- DROP TABLE t_busstop; +CREATE TABLE t_busstop (gid SERIAL PRIMARY KEY,name text,fixed integer,area integer,geom GEOMETRY(POINT, 4612)); +CREATE INDEX ix_busstop_geom ON t_busstop USING GiST (geom); + +-- DROP VIEW public."V_14kanagawa_busstop"; +CREATE OR REPLACE VIEW public."V_14kanagawa_busstop" AS + SELECT admarea.gid, + (( SELECT fixed FROM t_busstop WHERE st_within(t_busstop.geom, admarea.geom)))::integer AS stncnt, + admarea.geom, + admarea.n03_004 AS name + FROM admarea; + +-- DROP VIEW public."V_busstop_0"; +CREATE OR REPLACE VIEW public."V_busstop_0" AS + SELECT gid,name,fixed,area,geom FROM t_busstop WHERE fixed=0; +CREATE OR REPLACE VIEW public."V_busstop_1" AS + SELECT gid,name,fixed,area,geom FROM t_busstop WHERE fixed>0; + + SELECT t_busstop.gid, + t_busstop.geom + FROM t_busstop,admarea + WHERE st_within(rail.c_geom, admarea.geom); + + +ALTER TABLE public."V_kanagawa" + OWNER TO yuu; diff --git a/src/osm/jp/api/HttpGET.java b/src/osm/jp/api/HttpGET.java new file mode 100644 index 0000000..bcde9ad --- /dev/null +++ b/src/osm/jp/api/HttpGET.java @@ -0,0 +1,302 @@ +package osm.jp.api; + +import java.net.*; +import java.io.*; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * Java HTTP クライアントサンプル - HttpURLConnection 版 - + * + * @author 68user http://X68000.q-e-d.net/~68user/ + */ +public class HttpGET { + //public static String host = "http://api06.dev.openstreetmap.org"; + public static String host = "http://api.openstreetmap.org"; + + public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException { + getCapabilities(); + + /* + + + + + + + + + + */ + + double minlon = 139.4197591d; + double maxlon = 139.4279939d; + double minlat = 35.4320438d; + double maxlat = 35.4375923d; + HttpGET.getMap(minlon, minlat, maxlon, maxlat); + + HttpGET.getMap(35.4350644157973d, 139.423684433498d, 50); // あやせ荘 + HttpGET.getMap(35.4341675801122d, 139.418362759267d, 50); // 武者奇橋 + HttpGET.getMap(35.4369651010672d, 139.426400070915d, 50); // 綾瀬市役所 + } + + public static void getCapabilities() throws MalformedURLException, ProtocolException, IOException { + System.out.println(host + "/api/capabilities"); + URL url = new URL(host + "/api/capabilities"); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("GET"); + urlconn.setInstanceFollowRedirects(false); + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.connect(); + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + System.out.println("\n---- ボディ ----"); + + BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); + + while (true){ + String line = reader.readLine(); + if ( line == null ){ + break; + } + System.out.println(line); + } + + reader.close(); + urlconn.disconnect(); + } + + + public static final double BIG_Y = (6378137.0d + 6356752.314d) / 2d; // 地球の平均半径(m) + public static final double LAT1 = Math.PI * 2d * BIG_Y / 360d; // 緯度1度の距離(m) + public static int getMap(double lat, double lon, int m) throws MalformedURLException, ProtocolException, IOException { + double dLat = m / LAT1; // 距離を表す緯度(差分) + double minlat = lat - dLat; // 底辺(緯度) + double maxlat = lat + dLat; // 上辺(緯度) + + double y = Math.cos(lat / 180.0d * Math.PI) * BIG_Y; // 緯線上の地球の半径 + double lon1 = (y * 2.0d * Math.PI) / 360; // 経度1度の距離(m) + double dLon = m / lon1; // 距離を表す経度(差分) + double minlon = lon - dLon; // 左辺 + double maxlon = lon + dLon; // 右辺 + + System.out.println("緯線上の地球の半径= "+ y); + System.out.println("緯度1秒の長さ(m)= "+ LAT1 / 3600); + System.out.println("経度1秒の長さ(m)= "+ lon1 / 3600); + + return getMap(minlon, minlat, maxlon, maxlat); + } + + public static int getMap(RectArea center) throws MalformedURLException, ProtocolException, IOException { + return getMap(center.minlon, center.minlat, center.maxlon, center.maxlat); + } + + public static int getMap(double minlon, double minlat, double maxlon, double maxlat) throws MalformedURLException, ProtocolException, IOException { + String param = host + "/api/0.6/map" + "?bbox="+ Double.toString(minlon) +","+ Double.toString(minlat) +","+ Double.toString(maxlon) +","+ Double.toString(maxlat); + + System.out.println(param); + URL url = new URL(param); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("GET"); + urlconn.setInstanceFollowRedirects(false); + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.connect(); + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + System.out.println("\n---- ボディ ----"); + + //------------------------------------------ + DocumentBuilderFactory factory; + DocumentBuilder builder; + Node root; + + try { + factory = DocumentBuilderFactory.newInstance(); + builder = factory.newDocumentBuilder(); + factory.setIgnoringElementContentWhitespace(true); + factory.setIgnoringComments(true); + factory.setValidating(true); + root = builder.parse(urlconn.getInputStream()); + + counter = 0; + checkNodes(root); + } catch (ParserConfigurationException e0) { + System.out.println(e0.getMessage()); + } catch (SAXException e1){ + System.out.println(e1.getMessage()); + } catch (IOException e2) { + System.out.println(e2.getMessage()); + } + + System.out.println("バス停数["+ counter +"]"); + urlconn.disconnect(); + + return counter; + } + + public void sendCMD(String api) throws MalformedURLException, ProtocolException, IOException { + System.out.println(host + api); + URL url = new URL(host + api); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("GET"); + urlconn.setInstanceFollowRedirects(false); + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.connect(); + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + System.out.println("\n---- ボディ ----"); + + BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); + + while (true){ + String line = reader.readLine(); + if ( line == null ){ + break; + } + System.out.println(line); + } + + reader.close(); + urlconn.disconnect(); + } + + public static int counter; + public static void checkNodes(Node node) { + if (isBusstop(node)) { + counter++; + showNode(node); + } + + NodeList nodes = node.getChildNodes(); + for (int i=0; i"+ node.getNodeValue()); + + NodeList nodes = node.getChildNodes(); + for (int i=0; i"); + } + + class RectArea { + public double minlon; + public double maxlon; + public double minlat; + public double maxlat; + + /** + * 矩形領域を中心点と中心点からの距離(メートル)でセットする + * + * @param lat // 中心点の緯度 + * @param lon // 中心点の経度 + * @param m // 距離 領域の一辺の長さの半分 + */ + public RectArea(double lat, double lon, int m) { + double BIG_Y = (40000000.0d / 2.0d / Math.PI); // 地球の半径 + double LAT1 = (10000000.0d / 90.0d); // 緯度1度の距離(m) + double dLat = m / LAT1; // 距離を表す緯度(差分) + minlat = lat - dLat; // 底辺(緯度) + maxlat = lat + dLat; // 上辺(緯度) + double y = Math.sin((90.0d - lat) / 180.0d) * BIG_Y; // 緯線上の地球の半径 + double lon1 = y * 2.0d * Math.PI; // 経度1度の距離(m) + double dLon = m / lon1; // 距離を表す経度(差分) + minlon = lon - dLon; // 左辺 + maxlon = lon + dLon; // 右辺 + } + } +} diff --git a/src/osm/jp/api/HttpPOST.java b/src/osm/jp/api/HttpPOST.java new file mode 100644 index 0000000..d66b6cc --- /dev/null +++ b/src/osm/jp/api/HttpPOST.java @@ -0,0 +1,73 @@ +package osm.jp.api; + +import java.net.*; +import java.io.*; + +/** + * Java HTTP クライアントサンプル - HttpURLConnection 版 - + * + * @author 68user http://X68000.q-e-d.net/~68user/ + */ +public class HttpPOST { + //public static String host = "http://api06.dev.openstreetmap.org"; + //public static String host = "http://api.openstreetmap.org"; + public static String host = "http://overpass-api.de"; + + public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException { + double minlat = 35.13d; + double maxlat = 35.66d; + double minlon = 138.99d; + double maxlon = 139.79d; + getCapabilities(new File("."), minlat, maxlat, minlon, maxlon); + } + + public static void getCapabilities(File oFile, 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"); + + HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); + urlconn.setRequestMethod("POST"); + urlconn.setDoOutput(true); // POSTのデータを後ろに付ける + urlconn.setInstanceFollowRedirects(false); // 勝手にリダイレクトさせない + urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); + urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8"); + urlconn.connect(); + + // 送信 + PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8"))); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.print(""); + pw.close(); // closeで送信完了 + + System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " + + "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]"); + 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) { + break; + } + hw.write(line); + hw.newLine(); + } + hw.close(); + reader.close(); + urlconn.disconnect(); + } +} diff --git a/src/osm/jp/coverage/busstop/ConvBusstop.java b/src/osm/jp/coverage/busstop/ConvBusstop.java new file mode 100644 index 0000000..234bb59 --- /dev/null +++ b/src/osm/jp/coverage/busstop/ConvBusstop.java @@ -0,0 +1,1272 @@ +package osm.jp.coverage.busstop; +import osm.jp.api.HttpPOST; + +import javax.xml.parsers.*; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.*; +import org.xml.sax.*; + +import java.io.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; + +import jp.co.areaweb.tools.database.*; + +public class ConvBusstop { + + String filter = ""; + String urlStr = ""; + + public static final boolean DB_INIT = false; + + // 近くのバス停を探す範囲(バス停を中心としたNEER×2m四方の領域 + static final int NEER = 150; // 150m(0.15km) + static boolean nocheck = false; + + public static SimpleDateFormat timeStampFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + /** + * メイン + * + * java -cp .:ConvBusstop.jar:hayashi_0225.jar:hsqldb_2.2.9.jar osm.jp.ConvBusstop