/[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 2321 by torben, Tue Feb 17 09:06:42 2015 UTC revision 2361 by torben, Tue Feb 24 11:27:10 2015 UTC
# Line 13  import com.google.code.geocoder.model.Ge Line 13  import com.google.code.geocoder.model.Ge
13  import com.google.code.geocoder.model.GeocoderStatus;  import com.google.code.geocoder.model.GeocoderStatus;
14  import com.google.gson.Gson;  import com.google.gson.Gson;
15    
16    import dk.daoas.daoadresseservice.admin.ServiceConfig;
17  import dk.daoas.daoadresseservice.beans.OSMAddress;  import dk.daoas.daoadresseservice.beans.OSMAddress;
18  import dk.daoas.daoadresseservice.util.HttpUtil;  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 {          public static void main(String[] args) throws IOException {
29                    ServiceConfig conf = new ServiceConfig();
30                    
31                  int post = 8700;                  int post = 8700;
32                  String vej = "Enebarvej";                  String vej = "Enebarvej";
33                                    
34                  long start1 = System.currentTimeMillis();                  long start1 = System.currentTimeMillis();
35                  System.out.println( "Google:" +  GeocodeHelper.googleHelper(post, vej) );                                System.out.println( "Google:" +  GeocodeHelper.googleHelper(conf, post, vej) );        
36                  long stop1 = System.currentTimeMillis();                  long stop1 = System.currentTimeMillis();
37                                    
38                  long start2 = System.currentTimeMillis();                  long start2 = System.currentTimeMillis();
# Line 41  public class GeocodeHelper { Line 46  public class GeocodeHelper {
46          public static String openstreetmapHelper(int postnr, String vejnavn) {          public static String openstreetmapHelper(int postnr, String vejnavn) {
47                                    
48                  try {                  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");                          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";                          String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
# Line 54  public class GeocodeHelper { Line 99  public class GeocodeHelper {
99                          Gson gson = new Gson();                          Gson gson = new Gson();
100                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
101                                                    
102                            //timer.printElapsed("OSM elapsed");
103                            
104                          //System.out.println("Count: " + adrList.length);                          //System.out.println("Count: " + adrList.length);
105                          if (adrList.length != 1)                          if (adrList.length != 1)
106                                  return null;                                  return null;
# Line 62  public class GeocodeHelper { Line 109  public class GeocodeHelper {
109                                  return adrList[0].address.road;                                  return adrList[0].address.road;
110                          }                          }
111    
112                                                    return null;
113                  } catch (Exception e) {                  }
                         System.out.println( "Error: " + e.getMessage() );  
                 }                                                                
                 return null;  
114          }          }
115                    
116                    
117                    public static class GoogleInvocation implements CircuitInvocation {
118          public static String googleHelper(int postnr, String vejnavn) {                  ServiceConfig conf;
119                  try {                  int postnr;
120                          //Todo: Load api key from context config                  String vejnavn;
121                          final Geocoder geocoder = new Geocoder();                  
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 101  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.2321  
changed lines
  Added in v.2361

  ViewVC Help
Powered by ViewVC 1.1.20