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