| | package osm.jp.api; |
---|
| | |
---|
| | import hayashi.yuu.tools.json.JsonTool; |
---|
| | import java.io.*; |
---|
| | import java.sql.Connection; |
---|
| | import java.sql.PreparedStatement; |
---|
| | import java.sql.ResultSet; |
---|
| |
---|
| | import org.w3c.dom.NamedNodeMap; |
---|
| | import org.w3c.dom.Node; |
---|
| | import org.w3c.dom.NodeList; |
---|
| | import org.xml.sax.SAXException; |
---|
| | import osm.jp.coverage.busstop.DbBusstop; |
---|
| | |
---|
| | public abstract class Osmdb { |
---|
| | public String TABLE_NAME = "EXIST_osm"; |
---|
| | public String tableName = TABLE_NAME; |
---|
| |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |
---|
| | public int importExistingNode(Connection osmdb, ResultSet rset, int point) throws IOException, SQLException { |
---|
| | |
---|
| | public int importExistingNode(Connection osmdb, ResultSet rset, int point, boolean removed) throws IOException, SQLException { |
---|
| | String osmidStr = rset.getString("osm_id"); |
---|
| | String latStr = rset.getString("lat"); |
---|
| | String lonStr = rset.getString("lon"); |
---|
| | String brandStr = rset.getString("brand"); |
---|
| |
---|
| | key = true; |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | System.out.println(tags); |
---|
| | //System.out.println(tags); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| |
---|
| | } |
---|
| | |
---|
| | if (score > 0) { |
---|
| | // idref と brandStr をデータベースに格納する |
---|
| | System.out.println("INSERT INTO "+ tableName +" (idref,lat,lon,score,name) VALUES ("+ osmidStr +","+ latStr +","+ lonStr+","+ Integer.toString(score) +",'"+ nameStr +"')N"); |
---|
| | System.out.println( |
---|
| | "INSERT INTO "+ tableName |
---|
| | + " (idref,lat,lon,score,name,removed) VALUES (" |
---|
| | + osmidStr +"," |
---|
| | + latStr +"," |
---|
| | + lonStr +"," |
---|
| | + Integer.toString(score) +"," |
---|
| | + "'"+ nameStr +"'," |
---|
| | + "false" |
---|
| | + ")N" |
---|
| | ); |
---|
| | try (PreparedStatement ps5 = hsqldb.prepareStatement( |
---|
| | "INSERT INTO "+ tableName +" (idref,lat,lon,score,name) VALUES (?,?,?,?,?)" |
---|
| | "INSERT INTO "+ tableName +" (idref,lat,lon,score,name,removed) VALUES (?,?,?,?,?,?)" |
---|
| | )) { |
---|
| | ps5.setString(1, osmidStr); |
---|
| | ps5.setDouble(2, Double.parseDouble(latStr)); |
---|
| | ps5.setDouble(3, Double.parseDouble(lonStr)); |
---|
| | ps5.setInt(4, score); |
---|
| | ps5.setString(5, nameStr); |
---|
| | ps5.setBoolean(6, removed); |
---|
| | ps5.executeUpdate(); |
---|
| | return 1; |
---|
| | } |
---|
| | catch (HsqlException | SQLIntegrityConstraintViolationException e) { |
---|
| |
---|
| | * その際に、OSMノードを評価し、scoreを算定する |
---|
| | * |
---|
| | * @param sqlWhere POI条件 |
---|
| | * @param point 評価ポイント[POINT_NO|POINT_BRAND|POINT_NAME] 1: 'brand' 2:'name' |
---|
| | * @param removed |
---|
| | * @throws Exception エラー |
---|
| | */ |
---|
| | public void readExisting(String sqlWhere, int point) throws Exception { |
---|
| | Connection osmdb = null; |
---|
| | public void readExisting(String sqlWhere, int point, boolean removed) throws Exception { |
---|
| | long counter = 0L; |
---|
| | try { |
---|
| | osmdb = DatabaseTool.openDb("osmdb"); |
---|
| | |
---|
| | try (Connection osmdb = DatabaseTool.openDb("osmdb")) { |
---|
| | String sqlSelect = "osm_id," |
---|
| | + "brand," |
---|
| | + "disused," |
---|
| | + "name," |
---|
| | + "ST_Y(ST_Transform(way,4326)) as lat," |
---|
| | + "ST_X(ST_Transform(way,4326)) as lon"; |
---|
| | String sqlNode = String.format( |
---|
| | "SELECT %s FROM planet_osm_point %s", sqlSelect, sqlWhere |
---|
| | "SELECT %s FROM planet_osm_point %s", |
---|
| | sqlSelect, |
---|
| | sqlWhere |
---|
| | ); |
---|
| | System.out.println(sqlNode); |
---|
| | |
---|
| | PreparedStatement ps1 = osmdb.prepareStatement(sqlNode); |
---|
| | try (ResultSet rset1 = ps1.executeQuery()) { |
---|
| | while (rset1.next()) { |
---|
| | counter += importExistingNode(osmdb, rset1, point); |
---|
| | counter += importExistingNode(osmdb, rset1, point, removed); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | sqlSelect = "osm_id,brand,disused,name,ST_Y(ST_Transform(ST_Centroid(way),4326)) as lat,ST_X(ST_Transform(ST_Centroid(way),4326)) as lon"; |
---|
| | String sqlArea = String.format( |
---|
| | "SELECT %s FROM planet_osm_polygon %s", sqlSelect, sqlWhere |
---|
| | "SELECT %s FROM planet_osm_polygon %s", |
---|
| | "osm_id,brand,disused,name,ST_Y(ST_Transform(ST_Centroid(way),4326)) as lat,ST_X(ST_Transform(ST_Centroid(way),4326)) as lon", |
---|
| | sqlWhere |
---|
| | ); |
---|
| | System.out.println(sqlArea); |
---|
| | |
---|
| | PreparedStatement ps2 = osmdb.prepareStatement(sqlArea); |
---|
| | try (ResultSet rset2 = ps2.executeQuery()) { |
---|
| | while (rset2.next()) { |
---|
| | counter += importExistingNode(osmdb, rset2, point); |
---|
| | counter += importExistingNode(osmdb, rset2, point, removed); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | System.out.println("Exists Node count = " + counter); |
---|
| | } |
---|
| | finally { |
---|
| | DatabaseTool.closeDb(osmdb); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | /** |
---|
| |
---|
| | * @param point |
---|
| | * @throws Exception |
---|
| | */ |
---|
| | public void readExistingSub(String kStr, String vStr, int point) throws Exception { |
---|
| | readExistingSub(kStr, vStr, point, true); |
---|
| | } |
---|
| | |
---|
| | public void readExistingSub(String kStr, String vStr, int point, boolean removed) throws Exception { |
---|
| | JsonBuilderFactory factory = Json.createBuilderFactory(null); |
---|
| | JsonObjectBuilder builder = factory.createObjectBuilder(); |
---|
| | builder.add("k",kStr); |
---|
| | builder.add("v", vStr); |
---|
| | JsonObject tag = builder.build(); |
---|
| | readExistingSub(tag, point); |
---|
| | readExistingSub(tag, point, removed); |
---|
| | } |
---|
| | |
---|
| | /** |
---|
| | * |
---|
| |
---|
| | * @param point |
---|
| | * @throws Exception |
---|
| | */ |
---|
| | public void readExistingSub(JsonObject tag,int point) throws Exception { |
---|
| | boolean removed = true; |
---|
| | Connection osmdb = null; |
---|
| | readExistingSub(tag, point, true); |
---|
| | } |
---|
| | |
---|
| | /** |
---|
| | * |
---|
| | * @param tag {k: string, v: string} |
---|
| | * @param point |
---|
| | * @param removed |
---|
| | * @throws Exception |
---|
| | */ |
---|
| | public void readExistingSub(JsonObject tag,int point, boolean removed) throws Exception { |
---|
| | long counter = 0L; |
---|
| | try { |
---|
| | osmdb = DatabaseTool.openDb("osmdb"); |
---|
| | try (Connection osmdb = DatabaseTool.openDb("osmdb")) { |
---|
| | String sqlSelect = "SELECT tags,id,ST_Y(ST_Transform(way,4326)) as lat,ST_X(ST_Transform(way,4326)) as lon"; |
---|
| | String sqlWhere = String.format( |
---|
| | "WHERE (osm_id=id AND '%s'=ANY(tags) AND '%s'=ANY(tags))", |
---|
| | tag.getString("k"), tag.getString("v") |
---|
| |
---|
| | } |
---|
| | } |
---|
| | System.out.println("Exists Node count = " + counter); |
---|
| | } |
---|
| | finally { |
---|
| | DatabaseTool.closeDb(osmdb); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | /** |
---|
| | * |
---|
| |
---|
| | * @param point |
---|
| | * @throws Exception |
---|
| | */ |
---|
| | public void readExistingSub(JsonArray array,int point) throws Exception { |
---|
| | boolean removed = true; |
---|
| | Connection osmdb = null; |
---|
| | readExistingSub(array,point, true); |
---|
| | } |
---|
| | |
---|
| | /** |
---|
| | * |
---|
| | * @param array tags: [{k: string, v: string}] |
---|
| | * @param point |
---|
| | * @param removed |
---|
| | * @throws Exception |
---|
| | */ |
---|
| | public void readExistingSub(JsonArray array,int point, boolean removed) throws Exception { |
---|
| | long counter = 0L; |
---|
| | try { |
---|
| | osmdb = DatabaseTool.openDb("osmdb"); |
---|
| | try (Connection osmdb = DatabaseTool.openDb("osmdb")) { |
---|
| | String sqlSelect = "SELECT " |
---|
| | + "tags," |
---|
| | + "id," |
---|
| | + "ST_Y(ST_Transform(way,4326)) as lat," |
---|
| |
---|
| | } |
---|
| | } |
---|
| | System.out.println("Exists Node count = " + counter); |
---|
| | } |
---|
| | finally { |
---|
| | DatabaseTool.closeDb(osmdb); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | int score(int point, String tags) { |
---|
| | int score = 50; |
---|
| |
---|
| | * @throws SQLException |
---|
| | */ |
---|
| | public int insertExistingNode(String idref, double lat, double lon, int score, String name, boolean removed) throws IOException, SQLException { |
---|
| | // idref と brandStr をデータベースに格納する |
---|
| | String sql = String.format( |
---|
| | "INSERT INTO %s (idref,lat,lon,score,name) VALUES (%s,%2.7f,%3.7f,%d,%s)", |
---|
| | tableName, |
---|
| | idref, lat, lon, score, name |
---|
| | ); |
---|
| | System.out.println(sql); |
---|
| | |
---|
| | sql = String.format("DELETE FROM %s WHERE idref=?", tableName); |
---|
| | /* |
---|
| | */ |
---|
| | |
---|
| | String sql = String.format("DELETE FROM %s WHERE idref=?", tableName); |
---|
| | try (PreparedStatement ps5 = hsqldb.prepareStatement(sql)) { |
---|
| | ps5.setString(1, idref); |
---|
| | ps5.executeUpdate(); |
---|
| | } |
---|
| |
---|
| | // integrity constraint violation: unique constraint or index violation; SYS_PK_10069 table: FUEL_EXIST |
---|
| | // [SQLIntegrityConstraintViolationException]は、無視する |
---|
| | } |
---|
| | |
---|
| | sql = String.format( |
---|
| | "INSERT INTO %s (idref,lat,lon,score,name,removed) VALUES (%s,%2.7f,%3.7f,%d,%s,%s)", |
---|
| | tableName, |
---|
| | idref, lat, lon, score, name, (removed ? "true":"false") |
---|
| | ); |
---|
| | System.out.println(sql); |
---|
| | |
---|
| | sql = String.format( |
---|
| | "INSERT INTO %s (idref,lat,lon,score,name,removed) VALUES (?,?,?,?,?,?)", |
---|
| | tableName |
---|
| | ); |
---|
| |
---|
| | } |
---|
| | return 0; |
---|
| | } |
---|
| | |
---|
| | /** |
---|
| | * REMOVEDファイルからデータを揉み込む |
---|
| | * |
---|
| | * @param revovedFile |
---|
| | * @throws FileNotFoundException |
---|
| | * @throws IOException |
---|
| | * @throws SQLException |
---|
| | */ |
---|
| | public void loadRemoved(File revovedFile) throws FileNotFoundException, IOException, SQLException { |
---|
| | if (revovedFile == null) { |
---|
| | throw new FileNotFoundException(); |
---|
| | } |
---|
| | if (!revovedFile.exists()) { |
---|
| | throw new FileNotFoundException(); |
---|
| | } |
---|
| | if (!revovedFile.isFile()) { |
---|
| | throw new FileNotFoundException(); |
---|
| | } |
---|
| | |
---|
| | try (PreparedStatement ps = hsqldb.prepareStatement( |
---|
| | "UPDATE " |
---|
| | + tableName |
---|
| | + " SET idref=?,removed=true,fixed1=100" |
---|
| | + " WHERE gmlid=? AND area=?" |
---|
| | )) { |
---|
| | LineNumberReader reader = new LineNumberReader( |
---|
| | new InputStreamReader(new FileInputStream(revovedFile)) |
---|
| | ); |
---|
| | while (reader.readLine() != null) { |
---|
| | String lineStr = reader.readLine(); |
---|
| | if (lineStr == null) { |
---|
| | continue; |
---|
| | } |
---|
| | if (lineStr.trim().length() < 3) { |
---|
| | continue; |
---|
| | } |
---|
| | |
---|
| | JsonObject json = JsonTool.parse(lineStr); |
---|
| | String gmlid = json.getString("gmlid"); |
---|
| | String idref = json.getString("idref"); |
---|
| | int area = json.getInt("area"); |
---|
| | |
---|
| | ps.setString(1, idref); |
---|
| | ps.setString(2, gmlid); |
---|
| | ps.setInt(3, area); |
---|
| | ps.executeUpdate(); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | /** |
---|
| | * removedデータをファイルに出力する |
---|
| | * |
---|
| | * @param removedFile |
---|
| | * @throws SQLException |
---|
| | * @throws java.io.IOException |
---|
| | */ |
---|
| | public void outputRemoved(File removedFile) throws SQLException, IOException { |
---|
| | String whereStr = "WHERE (removed=?)"; |
---|
| | String fromStr = "FROM "+ DbBusstop.TABLE_NAME; |
---|
| | String sortStr = "ORDER BY area,gmlid"; |
---|
| | String sql = String.format("SELECT * %s %s %s", fromStr, whereStr, sortStr); |
---|
| | |
---|
| | removedFile.deleteOnExit(); |
---|
| | try (FileWriter fw = new FileWriter(removedFile); |
---|
| | PreparedStatement ps1 = hsqldb.prepareStatement(sql)) |
---|
| | { |
---|
| | System.out.println(sql); |
---|
| | ps1.setBoolean(1, true); |
---|
| | try (ResultSet rset1 = ps1.executeQuery()) { |
---|
| | while (rset1.next()) { |
---|
| | String gmlid = rset1.getString("gmlid"); |
---|
| | int area = rset1.getInt("area"); |
---|
| | String idref = rset1.getString("idref"); |
---|
| | |
---|
| | JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(); |
---|
| | jsonBuilder.add("gmlid", gmlid); |
---|
| | jsonBuilder.add("idref", idref); |
---|
| | jsonBuilder.add("area", area); |
---|
| | JsonObject value = jsonBuilder.build(); |
---|
| | String line = value.toString(); |
---|
| | |
---|
| | fw.write(line); |
---|
| | fw.write("\n"); |
---|
| | fw.flush(); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | boolean isNull(String vstr) { |
---|
| | if (vstr == null) { |
---|
| | return true; |
---|
| | } |
---|
| |
---|
| | |