/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/OSMStreetnameHelper.java
ViewVC logotype

Contents of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/OSMStreetnameHelper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2998 - (show annotations) (download)
Fri Apr 15 06:19:18 2016 UTC (8 years, 1 month ago) by torben
File size: 2887 byte(s)
Add link to osm / nominatim usage policy
1 package dk.daoas.daoadresseservice;
2
3 import java.net.URLEncoder;
4
5 import com.google.gson.Gson;
6
7 import com.google.common.util.concurrent.RateLimiter;
8
9
10 import dk.daoas.daoadresseservice.admin.ServiceConfig;
11 import dk.daoas.daoadresseservice.beans.OSMAddress;
12 import dk.daoas.daoadresseservice.beans.SearchRequest;
13 import dk.daoas.daoadresseservice.beans.SearchResult;
14 import dk.daoas.daoadresseservice.util.HttpUtil;
15 import dk.thoerup.circuitbreaker.CircuitBreaker;
16 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17 import dk.thoerup.circuitbreaker.CircuitInvocation;
18
19 public class OSMStreetnameHelper implements StreetnameHelper {
20
21 ServiceConfig conf;
22 RateLimiter limiter;
23
24 public OSMStreetnameHelper(ServiceConfig conf) {
25 this.conf = conf;
26
27 // http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy
28 limiter = RateLimiter.create( 10.0 ); //Max 10 requests pr sekund
29 }
30
31 @Override
32 public String proposeStreetName(SearchRequest request, SearchResult result) {
33 if (conf.useOpenStreetMaps == false)
34 return null;
35
36
37 result.osm = true;
38
39 try {
40
41 OSMInvocation wrapper = new OSMInvocation( limiter, conf, request.postnr, request.vejnavn );
42 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm");
43
44 result.osmVej = (String) breaker.invoke(wrapper);
45 return result.osmVej;
46
47 } catch (Exception e) {
48 System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() );
49 }
50 return null;
51 }
52
53 static class OSMInvocation implements CircuitInvocation {
54 int postnr;
55 String vejnavn;
56 ServiceConfig conf;
57 RateLimiter limiter;
58
59 public OSMInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) {
60 this.limiter = limiter;
61 this.conf = conf;
62 this.postnr= postnr;
63 this.vejnavn = vejnavn;
64 }
65
66 @Override
67 public String proceed() throws Exception {
68 limiter.acquire();
69
70
71 //TimingHelper timer = new TimingHelper();
72 String encVej = URLEncoder.encode(vejnavn, "UTF-8");
73
74
75 String url = conf.nominatimBase + "/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
76
77 //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
78 //System.out.println (url);
79
80 String json = HttpUtil.getContentString(url, conf.osmTimeout);
81 //System.out.println(json);
82
83 Gson gson = new Gson();
84 OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
85
86 //timer.printElapsed("OSM elapsed");
87
88 //System.out.println("Count: " + adrList.length);
89 if (adrList.length != 1)
90 return null;
91
92 if (adrList[0].getAddress() != null) {
93 return adrList[0].getAddress().getRoad();
94 }
95
96 return null;
97 }
98 }
99
100
101 }

  ViewVC Help
Powered by ViewVC 1.1.20