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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2998 - (hide 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 torben 2409 package dk.daoas.daoadresseservice;
2    
3     import java.net.URLEncoder;
4    
5     import com.google.gson.Gson;
6    
7 torben 2566 import com.google.common.util.concurrent.RateLimiter;
8    
9    
10 torben 2409 import dk.daoas.daoadresseservice.admin.ServiceConfig;
11     import dk.daoas.daoadresseservice.beans.OSMAddress;
12 torben 2411 import dk.daoas.daoadresseservice.beans.SearchRequest;
13 torben 2409 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 torben 2566 RateLimiter limiter;
23 torben 2409
24     public OSMStreetnameHelper(ServiceConfig conf) {
25     this.conf = conf;
26 torben 2566
27 torben 2998 // http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy
28 torben 2566 limiter = RateLimiter.create( 10.0 ); //Max 10 requests pr sekund
29 torben 2409 }
30    
31     @Override
32 torben 2411 public String proposeStreetName(SearchRequest request, SearchResult result) {
33 torben 2409 if (conf.useOpenStreetMaps == false)
34     return null;
35    
36 torben 2566
37 torben 2409 result.osm = true;
38    
39     try {
40    
41 torben 2567 OSMInvocation wrapper = new OSMInvocation( limiter, conf, request.postnr, request.vejnavn );
42 torben 2409 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 torben 2567 RateLimiter limiter;
58 torben 2409
59 torben 2567 public OSMInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) {
60     this.limiter = limiter;
61 torben 2409 this.conf = conf;
62     this.postnr= postnr;
63     this.vejnavn = vejnavn;
64     }
65    
66     @Override
67     public String proceed() throws Exception {
68 torben 2567 limiter.acquire();
69    
70    
71 torben 2409 //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 torben 2620 if (adrList[0].getAddress() != null) {
93     return adrList[0].getAddress().getRoad();
94 torben 2409 }
95    
96     return null;
97     }
98     }
99    
100    
101     }

  ViewVC Help
Powered by ViewVC 1.1.20