Skip to content

Commit a24c2c3

Browse files
committed
initialise NominatimResult through static functions
1 parent ccb9245 commit a24c2c3

File tree

5 files changed

+136
-124
lines changed

5 files changed

+136
-124
lines changed

src/main/java/de/komoot/photon/nominatim/ImportThread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ImportThread {
1515
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(ImportThread.class);
1616

1717
private static final int PROGRESS_INTERVAL = 50000;
18-
private static final NominatimResult FINAL_DOCUMENT = new NominatimResult(new PhotonDoc(0, null, 0, null, null));
18+
private static final NominatimResult FINAL_DOCUMENT = NominatimResult.fromAddress(new PhotonDoc(0, null, 0, null, null), null);
1919
private final BlockingQueue<NominatimResult> documents = new LinkedBlockingDeque<>(100);
2020
private final AtomicLong counter = new AtomicLong();
2121
private final Importer importer;

src/main/java/de/komoot/photon/nominatim/NominatimConnector.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class NominatimConnector {
2323

2424
private static final String SELECT_COLS_PLACEX = "SELECT place_id, osm_type, osm_id, class, type, name, postcode, address, extratags, ST_Envelope(geometry) AS bbox, parent_place_id, linked_place_id, rank_address, rank_search, importance, country_code, centroid";
2525
private static final String SELECT_COLS_ADDRESS = "SELECT p.name, p.class, p.type, p.rank_address";
26+
private static final String SELECT_OSMLINE_OLD_STYLE = "SELECT place_id, osm_id, parent_place_id, startnumber, endnumber, interpolationtype, postcode, country_code, linegeo";
27+
private static final String SELECT_OSMLINE_NEW_STYLE = "SELECT place_id, osm_id, parent_place_id, startnumber, endnumber, step, postcode, country_code, linegeo";
2628

2729
private final DBDataAdapter dbutils;
2830
private final JdbcTemplate template;
@@ -33,8 +35,8 @@ public class NominatimConnector {
3335
* This may be old-style interpolation (using interpolationtype) or
3436
* new-style interpolation (using step).
3537
*/
36-
private final RowMapper<NominatimResult> osmlineRowMapper;
37-
private final String selectOsmlineSql;
38+
private final RowMapper<NominatimResult> osmlineToNominatimResult;
39+
private final boolean hasNewStyleInterpolation;
3840

3941

4042
/**
@@ -77,17 +79,14 @@ public NominatimConnector(String host, int port, String database, String usernam
7779

7880
doc.setCountry(countryNames.get(rs.getString("country_code")));
7981

80-
NominatimResult result = new NominatimResult(doc);
81-
result.addHousenumbersFromAddress(address);
82-
83-
return result;
82+
return NominatimResult.fromAddress(doc, address);
8483
};
8584

85+
hasNewStyleInterpolation = dbutils.hasColumn(template, "location_property_osmline", "step");
8686
// Setup handling of interpolation table. There are two different formats depending on the Nominatim version.
87-
if (dbutils.hasColumn(template, "location_property_osmline", "step")) {
87+
if (hasNewStyleInterpolation) {
8888
// new-style interpolations
89-
selectOsmlineSql = "SELECT place_id, osm_id, parent_place_id, startnumber, endnumber, step, postcode, country_code, linegeo";
90-
osmlineRowMapper = (rs, rownum) -> {
89+
osmlineToNominatimResult = (rs, rownum) -> {
9190
Geometry geometry = dbutils.extractGeometry(rs, "linegeo");
9291

9392
PhotonDoc doc = new PhotonDoc(rs.getLong("place_id"), "W", rs.getLong("osm_id"),
@@ -100,16 +99,13 @@ public NominatimConnector(String host, int port, String database, String usernam
10099

101100
doc.setCountry(countryNames.get(rs.getString("country_code")));
102101

103-
NominatimResult result = new NominatimResult(doc);
104-
result.addHouseNumbersFromInterpolation(rs.getLong("startnumber"), rs.getLong("endnumber"),
102+
return NominatimResult.fromInterpolation(
103+
doc, rs.getLong("startnumber"), rs.getLong("endnumber"),
105104
rs.getLong("step"), geometry);
106-
107-
return result;
108105
};
109106
} else {
110107
// old-style interpolations
111-
selectOsmlineSql = "SELECT place_id, osm_id, parent_place_id, startnumber, endnumber, interpolationtype, postcode, country_code, linegeo";
112-
osmlineRowMapper = (rs, rownum) -> {
108+
osmlineToNominatimResult = (rs, rownum) -> {
113109
Geometry geometry = dbutils.extractGeometry(rs, "linegeo");
114110

115111
PhotonDoc doc = new PhotonDoc(rs.getLong("place_id"), "W", rs.getLong("osm_id"),
@@ -122,11 +118,9 @@ public NominatimConnector(String host, int port, String database, String usernam
122118

123119
doc.setCountry(countryNames.get(rs.getString("country_code")));
124120

125-
NominatimResult result = new NominatimResult(doc);
126-
result.addHouseNumbersFromInterpolation(rs.getLong("startnumber"), rs.getLong("endnumber"),
121+
return NominatimResult.fromInterpolation(
122+
doc, rs.getLong("startnumber"), rs.getLong("endnumber"),
127123
rs.getString("interpolationtype"), geometry);
128-
129-
return result;
130124
};
131125
}
132126
}
@@ -165,9 +159,10 @@ public List<PhotonDoc> getByPlaceId(long placeId) {
165159
}
166160

167161
public List<PhotonDoc> getInterpolationsByPlaceId(long placeId) {
168-
List<NominatimResult> result = template.query(selectOsmlineSql
169-
+ " FROM location_property_osmline WHERE place_id = ? and indexed_status = 0",
170-
osmlineRowMapper, placeId);
162+
List<NominatimResult> result = template.query(
163+
(hasNewStyleInterpolation ? SELECT_OSMLINE_NEW_STYLE : SELECT_OSMLINE_OLD_STYLE)
164+
+ " FROM location_property_osmline WHERE place_id = ? and indexed_status = 0",
165+
osmlineToNominatimResult, placeId);
171166

172167
return result.isEmpty() ? null : result.get(0).getDocsWithHousenumber();
173168
}
@@ -248,16 +243,15 @@ public void readCountry(String countryCode, ImportThread importThread) {
248243

249244
doc.setCountry(cnames);
250245

251-
NominatimResult result = new NominatimResult(doc);
252-
result.addHousenumbersFromAddress(address);
246+
var result = NominatimResult.fromAddress(doc, address);
253247

254248
if (result.isUsefulForIndex()) {
255249
importThread.addDocument(result);
256250
}
257251
};
258252

259253
final RowCallbackHandler osmlineMapper = rs -> {
260-
NominatimResult docs = osmlineRowMapper.mapRow(rs, 0);
254+
NominatimResult docs = osmlineToNominatimResult.mapRow(rs, 0);
261255
assert (docs != null);
262256

263257
if (docs.isUsefulForIndex()) {
@@ -270,16 +264,18 @@ public void readCountry(String countryCode, ImportThread importThread) {
270264
" WHERE linked_place_id IS NULL AND centroid IS NOT NULL AND country_code is null" +
271265
" ORDER BY geometry_sector, parent_place_id; ", placeMapper);
272266

273-
template.query(selectOsmlineSql + " FROM location_property_osmline " +
274-
"WHERE startnumber is not null AND country_code is null " +
267+
template.query((hasNewStyleInterpolation ? SELECT_OSMLINE_NEW_STYLE : SELECT_OSMLINE_OLD_STYLE) +
268+
" FROM location_property_osmline" +
269+
" WHERE startnumber is not null AND country_code is null" +
275270
" ORDER BY geometry_sector, parent_place_id; ", osmlineMapper);
276271
} else {
277272
template.query(SELECT_COLS_PLACEX + " FROM placex " +
278273
" WHERE linked_place_id IS NULL AND centroid IS NOT NULL AND country_code = ?" +
279274
" ORDER BY geometry_sector, parent_place_id; ", placeMapper, countryCode);
280275

281-
template.query(selectOsmlineSql + " FROM location_property_osmline " +
282-
"WHERE startnumber is not null AND country_code = ?" +
276+
template.query((hasNewStyleInterpolation ? SELECT_OSMLINE_NEW_STYLE : SELECT_OSMLINE_OLD_STYLE) +
277+
" FROM location_property_osmline" +
278+
" WHERE startnumber is not null AND country_code = ?" +
283279
" ORDER BY geometry_sector, parent_place_id; ", osmlineMapper, countryCode);
284280

285281
}
Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package de.komoot.photon.nominatim;
22

3+
import de.komoot.photon.PhotonDoc;
34
import org.locationtech.jts.geom.Geometry;
45
import org.locationtech.jts.geom.GeometryFactory;
56
import org.locationtech.jts.geom.Point;
67
import org.locationtech.jts.linearref.LengthIndexedLine;
7-
import de.komoot.photon.PhotonDoc;
8+
import org.slf4j.Logger;
89

910
import java.util.*;
1011
import java.util.regex.Pattern;
@@ -20,7 +21,7 @@ class NominatimResult {
2021
private static final Pattern HOUSENUMBER_CHECK = Pattern.compile("(\\A|.*,)[^\\d,]{3,}(,.*|\\Z)");
2122
private static final Pattern HOUSENUMBER_SPLIT = Pattern.compile("\\s*[;,]\\s*");
2223

23-
public NominatimResult(PhotonDoc baseobj) {
24+
private NominatimResult(PhotonDoc baseobj) {
2425
doc = baseobj;
2526
housenumbers = null;
2627
}
@@ -58,7 +59,7 @@ List<PhotonDoc> getDocsWithHousenumber() {
5859
*
5960
* @param str House number string. May be null, in which case nothing is added.
6061
*/
61-
public void addHousenumbersFromString(String str) {
62+
private void addHousenumbersFromString(String str) {
6263
if (str == null || str.isEmpty())
6364
return;
6465

@@ -68,9 +69,6 @@ public void addHousenumbersFromString(String str) {
6869
return;
6970
}
7071

71-
if (housenumbers == null)
72-
housenumbers = new HashMap<>();
73-
7472
String[] parts = HOUSENUMBER_SPLIT.split(str);
7573
for (String part : parts) {
7674
String h = part.trim();
@@ -79,14 +77,17 @@ public void addHousenumbersFromString(String str) {
7977
}
8078
}
8179

82-
public void addHousenumbersFromAddress(Map<String, String> address) {
83-
if (address == null) {
84-
return;
80+
public static NominatimResult fromAddress(PhotonDoc doc, Map<String, String> address) {
81+
NominatimResult result = new NominatimResult(doc);
82+
83+
if (address != null) {
84+
result.housenumbers = new HashMap<>();
85+
result.addHousenumbersFromString(address.get("housenumber"));
86+
result.addHousenumbersFromString(address.get("streetnumber"));
87+
result.addHousenumbersFromString(address.get("conscriptionnumber"));
8588
}
8689

87-
addHousenumbersFromString(address.get("housenumber"));
88-
addHousenumbersFromString(address.get("streetnumber"));
89-
addHousenumbersFromString(address.get("conscriptionnumber"));
90+
return result;
9091
}
9192

9293
/**
@@ -101,35 +102,36 @@ public void addHousenumbersFromAddress(Map<String, String> address) {
101102
* @param interpoltype Kind of interpolation (odd, even or all).
102103
* @param geom Geometry of the interpolation line.
103104
*/
104-
public void addHouseNumbersFromInterpolation(long first, long last, String interpoltype, Geometry geom) {
105-
if (last <= first || (last - first) > 1000)
106-
return;
105+
public static NominatimResult fromInterpolation(PhotonDoc doc, long first, long last, String interpoltype, Geometry geom) {
106+
NominatimResult result = new NominatimResult(doc);
107+
if (last > first && (last - first) < 1000) {
108+
result.housenumbers = new HashMap<>();
107109

108-
if (housenumbers == null)
109-
housenumbers = new HashMap<>();
110-
111-
LengthIndexedLine line = new LengthIndexedLine(geom);
112-
double si = line.getStartIndex();
113-
double ei = line.getEndIndex();
114-
double lstep = (ei - si) / (last - first);
115-
116-
// leave out first and last, they have a distinct OSM node that is already indexed
117-
long step = 2;
118-
long num = 1;
119-
if (interpoltype.equals("odd")) {
120-
if (first % 2 == 1)
121-
++num;
122-
} else if (interpoltype.equals("even")) {
123-
if (first % 2 == 0)
124-
++num;
125-
} else {
126-
step = 1;
127-
}
110+
LengthIndexedLine line = new LengthIndexedLine(geom);
111+
double si = line.getStartIndex();
112+
double ei = line.getEndIndex();
113+
double lstep = (ei - si) / (last - first);
128114

129-
GeometryFactory fac = geom.getFactory();
130-
for (; first + num < last; num += step) {
131-
housenumbers.put(String.valueOf(num + first), fac.createPoint(line.extractPoint(si + lstep * num)));
115+
// leave out first and last, they have a distinct OSM node that is already indexed
116+
long step = 2;
117+
long num = 1;
118+
if (interpoltype.equals("odd")) {
119+
if (first % 2 == 1)
120+
++num;
121+
} else if (interpoltype.equals("even")) {
122+
if (first % 2 == 0)
123+
++num;
124+
} else {
125+
step = 1;
126+
}
127+
128+
GeometryFactory fac = geom.getFactory();
129+
for (; first + num < last; num += step) {
130+
result.housenumbers.put(String.valueOf(num + first), fac.createPoint(line.extractPoint(si + lstep * num)));
131+
}
132132
}
133+
134+
return result;
133135
}
134136

135137
/**
@@ -143,25 +145,27 @@ public void addHouseNumbersFromInterpolation(long first, long last, String inter
143145
* @param step Gap to leave between each interpolated house number.
144146
* @param geom Geometry of the interpolation line.
145147
*/
146-
public void addHouseNumbersFromInterpolation(long first, long last, long step, Geometry geom) {
147-
if (last < first || (last - first) > 1000)
148-
return;
149-
150-
if (housenumbers == null)
151-
housenumbers = new HashMap<>();
152-
153-
if (last == first) {
154-
housenumbers.put(String.valueOf(first), geom.getCentroid());
155-
} else {
156-
LengthIndexedLine line = new LengthIndexedLine(geom);
157-
double si = line.getStartIndex();
158-
double ei = line.getEndIndex();
159-
double lstep = (ei - si) / (last - first);
160-
161-
GeometryFactory fac = geom.getFactory();
162-
for (long num = 0; first + num <= last; num += step) {
163-
housenumbers.put(String.valueOf(num + first), fac.createPoint(line.extractPoint(si + lstep * num)));
148+
public static NominatimResult fromInterpolation(PhotonDoc doc, long first, long last, long step, Geometry geom) {
149+
NominatimResult result = new NominatimResult(doc);
150+
if (last >= first && (last - first) < 1000) {
151+
result.housenumbers = new HashMap<>();
152+
153+
if (last == first) {
154+
result.housenumbers.put(String.valueOf(first), geom.getCentroid());
155+
} else {
156+
LengthIndexedLine line = new LengthIndexedLine(geom);
157+
double si = line.getStartIndex();
158+
double ei = line.getEndIndex();
159+
double lstep = (ei - si) / (last - first);
160+
161+
GeometryFactory fac = geom.getFactory();
162+
for (long num = 0; first + num <= last; num += step) {
163+
result.housenumbers.put(String.valueOf(num + first), fac.createPoint(line.extractPoint(si + lstep * num)));
164+
}
164165
}
166+
165167
}
168+
169+
return result;
166170
}
167171
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.komoot.photon.nominatim.model;
2+
3+
import de.komoot.photon.PhotonDoc;
4+
import org.springframework.jdbc.core.RowMapper;
5+
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
9+
public class OsmlineRowMapper implements RowMapper<PhotonDoc> {
10+
@Override
11+
public PhotonDoc mapRow(ResultSet rs, int rowNum) throws SQLException {
12+
return new PhotonDoc(
13+
rs.getLong("place_id"),
14+
"W", rs.getLong("osm_id"),
15+
"place", "house_number")
16+
.parentPlaceId(rs.getLong("parent_place_id"))
17+
.countryCode(rs.getString("country_code"))
18+
.postcode(rs.getString("postcode"));
19+
}
20+
}

0 commit comments

Comments
 (0)