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.admin.ServiceConfig; |
17 |
import dk.daoas.daoadresseservice.beans.OSMAddress; |
18 |
import dk.daoas.daoadresseservice.util.HttpUtil; |
19 |
|
20 |
public class GeocodeHelper { |
21 |
|
22 |
|
23 |
|
24 |
public static void main(String[] args) throws IOException { |
25 |
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 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 { |
77 |
//Todo: Load api key from context config |
78 |
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"; |
87 |
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest(); |
88 |
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest); |
89 |
|
90 |
|
91 |
//System.out.println( "Status: >" + geocoderResponse.getStatus() + "<"); |
92 |
if ( geocoderResponse.getStatus() != GeocoderStatus.OK) { |
93 |
System.out.println("Google responded with " + geocoderResponse.getStatus() ); |
94 |
return null; |
95 |
} |
96 |
|
97 |
List<GeocoderResult> resList = geocoderResponse.getResults(); |
98 |
//System.out.println( "Count: " + resList.size() ); |
99 |
|
100 |
if (resList.size() != 1) { |
101 |
return null; |
102 |
} |
103 |
GeocoderResult res = resList.get(0); |
104 |
|
105 |
List<GeocoderAddressComponent> compList = res.getAddressComponents(); |
106 |
for (GeocoderAddressComponent c : compList) { |
107 |
//System.out.println(c); |
108 |
if (c.getTypes().contains("route")) { |
109 |
return c.getLongName(); |
110 |
} |
111 |
} |
112 |
|
113 |
} catch (Exception e) { |
114 |
System.out.println( "GoogleError: " + e.getMessage() ); |
115 |
} |
116 |
return null; |
117 |
|
118 |
} |
119 |
|
120 |
} |