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

  ViewVC Help
Powered by ViewVC 1.1.20