/[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 2368 by torben, Tue Feb 24 13:00:46 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.thoerup.circuitbreaker.CircuitBreaker;
20    import dk.thoerup.circuitbreaker.CircuitBreakerManager;
21    import dk.thoerup.circuitbreaker.CircuitInvocation;
22    
23  public class GeocodeHelper {  public class GeocodeHelper {
24                            
           
           
25    
26    
27          public static void main(String[] args) throws IOException {          public static void main(String[] args) throws IOException {
28                    ServiceConfig conf = new ServiceConfig();
29                    conf.osmTimeout = 1000;
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();
39                  System.out.println( "OSM:" +  GeocodeHelper.openstreetmapHelper(post, vej) );                  System.out.println( "OSM:" +  GeocodeHelper.openstreetmapHelper(conf, post, vej) );
40                  long stop2 = System.currentTimeMillis();                  long stop2 = System.currentTimeMillis();
41                                    
42                  System.out.println("Google: " + (stop1-start1));                  System.out.println("Google: " + (stop1-start1));
43                  System.out.println("OSM: " + (stop2-start2));                  System.out.println("OSM: " + (stop2-start2));
44          }          }
45                    
46          public static String openstreetmapHelper(int postnr, String vejnavn) {          public static String openstreetmapHelper(ServiceConfig conf, int postnr, String vejnavn) {
47                                    
48                  try {                  try {
49                                    
50                            OSMInvocation wrapper = new OSMInvocation( conf, 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                    ServiceConfig conf;
82                    
83                    public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) {
84                            this.conf = conf;
85                            this.postnr= postnr;
86                            this.vejnavn = vejnavn;                
87                    }
88                    
89                    @Override
90                    public String proceed() throws Exception {
91                            //TimingHelper timer = new TimingHelper();
92                          String encVej = URLEncoder.encode(vejnavn, "UTF-8");                          String encVej = URLEncoder.encode(vejnavn, "UTF-8");
93                                                    
94                          String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";                          
95                            String url = conf.nominatimBase + "/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
96                            
97                          //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";                          //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
98                          //System.out.println (url);                          //System.out.println (url);
99                                                    
100                          String json = HttpUtil.getContentString(url, 1000);                          String json = HttpUtil.getContentString(url, conf.osmTimeout);
101                          //System.out.println(json);                          //System.out.println(json);
102                                                    
103                          Gson gson = new Gson();                          Gson gson = new Gson();
104                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
105                                                    
106                            //timer.printElapsed("OSM elapsed");
107                            
108                          //System.out.println("Count: " + adrList.length);                          //System.out.println("Count: " + adrList.length);
109                          if (adrList.length != 1)                          if (adrList.length != 1)
110                                  return null;                                  return null;
# Line 62  public class GeocodeHelper { Line 113  public class GeocodeHelper {
113                                  return adrList[0].address.road;                                  return adrList[0].address.road;
114                          }                          }
115    
116                                                    return null;
117                  } catch (Exception e) {                  }
                         System.out.println( "Error: " + e.getMessage() );  
                 }                                                                
                 return null;  
118          }          }
119                    
120                    
121                    public static class GoogleInvocation implements CircuitInvocation {
122          public static String googleHelper(int postnr, String vejnavn) {                  ServiceConfig conf;
123                  try {                  int postnr;
124                          //Todo: Load api key from context config                  String vejnavn;
125                          final Geocoder geocoder = new Geocoder();                  
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                            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                            //TimingHelper timer = new TimingHelper();
143                                                    
144                          String search = vejnavn + ", " + postnr + ", Denmark";                          String search = vejnavn + ", " + postnr + ", Denmark";
145                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
146                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
147                                                            
148                            //timer.printElapsed("Google elapsed");
149                                                    
150                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
151                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                              
152                                  return null;                                  
153                                    if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
154                                            return null;
155                                    } else {
156                                            System.out.println("Google responded with " + geocoderResponse.getStatus() );
157                                            //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                          }                          }
162                                                    
163                          List<GeocoderResult> resList = geocoderResponse.getResults();                          List<GeocoderResult> resList = geocoderResponse.getResults();
# Line 101  public class GeocodeHelper { Line 175  public class GeocodeHelper {
175                                          return c.getLongName();                                          return c.getLongName();
176                                  }                                  }
177                          }                          }
178                                                    return null;
                 } catch (IOException e) {  
                         System.out.println( "GoogleError: " + e.getMessage() );  
179                  }                  }
                 return null;  
180                                    
181          }          }
182    

Legend:
Removed from v.2321  
changed lines
  Added in v.2368

  ViewVC Help
Powered by ViewVC 1.1.20