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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2298 by torben, Sun Feb 15 11:00:48 2015 UTC revision 2361 by torben, Tue Feb 24 11:27:10 2015 UTC
# Line 1  Line 1 
1  package dk.daoas.daoadresseservice;  package dk.daoas.daoadresseservice;
2    
3  import java.io.IOException;  import java.io.IOException;
4    import java.net.URLEncoder;
5  import java.util.List;  import java.util.List;
6    
7  import com.google.code.geocoder.Geocoder;  import com.google.code.geocoder.Geocoder;
# Line 10  import com.google.code.geocoder.model.Ge Line 11  import com.google.code.geocoder.model.Ge
11  import com.google.code.geocoder.model.GeocoderRequest;  import com.google.code.geocoder.model.GeocoderRequest;
12  import com.google.code.geocoder.model.GeocoderResult;  import com.google.code.geocoder.model.GeocoderResult;
13  import com.google.code.geocoder.model.GeocoderStatus;  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 {  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          public static void main(String[] args) throws IOException {                                      
56                  System.out.println(  GeocodeHelper.googleHelper(2800, "Chr. Xs Alle") );                  } catch (Exception e) {
57                            System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() );
58                    }                                                              
59                    return null;
60          }          }
61                    
62          public static String googleHelper(int postnr, String vejnavn) {          
63            
64            public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) {
65                  try {                  try {
66                          //Todo: Load api key from context config                          GoogleInvocation wrapper = new GoogleInvocation( conf, postnr, vejnavn );
67                          final Geocoder geocoder = new Geocoder();                          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";                          String search = vejnavn + ", " + postnr + ", Denmark";
141                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
142                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
143                                                            
144                            //timer.printElapsed("Google elapsed");
145                                                    
146                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
147                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                              
148                                  return null;                                  
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();                          List<GeocoderResult> resList = geocoderResponse.getResults();
# Line 50  public class GeocodeHelper { Line 171  public class GeocodeHelper {
171                                          return c.getLongName();                                          return c.getLongName();
172                                  }                                  }
173                          }                          }
174                                                    return null;
                 } catch (IOException e) {  
                         System.out.println( "GoogleError: " + e.getMessage() );  
175                  }                  }
                 return null;  
176                                    
177          }          }
178    

Legend:
Removed from v.2298  
changed lines
  Added in v.2361

  ViewVC Help
Powered by ViewVC 1.1.20