/WhereAbout/src/com/google/marvin/whereabout/StreetLocator.java

http://eyes-free.googlecode.com/ · Java · 320 lines · 191 code · 21 blank · 108 comment · 17 complexity · 7ae08fba92332ca87cc62b993d260904 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package com.google.marvin.whereabout;
  17. import android.location.Location;
  18. import android.location.LocationManager;
  19. import android.util.Log;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22. import java.io.BufferedReader;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.net.HttpURLConnection;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. import java.util.HashMap;
  30. import java.util.HashSet;
  31. import java.util.Set;
  32. import java.util.regex.Matcher;
  33. import java.util.regex.Pattern;
  34. /**
  35. * This utility class implements methods to get street address from lat-lon
  36. * using reverse geocoding API through HTTP.
  37. * TODO(chaitanyag): Add code to replace state abbreviations.
  38. *
  39. * @author chaitanyag@google.com (Chaitanya Gharpure)
  40. */
  41. public class StreetLocator {
  42. private static final HashMap<String, String> roadTypeMap =
  43. new HashMap<String, String>();
  44. private static final String ENCODING = "UTF-8";
  45. // URL for obtaining navigation directions
  46. private static final String URL_NAV_STRING =
  47. "http://maps.google.com/maps/nav?";
  48. //URL for obtaining reverse geocoded location
  49. private static final String URL_GEO_STRING =
  50. "http://maps.google.com/maps/geo?";
  51. /** Private Constructor for this utility class */
  52. private StreetLocator() {
  53. }
  54. /**
  55. * Queries the map server and obtains the street names at the specified
  56. * location. This is done by obtaining street name at specified location,
  57. * and at locations X meters to the N, S, E, and W of the specified location.
  58. * @param lat The latitude in degrees
  59. * @param lon The longitude in degrees
  60. * @return Returns the string array containing street names
  61. */
  62. public static String[] getStreetIntersection(double lat, double lon) {
  63. HashSet<String> streets = new HashSet<String>();
  64. try {
  65. for (int i = 0; i < 5; i++) {
  66. // Find street address at lat-lon x meters to the N, S, E and W of
  67. // the given lat-lon
  68. String street =
  69. parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  70. if (street != null) {
  71. streets.add(street);
  72. }
  73. // get points 150m away, towards N, E, S, and W
  74. if (i < 4) {
  75. Location nextLoc = endLocation(lat, lon, i * 90, 15);
  76. lat = nextLoc.getLatitude();
  77. lon = nextLoc.getLongitude();
  78. }
  79. }
  80. } catch (MalformedURLException mue) {
  81. Log.d("Locator", "Malformed URL: " + mue.getMessage());
  82. } catch (IOException e) {
  83. Log.d("Locator", "Error reading from Map server: " + e.toString());
  84. } catch (JSONException e) {
  85. Log.d("Locator", "Error in JSON response: " + e.getMessage());
  86. }
  87. String[] st = new String[streets.size()];
  88. int i = 0;
  89. for (String s : streets) {
  90. st[i++] = s;
  91. }
  92. return st;
  93. }
  94. /**
  95. * Queries the map server and obtains the reverse geocoded address of the
  96. * specified location.
  97. * @param lat The latitude in degrees
  98. * @param lon The longitude in degrees
  99. * @return Returns the reverse geocoded address
  100. */
  101. public static String getAddress(double lat, double lon) {
  102. try {
  103. String resp = getResult(makeGeoURL(lat, lon));
  104. JSONObject jsonObj = new JSONObject(resp);
  105. int code = jsonObj.getJSONObject("Status").getInt("code");
  106. if (code == 200) {
  107. return extendShorts(jsonObj.getJSONArray("Placemark")
  108. .getJSONObject(0).getString("address"));
  109. }
  110. } catch (MalformedURLException mue) {
  111. Log.d("Locator", "Malformed URL: " + mue.getMessage());
  112. } catch (IOException e) {
  113. Log.d("Locator", "Error reading from Map server: " + e.toString());
  114. } catch (JSONException e) {
  115. Log.d("Locator", "Error in JSON response: " + e.getMessage());
  116. }
  117. return null;
  118. }
  119. /**
  120. * Parses the JSON response to extract the street name.
  121. * @param resp The String representation of the JSON response
  122. * @return Returns the street name
  123. * @throws JSONException
  124. */
  125. private static String parseStreetName(String resp) throws JSONException {
  126. JSONObject jsonObj = new JSONObject(resp);
  127. int code = jsonObj.getJSONObject("Status").getInt("code");
  128. if (code == 200) {
  129. return extendShorts(jsonObj.getJSONArray("Placemark")
  130. .getJSONObject(0).getString("address"));
  131. }
  132. return null;
  133. }
  134. /**
  135. * Sends a request to the specified URL and obtains the result from
  136. * the sever.
  137. * @param url The URL to connect to
  138. * @return the server response
  139. * @throws IOException
  140. */
  141. private static String getResult(URL url) throws IOException {
  142. Log.d("Locator", url.toString());
  143. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  144. conn.setDoInput(true);
  145. conn.setDoOutput(true);
  146. InputStream is = conn.getInputStream();
  147. String result = toString(is);
  148. return result;
  149. }
  150. /**
  151. * Prepares the URL to connect to navigation server, from the specified
  152. * start and end location coordinates
  153. * @param lat1 Start location latitude in degrees
  154. * @param lon1 Start location longitude in degrees
  155. * @param lat2 End location latitude in degrees
  156. * @param lon2 End location longitude in degrees
  157. * @return a well-formed URL
  158. * @throws MalformedURLException
  159. */
  160. private static URL makeNavURL(double lat1, double lon1,
  161. double lat2, double lon2)
  162. throws MalformedURLException {
  163. StringBuilder url = new StringBuilder();
  164. url.append(URL_NAV_STRING).append("hl=").append(R.string.English)
  165. .append("&gl=").append(R.string.English)
  166. .append("&output=js&oe=utf8&q=from%3A").append(lat1).append(",")
  167. .append(lon1).append("+to%3A").append(lat2).append(",").append(lon2);
  168. return new URL(url.toString());
  169. }
  170. /**
  171. * Prepares the URL to connect to the reverse geocoding server from the
  172. * specified location coordinates.
  173. * @param lat latitude in degrees of the location to reverse geocode
  174. * @param lon longitude in degrees of the location to reverse geocode
  175. * @return
  176. * @throws MalformedURLException
  177. */
  178. private static URL makeGeoURL(double lat, double lon)
  179. throws MalformedURLException {
  180. StringBuilder url = new StringBuilder();
  181. url.append(URL_GEO_STRING).append("q=").append(lat).append(",").append(lon);
  182. return new URL(url.toString());
  183. }
  184. /**
  185. * Reads an InputStream and returns its contents as a String.
  186. * @param inputStream The InputStream to read from.
  187. * @return The contents of the InputStream as a String.
  188. * @throws Exception
  189. */
  190. private static String toString(InputStream inputStream) throws IOException {
  191. StringBuilder outputBuilder = new StringBuilder();
  192. String string;
  193. if (inputStream != null) {
  194. BufferedReader reader =
  195. new BufferedReader(new InputStreamReader(inputStream, ENCODING));
  196. while (null != (string = reader.readLine())) {
  197. outputBuilder.append(string).append('\n');
  198. }
  199. }
  200. return outputBuilder.toString();
  201. }
  202. /**
  203. * Replaces the short forms in the address by their longer forms, so that TTS
  204. * speaks the addresses properly. The short forms are replaced only if they
  205. * are followed by a non-letter and a non-space character, or if it is at the
  206. * end of the string.
  207. * @param addr The address from which to replace short forms
  208. * @return the modified address string
  209. */
  210. private static String extendShorts(String addr) {
  211. if (roadTypeMap.size() == 0) {
  212. populateRoadTypeMap();
  213. }
  214. Set<String> abbrevs = roadTypeMap.keySet();
  215. for (String abbrev : abbrevs) {
  216. addr = replaceAll(abbrev + "[\\W&&\\S]|" + abbrev + "\\s*$",
  217. addr, roadTypeMap.get(abbrev));
  218. }
  219. return addr;
  220. }
  221. /**
  222. * Replaces every occurrence of the pattern in the parent string by the
  223. * specified replacement string.
  224. * @param regex The regular expression for the pattern to replace
  225. * @param str The parent string
  226. * @param repl The replacement string
  227. * @return
  228. */
  229. private static String replaceAll(String regex, String str, String repl) {
  230. Pattern p = Pattern.compile(regex);
  231. Matcher m = p.matcher(str);
  232. return m.replaceAll(repl);
  233. }
  234. /**
  235. * Computes the new location at a particular direction and distance from the
  236. * specified location using the inverse Vincenti formula.
  237. * @param lat1 latitude of source location in degrees
  238. * @param lon1 longitude of source location in degrees
  239. * @param brng Direction in degrees wrt source location
  240. * @param dist Distance from the source location
  241. * @return the new location
  242. */
  243. private static Location endLocation(double lat1, double lon1,
  244. double brng, double dist) {
  245. double a = 6378137, b = 6356752.3142, f = 1 / 298.257223563;
  246. double s = dist;
  247. double alpha1 = Math.toRadians(brng);
  248. double sinAlpha1 = Math.sin(alpha1), cosAlpha1 = Math.cos(alpha1);
  249. double tanU1 = (1 - f) * Math.tan(Math.toRadians(lat1));
  250. double cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)), sinU1 = tanU1 * cosU1;
  251. double sigma1 = Math.atan2(tanU1, cosAlpha1);
  252. double sinAlpha = cosU1 * sinAlpha1;
  253. double cosSqAlpha = 1 - sinAlpha * sinAlpha;
  254. double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
  255. double aa = 1 + uSq / 16384 * (4096 + uSq *
  256. (-768 + uSq * (320 - 175 * uSq)));
  257. double bb = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
  258. double sigma = s / (b * aa), sigmaP = 2 * Math.PI;
  259. double cos2SigmaM = 0, sinSigma = 0, deltaSigma = 0, cosSigma = 0;
  260. while (Math.abs(sigma - sigmaP) > 1e-12) {
  261. cos2SigmaM = Math.cos(2 * sigma1 + sigma);
  262. sinSigma = Math.sin(sigma);
  263. cosSigma = Math.cos(sigma);
  264. deltaSigma = bb * sinSigma * (cos2SigmaM + bb / 4 * (cosSigma *
  265. (-1 + 2 * cos2SigmaM * cos2SigmaM) -
  266. bb / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) *
  267. (-3 + 4 * cos2SigmaM * cos2SigmaM)));
  268. sigmaP = sigma;
  269. sigma = s / (b * aa) + deltaSigma;
  270. }
  271. double tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
  272. double lat2 = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1,
  273. (1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp));
  274. double lambda = Math.atan2(sinSigma * sinAlpha1,
  275. cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
  276. double cc = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
  277. double ll = lambda - (1 - cc) * f * sinAlpha *
  278. (sigma + cc * sinSigma * (cos2SigmaM + cc * cosSigma *
  279. (-1 + 2 * cos2SigmaM * cos2SigmaM)));
  280. Location l = new Location(LocationManager.GPS_PROVIDER);
  281. l.setLatitude(Math.toDegrees(lat2));
  282. l.setLongitude(lon1 + Math.toDegrees(ll));
  283. return l;
  284. }
  285. /**
  286. * Adds mappings from abbreviations to full forms.
  287. */
  288. private static void populateRoadTypeMap() {
  289. roadTypeMap.put("St", "Street ");
  290. roadTypeMap.put("Fwy", "Freeway ");
  291. roadTypeMap.put("Hwy", "Highway ");
  292. roadTypeMap.put("Expy", "Expressway ");
  293. roadTypeMap.put("Blvd", "Boulevard ");
  294. roadTypeMap.put("Dr", "Drive ");
  295. roadTypeMap.put("Rd", "Road ");
  296. roadTypeMap.put("Ave", "Avenue ");
  297. roadTypeMap.put("Pkwy", "Parkway ");
  298. }
  299. }