/[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 2349 by torben, Mon Feb 23 15:02:48 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    
20  public class GeocodeHelper {  public class GeocodeHelper {
21                            
           
22    
23    
24          public static void main(String[] args) throws IOException {                      public static void main(String[] args) throws IOException {
25                  System.out.println(  GeocodeHelper.googleHelper(2800, "Chr. Xs Alle") );                  ServiceConfig conf = new ServiceConfig();
26                    
27                    int post = 8700;
28                    String vej = "Enebarvej";
29                    
30                    long start1 = System.currentTimeMillis();
31                    System.out.println( "Google:" +  GeocodeHelper.googleHelper(conf, post, vej) );        
32                    long stop1 = System.currentTimeMillis();
33                    
34                    long start2 = System.currentTimeMillis();
35                    System.out.println( "OSM:" +  GeocodeHelper.openstreetmapHelper(post, vej) );
36                    long stop2 = System.currentTimeMillis();
37                    
38                    System.out.println("Google: " + (stop1-start1));
39                    System.out.println("OSM: " + (stop2-start2));
40          }          }
41                    
42          public static String googleHelper(int postnr, String vejnavn) {          public static String openstreetmapHelper(int postnr, String vejnavn) {
43                    
44                    try {
45                                    
46                            String encVej = URLEncoder.encode(vejnavn, "UTF-8");
47                            
48                            String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
49                            //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
50                            //System.out.println (url);
51                            
52                            String json = HttpUtil.getContentString(url, 1000);
53                            //System.out.println(json);
54                            
55                            Gson gson = new Gson();
56                            OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
57                            
58                            //System.out.println("Count: " + adrList.length);
59                            if (adrList.length != 1)
60                                    return null;
61                                    
62                            if (adrList[0].address != null) {
63                                    return adrList[0].address.road;
64                            }
65    
66                            
67                    } catch (Exception e) {
68                            System.out.println( "Error: " + e.getMessage() );
69                    }                                                              
70                    return null;
71            }
72            
73            
74            
75            public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) {
76                  try {                  try {
77                          //Todo: Load api key from context config                          //Todo: Load api key from context config
78                          final Geocoder geocoder = new Geocoder();                          final Geocoder geocoder;
79                            if ( conf.googleApiKey != null ) {
80                                    geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException
81                            } else {
82                                    geocoder = new Geocoder();
83                            }
84                            
85                                                    
86                          String search = vejnavn + ", " + postnr + ", Denmark";                          String search = vejnavn + ", " + postnr + ", Denmark";
87                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
# Line 32  public class GeocodeHelper { Line 90  public class GeocodeHelper {
90                                                    
91                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
92                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {
93                                    System.out.println("Google responded with " + geocoderResponse.getStatus() );
94                                  return null;                                  return null;
95                          }                          }
96                                                    
# Line 51  public class GeocodeHelper { Line 110  public class GeocodeHelper {
110                                  }                                  }
111                          }                          }
112                                                    
113                  } catch (IOException e) {                  } catch (Exception e) {
114                          System.out.println( "GoogleError: " + e.getMessage() );                          System.out.println( "GoogleError: " + e.getMessage() );
115                  }                  }
116                  return null;                  return null;

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

  ViewVC Help
Powered by ViewVC 1.1.20