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