/
NominatimConnector.java
255 lines (223 loc) · 8.49 KB
/
NominatimConnector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package de.komoot.photon.importer.nominatim;
import com.neovisionaries.i18n.CountryCode;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import de.komoot.photon.importer.Importer;
import de.komoot.photon.importer.model.PhotonDoc;
import de.komoot.photon.importer.nominatim.model.AddressRow;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbcp.BasicDataSource;
import org.postgis.jts.JtsWrapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicLong;
/**
* Export nominatim data
*
* @author felix, christoph
*/
@Slf4j
public class NominatimConnector {
private final JdbcTemplate template;
private Map<String, Map<String, String>> countryNames;
/**
* maps a placex row in nominatim to a photon doc, some attributes are still missing and can be derived by connected address items.
*/
private final RowMapper<PhotonDoc> placeRowMapper = new RowMapper<PhotonDoc>() {
@Override
public PhotonDoc mapRow(ResultSet rs, int rowNum) throws SQLException {
Double importance = rs.getDouble("importance");
if(rs.wasNull()) {
// https://github.com/komoot/photon/issues/12
int rankSearch = rs.getInt("rank_search");
importance = 0.75 - rankSearch / 40d;
}
Geometry geometry = DBUtils.extractGeometry(rs, "bbox");
Envelope envelope = geometry != null ? geometry.getEnvelopeInternal() : null;
PhotonDoc doc = new PhotonDoc(
rs.getLong("place_id"),
rs.getString("osm_type"),
rs.getLong("osm_id"),
rs.getString("class"),
rs.getString("type"),
DBUtils.getMap(rs, "name"),
rs.getString("housenumber"),
DBUtils.getMap(rs, "extratags"),
envelope,
rs.getLong("parent_place_id"),
importance,
CountryCode.getByCode(rs.getString("calculated_country_code")),
(Point) DBUtils.extractGeometry(rs, "centroid"),
rs.getLong("linked_place_id"),
rs.getInt("rank_search")
);
doc.setPostcode(rs.getString("postcode"));
doc.setCountry(getCountryNames(rs.getString("calculated_country_code")));
return doc;
}
};
private final String selectColsPlaceX = "place_id, osm_type, osm_id, class, type, name, housenumber, postcode, extratags, ST_Envelope(geometry) AS bbox, parent_place_id, linked_place_id, rank_search, importance, calculated_country_code, centroid";
private Importer importer;
private Map<String, String> getCountryNames(String countrycode) {
if(countryNames == null) {
countryNames = new HashMap<String, Map<String, String>>();
template.query("SELECT country_code, name FROM country_name;", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
countryNames.put(rs.getString("country_code"), DBUtils.getMap(rs, "name"));
}
}
);
}
return countryNames.get(countrycode);
}
/**
* @param host database host
* @param port database port
* @param database database name
* @param username db username
* @param password db username's password
*/
public NominatimConnector(String host, int port, String database, String username, String password) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(String.format("jdbc:postgres_jts://%s:%d/%s", host, port, database));
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(JtsWrapper.class.getCanonicalName());
dataSource.setDefaultAutoCommit(false);
template = new JdbcTemplate(dataSource);
template.setFetchSize(100000);
}
public void setImporter(Importer importer) {
this.importer = importer;
}
public PhotonDoc getByPlaceId(long placeId) {
return template.queryForObject("SELECT " + selectColsPlaceX + " FROM placex WHERE place_id = ?", new Object[]{placeId}, placeRowMapper);
}
public List<AddressRow> getAddresses(PhotonDoc doc) {
long placeId = doc.getPlaceId();
if(doc.getRankSearch() > 28)
placeId = doc.getParentPlaceId();
return template.query("SELECT p.place_id, p.name, p.class, p.type, p.rank_address, p.admin_level, p.postcode FROM placex p, place_addressline pa WHERE p.place_id = pa.address_place_id and pa.place_id = ? and pa.cached_rank_address > 4 and pa.address_place_id != ? and pa.isaddress order by rank_address desc,fromarea desc,distance asc,rank_search desc", new Object[]{placeId, doc.getPlaceId()}, new RowMapper<AddressRow>() {
@Override
public AddressRow mapRow(ResultSet rs, int rowNum) throws SQLException {
Integer adminLevel = rs.getInt("admin_level");
if(rs.wasNull()) {
adminLevel = null;
}
return new AddressRow(
rs.getLong("place_id"),
DBUtils.getMap(rs, "name"),
rs.getString("class"),
rs.getString("type"),
rs.getInt("rank_address"),
adminLevel,
rs.getString("postcode")
);
}
});
}
static final PhotonDoc FINAL_DOCUMENT =
new PhotonDoc(0, null, 0, null, null, null, null, null, null, 0, 0, null, null, 0, 0);
private class ImportThread implements Runnable {
private BlockingQueue<PhotonDoc> documents;
public ImportThread(BlockingQueue<PhotonDoc> documents) {
this.documents = documents;
}
@Override
public void run() {
while(true) {
PhotonDoc doc = null;
try {
doc = documents.take();
if(doc == FINAL_DOCUMENT)
break;
importer.add(doc);
} catch(InterruptedException e) { /* safe to ignore? */ }
}
importer.finish();
}
}
/**
* parses every relevant row in placex, creates a corresponding document and calls the {@link #importer} for every document
*/
public void readEntireDatabase() {
log.info("start importing documents from nominatim ...");
final AtomicLong counter = new AtomicLong();
final int progressInterval = 5000;
final long startMillis = System.currentTimeMillis();
final BlockingQueue<PhotonDoc> documents = new LinkedBlockingDeque<PhotonDoc>(20);
Thread importThread = new Thread(new ImportThread(documents));
importThread.start();
template.query("SELECT " + selectColsPlaceX + " FROM placex WHERE linked_place_id IS NULL order by geometry_sector; ", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
// turns a placex row into a photon document that gathers all de-normalised informations
PhotonDoc doc = placeRowMapper.mapRow(rs, 0);
if(!doc.isUsefulForIndex()) return; // do not import document
// finalize document by taking into account the higher level placex rows assigned to this row
final List<AddressRow> addresses = getAddresses(doc);
for(AddressRow address : addresses) {
if(address.hasPostcode() && doc.getPostcode() == null) {
doc.setPostcode(address.getPostcode());
}
if(address.isCity()) {
if(doc.getCity() != null) {
doc.getContext().add(doc.getCity());
}
doc.setCity(address.getName());
continue;
}
if(address.isStreet() && doc.getStreet() == null) {
doc.setStreet(address.getName());
continue;
}
if(address.isState() && doc.getState() == null) {
doc.setState(address.getName());
continue;
}
// no specifically handled item, check if useful for context
if(address.isUsefulForContext()) {
doc.getContext().add(address.getName());
}
}
//importer.add(doc);
while(true) {
try {
documents.put(doc);
} catch(InterruptedException e) {
log.warn("Thread interrupted while placing document in queue.");
continue;
}
break;
}
if(counter.incrementAndGet() % progressInterval == 0) {
final double documentsPerSecond = 1000d * counter.longValue() / (System.currentTimeMillis() - startMillis);
log.info(String.format("imported %s documents [%.1f/second]", MessageFormat.format("{0}", counter.longValue()), documentsPerSecond));
}
}
});
while(true) {
try {
documents.put(FINAL_DOCUMENT);
importThread.join();
} catch(InterruptedException e) {
log.warn("Thread interrupted while placing document in queue.");
continue;
}
break;
}
//importer.finish();
log.info(String.format("finished import of %s photon documents.", MessageFormat.format("{0}", counter.longValue())));
}
}