/[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 2352 by torben, Mon Feb 23 15:49:00 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.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 {
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          public static void main(String[] args) throws IOException {                                      
55                  System.out.println(  GeocodeHelper.googleHelper(2800, "Chr. Xs Alle") );                  } catch (Exception e) {
56                            System.out.println( "Error: " + e.getMessage() );
57                    }                                                              
58                    return null;
59          }          }
60                    
61          public static String googleHelper(int postnr, String vejnavn) {          
62            
63            public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) {
64                  try {                  try {
65                          //Todo: Load api key from context config                          GoogleInvocation wrapper = new GoogleInvocation( conf, postnr, vejnavn );
66                          final Geocoder geocoder = new Geocoder();                          CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google");
67    
68                            return (String) breaker.invoke(wrapper);
69                            
70                    } catch (Exception e) {
71                            System.out.println( "GoogleError: " + 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                            String encVej = URLEncoder.encode(vejnavn, "UTF-8");
89                            
90                            String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
91                            //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
92                            //System.out.println (url);
93                            
94                            String json = HttpUtil.getContentString(url, 1000);
95                            //System.out.println(json);
96                            
97                            Gson gson = new Gson();
98                            OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
99                            
100                            //System.out.println("Count: " + adrList.length);
101                            if (adrList.length != 1)
102                                    return null;
103                                    
104                            if (adrList[0].address != null) {
105                                    return adrList[0].address.road;
106                            }
107    
108                            return null;
109                    }
110            }
111            
112            
113            public static class GoogleInvocation implements CircuitInvocation {
114                    ServiceConfig conf;
115                    int postnr;
116                    String vejnavn;
117                    
118                    public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) {
119                            this.conf = conf;
120                            this.postnr= postnr;
121                            this.vejnavn = vejnavn;
122                            
123                    }
124    
125                    @Override
126                    public String proceed() throws Exception {
127                            
128                            final Geocoder geocoder;
129                            if ( conf.googleApiKey != null ) {
130                                    geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException
131                            } else {
132                                    geocoder = new Geocoder();
133                            }
134                            
135                                                    
136                          String search = vejnavn + ", " + postnr + ", Denmark";                          String search = vejnavn + ", " + postnr + ", Denmark";
137                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
138                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
139                                                            
140                                                    
141                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
142                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {
143                                  return null;                                  System.out.println("Google responded with " + geocoderResponse.getStatus() );
144                                    
145                                    if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
146                                            return null;
147                                    } else {
148                                            //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren
149                                            throw new Exception("Google responded with " + geocoderResponse.getStatus() );
150                                    }
151                                    
152                          }                          }
153                                                    
154                          List<GeocoderResult> resList = geocoderResponse.getResults();                          List<GeocoderResult> resList = geocoderResponse.getResults();
# Line 50  public class GeocodeHelper { Line 166  public class GeocodeHelper {
166                                          return c.getLongName();                                          return c.getLongName();
167                                  }                                  }
168                          }                          }
169                                                    return null;
                 } catch (IOException e) {  
                         System.out.println( "GoogleError: " + e.getMessage() );  
170                  }                  }
                 return null;  
171                                    
172          }          }
173    

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

  ViewVC Help
Powered by ViewVC 1.1.20