Merge pull request #828 from afischerdev/pseudo-dbtags
Some checks are pending
Docker / build (push) Waiting to run
Java CI with Gradle / build (push) Waiting to run

Enable database access for pseudo tags
This commit is contained in:
afischerdev 2025-08-27 18:52:44 +02:00 committed by GitHub
commit b1e2fd03c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 90 additions and 8 deletions

View file

@ -90,7 +90,69 @@ public class DatabasePseudoTagProvider {
} }
} }
public DatabasePseudoTagProvider(String filename) { public DatabasePseudoTagProvider(String filename, String jdbcurl) {
if (filename != null) doFileImport(filename);
if (jdbcurl != null) doDatabaseImport(jdbcurl);
}
private void doDatabaseImport(String jdbcurl) {
try (Connection conn = DriverManager.getConnection(jdbcurl)) {
System.out.println("DatabasePseudoTagProvider reading from database: " + jdbcurl);
conn.setAutoCommit(false);
Map<Map<String, String>, Map<String, String>> mapUnifier = new HashMap<>();
CompactLongMap<Map<String, String>> data = new CompactLongMap<>();
String sql_all_tags = "SELECT * from all_tags";
try(PreparedStatement psAllTags = conn.prepareStatement(sql_all_tags)) {
psAllTags.setFetchSize(100);
// process the results
ResultSet rs = psAllTags.executeQuery();
long dbRows = 0L;
while (rs.next()) {
long osm_id = rs.getLong("losmid");
Map<String, String> row = new HashMap<>(5);
addDBTag(row, rs, "noise_class");
addDBTag(row, rs, "river_class");
addDBTag(row, rs, "forest_class");
addDBTag(row, rs, "town_class");
addDBTag(row, rs, "traffic_class");
// apply the instance-unifier for the row-map
Map<String, String> knownRow = mapUnifier.get(row);
if (knownRow != null) {
row = knownRow;
} else {
mapUnifier.put(row, row);
}
data.put(osm_id, row);
dbRows++;
if (dbRows % 1000000L == 0L) {
System.out.println(".. from database: rows =" + dbRows);
}
}
System.out.println("freezing result map..");
dbData = new FrozenLongMap<>(data);
System.out.println("read from database: rows =" + dbData.size() + " unique rows=" + mapUnifier.size());
}
} catch (SQLException g) {
System.err.format("DatabasePseudoTagProvider execute sql .. SQL State: %s\n%s\n", g.getSQLState(), g.getMessage());
System.exit(1);
} catch (Exception f) {
f.printStackTrace();
System.exit(1);
}
}
private void doFileImport(String filename) {
try (BufferedReader br = new BufferedReader(new InputStreamReader( try (BufferedReader br = new BufferedReader(new InputStreamReader(
filename.endsWith(".gz") ? new GZIPInputStream(new FileInputStream(filename)) : new FileInputStream(filename)))) { filename.endsWith(".gz") ? new GZIPInputStream(new FileInputStream(filename)) : new FileInputStream(filename)))) {
@ -158,12 +220,22 @@ public class DatabasePseudoTagProvider {
return l; return l;
} }
private static void addTag(Map<String, String> row, String s, String name) { private static void addTag(Map<String, String> row, String s, String name) {
if (!s.isEmpty()) { if (!s.isEmpty()) {
row.put(name, s); row.put(name, s);
} }
} }
private static void addDBTag(Map<String, String> row, ResultSet rs, String name) {
String v = null;
try {
v = rs.getString(name);
} catch (Exception e) {}
if (v != null) {
row.put("estimated_" + name, v);
}
}
public void addTags(long osm_id, Map<String, String> map) { public void addTags(long osm_id, Map<String, String> map) {
if (map == null || !map.containsKey("highway")) { if (map == null || !map.containsKey("highway")) {
@ -194,6 +266,6 @@ public class DatabasePseudoTagProvider {
pseudoTagsFound.put(key, cnt + 1L); pseudoTagsFound.put(key, cnt + 1L);
} }
} }
} }

View file

@ -113,7 +113,11 @@ public class OsmCutter extends MapCreatorBase {
} }
public void setDbTagFilename(String filename) { public void setDbTagFilename(String filename) {
dbPseudoTagProvider = new DatabasePseudoTagProvider(filename); dbPseudoTagProvider = new DatabasePseudoTagProvider(filename, null);
}
public void setDbTagDatabase(String jdbcurl) {
dbPseudoTagProvider = new DatabasePseudoTagProvider(null, jdbcurl);
} }
@Override @Override

View file

@ -13,7 +13,7 @@ public class OsmFastCutter extends MapCreatorBase {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.out.println("*** OsmFastCutter: cut an osm map in node-tiles + way-tiles"); System.out.println("*** OsmFastCutter: cut an osm map in node-tiles + way-tiles");
if (args.length != 11 && args.length != 12 && args.length != 13) { if (args.length != 11 && args.length != 12 && args.length != 13) {
String common = "java OsmFastCutter <lookup-file> <node-dir> <way-dir> <node55-dir> <way55-dir> <border-file> <out-rel-file> <out-res-file> <filter-profile> <report-profile> <check-profile> <map-file> [db-tag-filename]"; String common = "java OsmFastCutter <lookup-file> <node-dir> <way-dir> <node55-dir> <way55-dir> <border-file> <out-rel-file> <out-res-file> <filter-profile> <report-profile> <check-profile> <map-file> [db-tag-filename | db-tag-jdbcurl]";
System.out.println("usage: bzip2 -dc <map> | " + common); System.out.println("usage: bzip2 -dc <map> | " + common);
System.out.println("or : " + common + " <inputfile> "); System.out.println("or : " + common + " <inputfile> ");
@ -37,10 +37,16 @@ public class OsmFastCutter extends MapCreatorBase {
); );
} }
public static void doCut(File lookupFile, File nodeDir, File wayDir, File node55Dir, File way55Dir, File borderFile, File relFile, File resFile, File profileAll, File profileReport, File profileCheck, File mapFile, String dbTagFilename) throws Exception { public static void doCut(File lookupFile, File nodeDir, File wayDir, File node55Dir, File way55Dir, File borderFile, File relFile, File resFile, File profileAll, File profileReport, File profileCheck, File mapFile, String dbTagInfo) throws Exception {
// **** run OsmCutter **** // **** run OsmCutter ****
OsmCutter cutter = new OsmCutter(); OsmCutter cutter = new OsmCutter();
if (dbTagFilename != null) cutter.setDbTagFilename(dbTagFilename); if (dbTagInfo != null) {
if (dbTagInfo.toLowerCase().startsWith("jdbc")) {
cutter.setDbTagDatabase(dbTagInfo);
} else {
cutter.setDbTagFilename(dbTagInfo);
}
}
// ... inject WayCutter // ... inject WayCutter
cutter.wayCutter = new WayCutter(); cutter.wayCutter = new WayCutter();