diff --git a/src/ksj_police.class.violet.html b/src/ksj_police.class.violet.html new file mode 100644 index 0000000..8d5b5f0 --- /dev/null +++ b/src/ksj_police.class.violet.html @@ -0,0 +1,612 @@ + + + + + + + + + This file was generated with Violet UML Editor 2.1.0. +   ( View Source / Download Violet ) +
+
+ +
+
+ embedded diagram image + + \ No newline at end of file diff --git a/src/osm/jp/coverage/police/DbPolice.java b/src/osm/jp/coverage/police/DbPolice.java index 5d572b1..a65e110 100644 --- a/src/osm/jp/coverage/police/DbPolice.java +++ b/src/osm/jp/coverage/police/DbPolice.java @@ -1,12 +1,11 @@ package osm.jp.coverage.police; -import osm.jp.coverage.busstop.*; +import hayashi.tools.files.DeleteDir; import java.io.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -16,10 +15,11 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import osm.jp.api.HttpPOST; +import osm.jp.api.Db; public class DbPolice { - public static final String TABLE_NAME = "bus_stop"; + public static final String TABLE_NAME = "POLICE"; + public static final String TABLE_NAME2 = "POLICE2"; File inputFile; String filter = ""; @@ -41,35 +41,32 @@ * @throws org.xml.sax.SAXException */ public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException, SQLException, ParserConfigurationException, SAXException { - // HSQLディレクトリがなければエラー - File dbdir = new File("database"); - if (!dbdir.isDirectory()) { - throw new FileNotFoundException("Directory 'database' is not found."); + File dir = new File("database"); + if (dir.exists()) { + if (dir.isDirectory()) { + DeleteDir.delete(dir); + } } Connection conHsql = null; try { + // DB.tableを作成 conHsql = DatabaseTool.openDb("database"); - HttpPOST.sql(conHsql, "DROP TABLE IF EXISTS "+ TABLE_NAME +" CASCADE"); create(conHsql); /** * 都道府県ごとのGMLディレクトリの処理 */ int fcounter = 0; - File dir = new File("GML_BUSSTOP"); - for (File gmldir : dir.listFiles()) { - if (checkGMLdir(gmldir)) { - // GMLディレクトリを処理する - int areacode = Integer.parseInt(gmldir.getName().substring(GML_DIR_PREFIX.length(), GML_DIR_PREFIX.length()+2)); - File[] files = gmldir.listFiles(); - for (File iFile : files) { - // 対象のファイルが「数値地図情報のGMLデータファイル」の時のみ処理を行う。 - if (checkFile(iFile, areacode)) { - importBusstop(conHsql, iFile, areacode); - fcounter++; - } - } + File folder = new File("GML_POLICE"); + for (File xmlfile : folder.listFiles()) { + // GMLディレクトリを処理する + int areacode = Integer.parseInt(xmlfile.getName().substring(GML_XML_PREFIX.length(), GML_XML_PREFIX.length()+2)); + + // 対象のファイルが「数値地図情報のGMLデータファイル」の時のみ処理を行う。 + if (checkFile(xmlfile, areacode)) { + importPOI(conHsql, xmlfile, areacode); + fcounter++; } } System.out.println("["+ fcounter +"]つのGMLファイルをインポートしました。"); @@ -95,9 +92,8 @@ * @throws ParserConfigurationException * @throws SAXException */ - public static void importBusstop (Connection conHsql, File iFile, int areacode) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { + public static void importPOI (Connection conHsql, File iFile, int areacode) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException, ParserConfigurationException, SAXException { int iCounter = 0; - String timeStampStr = null; String iStr = iFile.getName(); @@ -112,21 +108,20 @@ factory.setValidating(true); root = builder.parse(iFile); - iCounter += showNodes(conHsql, root, iStr.substring(0, iStr.length() - 4), areacode); - System.out.println("("+ areacode +") バス停数["+ iCounter +"]"); + iCounter += showNodes(conHsql, root, areacode); + System.out.println("("+ areacode +") POI数["+ iCounter +"]"); } /** * * @param con * @param node - * @param iFileName // ソースファイル名(拡張子を含まない) * @param areacode * @return * @throws IOException * @throws SQLException */ - public static int showNodes(Connection con, Node node, String iFileName, int areacode) throws IOException, SQLException { + public static int showNodes(Connection con, Node node, int areacode) throws IOException, SQLException { int iCounter = 0; NodeList nodes = node.getChildNodes(); for (int i=0; i + * * 35.14591397 139.10569573 * * @@ -159,7 +154,6 @@ * @throws SQLException */ public static void showGmlPoint(Connection con, Node node, int areacode) throws IOException, SQLException { - String positionStr = ""; String latStr = ""; String lonStr = ""; String idStr = ""; @@ -177,152 +171,89 @@ for (int i=0; i < nodes.getLength(); i++) { Node node2 = nodes.item(i); if (node2.getNodeName().equals("gml:pos")) { - positionStr = node2.getTextContent().trim(); + String positionStr = node2.getTextContent(); String[] str4Ary = positionStr.split(" "); latStr = str4Ary[0]; lonStr = str4Ary[1]; - - try (PreparedStatement ps6 = con.prepareStatement("INSERT INTO "+ TABLE_NAME +" (lat,lon,fixed,fixed1,area,gmlid,up) VALUES (?,?,0,0,?,?,2)")) { - double lat = Double.parseDouble(latStr); - double lon = Double.parseDouble(lonStr); - System.out.println("INSERT INTO "+ TABLE_NAME +" (lat,lon,fixed,fixed1,area,gmlid,up) VALUES ('"+ latStr +"','"+ lonStr +"',0,0,"+ areacode +",'"+ idStr +"',2)"); - - ps6.setDouble(1, lat); - ps6.setDouble(2, lon); - ps6.setInt(3, areacode); - ps6.setString(4, idStr); - ps6.executeUpdate(); - } + break; } } + + try (PreparedStatement ps = con.prepareStatement("INSERT INTO "+ TABLE_NAME2 +"(idref,area,lat,lon) VALUES(?,?,?,?)")) { + double lat = Double.parseDouble(latStr); + double lon = Double.parseDouble(lonStr); + ps.setString(1, idStr); + ps.setInt(2, areacode); + ps.setDouble(3, lat); + ps.setDouble(4, lon); + System.out.println("INSERT INTO "+ TABLE_NAME2 +"("+ idStr +", area="+ areacode +", lat="+ lat +", lon="+ lon +")"); + ps.executeUpdate(); + } } /** - * - * - * 城堀 - * - * - * 1 - * 箱根登山バス - * 小01 - * - * - * - * - * 1 - * 箱根登山バス - * 湯07 - * - * - * - * - * 1 - * 箱根登山バス - * 湯11 - * - * - * + *
2code{
+        
+            江差警察署鶉駐在所
+            
+            01363
+            5
+            厚沢部町鶉町213
+        
+      }
* * @param con * @param node - * @param iFileName // ソースファイル名(拡張子を含まない) * @param areacode * @throws IOException * @throws SQLException */ - public static void showBusStop(Connection con, Node node, String iFileName, int areacode) throws IOException, SQLException { + public static void showPoliceStation(Connection con, Node node, int areacode) throws IOException, SQLException { String gmlidStr = ""; String nameStr = ""; - try (PreparedStatement ps2 = con.prepareStatement("UPDATE bus_stop SET name=?,ifile=? WHERE (gmlid=? and area=?)")) { - ArrayList bris = new ArrayList<>(); - NodeList nodes = node.getChildNodes(); - for (int i=0; i < nodes.getLength(); i++) { - Node node2 = nodes.item(i); - switch (node2.getNodeName()) { - case "ksj:position": - NamedNodeMap nodeMap = node2.getAttributes(); - if (null != nodeMap) { - for ( int j=0; j < nodeMap.getLength(); j++ ) { - if (nodeMap.item(j).getNodeName().equals("xlink:href")) { - gmlidStr = nodeMap.item(j).getNodeValue(); - gmlidStr = gmlidStr.substring(1); - System.out.println("found gmlid='"+ gmlidStr +"'"); - break; - } + int code = 0; + + NodeList nodes = node.getChildNodes(); + for (int i=0; i < nodes.getLength(); i++) { + Node node2 = nodes.item(i); + switch (node2.getNodeName()) { + case "ksj:pos": + NamedNodeMap nodeMap = node2.getAttributes(); + if (null != nodeMap) { + for ( int j=0; j < nodeMap.getLength(); j++ ) { + if (nodeMap.item(j).getNodeName().equals("xlink:href")) { + gmlidStr = nodeMap.item(j).getNodeValue(); + gmlidStr = gmlidStr.substring(1); + System.out.println("found gmlid='"+ gmlidStr +"'"); + break; } - } break; - case "ksj:busStopName": - nameStr = node2.getTextContent(); - break; - case "ksj:busRouteInformation": - String[] rtn = anaCommJGD(node2); - if (rtn != null) { - bris.add(rtn); - } break; - default: - break; - } - } - - // gmlid と nameStr をデータベースに格納する - ps2.setString(1, nameStr); - ps2.setString(2, iFileName); - ps2.setString(3, gmlidStr); - ps2.setInt(4, areacode); - System.out.println("UPDATE bus_stop SET name='"+ nameStr +"',ifile='"+ iFileName +"' WHERE (gmlid='"+ gmlidStr +"' and area="+ areacode +")"); - ps2.executeUpdate(); - } - } - - /** - * - * - * - * 1 - * 箱根登山バス - * 小01 - * - * - * - * @param briNode - * @return - */ - public static String[] anaCommJGD(Node briNode) { - String[] rtn = new String[3]; - int vcnt = 0; - - NodeList nodes2 = briNode.getChildNodes(); - for (int i=0; i < nodes2.getLength(); i++) { - Node node2 = nodes2.item(i); - if (node2.getNodeName().equals("ksj:BusRouteInformation")) { - NodeList nodes3 = node2.getChildNodes(); - for (int j=0; j < nodes3.getLength(); j++) { - Node node3 = nodes3.item(j); - switch (node3.getNodeName()) { - case "ksj:busType": - rtn[0] = node3.getTextContent(); - vcnt++; - break; - case "ksj:busLineName": - rtn[1] = node3.getTextContent(); - vcnt++; - break; - case "ksj:busOperationCompany": - rtn[2] = node3.getTextContent(); - vcnt++; - break; - default: - break; - } - } + } + } break; + case "ksj:psn": + nameStr = node2.getTextContent(); + break; + case "ksj:ccd": + String ccdStr = node2.getTextContent(); + code = Integer.parseInt(ccdStr); + break; + default: + break; } } - if (vcnt > 0) { - return rtn; + // gmlid と nameStr をデータベースに格納する + try (PreparedStatement ps = con.prepareStatement("INSERT INTO "+ TABLE_NAME +"(gid,fixed,idref,name, code, area,up,fixed1) VALUES(?,?,?,?,?,?,?,?)")) { + ps.setInt(1, 0); // gid + ps.setInt(2, 0); // fixed + ps.setString(3, gmlidStr); // idref "n0000" + ps.setString(4, nameStr); + ps.setInt(5, code); + ps.setInt(6, areacode); // 都道府県コード + ps.setInt(7, 2); // up + ps.setInt(8, 0); // fixed1 + System.out.println("INSERT INTO "+ TABLE_NAME +"(gid=0, fixed=0, idref='"+ gmlidStr +"', name='"+ nameStr +"', area="+ 0 +", up=2, fixed1=0)"); + ps.executeUpdate(); } - return null; } /** @@ -334,32 +265,38 @@ public static void create(Connection conHsql) throws SQLException { String createSt; - // 'table.BUS_STOP'を新規に作る - createSt = "CREATE TABLE "+ TABLE_NAME +" (gmlid VARCHAR(12) NOT NULL, name VARCHAR(128), lat DOUBLE, lon DOUBLE, fixed INT, fixed1 INT, area INT, ifile VARCHAR(128), up INT, CONSTRAINT "+ TABLE_NAME +"_pk PRIMARY KEY(gmlid, area));"; - HttpPOST.sql(conHsql, createSt); + createSt = "CREATE TABLE "+ TABLE_NAME2 +" (idref VARCHAR(12) NOT NULL, area INT, lat DOUBLE, lon DOUBLE)"; + Db.updateSQL(conHsql, createSt); - createSt = "CREATE INDEX "+ TABLE_NAME +"_index ON "+ TABLE_NAME +" (lat,lon);"; - HttpPOST.sql(conHsql, createSt); + createSt = "CREATE UNIQUE INDEX id2 ON "+ TABLE_NAME2 +" (idref, area)"; + Db.updateSQL(conHsql, createSt); + + createSt = "CREATE TABLE "+ TABLE_NAME +" (gid INT, idref VARCHAR(12) NOT NULL, name VARCHAR(255), code INT, area INT, lat DOUBLE, lon DOUBLE, up INT, fixed INT, fixed1 INT)"; + Db.updateSQL(conHsql, createSt); + + createSt = "CREATE INDEX id1 ON "+ TABLE_NAME +" (idref, area, lat, lon)"; + Db.updateSQL(conHsql, createSt); } /** - * 'table.BUS_STOP'の内容をCSV形式にして標準出力に出力する + * 'table.TABLE_NAME'の内容をCSV形式にして標準出力に出力する * @param con */ public static void export(Connection con) { try { - System.out.println("TABLE: BUS_STOP"); - System.out.println("\"gmlid\",\"name\",\"lat\",\"lon\",\"fixed\",\"ifile\""); - PreparedStatement ps8 = con.prepareStatement("SELECT gmlid,name,lat,lon,fixed,ifile FROM bus_stop"); + System.out.println("TABLE: "+ TABLE_NAME); + System.out.println("\"gmlid\",\"name\",\"lat\",\"lon\",\"fixed\""); + PreparedStatement ps8 = con.prepareStatement("SELECT idref,area,name,code,lat,lon,fixed FROM "+ TABLE_NAME); try (ResultSet rset8 = ps8.executeQuery()) { while (rset8.next()) { - String gmlid = rset8.getString("gmlid"); + String idref = rset8.getString("idref"); + int area = rset8.getInt("area"); String name = rset8.getString("name"); + int code = rset8.getInt("code"); Double lat = rset8.getDouble("lat"); Double lon = rset8.getDouble("lon"); int fixed = rset8.getInt("fixed"); - String ifile = rset8.getString("ifile"); - System.out.println("\""+ gmlid +"\",\""+ name +"\","+ lat +","+ lon +","+ fixed +",\""+ ifile +"\""); + System.out.println("\""+ idref +"\","+ area +",\""+ name +"\","+ code +","+ lat +","+ lon +","+ fixed +""); } } } @@ -375,31 +312,15 @@ */ static boolean checkFile(File f, int areacode) { String name = f.getName(); - if (!name.startsWith(GML_DIR_PREFIX)) { + if (!name.startsWith(GML_XML_PREFIX)) { return false; } if (!name.toUpperCase().endsWith(".XML")) { return false; } - return Integer.parseInt(name.substring(GML_DIR_PREFIX.length(), GML_DIR_PREFIX.length()+2)) == areacode; + return Integer.parseInt(name.substring(GML_XML_PREFIX.length(), GML_XML_PREFIX.length()+2)) == areacode; } - /** - * 数値地図情報のデータディレクトリかどうかを見極める - * @param f - * @return - */ - public static boolean checkGMLdir(File f) { - if (!f.isDirectory()) { - return false; - } - String name = f.getName(); - if (!name.startsWith(GML_DIR_PREFIX)) { - return false; - } - return name.toUpperCase().endsWith(GML_DIR_PRIFIX); - } - - public static final String GML_DIR_PREFIX = "P11-10_"; - public static final String GML_DIR_PRIFIX = "_GML"; + public static final String GML_XML_PREFIX = "P18-12_"; + public static final String GML_XML_PRIFIX = ".xml"; } \ No newline at end of file diff --git a/test/osm/jp/coverage/police/DbPoliceTest.java b/test/osm/jp/coverage/police/DbPoliceTest.java new file mode 100644 index 0000000..1cdcb4f --- /dev/null +++ b/test/osm/jp/coverage/police/DbPoliceTest.java @@ -0,0 +1,157 @@ +package osm.jp.coverage.police; + +import java.io.File; +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.ParserConfigurationException; +import jp.co.areaweb.tools.database.DatabaseTool; +import static org.hamcrest.CoreMatchers.is; +import org.junit.*; +import static org.junit.Assert.*; +import org.xml.sax.SAXException; + +/** + * + * @author yuu + */ +public class DbPoliceTest { + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test02_police() { + try { + String[] args = new String[0]; + DbPolice.main(args); + } + catch (IOException | ClassNotFoundException | SQLException | ParserConfigurationException | SAXException ex) { + ex.printStackTrace(); + fail(ex.toString()); + } + + File dir = new File("database"); + assertTrue(dir.exists()); + assertTrue(dir.isDirectory()); + + Connection hsqldb = null; + try { + hsqldb = DatabaseTool.openDb("database"); + + PreparedStatement ps1 = hsqldb.prepareStatement("SELECT count(*) FROM "+ DbPolice.TABLE_NAME); + try (ResultSet rset1 = ps1.executeQuery()) { + if (rset1.next()) { + long cnt = rset1.getLong(1); + assertThat((cnt > 0), is(true)); + } + else { + fail(); + } + } + + // gml:id="pt_psn549" name=江差警察署鶉駐在所 code=5 場所: 41.933898 140.324846 + checkSql(hsqldb, "pt_psn549", 1, 5, "41.933898", "140.324846"); + + // pt_psn2 1 北海道警察函館方面本部 函館市五稜郭町15-5 41.796013 140.751973 + checkSql(hsqldb, "pt_psn2", 1, 1, "41.796013", "140.751973"); + + // pt_psn597 area=1 code=4 栗山警察署長沼交番 長沼町錦町北1-2-18 43.011878 141.691813 + checkSql(hsqldb, "pt_psn597", 1, 4, "43.011878", "141.691813"); + + // pt_psn6 area=1 code=2 中央警察署 札幌市中央区北1条西5丁目4 43.062164 141.349533 + checkSql(hsqldb, "pt_psn6", 1, 2, "43.062164", "141.349533"); + + // pt_psn814 area=1 code=6 南警察署旭ヶ丘警備派出所 札幌市中央区南13条西23丁目19 43.039666 141.324324 + checkSql(hsqldb, "pt_psn814", 1, 6, "43.039666", "141.324324"); + + // pt_psn831 area=1 code=7 北海道警察学校 札幌市南区真駒内南町5丁目1-7 42.979149 141.348956 + checkSql(hsqldb, "pt_psn831", 1, 7, "42.979149", "141.348956"); + + // pt_psn833 area=1 code=8 西警察署山の手連絡所 札幌市西区山の手2条6丁目2番14号 43.066532 141.29754 + checkSql(hsqldb, "pt_psn833", 1, 8, "43.066532", "141.29754"); + + // pt_psn178 area=47 code=7 沖縄県警察学校 うるま市石川3402 26.441508 127.83277 + checkSql(hsqldb, "pt_psn178", 47, 7, "26.441508", "127.83277"); + + // pt_psn172 area=47 code=6 豊見城警察署那覇空港警備派出所 那覇市字鏡水150 26.208003 127.650617 + checkSql(hsqldb, "pt_psn172", 47, 6, "26.208003", "127.650617"); + + // pt_psn87 area=47 code=5 八重山警察署伊原間駐在所 石垣市字伊原間40-1 24.508835 124.28265 + checkSql(hsqldb, "pt_psn87", 47, 5, "24.508835", "124.28265"); + + // pt_psn179 area=47 code=4 那覇警察署おもろまち交番 那覇市おもろまち1-2-32 26.22469593 127.694261 + checkSql(hsqldb, "pt_psn179", 47, 4, "26.22469593", "127.694261"); + + // pt_psn2 area=47 code=2 那覇警察署 那覇市与儀1-2-9 26.207533 127.691753 + checkSql(hsqldb, "pt_psn2", 47, 2, "26.207533", "127.691753"); + + // pt_psn1 area=47 code=1 沖縄県警察本部 那覇市泉崎1-2-2 26.211504 127.681059 + checkSql(hsqldb, "pt_psn1", 47, 1, "26.211504", "127.681059"); + + + } catch (ClassNotFoundException | SQLException | IOException ex) { + Logger.getLogger(DbPoliceTest.class.getName()).log(Level.SEVERE, null, ex); + fail(); + } finally { + DatabaseTool.closeDb(hsqldb); + } + } + + void checkSql(Connection hsqldb, String idref, int area, int code, String lat, String lon) throws SQLException { + String sql = String.format("SELECT %s.idref, %s.area, %s.code, %s.lat, %s.lon", + DbPolice.TABLE_NAME, DbPolice.TABLE_NAME, DbPolice.TABLE_NAME, DbPolice.TABLE_NAME2, DbPolice.TABLE_NAME2) + + String.format(" FROM %s,%s", DbPolice.TABLE_NAME, DbPolice.TABLE_NAME2) + + String.format(" WHERE (%s.idref=%s.idref) and (%s.area=%s.area) and (%s.idref='%s') and (%s.area=%d)", + DbPolice.TABLE_NAME, DbPolice.TABLE_NAME2, DbPolice.TABLE_NAME, DbPolice.TABLE_NAME2, DbPolice.TABLE_NAME, + idref, DbPolice.TABLE_NAME, area); + System.out.println(sql); + PreparedStatement ps1 = hsqldb.prepareStatement(sql); + try (ResultSet rset1 = ps1.executeQuery()) { + boolean ok = false; + while (rset1.next()) { + assertThat(rset1.getString(1), is(idref)); // idref + assertThat(rset1.getInt(2), is(area)); // area + assertThat(rset1.getInt(3), is(code)); // code + assertThat(checkRenge(rset1, lat, lon), is(true)); + ok = true; + } + if (!ok) { + fail(); + } + } + + } + + boolean checkRenge(ResultSet rset, String latStr, String lonStr) throws SQLException { + if (checkRenge(rset.getDouble("lat"), latStr)) { + if (checkRenge(rset.getDouble("lon"), lonStr)) { + return true; + } + } + return false; + } + + boolean checkRenge(double d1, String str) throws SQLException { + double base = Double.parseDouble(str); + double up = d1 + 0.0000005D; + double down = d1 - 0.0000005D; + boolean ret = true; + if (Double.compare(base, up) > 0) { + ret = false; + } + if (Double.compare(base, down) < 0) { + ret = false; + } + System.out.println("d1: "+ d1 +" : "+ str +" --> "+ (ret ? "IN" : "out")); + return ret; + } +}