/[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 2352 by torben, Mon Feb 23 15:49:00 2015 UTC revision 2367 by torben, Tue Feb 24 12:23:59 2015 UTC
# Line 26  public class GeocodeHelper { Line 26  public class GeocodeHelper {
26    
27          public static void main(String[] args) throws IOException {          public static void main(String[] args) throws IOException {
28                  ServiceConfig conf = new ServiceConfig();                  ServiceConfig conf = new ServiceConfig();
29                    conf.osmTimeout = 1000;
30                                    
31                  int post = 8700;                  int post = 8700;
32                  String vej = "Enebarvej";                  String vej = "Enebarvej";
# Line 35  public class GeocodeHelper { Line 36  public class GeocodeHelper {
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( postnr, vejnavn );                          OSMInvocation wrapper = new OSMInvocation( conf, postnr, vejnavn );
51                          CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm");                          CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm");
52    
53                          return (String) breaker.invoke(wrapper);                          return (String) breaker.invoke(wrapper);
54    
55                                                    
56                  } catch (Exception e) {                  } catch (Exception e) {
57                          System.out.println( "Error: " + e.getMessage() );                          System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() );
58                  }                                                                                }                                                              
59                  return null;                  return null;
60          }          }
# Line 68  public class GeocodeHelper { Line 69  public class GeocodeHelper {
69                          return (String) breaker.invoke(wrapper);                          return (String) breaker.invoke(wrapper);
70                                                    
71                  } catch (Exception e) {                  } catch (Exception e) {
72                          System.out.println( "GoogleError: " + e.getMessage() );                          System.out.println( "GoogleError: " + e.getClass().getName() +" / "+ e.getMessage() );
73                  }                  }
74                  return null;                      return null;    
75          }          }
# Line 77  public class GeocodeHelper { Line 78  public class GeocodeHelper {
78          public static class OSMInvocation implements CircuitInvocation {          public static class OSMInvocation implements CircuitInvocation {
79                  int postnr;                  int postnr;
80                  String vejnavn;                  String vejnavn;
81                    ServiceConfig conf;
82                                    
83                  public OSMInvocation(int postnr, String vejnavn) {                  public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) {
84                            this.conf = conf;
85                          this.postnr= postnr;                          this.postnr= postnr;
86                          this.vejnavn = vejnavn;                                          this.vejnavn = vejnavn;                
87                  }                  }
88                                    
89                  @Override                  @Override
90                  public String proceed() throws Exception {                  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";                          String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
95                          //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";
96                          //System.out.println (url);                          //System.out.println (url);
97                                                    
98                          String json = HttpUtil.getContentString(url, 1000);                          String json = HttpUtil.getContentString(url, conf.osmTimeout);
99                          //System.out.println(json);                          //System.out.println(json);
100                                                    
101                          Gson gson = new Gson();                          Gson gson = new Gson();
102                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
103                                                    
104                            //timer.printElapsed("OSM elapsed");
105                            
106                          //System.out.println("Count: " + adrList.length);                          //System.out.println("Count: " + adrList.length);
107                          if (adrList.length != 1)                          if (adrList.length != 1)
108                                  return null;                                  return null;
# Line 131  public class GeocodeHelper { Line 137  public class GeocodeHelper {
137                          } else {                          } else {
138                                  geocoder = new Geocoder();                                  geocoder = new Geocoder();
139                          }                          }
140                                                    //TimingHelper timer = new TimingHelper();
141                                                    
142                          String search = vejnavn + ", " + postnr + ", Denmark";                          String search = vejnavn + ", " + postnr + ", Denmark";
143                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
144                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
145                                                                                    
146                            //timer.printElapsed("Google elapsed");
147                                                    
148                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
149                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                              
                                 System.out.println("Google responded with " + geocoderResponse.getStatus() );  
150                                                                    
151                                  if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {                                  if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
152                                          return null;                                          return null;
153                                  } else {                                  } else {
154                                            System.out.println("Google responded with " + geocoderResponse.getStatus() );
155                                          //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren                                          //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren
156                                          throw new Exception("Google responded with " + geocoderResponse.getStatus() );                                          throw new Exception("Google responded with " + geocoderResponse.getStatus() );
157                                  }                                  }

Legend:
Removed from v.2352  
changed lines
  Added in v.2367

  ViewVC Help
Powered by ViewVC 1.1.20