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

Contents of /dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2367 - (show annotations) (download)
Tue Feb 24 12:23:59 2015 UTC (9 years, 2 months ago) by torben
File size: 5556 byte(s)
Load OSM timeout from config
1 package dk.daoas.daoadresseservice;
2
3 import java.io.IOException;
4 import java.net.URLEncoder;
5 import java.util.List;
6
7 import com.google.code.geocoder.Geocoder;
8 import com.google.code.geocoder.GeocoderRequestBuilder;
9 import com.google.code.geocoder.model.GeocodeResponse;
10 import com.google.code.geocoder.model.GeocoderAddressComponent;
11 import com.google.code.geocoder.model.GeocoderRequest;
12 import com.google.code.geocoder.model.GeocoderResult;
13 import com.google.code.geocoder.model.GeocoderStatus;
14 import com.google.gson.Gson;
15
16 import dk.daoas.daoadresseservice.admin.ServiceConfig;
17 import dk.daoas.daoadresseservice.beans.OSMAddress;
18 import dk.daoas.daoadresseservice.util.HttpUtil;
19 import dk.thoerup.circuitbreaker.CircuitBreaker;
20 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
21 import dk.thoerup.circuitbreaker.CircuitInvocation;
22
23 public class GeocodeHelper {
24
25
26
27 public static void main(String[] args) throws IOException {
28 ServiceConfig conf = new ServiceConfig();
29 conf.osmTimeout = 1000;
30
31 int post = 8700;
32 String vej = "Enebarvej";
33
34 long start1 = System.currentTimeMillis();
35 System.out.println( "Google:" + GeocodeHelper.googleHelper(conf, post, vej) );
36 long stop1 = System.currentTimeMillis();
37
38 long start2 = System.currentTimeMillis();
39 System.out.println( "OSM:" + GeocodeHelper.openstreetmapHelper(conf, post, vej) );
40 long stop2 = System.currentTimeMillis();
41
42 System.out.println("Google: " + (stop1-start1));
43 System.out.println("OSM: " + (stop2-start2));
44 }
45
46 public static String openstreetmapHelper(ServiceConfig conf, int postnr, String vejnavn) {
47
48 try {
49
50 OSMInvocation wrapper = new OSMInvocation( conf, postnr, vejnavn );
51 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm");
52
53 return (String) breaker.invoke(wrapper);
54
55
56 } catch (Exception e) {
57 System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() );
58 }
59 return null;
60 }
61
62
63
64 public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) {
65 try {
66 GoogleInvocation wrapper = new GoogleInvocation( conf, postnr, vejnavn );
67 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google");
68
69 return (String) breaker.invoke(wrapper);
70
71 } catch (Exception e) {
72 System.out.println( "GoogleError: " + e.getClass().getName() +" / "+ e.getMessage() );
73 }
74 return null;
75 }
76
77
78 public static class OSMInvocation implements CircuitInvocation {
79 int postnr;
80 String vejnavn;
81 ServiceConfig conf;
82
83 public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) {
84 this.conf = conf;
85 this.postnr= postnr;
86 this.vejnavn = vejnavn;
87 }
88
89 @Override
90 public String proceed() throws Exception {
91 //TimingHelper timer = new TimingHelper();
92 String encVej = URLEncoder.encode(vejnavn, "UTF-8");
93
94 String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
95 //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
96 //System.out.println (url);
97
98 String json = HttpUtil.getContentString(url, conf.osmTimeout);
99 //System.out.println(json);
100
101 Gson gson = new Gson();
102 OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
103
104 //timer.printElapsed("OSM elapsed");
105
106 //System.out.println("Count: " + adrList.length);
107 if (adrList.length != 1)
108 return null;
109
110 if (adrList[0].address != null) {
111 return adrList[0].address.road;
112 }
113
114 return null;
115 }
116 }
117
118
119 public static class GoogleInvocation implements CircuitInvocation {
120 ServiceConfig conf;
121 int postnr;
122 String vejnavn;
123
124 public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) {
125 this.conf = conf;
126 this.postnr= postnr;
127 this.vejnavn = vejnavn;
128
129 }
130
131 @Override
132 public String proceed() throws Exception {
133
134 final Geocoder geocoder;
135 if ( conf.googleApiKey != null ) {
136 geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException
137 } else {
138 geocoder = new Geocoder();
139 }
140 //TimingHelper timer = new TimingHelper();
141
142 String search = vejnavn + ", " + postnr + ", Denmark";
143 GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
144 GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
145
146 //timer.printElapsed("Google elapsed");
147
148 //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
149 if ( geocoderResponse.getStatus() != GeocoderStatus.OK) {
150
151 if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
152 return null;
153 } else {
154 System.out.println("Google responded with " + geocoderResponse.getStatus() );
155 //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren
156 throw new Exception("Google responded with " + geocoderResponse.getStatus() );
157 }
158
159 }
160
161 List<GeocoderResult> resList = geocoderResponse.getResults();
162 //System.out.println( "Count: " + resList.size() );
163
164 if (resList.size() != 1) {
165 return null;
166 }
167 GeocoderResult res = resList.get(0);
168
169 List<GeocoderAddressComponent> compList = res.getAddressComponents();
170 for (GeocoderAddressComponent c : compList) {
171 //System.out.println(c);
172 if (c.getTypes().contains("route")) {
173 return c.getLongName();
174 }
175 }
176 return null;
177 }
178
179 }
180
181 }

  ViewVC Help
Powered by ViewVC 1.1.20