Skip to content

Commit b1e2fd0

Browse files
authored
Merge pull request abrensch#828 from afischerdev/pseudo-dbtags
Enable database access for pseudo tags
2 parents 209c581 + 0ca2c3b commit b1e2fd0

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

brouter-map-creator/src/main/java/btools/mapcreator/DatabasePseudoTagProvider.java

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,69 @@ private static void appendDBTag(StringBuilder sb, ResultSet rs, String name) thr
9090
}
9191
}
9292

93-
public DatabasePseudoTagProvider(String filename) {
93+
public DatabasePseudoTagProvider(String filename, String jdbcurl) {
94+
if (filename != null) doFileImport(filename);
95+
if (jdbcurl != null) doDatabaseImport(jdbcurl);
96+
}
97+
98+
private void doDatabaseImport(String jdbcurl) {
99+
100+
try (Connection conn = DriverManager.getConnection(jdbcurl)) {
101+
102+
System.out.println("DatabasePseudoTagProvider reading from database: " + jdbcurl);
103+
conn.setAutoCommit(false);
104+
105+
106+
Map<Map<String, String>, Map<String, String>> mapUnifier = new HashMap<>();
107+
CompactLongMap<Map<String, String>> data = new CompactLongMap<>();
108+
109+
String sql_all_tags = "SELECT * from all_tags";
110+
try(PreparedStatement psAllTags = conn.prepareStatement(sql_all_tags)) {
111+
112+
psAllTags.setFetchSize(100);
113+
114+
// process the results
115+
ResultSet rs = psAllTags.executeQuery();
116+
117+
long dbRows = 0L;
118+
while (rs.next()) {
119+
long osm_id = rs.getLong("losmid");
120+
Map<String, String> row = new HashMap<>(5);
121+
addDBTag(row, rs, "noise_class");
122+
addDBTag(row, rs, "river_class");
123+
addDBTag(row, rs, "forest_class");
124+
addDBTag(row, rs, "town_class");
125+
addDBTag(row, rs, "traffic_class");
126+
127+
// apply the instance-unifier for the row-map
128+
Map<String, String> knownRow = mapUnifier.get(row);
129+
if (knownRow != null) {
130+
row = knownRow;
131+
} else {
132+
mapUnifier.put(row, row);
133+
}
134+
data.put(osm_id, row);
135+
dbRows++;
136+
if (dbRows % 1000000L == 0L) {
137+
System.out.println(".. from database: rows =" + dbRows);
138+
}
139+
}
140+
System.out.println("freezing result map..");
141+
dbData = new FrozenLongMap<>(data);
142+
System.out.println("read from database: rows =" + dbData.size() + " unique rows=" + mapUnifier.size());
143+
}
144+
145+
} catch (SQLException g) {
146+
System.err.format("DatabasePseudoTagProvider execute sql .. SQL State: %s\n%s\n", g.getSQLState(), g.getMessage());
147+
System.exit(1);
148+
} catch (Exception f) {
149+
f.printStackTrace();
150+
System.exit(1);
151+
}
152+
153+
}
154+
155+
private void doFileImport(String filename) {
94156

95157
try (BufferedReader br = new BufferedReader(new InputStreamReader(
96158
filename.endsWith(".gz") ? new GZIPInputStream(new FileInputStream(filename)) : new FileInputStream(filename)))) {
@@ -158,12 +220,22 @@ private List<String> tokenize(String s) {
158220
return l;
159221
}
160222

161-
private static void addTag(Map<String, String> row, String s, String name) {
223+
private static void addTag(Map<String, String> row, String s, String name) {
162224
if (!s.isEmpty()) {
163225
row.put(name, s);
164226
}
165227
}
166228

229+
private static void addDBTag(Map<String, String> row, ResultSet rs, String name) {
230+
String v = null;
231+
try {
232+
v = rs.getString(name);
233+
} catch (Exception e) {}
234+
if (v != null) {
235+
row.put("estimated_" + name, v);
236+
}
237+
}
238+
167239
public void addTags(long osm_id, Map<String, String> map) {
168240

169241
if (map == null || !map.containsKey("highway")) {
@@ -194,6 +266,6 @@ public void addTags(long osm_id, Map<String, String> map) {
194266
pseudoTagsFound.put(key, cnt + 1L);
195267
}
196268
}
197-
198-
269+
270+
199271
}

brouter-map-creator/src/main/java/btools/mapcreator/OsmCutter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ private String statsLine() {
113113
}
114114

115115
public void setDbTagFilename(String filename) {
116-
dbPseudoTagProvider = new DatabasePseudoTagProvider(filename);
116+
dbPseudoTagProvider = new DatabasePseudoTagProvider(filename, null);
117+
}
118+
119+
public void setDbTagDatabase(String jdbcurl) {
120+
dbPseudoTagProvider = new DatabasePseudoTagProvider(null, jdbcurl);
117121
}
118122

119123
@Override

brouter-map-creator/src/main/java/btools/mapcreator/OsmFastCutter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class OsmFastCutter extends MapCreatorBase {
1313
public static void main(String[] args) throws Exception {
1414
System.out.println("*** OsmFastCutter: cut an osm map in node-tiles + way-tiles");
1515
if (args.length != 11 && args.length != 12 && args.length != 13) {
16-
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]";
16+
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]";
1717

1818
System.out.println("usage: bzip2 -dc <map> | " + common);
1919
System.out.println("or : " + common + " <inputfile> ");
@@ -37,10 +37,16 @@ public static void main(String[] args) throws Exception {
3737
);
3838
}
3939

40-
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 {
40+
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 {
4141
// **** run OsmCutter ****
4242
OsmCutter cutter = new OsmCutter();
43-
if (dbTagFilename != null) cutter.setDbTagFilename(dbTagFilename);
43+
if (dbTagInfo != null) {
44+
if (dbTagInfo.toLowerCase().startsWith("jdbc")) {
45+
cutter.setDbTagDatabase(dbTagInfo);
46+
} else {
47+
cutter.setDbTagFilename(dbTagInfo);
48+
}
49+
}
4450

4551
// ... inject WayCutter
4652
cutter.wayCutter = new WayCutter();

0 commit comments

Comments
 (0)