/[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 2361 - (show annotations) (download)
Tue Feb 24 11:27:10 2015 UTC (9 years, 2 months ago) by torben
File size: 5477 byte(s)
Add timer helper
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.daoas.daoadresseservice.util.TimingHelper;
20 import dk.thoerup.circuitbreaker.CircuitBreaker;
21 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
22 import dk.thoerup.circuitbreaker.CircuitInvocation;
23
24 public class GeocodeHelper {
25
26
27
28 public static void main(String[] args) throws IOException {
29 ServiceConfig conf = new ServiceConfig();
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(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(int postnr, String vejnavn) {
47
48 try {
49
50 OSMInvocation wrapper = new OSMInvocation( 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
82 public OSMInvocation(int postnr, String vejnavn) {
83 this.postnr= postnr;
84 this.vejnavn = vejnavn;
85 }
86
87 @Override
88 public String proceed() throws Exception {
89 //TimingHelper timer = new TimingHelper();
90 String encVej = URLEncoder.encode(vejnavn, "UTF-8");
91
92 String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
93 //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
94 //System.out.println (url);
95
96 String json = HttpUtil.getContentString(url, 1000);
97 //System.out.println(json);
98
99 Gson gson = new Gson();
100 OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
101
102 //timer.printElapsed("OSM elapsed");
103
104 //System.out.println("Count: " + adrList.length);
105 if (adrList.length != 1)
106 return null;
107
108 if (adrList[0].address != null) {
109 return adrList[0].address.road;
110 }
111
112 return null;
113 }
114 }
115
116
117 public static class GoogleInvocation implements CircuitInvocation {
118 ServiceConfig conf;
119 int postnr;
120 String vejnavn;
121
122 public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) {
123 this.conf = conf;
124 this.postnr= postnr;
125 this.vejnavn = vejnavn;
126
127 }
128
129 @Override
130 public String proceed() throws Exception {
131
132 final Geocoder geocoder;
133 if ( conf.googleApiKey != null ) {
134 geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException
135 } else {
136 geocoder = new Geocoder();
137 }
138 //TimingHelper timer = new TimingHelper();
139
140 String search = vejnavn + ", " + postnr + ", Denmark";
141 GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
142 GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
143
144 //timer.printElapsed("Google elapsed");
145
146 //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
147 if ( geocoderResponse.getStatus() != GeocoderStatus.OK) {
148
149 if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
150 return null;
151 } else {
152 System.out.println("Google responded with " + geocoderResponse.getStatus() );
153 //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren
154 throw new Exception("Google responded with " + geocoderResponse.getStatus() );
155 }
156
157 }
158
159 List<GeocoderResult> resList = geocoderResponse.getResults();
160 //System.out.println( "Count: " + resList.size() );
161
162 if (resList.size() != 1) {
163 return null;
164 }
165 GeocoderResult res = resList.get(0);
166
167 List<GeocoderAddressComponent> compList = res.getAddressComponents();
168 for (GeocoderAddressComponent c : compList) {
169 //System.out.println(c);
170 if (c.getTypes().contains("route")) {
171 return c.getLongName();
172 }
173 }
174 return null;
175 }
176
177 }
178
179 }

  ViewVC Help
Powered by ViewVC 1.1.20