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