summaryrefslogtreecommitdiff
path: root/src/Chapter2/Location/LocationTranslationExample.java
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-23 12:22:20 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-23 12:22:20 +0200
commit14d7547cd31c5be878e377a4a5370f604c8d59d4 (patch)
tree003840f1a21d39b07d45cd3112c38b6eed40e3ab /src/Chapter2/Location/LocationTranslationExample.java
downloadTwitterDataAnalytics-14d7547cd31c5be878e377a4a5370f604c8d59d4.tar.gz
Initial commit
build.xml, etc. are modified a bit after opening in Netbeans 7.4.
Diffstat (limited to 'src/Chapter2/Location/LocationTranslationExample.java')
-rw-r--r--src/Chapter2/Location/LocationTranslationExample.java124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/Chapter2/Location/LocationTranslationExample.java b/src/Chapter2/Location/LocationTranslationExample.java
new file mode 100644
index 0000000..69178dc
--- /dev/null
+++ b/src/Chapter2/Location/LocationTranslationExample.java
@@ -0,0 +1,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());
+ }
+ }
+ }
+ }
+}