6 |
import java.util.Collections; |
import java.util.Collections; |
7 |
import java.util.Date; |
import java.util.Date; |
8 |
import java.util.HashMap; |
import java.util.HashMap; |
|
import java.util.HashSet; |
|
9 |
import java.util.List; |
import java.util.List; |
10 |
import java.util.Map; |
import java.util.Map; |
11 |
|
import java.util.Map.Entry; |
12 |
import java.util.Set; |
import java.util.Set; |
13 |
|
import java.util.TreeMap; |
14 |
|
import java.util.TreeSet; |
15 |
import java.util.concurrent.ConcurrentHashMap; |
import java.util.concurrent.ConcurrentHashMap; |
16 |
|
|
17 |
|
import org.apache.commons.lang3.StringUtils; |
18 |
|
|
19 |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
20 |
import dk.daoas.daoadresseservice.beans.Address; |
import dk.daoas.daoadresseservice.beans.Address; |
21 |
|
import dk.daoas.daoadresseservice.beans.AliasBean; |
22 |
import dk.daoas.daoadresseservice.beans.DataStatisticsBean; |
import dk.daoas.daoadresseservice.beans.DataStatisticsBean; |
23 |
import dk.daoas.daoadresseservice.beans.ExtendedBean; |
import dk.daoas.daoadresseservice.beans.ExtendedBean; |
24 |
import dk.daoas.daoadresseservice.beans.HundredePctBean; |
import dk.daoas.daoadresseservice.beans.HundredePctBean; |
25 |
|
import dk.daoas.daoadresseservice.beans.SearchRequest; |
26 |
import dk.daoas.daoadresseservice.beans.SearchResult; |
import dk.daoas.daoadresseservice.beans.SearchResult; |
27 |
import dk.daoas.daoadresseservice.beans.SearchResult.Status; |
import dk.daoas.daoadresseservice.beans.SearchResult.Status; |
28 |
import dk.daoas.daoadresseservice.db.DatabaseLayer; |
import dk.daoas.daoadresseservice.db.DatabaseLayer; |
29 |
|
import dk.daoas.daoadresseservice.util.DaoUtils; |
30 |
|
import dk.daoas.daoadresseservice.util.DeduplicateHelper; |
31 |
|
import dk.daoas.daoadresseservice.util.NaturalOrderComparator; |
32 |
|
import dk.daoas.daoadresseservice.util.NearestShortTreeMap; |
33 |
|
|
34 |
public class AdressSearch { |
public class AddressSearch { |
35 |
|
|
36 |
private Map<Integer, Map<String,Long>> searchPostnrVejnavnGadeid; |
private Map<Short, Map<String,Integer>> searchPostnrVejnavnGadeid; |
37 |
private Map<Long, Map<String,Address>> searchGadeidentAdresser; |
private Map<Integer, Map<Short, Map<String,Address>>> searchGadeidentAdresser; // Gadeident -> husnrumre -> litra |
38 |
|
|
39 |
|
|
40 |
private List<Address> alleAdresser; |
private List<Address> alleAdresser; |
41 |
|
|
42 |
private Map<String,Long> helperCache; |
private Map<String,Integer> helperCache; |
43 |
|
|
44 |
private DataStatisticsBean stats = new DataStatisticsBean(); |
private DataStatisticsBean stats = new DataStatisticsBean(); |
45 |
|
|
46 |
ServiceConfig config; |
private DatabaseLayer db; |
47 |
|
private ServiceConfig config; |
48 |
|
|
49 |
|
|
50 |
public AdressSearch(ServiceConfig config) { |
StreetnameHelper levenshteinHelper; |
51 |
|
StreetnameHelper metaphoneHelper; |
52 |
|
StreetnameHelper googleHelper; |
53 |
|
StreetnameHelper osmHelper; |
54 |
|
|
55 |
|
|
56 |
|
public AddressSearch(DatabaseLayer db, ServiceConfig config) { |
57 |
|
|
58 |
|
this.db = db; |
59 |
this.config = config; |
this.config = config; |
60 |
|
|
61 |
|
levenshteinHelper = new LevenshteinStreetnameHelper( this ); |
62 |
|
metaphoneHelper = new DoubleMetaphoneStreetnameHelper(config); |
63 |
|
googleHelper = new GoogleStreetnameHelper( config ); |
64 |
|
osmHelper = new OSMStreetnameHelper( config ); |
65 |
} |
} |
66 |
|
|
67 |
|
public Address getAddressSafe(int gadeid, short husnr, String litra) throws Exception { |
68 |
|
|
69 |
|
Map<Short, Map<String,Address>> gade = searchGadeidentAdresser.get(gadeid); |
70 |
|
|
71 |
|
if (gade == null) { |
72 |
|
throw new Exception("GadeID ikke fundet"); |
73 |
|
} |
74 |
|
|
75 |
|
Map<String,Address> litraList = gade.get(husnr); |
76 |
|
|
77 |
|
Address addr = litraList.get(litra); |
78 |
|
|
79 |
|
if (addr == null) { |
80 |
|
throw new Exception("Adresse ikke ikke fundet"); |
81 |
|
} |
82 |
|
|
83 |
|
return addr; |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
public SearchResult search(String postnrStr, String adresse) { |
public SearchResult search(String postnrStr, String adresse) { |
88 |
|
return search(postnrStr, adresse, false); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
public SearchResult search(String postnrStr, String adresse, boolean naermesteHusnr) { |
93 |
|
|
94 |
|
postnrStr = postnrStr.trim(); |
95 |
|
adresse = adresse.trim(); |
96 |
|
|
97 |
|
|
98 |
|
SearchRequest request = new SearchRequest(); |
99 |
SearchResult result = new SearchResult(); |
SearchResult result = new SearchResult(); |
100 |
|
|
|
int postnr=0; |
|
|
|
|
|
String helperSearchKey = ""; |
|
101 |
|
|
102 |
try { |
try { |
103 |
postnr = Integer.parseInt(postnrStr); |
request.postnr = Short.parseShort(postnrStr); |
104 |
} catch (Exception E) { |
} catch (Exception E) { |
105 |
result.status = Status.ERROR_UNKNOWN_POSTAL; |
result.status = Status.ERROR_UNKNOWN_POSTAL; |
106 |
return result; |
return result; |
107 |
} |
} |
108 |
|
|
|
Map<String,Long> postnrVeje = searchPostnrVejnavnGadeid.get(postnr); |
|
109 |
|
|
110 |
if (postnrVeje == null) { |
request.streetNames = searchPostnrVejnavnGadeid.get(request.postnr); |
111 |
|
|
112 |
|
if (request.streetNames == null) { |
113 |
result.status = Status.ERROR_UNKNOWN_POSTAL; |
result.status = Status.ERROR_UNKNOWN_POSTAL; |
114 |
return result; |
return result; |
115 |
} |
} |
116 |
|
|
117 |
|
|
118 |
result.splitReult = AddressUtils.splitAdresse(adresse); |
result.splitResult = AddressUtils.splitAdresse(adresse); |
119 |
|
request.vejnavn = result.splitResult.vej; |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
if (result.splitReult.husnr.length() == 0) { |
if (result.splitResult.husnr.length() == 0) { |
124 |
result.status = Status.ERROR_MISSING_HOUSENUMBER; |
result.status = Status.ERROR_MISSING_HOUSENUMBER; |
125 |
return result; |
return result; |
126 |
} |
} |
127 |
|
|
128 |
String vasketVejnavn = AddressUtils.vaskVejnavn( result.splitReult.vej ); |
result.vasketVejnavn = AddressUtils.vaskVejnavn( request.vejnavn ); |
|
|
|
|
Long gadeident = postnrVeje.get(vasketVejnavn); |
|
|
|
|
129 |
|
|
130 |
if ( gadeident == null) { |
|
131 |
helperSearchKey = "" + postnr + "/" + vasketVejnavn; |
if (result.vasketVejnavn.indexOf("pakkebo") > -1 |
132 |
gadeident = helperCache.get(helperSearchKey); |
|| result.vasketVejnavn.indexOf("pakkepost") > -1 |
133 |
|
|| result.vasketVejnavn.indexOf("postbo") > -1 // postbox/postboks |
134 |
|
|| result.vasketVejnavn.indexOf("døgnpost") > -1 |
135 |
|
|| result.vasketVejnavn.indexOf("døgnbo") > -1 // døgnbox // døgnboks |
136 |
|
|| result.vasketVejnavn.equals("id") |
137 |
|
) { |
138 |
|
result.status = Status.ERROR_POSTBOX; |
139 |
|
return result; |
140 |
} |
} |
141 |
|
|
142 |
|
// LookupStreetname er en indkapsling af alm + levenstein + google + osm |
143 |
|
Integer gadeident = lookupStretname(request, result, true); |
144 |
|
|
|
if ( gadeident == null) { |
|
|
if (config.useGoogle) { |
|
|
result.googleVej = GeocodeHelper.googleHelper(config, postnr, result.splitReult.vej ); |
|
|
result.google = true; |
|
|
|
|
|
if (result.googleVej != null) { |
|
|
String googleVasket = AddressUtils.vaskVejnavn( result.googleVej ); |
|
|
gadeident = postnrVeje.get(googleVasket); |
|
|
|
|
|
if (gadeident != null) { |
|
|
helperCache.put(helperSearchKey, gadeident); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
145 |
|
|
146 |
if ( gadeident == null) { |
/* |
147 |
if (config.useOpenStreetMaps) { |
* Hvis der ikke er direkte hits, prøver vi at skære ord af vejnavn |
148 |
result.osmVej = GeocodeHelper.openstreetmapHelper(postnr, result.splitReult.vej ); |
* |
149 |
result.osm = true; |
* <strike>Denne sektion er MEGET experimental da den øger antallet af kald til google/OSM væsentligt</strike>*/ |
150 |
|
if (gadeident == null) { |
151 |
if (result.osmVej != null) { |
final int MAX_TRIES = 4; |
152 |
String osmVasket = AddressUtils.vaskVejnavn( result.osmVej ); |
|
153 |
gadeident = postnrVeje.get(osmVasket); |
String vejnavnParts[] = request.vejnavn.split(" "); |
154 |
|
|
155 |
if (gadeident != null) { |
for (int i=1; i<=MAX_TRIES && i<vejnavnParts.length; i++) { |
156 |
helperCache.put(helperSearchKey, gadeident); |
String subVejnavn = DaoUtils.joinHelper(vejnavnParts, i); |
157 |
} |
|
158 |
} |
request.vejnavn = subVejnavn; |
159 |
} |
result.vasketVejnavn = AddressUtils.vaskVejnavn( request.vejnavn ); |
160 |
|
|
161 |
|
gadeident = lookupStretname(request, result, false);//prøv opslag MEN UDEN GOOGLE/OSM !!! |
162 |
|
|
163 |
|
//hvis vi fandt vejen ud fra substrengen |
164 |
|
if (gadeident != null) { |
165 |
|
break; |
166 |
|
} |
167 |
|
|
168 |
|
//hvis vi ikke fandt kopierer vi vejnavn fra split igen |
169 |
|
request.vejnavn = result.splitResult.vej; |
170 |
|
result.vasketVejnavn = AddressUtils.vaskVejnavn( request.vejnavn ); |
171 |
|
} |
172 |
} |
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
if (gadeident == null) { |
if (gadeident == null) { |
177 |
result.status = Status.ERROR_UNKNOWN_STREETNAME; |
result.status = Status.ERROR_UNKNOWN_STREETNAME; |
178 |
return result; |
return result; |
179 |
} |
} |
180 |
|
|
181 |
Map<String, Address> gade = searchGadeidentAdresser.get(gadeident); |
result.gadeident = gadeident; |
182 |
|
|
183 |
|
|
184 |
|
Map<Short, Map<String,Address>> gade = searchGadeidentAdresser.get(gadeident); |
185 |
if (gade == null) { //Denne søgning må ikke fejle |
if (gade == null) { //Denne søgning må ikke fejle |
186 |
result.status = Status.ERROR_INTERNAL; |
result.status = Status.ERROR_INTERNAL; |
187 |
return result; |
return result; |
188 |
} |
} |
189 |
|
|
190 |
|
|
191 |
String husnrSearch = "" + result.splitReult.husnr + result.splitReult.litra; |
//Kunne evt klares med Iterables.get fra Guava/Collections |
192 |
Address addr = gade.get(husnrSearch); |
Address firstAddress = gade.values().iterator().next().values().iterator().next(); //Første husnr -> første litra |
193 |
|
result.vej = firstAddress.vejnavn; |
194 |
|
|
195 |
|
short husnrSearch = Short.parseShort(result.splitResult.husnr ); |
196 |
|
Map<String,Address> litraList = gade.get(husnrSearch); |
197 |
|
|
198 |
|
|
199 |
if (addr == null) { |
if (litraList == null) { //Husnr ikke fundet |
200 |
result.status = Status.ERROR_UNKNOWN_ADDRESSPOINT; |
|
201 |
return result; |
if (naermesteHusnr) { |
202 |
|
TreeMap<Short, Map<String,Address>> gadeTreeMap = (TreeMap<Short, Map<String,Address>>) gade; |
203 |
|
result.anvendtHusnr = NearestShortTreeMap.getNearestKey(husnrSearch, gadeTreeMap); |
204 |
|
|
205 |
|
result.nearestHusnr = true; |
206 |
|
litraList = gade.get(result.anvendtHusnr); |
207 |
|
|
208 |
|
|
209 |
|
} else { |
210 |
|
result.status = Status.ERROR_UNKNOWN_ADDRESSPOINT; |
211 |
|
return result; |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
|
Address addr = litraList.get( result.splitResult.litra ); |
216 |
|
|
217 |
|
if (addr == null) { //litra ikke fundet |
218 |
|
if (config.nearestLitra == true || naermesteHusnr == true) { |
219 |
|
result.nearestLitra = true; |
220 |
|
addr = litraList.values().iterator().next(); |
221 |
|
} else { |
222 |
|
//husnr fundet men litra blev ikke fundet |
223 |
|
result.status = Status.ERROR_UNKNOWN_ADDRESSPOINT; |
224 |
|
return result; |
225 |
|
} |
226 |
} |
} |
227 |
|
|
228 |
result.address = addr; |
result.address = addr; |
229 |
|
|
230 |
if (addr.distributor.equals("LUKKET")) { |
|
231 |
|
if ( StringUtils.equals(addr.distributor, "LUKKET") ) { |
232 |
result.status = Status.STATUS_NOT_COVERED; //Skal vi have en special status til Lukkede adresser ? |
result.status = Status.STATUS_NOT_COVERED; //Skal vi have en special status til Lukkede adresser ? |
233 |
return result; |
return result; |
234 |
} |
} |
243 |
|
|
244 |
return result; |
return result; |
245 |
} |
} |
246 |
|
|
247 |
|
|
248 |
|
private Integer lookupStretname(SearchRequest request, SearchResult result, boolean useExternal) { |
249 |
|
String helperSearchKey = ""; |
250 |
|
|
251 |
|
Integer gadeident = request.streetNames.get(result.vasketVejnavn); |
252 |
|
|
253 |
|
|
254 |
|
if ( gadeident == null) { |
255 |
|
helperSearchKey = "" + request.postnr + "/" + result.vasketVejnavn; |
256 |
|
gadeident = helperCache.get(helperSearchKey); |
257 |
|
} |
258 |
|
|
259 |
|
if (gadeident == null) { |
260 |
|
String vej = levenshteinHelper.proposeStreetName(request, result); |
261 |
|
if (vej != null) { |
262 |
|
gadeident = request.streetNames.get(vej); |
263 |
|
} |
264 |
|
} |
265 |
|
|
266 |
|
if (gadeident == null) { |
267 |
|
String vej = metaphoneHelper.proposeStreetName(request, result); |
268 |
|
if (vej != null) { |
269 |
|
gadeident = request.streetNames.get(vej); |
270 |
|
} |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
|
// Brug OpenStreetMap før vi prøver google |
275 |
|
// For google har en request limit, det har OSM ikke! |
276 |
|
if ( gadeident == null && useExternal) { |
277 |
|
String vej = osmHelper.proposeStreetName(request, result); |
278 |
|
gadeident = helperWrapper(vej, request.streetNames, helperSearchKey); |
279 |
|
} |
280 |
|
|
281 |
|
if ( gadeident == null && useExternal) { |
282 |
|
String vej = googleHelper.proposeStreetName(request, result); |
283 |
|
gadeident = helperWrapper(vej, request.streetNames, helperSearchKey); |
284 |
|
} |
285 |
|
return gadeident; |
286 |
|
} |
287 |
|
|
288 |
|
private Integer helperWrapper(String vej, Map<String,Integer> postnrVeje, String helperSearchKey) { |
289 |
|
if (vej == null) |
290 |
|
return null; |
291 |
|
|
292 |
|
String vejVasket = AddressUtils.vaskVejnavn( vej ); |
293 |
|
Integer gadeident = postnrVeje.get(vejVasket); |
294 |
|
|
295 |
|
if (gadeident != null) { |
296 |
|
helperCache.put(helperSearchKey, gadeident); |
297 |
|
} |
298 |
|
return gadeident; |
299 |
|
|
300 |
|
} |
301 |
|
|
302 |
|
public Map<String,Integer> getStretsByPostal(short zip) { |
303 |
|
return searchPostnrVejnavnGadeid.get(zip); |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
|
|
308 |
public void buildSearchStructures() throws SQLException{ |
public void buildSearchStructures() throws SQLException{ |
309 |
searchPostnrVejnavnGadeid = new HashMap<Integer, Map<String,Long>>(); |
searchPostnrVejnavnGadeid = new HashMap<Short, Map<String,Integer>>(); |
310 |
searchGadeidentAdresser = new HashMap<Long, Map<String,Address>>(); |
searchGadeidentAdresser = new HashMap<Integer, Map<Short, Map<String,Address>>>(); |
311 |
helperCache = new ConcurrentHashMap<String,Long>(); |
helperCache = new ConcurrentHashMap<String,Integer>(); |
312 |
|
|
313 |
long start1 = System.currentTimeMillis(); |
long start1 = System.currentTimeMillis(); |
314 |
System.out.println("Build -- stage 1"); |
System.out.println("Build -- stage 1"); |
315 |
|
|
316 |
alleAdresser = DatabaseLayer.getAllAdresses(); |
alleAdresser = db.getAllAdresses(); |
317 |
|
|
318 |
|
|
319 |
/* Mapper mellem db Row ID og adresse noden */ |
/* Mapper mellem db Row ID og adresse noden */ |
320 |
Map<Integer,Address> idAddressMap = new HashMap<Integer,Address>( alleAdresser.size() ); |
Map<Integer,Address> idAddressMap = new HashMap<Integer,Address>( alleAdresser.size() ); |
321 |
|
DeduplicateHelper<String> aliasHelper = new DeduplicateHelper<String>(400000); // Indeholder pt 379664 entries |
322 |
|
|
323 |
|
DeduplicateHelper<Short> shortHelper = new DeduplicateHelper<Short>(); |
324 |
|
DeduplicateHelper<Integer> intHelper = new DeduplicateHelper<Integer>(); |
325 |
|
|
326 |
|
AliasGenerator aliasGenerator = new AliasGenerator(); |
327 |
|
|
328 |
|
|
329 |
for (Address a : alleAdresser) { |
for (Address a : alleAdresser) { |
330 |
idAddressMap.put(a.id, a); |
idAddressMap.put(a.id, a); |
331 |
|
|
332 |
Map<String,Long> postnrVeje = searchPostnrVejnavnGadeid.get(a.postnr); |
Short postnr = shortHelper.getInstance(a.postnr); |
333 |
|
|
334 |
|
Map<String,Integer> postnrVeje = searchPostnrVejnavnGadeid.get(postnr); |
335 |
|
|
336 |
if (postnrVeje == null) { |
if (postnrVeje == null) { |
337 |
postnrVeje = new ConcurrentHashMap<String,Long>(); |
postnrVeje = new HashMap<String,Integer>(10240); //lige nu indeholder den største 9500 entries |
338 |
searchPostnrVejnavnGadeid.put(a.postnr, postnrVeje); |
searchPostnrVejnavnGadeid.put(postnr, postnrVeje); |
339 |
} |
} |
340 |
|
|
341 |
|
|
342 |
String vasketVejnavn = AddressUtils.vaskVejnavn(a.vejnavn); |
String vasketVejnavn = AddressUtils.vaskVejnavn(a.vejnavn); |
343 |
Long gadeident = postnrVeje.get(vasketVejnavn); |
Integer gadeident = postnrVeje.get(vasketVejnavn); |
344 |
if (gadeident == null) { |
if (gadeident == null) { |
345 |
//postnrVeje.put(vasketVejnavn, a.gadeid); |
//postnrVeje.put(vasketVejnavn, a.gadeid); |
346 |
|
|
347 |
Set<String> aliaser = findVejAliaser(a.vejnavn); |
gadeident = intHelper.getInstance( a.gadeid ); |
348 |
|
|
349 |
|
Set<String> aliaser = aliasGenerator.findVejAliaser(a.vejnavn); |
350 |
for(String alias : aliaser) { |
for(String alias : aliaser) { |
351 |
String vasketAlias = AddressUtils.vaskVejnavn(alias); |
String vasketAlias = AddressUtils.vaskVejnavn(alias); |
352 |
postnrVeje.put(vasketAlias, a.gadeid); |
vasketAlias = aliasHelper.getInstance(vasketAlias); |
353 |
|
|
354 |
|
postnrVeje.put(vasketAlias, gadeident); |
355 |
} |
} |
356 |
|
|
357 |
} |
} |
358 |
|
|
359 |
Map<String, Address> gade = searchGadeidentAdresser.get(a.gadeid); |
|
360 |
|
Map<Short, Map<String,Address>> gade = searchGadeidentAdresser.get(gadeident); |
361 |
if (gade == null) { |
if (gade == null) { |
362 |
gade = new HashMap<String, Address>(); |
gade = new TreeMap<Short, Map<String,Address>>(); |
363 |
searchGadeidentAdresser.put(a.gadeid, gade); |
searchGadeidentAdresser.put(gadeident, gade); |
364 |
} |
} |
365 |
String husnrSearch = "" + a.husnr + a.husnrbogstav; |
|
366 |
gade.put(husnrSearch, a); |
Map<String,Address> litraList = gade.get(a.husnr); |
367 |
|
if (litraList == null) { |
368 |
|
litraList = new HashMap<String,Address>(); |
369 |
|
gade.put(a.husnr, litraList); |
370 |
|
} |
371 |
|
|
372 |
|
litraList.put(a.husnrbogstav, a); |
373 |
} |
} |
374 |
|
|
375 |
|
aliasHelper = null; //frigiv hukommelse inden stage2 |
376 |
|
aliasGenerator = null; |
377 |
|
|
378 |
|
|
379 |
//////////////////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////////////////// |
380 |
long start2 = System.currentTimeMillis(); |
long start2 = System.currentTimeMillis(); |
381 |
System.out.println("Build, stage1 elapsed: " + (start2-start1) ); |
System.out.println("Build, stage1 elapsed: " + (start2-start1) ); |
382 |
System.out.println("Build -- stage 2 udvidet dækning"); |
System.out.println("Build -- stage 2 alias tabel"); |
383 |
|
|
384 |
List<ExtendedBean> extDao = DatabaseLayer.getExtendedAdresslist(); |
int vaskCount = 0; |
385 |
|
List<AliasBean> aliasList = db.getAliasList(); |
386 |
|
for (AliasBean alias : aliasList) { |
387 |
|
Map<String,Integer> postnrVeje = searchPostnrVejnavnGadeid.get(alias.postnr); |
388 |
|
|
389 |
|
if (postnrVeje == null) { |
390 |
|
//Burde ikke kunne ske - men better safe than sorry |
391 |
|
continue; |
392 |
|
} |
393 |
|
|
394 |
|
String vasketVej = AddressUtils.vaskVejnavn(alias.vejnavn); |
395 |
|
String vasketAlias = AddressUtils.vaskVejnavn(alias.aliasVejnavn); |
396 |
|
|
397 |
|
Integer gadeident = postnrVeje.get(vasketVej); |
398 |
|
if (gadeident == null) { |
399 |
|
//Kender ikke den oprindelige vej |
400 |
|
continue; |
401 |
|
} |
402 |
|
|
403 |
|
Integer aliasIdent = postnrVeje.get(vasketAlias); |
404 |
|
|
405 |
|
if (aliasIdent == null) { //Vi kender ikke denne variant af vejnavnet |
406 |
|
postnrVeje.put(vasketAlias, gadeident); |
407 |
|
vaskCount++; |
408 |
|
} |
409 |
|
|
410 |
|
} |
411 |
|
System.out.println("Anvendte " + vaskCount + " aliaser fra databasen"); |
412 |
|
|
413 |
|
|
414 |
|
//////////////////////////////////////////////////////////////////////////////////////// |
415 |
|
long start3 = System.currentTimeMillis(); |
416 |
|
System.out.println("Build, stage2 elapsed: " + (start3-start2) ); |
417 |
|
System.out.println("Build -- stage 3 udvidet dækning"); |
418 |
|
|
419 |
|
DeduplicateHelper<String> ruteHelper = new DeduplicateHelper<String>(); |
420 |
|
DeduplicateHelper<String> korelisteHelper = new DeduplicateHelper<String>(); |
421 |
|
|
422 |
|
List<ExtendedBean> extDao = db.getExtendedAdresslist(); |
423 |
for (ExtendedBean eb : extDao) { |
for (ExtendedBean eb : extDao) { |
424 |
|
|
425 |
Address orgAddress = idAddressMap.get(eb.orgId); |
Address orgAddress = idAddressMap.get(eb.orgId); |
426 |
if (orgAddress == null) |
if (orgAddress == null) |
427 |
continue; |
continue; |
428 |
|
|
429 |
|
if (orgAddress.daekningsType != DaekningsType.DAEKNING_IKKEDAEKKET) { |
430 |
|
//kan være at adressen er blevet markeret dækket og - find-nærmeste data ikke er opdateret i mellemtiden |
431 |
|
continue; |
432 |
|
} |
433 |
|
|
434 |
Address targetAddress = idAddressMap.get(eb.targetId); |
Address targetAddress = idAddressMap.get(eb.targetId); |
435 |
if (targetAddress == null) |
if (targetAddress == null) { |
436 |
continue; |
continue; |
437 |
|
} |
438 |
|
|
439 |
if (orgAddress.distributor != null && orgAddress.distributor.equals("LUKKET")) { |
|
440 |
|
if ( StringUtils.equals(orgAddress.distributor, "LUKKET") ) { |
441 |
continue; |
continue; |
442 |
} |
} |
443 |
|
|
444 |
if (targetAddress.distributor.equals("LUKKET")) { |
|
445 |
|
|
446 |
|
if ( StringUtils.equals(targetAddress.distributor, "LUKKET") ) { |
447 |
continue; |
continue; |
448 |
} |
} |
449 |
|
|
452 |
|
|
453 |
boolean covered = false; |
boolean covered = false; |
454 |
if (targetAddress.distributor.equals("DAO")) { |
if (targetAddress.distributor.equals("DAO")) { |
455 |
orgAddress.rute = calculateExtendedDaoRoute(eb,orgAddress,targetAddress); |
orgAddress.rute = calculateExtendedDaoRoute(eb,orgAddress,targetAddress); |
456 |
|
orgAddress.rute = ruteHelper.getInstance(orgAddress.rute); |
457 |
|
|
458 |
if (orgAddress.rute != null) { |
if (orgAddress.rute != null) { |
459 |
orgAddress.koreliste = targetAddress.koreliste; |
orgAddress.koreliste = targetAddress.koreliste; |
460 |
covered = true; |
covered = true; |
462 |
} |
} |
463 |
|
|
464 |
if (targetAddress.distributor.equals("BK")) { |
if (targetAddress.distributor.equals("BK")) { |
465 |
orgAddress.koreliste = calculateExtendedBkKoreliste(eb,orgAddress,targetAddress); |
orgAddress.koreliste = calculateExtendedBkKoreliste(eb,orgAddress,targetAddress); |
466 |
|
orgAddress.koreliste = korelisteHelper.getInstance(orgAddress.koreliste); |
467 |
|
|
468 |
if (orgAddress.koreliste != null) { |
if (orgAddress.koreliste != null) { |
469 |
orgAddress.rute = targetAddress.rute; |
orgAddress.rute = targetAddress.rute; |
470 |
covered = true; |
covered = true; |
476 |
orgAddress.dbkBane = targetAddress.dbkBane; |
orgAddress.dbkBane = targetAddress.dbkBane; |
477 |
|
|
478 |
/* Sådan gør den gamle service */ |
/* Sådan gør den gamle service */ |
479 |
orgAddress.kommunekode = targetAddress.kommunekode; |
/* 20150520 THN- hvis vi bibeholder de originale kommune+vejkoder kan vi bruge DAVID til join i databasen*/ |
480 |
orgAddress.vejkode = targetAddress.vejkode; |
//orgAddress.kommunekode = targetAddress.kommunekode; |
481 |
|
//orgAddress.vejkode = targetAddress.vejkode; |
482 |
|
|
483 |
|
|
484 |
orgAddress.distributor = targetAddress.distributor; |
orgAddress.distributor = targetAddress.distributor; |
485 |
} |
} |
489 |
idAddressMap = null; |
idAddressMap = null; |
490 |
|
|
491 |
////////////////////////////////////////////////////////////////////////////////////// |
////////////////////////////////////////////////////////////////////////////////////// |
492 |
long start3 = System.currentTimeMillis(); |
long start4 = System.currentTimeMillis(); |
493 |
System.out.println("Build, stage2 elapsed: " + (start3-start2) ); |
System.out.println("Build, stage3 elapsed: " + (start4-start3) ); |
494 |
System.out.println("Build -- stage 3 - 100pct"); |
System.out.println("Build -- stage 4 - 100pct"); |
495 |
|
|
496 |
Map<Integer,HundredePctBean> hundredePct = DatabaseLayer.get100PctList(); |
Map<Short,HundredePctBean> hundredePct = db.get100PctList(); |
497 |
for (Address addr : alleAdresser) { |
for (Address addr : alleAdresser) { |
498 |
if (addr.daekningsType != DaekningsType.DAEKNING_IKKEDAEKKET) { |
if (addr.daekningsType != DaekningsType.DAEKNING_IKKEDAEKKET) { |
499 |
continue; |
continue; |
518 |
|
|
519 |
//////////////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////////////// |
520 |
long stop = System.currentTimeMillis(); |
long stop = System.currentTimeMillis(); |
521 |
System.out.println("Build, stage3 elapsed: " + (stop-start3) ); |
System.out.println("Build, stage4 elapsed: " + (stop-start4) ); |
522 |
System.out.println("Build -- Gathering statistics"); |
System.out.println("Build -- Gathering statistics"); |
523 |
|
|
524 |
for (Address addr : alleAdresser) { |
for (Address addr : alleAdresser) { |
536 |
stats.ikkeDaekketCount++; |
stats.ikkeDaekketCount++; |
537 |
} |
} |
538 |
} |
} |
539 |
|
stats.totalCount = alleAdresser.size(); |
540 |
|
|
541 |
|
|
542 |
stats.elapsed = stop-start1; |
stats.elapsed = stop-start1; |
543 |
stats.buildTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date() ); |
stats.buildTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date() ); |
544 |
|
|
545 |
|
System.out.println("Build: totalCount: " + stats.totalCount); |
546 |
System.out.println("Build: direkteCount: " + stats.direkteCount); |
System.out.println("Build: direkteCount: " + stats.direkteCount); |
547 |
System.out.println("Build: extendedCount: " + stats.extendedCount); |
System.out.println("Build: extendedCount: " + stats.extendedCount); |
548 |
System.out.println("Build: hundredePctCount: " + stats.hundredePctCount); |
System.out.println("Build: hundredePctCount: " + stats.hundredePctCount); |
557 |
return stats; |
return stats; |
558 |
} |
} |
559 |
|
|
560 |
|
public DatabaseLayer getDatabaseLayer() { |
561 |
|
return db; |
562 |
|
} |
563 |
|
|
564 |
public void clear() { |
public void clear() { |
565 |
searchPostnrVejnavnGadeid.clear(); |
searchPostnrVejnavnGadeid.clear(); |
566 |
searchGadeidentAdresser.clear(); |
searchGadeidentAdresser.clear(); |
570 |
|
|
571 |
|
|
572 |
private String calculateExtendedDaoRoute(ExtendedBean eb, Address orgAddress, Address targetAddress) { |
private String calculateExtendedDaoRoute(ExtendedBean eb, Address orgAddress, Address targetAddress) { |
573 |
|
if (targetAddress.rute == null) { //hvis targetAdress.rute er null så er adressen ikke dækket alligevel |
574 |
|
return null; |
575 |
|
} |
576 |
|
|
577 |
// /////////////////////////////////////////////////////////////////// |
// /////////////////////////////////////////////////////////////////// |
578 |
switch( eb.transport) { |
switch( eb.transport) { |
583 |
return ".." + targetAddress.rute; |
return ".." + targetAddress.rute; |
584 |
} else if (eb.afstand < 0.701) { |
} else if (eb.afstand < 0.701) { |
585 |
return "..." + targetAddress.rute; |
return "..." + targetAddress.rute; |
586 |
} else if (eb.afstand < 0.501) { |
} else if (eb.afstand < 1.001) { |
587 |
return "...." + targetAddress.rute; |
return "...." + targetAddress.rute; |
588 |
} |
} |
589 |
break; |
break; |
609 |
return "...." + targetAddress.rute; |
return "...." + targetAddress.rute; |
610 |
} |
} |
611 |
break; |
break; |
612 |
|
default: |
613 |
|
System.out.println("Ukendt transport type: " + eb ); |
614 |
} |
} |
615 |
return null; |
return null; |
616 |
} |
} |
626 |
return result; |
return result; |
627 |
} |
} |
628 |
|
|
629 |
|
public List<Address> get100PctAddresses() { |
630 |
|
List<Address> result = new ArrayList<Address>(120000); |
631 |
|
for (Address a : alleAdresser) { |
632 |
|
if ( a.daekningsType == DaekningsType.DAEKNING_100PCT) { |
633 |
|
result.add(a); |
634 |
|
} |
635 |
|
} |
636 |
|
Collections.sort(result ); |
637 |
|
return result; |
638 |
|
} |
639 |
|
|
640 |
/////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////// |
641 |
private String calculateExtendedBkKoreliste(ExtendedBean eb, Address orgAddress, Address targetAddress) { |
private String calculateExtendedBkKoreliste(ExtendedBean eb, Address orgAddress, Address targetAddress) { |
642 |
String inject; |
String inject; |
648 |
return AddressUtils.injectIntoBk(targetAddress.koreliste, inject); |
return AddressUtils.injectIntoBk(targetAddress.koreliste, inject); |
649 |
} |
} |
650 |
|
|
651 |
private Set<String> findVejAliaser(String vejnavn) { |
|
652 |
vejnavn = vejnavn.toLowerCase(); |
public Set<Short> getPostnumre() { |
653 |
HashSet<String> aliasSet = new HashSet<String>(); |
return new TreeSet<Short>( searchPostnrVejnavnGadeid.keySet() ); |
654 |
aliasSet.add(vejnavn); |
} |
655 |
|
|
656 |
aliasSet.add(vejnavn.replace("u", "ü") ); |
public Set<Entry<String,Integer>> getVejnavne(short postnr) { |
657 |
aliasSet.add(vejnavn.replace("ü", "u") ); |
Map<String,Integer> map = searchPostnrVejnavnGadeid.get(postnr); |
|
|
|
|
aliasSet.add(vejnavn.replace("alle", "allé") ); |
|
|
aliasSet.add(vejnavn.replace("allé", "alle") ); |
|
|
|
|
|
aliasSet.add(vejnavn.replace("dronningens", "dr") ); |
|
|
aliasSet.add(vejnavn.replace("dr.", "dronningens") ); |
|
|
aliasSet.add(vejnavn.replace("dr ", "dronningens") ); |
|
|
|
|
|
aliasSet.add(vejnavn.replace("kvt", "kvarter") ); |
|
|
aliasSet.add(vejnavn.replace("kvarter", "kvt") ); |
|
|
|
|
|
aliasSet.add(vejnavn.replace("gl", "gammel") ); |
|
|
aliasSet.add(vejnavn.replace("gammel", "gl") ); |
|
|
|
|
|
aliasSet.add(vejnavn.replace("lille", "ll") ); |
|
|
aliasSet.add(vejnavn.replace("ll ", "lille") ); |
|
|
aliasSet.add(vejnavn.replace("ll.", "lille") ); |
|
|
|
|
|
aliasSet.add(vejnavn.replace("store", "st") ); |
|
|
aliasSet.add(vejnavn.replace("st ", "store") ); |
|
|
aliasSet.add(vejnavn.replace("st.", "store") ); |
|
|
|
|
|
aliasSet.add(vejnavn.replace("søndre", "sdr") ); |
|
|
aliasSet.add(vejnavn.replace("sdr", "søndre") ); |
|
|
|
|
658 |
|
|
659 |
aliasSet.add(vejnavn.replace("nørre", "nr") ); |
TreeMap<String,Integer> newMap = new TreeMap<String,Integer>( map );//Lav et nyt TreeMap for at sikre sortering |
|
aliasSet.add(vejnavn.replace("nr", "nørre") ); |
|
660 |
|
|
661 |
aliasSet.add(vejnavn.replace("nordre", "ndr") ); |
return newMap.entrySet(); |
662 |
aliasSet.add(vejnavn.replace("ndr", "nordre") ); |
} |
663 |
|
|
664 |
|
public Set<String> getHusnumre(int gadeid) { |
665 |
|
|
666 |
aliasSet.add(vejnavn.replace("sankt", "skt") ); |
Map<Short,Map<String, Address>> gade = searchGadeidentAdresser.get(gadeid); |
|
aliasSet.add(vejnavn.replace("sankt", "sct") ); |
|
|
aliasSet.add(vejnavn.replace("skt", "sankt") ); |
|
|
|
|
|
aliasSet.add(vejnavn.replace("skt", "sct") ); |
|
|
aliasSet.add(vejnavn.replace("sct", "skt") ); |
|
|
|
|
|
// Opbyg æøå varianter over alle fundne aliaser |
|
667 |
|
|
668 |
@SuppressWarnings("unchecked") |
Set<String> set = new TreeSet<String>( new NaturalOrderComparator<String>() ); |
669 |
HashSet<String> variants = (HashSet<String>) aliasSet.clone(); |
for(Map<String,Address> litraList : gade.values()) { |
670 |
|
for (Address a: litraList.values()) { |
671 |
for (String vVejnavn : variants) { |
set.add( a.husnr + a.husnrbogstav ); |
672 |
// danske tegn 1 |
} |
|
aliasSet.add( vVejnavn.replace("æ", "ae") ); |
|
|
aliasSet.add( vVejnavn.replace("ø", "oe") ); |
|
|
aliasSet.add( vVejnavn.replace("å", "aa") ); |
|
|
aliasSet.add( vVejnavn.replace("ae", "æ") ); |
|
|
aliasSet.add( vVejnavn.replace("oe", "ø") ); |
|
|
aliasSet.add( vVejnavn.replace("aa", "å") ); |
|
|
|
|
|
//danske tegn 2 |
|
|
aliasSet.add( vVejnavn.replace("æ", "ae").replace("ø", "oe") ); |
|
|
aliasSet.add( vVejnavn.replace("æ", "ae").replace("å", "aa") ); |
|
|
aliasSet.add( vVejnavn.replace("ø", "ae").replace("å", "aa") ); |
|
|
aliasSet.add( vVejnavn.replace("ae", "æ").replace("oe","ø") ); |
|
|
aliasSet.add( vVejnavn.replace("ae", "æ").replace("aa","å") ); |
|
|
aliasSet.add( vVejnavn.replace("oe", "ø").replace("aa", "å") ); |
|
|
|
|
|
//danske tegn 3 |
|
|
aliasSet.add( vejnavn.replace("æ", "ae").replace("ø", "oe").replace("å", "aa") ); |
|
|
aliasSet.add( vejnavn.replace("ae", "æ").replace("oe", "ø").replace("aa", "å") ); |
|
673 |
} |
} |
674 |
|
|
675 |
return aliasSet; |
return set; |
676 |
} |
} |
677 |
|
|
678 |
|
public Address getAdresse(int gadeid, String husnrStr) { |
679 |
|
short husnr = Short.parseShort( husnrStr.replaceAll("[^\\d]","") ); |
680 |
|
String litra = husnrStr.replaceAll("\\d", ""); |
681 |
|
|
682 |
|
Map<Short,Map<String, Address>> gade = searchGadeidentAdresser.get(gadeid); |
683 |
|
Map<String, Address> litraList = gade.get(husnr); |
684 |
|
return litraList.get(litra); |
685 |
|
|
686 |
|
} |
687 |
|
|
688 |
} |
} |