blob: 69178dccf91d9789a7507f0db148f0f355f1fb77 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/* TweetTracker. Copyright (c) Arizona Board of Regents on behalf of Arizona State University
* @author shamanth
*/
package Chapter2.Location;
import Chapter2.support.Location;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
public class LocationTranslationExample
{
/**
* Translates a location string to coordinates using the database or Nominatim Service
* @param loc
* @return
*/
public Location TranslateLoc(String loc)
{
if(loc!=null&&!loc.isEmpty())
{
String encodedLoc="";
try {
//Step 1: Encode the location name
encodedLoc = URLEncoder.encode(loc, "UTF-8");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(LocationTranslationExample.class.getName()).log(Level.SEVERE, null, ex);
}
//Step 2: Create a get request to MapQuest API with the name of the location
String url= "http://open.mapquestapi.com/nominatim/v1/search?q="+encodedLoc+"&format=json";
String page = ReadHTML(url);
if(page!=null)
{
try{
JSONArray results = new JSONArray(page);
if(results.length()>0)
{
//Step 3: Read and extract the coordinates of the location as a JSONObject
Location loca = new Location(results.getJSONObject(0).getDouble("lat"),results.getJSONObject(0).getDouble("lon"));
return loca;
}
}catch(JSONException ex)
{
Logger.getLogger(LocationTranslationExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
/**
* Extracts the html content of a URL
* @param url
* @return html page
*/
public String ReadHTML(String url)
{
URLConnection conn = null;
URL theURL = null;
try
{
theURL = new URL(url);
}
catch ( MalformedURLException e)
{
System.out.println("Bad URL: " + theURL);
return null;
}
String page = "";
try
{
conn = theURL.openConnection();
HttpURLConnection huc = (HttpURLConnection) conn;
conn.setConnectTimeout(2000);
huc.setRequestProperty("User-Agent", "Mozilla/4.5");
//Set your email address in the request so MapQuest knows how to reach you in the event of problems
huc.setRequestProperty("Email", "twitterdataanalytics@gmail.com");
if(huc.getResponseCode()>=400&&huc.getResponseCode()<=404)
{
return null;
}
conn.connect();
BufferedReader bRead = new BufferedReader(new InputStreamReader((InputStream) conn.getContent()));
String temp=null;
while( (temp= bRead.readLine())!=null)
{
page = page+"\n"+temp;
}
bRead.close();
}
catch (IOException e) {
//System.out.print("ReadHTML IO Error:" + e.getMessage()+" \n");
return null;
}
return page;
}
public static void main(String[] args)
{
LocationTranslationExample lte = new LocationTranslationExample();
if(args!=null)
{
if(args.length>0)
{
for(int i=0;i<args.length;i++)
{
System.out.println(lte.TranslateLoc(args[i]).toString());
}
}
}
}
}
|