/[projects]/dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java
ViewVC logotype

Contents of /dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2307 - (show annotations) (download)
Sun Feb 15 17:53:36 2015 UTC (9 years, 3 months ago) by torben
File size: 3281 byte(s)
Add timing to geocode main
1 package dk.daoas.daoadresseservice;
2
3 import java.io.IOException;
4 import java.net.URLEncoder;
5 import java.util.List;
6
7 import com.google.code.geocoder.Geocoder;
8 import com.google.code.geocoder.GeocoderRequestBuilder;
9 import com.google.code.geocoder.model.GeocodeResponse;
10 import com.google.code.geocoder.model.GeocoderAddressComponent;
11 import com.google.code.geocoder.model.GeocoderRequest;
12 import com.google.code.geocoder.model.GeocoderResult;
13 import com.google.code.geocoder.model.GeocoderStatus;
14 import com.google.gson.Gson;
15
16 import dk.daoas.daoadresseservice.beans.OSMAddress;
17
18 public class GeocodeHelper {
19
20
21
22
23
24 public static void main(String[] args) throws IOException {
25 int post = 8700;
26 String vej = "Enebarvej";
27
28 long start1 = System.currentTimeMillis();
29 System.out.println( "Google:" + GeocodeHelper.googleHelper(post, vej) );
30 long stop1 = System.currentTimeMillis();
31
32 long start2 = System.currentTimeMillis();
33 System.out.println( "OSM:" + GeocodeHelper.openstreetmapHelper(post, vej) );
34 long stop2 = System.currentTimeMillis();
35
36 System.out.println("Google: " + (stop1-start1));
37 System.out.println("OSM: " + (stop2-start2));
38 }
39
40 public static String openstreetmapHelper(int postnr, String vejnavn) {
41
42 try {
43
44 String encVej = URLEncoder.encode(vejnavn, "UTF-8");
45
46 String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
47 //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
48 //System.out.println (url);
49
50 String json = HttpUtil.getContentString(url, 1000);
51 //System.out.println(json);
52
53 Gson gson = new Gson();
54 OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
55
56 //System.out.println("Count: " + adrList.length);
57 if (adrList.length != 1)
58 return null;
59
60 if (adrList[0].address != null) {
61 return adrList[0].address.road;
62 }
63
64
65 } catch (Exception e) {
66 System.out.println( "Error: " + e.getMessage() );
67 }
68 return null;
69 }
70
71
72
73 public static String googleHelper(int postnr, String vejnavn) {
74 try {
75 //Todo: Load api key from context config
76 final Geocoder geocoder = new Geocoder();
77
78 String search = vejnavn + ", " + postnr + ", Denmark";
79 GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
80 GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
81
82
83 //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
84 if ( geocoderResponse.getStatus() != GeocoderStatus.OK) {
85 return null;
86 }
87
88 List<GeocoderResult> resList = geocoderResponse.getResults();
89 //System.out.println( "Count: " + resList.size() );
90
91 if (resList.size() != 1) {
92 return null;
93 }
94 GeocoderResult res = resList.get(0);
95
96 List<GeocoderAddressComponent> compList = res.getAddressComponents();
97 for (GeocoderAddressComponent c : compList) {
98 //System.out.println(c);
99 if (c.getTypes().contains("route")) {
100 return c.getLongName();
101 }
102 }
103
104 } catch (IOException e) {
105 System.out.println( "GoogleError: " + e.getMessage() );
106 }
107 return null;
108
109 }
110
111 }

  ViewVC Help
Powered by ViewVC 1.1.20