/actionslib/src/com/google/android/marvin/commands/impls/StreetLocator.java

http://eyes-free.googlecode.com/ · Java · 436 lines · 260 code · 32 blank · 144 comment · 25 complexity · 1f16b80bf032f4dc017eb6cb6f1255e9 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 of
  6. * 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 under
  14. * the License.
  15. */
  16. package com.google.android.marvin.commands.impls;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19. import android.location.Location;
  20. import android.location.LocationManager;
  21. import android.util.Log;
  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.HashSet;
  30. /**
  31. * This class implements methods to get street address from lat-lon using the
  32. * reverse geocoding API through HTTP
  33. *
  34. * @author chaitanyag@google.com (Chaitanya Gharpure)
  35. */
  36. public class StreetLocator {
  37. public interface StreetLocatorListener {
  38. public void onIntersectionLocated(String[] streetnames);
  39. public void onAddressLocated(String address);
  40. public void onFrontBackLocated(String[] streetsFront, String[] streetsBack);
  41. }
  42. private StreetLocatorListener cb;
  43. private static final String ENCODING = "UTF-8";
  44. // URL for obtaining navigation directions
  45. private static final String URL_NAV_STRING = "http://maps.google.com/maps/nav?";
  46. // URL for obtaining reverse geocoded location
  47. private static final String URL_GEO_STRING = "http://maps.google.com/maps/geo?";
  48. public StreetLocator(StreetLocatorListener callback) {
  49. cb = callback;
  50. }
  51. /**
  52. * Queries the map server and obtains the street names at the specified
  53. * location. This is done by obtaining street name at specified location,
  54. * and at locations X meters to the N, S, E, and W of the specified
  55. * location.
  56. *
  57. * @param lat The latitude in degrees
  58. * @param lon The longitude in degrees
  59. */
  60. public void getStreetIntersectionAsync(double lat, double lon) {
  61. final double latitude = lat;
  62. final double longitude = lon;
  63. class IntersectionThread implements Runnable {
  64. public void run() {
  65. cb.onIntersectionLocated(getStreetIntersection(latitude, longitude));
  66. }
  67. }
  68. (new Thread(new IntersectionThread())).start();
  69. }
  70. /**
  71. * Queries the map server and obtains the street names at the specified
  72. * location. This is done by obtaining street name at specified location,
  73. * and at locations X meters to the N, S, E, and W of the specified
  74. * location.
  75. *
  76. * @param lat The latitude in degrees
  77. * @param lon The longitude in degrees
  78. */
  79. public void getStreetsInFrontAndBackAsync(double lat, double lon, double heading) {
  80. final double latitude = lat;
  81. final double longitude = lon;
  82. final double direction = heading;
  83. class IntersectionThread implements Runnable {
  84. public void run() {
  85. getStreetsInFrontAndBack(latitude, longitude, direction);
  86. }
  87. }
  88. (new Thread(new IntersectionThread())).start();
  89. }
  90. /**
  91. * Queries the map server and obtains the reverse geocoded address of the
  92. * specified location.
  93. *
  94. * @param lat The latitude in degrees
  95. * @param lon The longitude in degrees
  96. */
  97. public void getAddressAsync(double lat, double lon) {
  98. final double latitude = lat;
  99. final double longitude = lon;
  100. class AddressThread implements Runnable {
  101. public void run() {
  102. String address = getAddress(latitude, longitude);
  103. if (address != null) {
  104. cb.onAddressLocated(address);
  105. }
  106. }
  107. }
  108. (new Thread(new AddressThread())).start();
  109. }
  110. /**
  111. * Queries the map server and obtains the street names at the specified
  112. * location. This is done by obtaining street name at specified location,
  113. * and at locations X meters to the N, S, E, and W of the specified
  114. * location.
  115. *
  116. * @param lat The latitude in degrees
  117. * @param lon The longitude in degrees
  118. * @return Returns the string array containing street names
  119. */
  120. public String[] getStreetIntersection(double lat, double lon) {
  121. HashSet<String> streets = new HashSet<String>();
  122. try {
  123. for (int i = 0; i < 5; i++) {
  124. // Find street address at lat-lon x meters to the N, S, E and W
  125. // of
  126. // the given lat-lon
  127. String street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  128. if (street != null) {
  129. streets.add(street);
  130. }
  131. // get points 150m away, towards N, E, S, and W
  132. if (i < 4) {
  133. Location nextLoc = endLocation(lat, lon, i * 90, 15);
  134. lat = nextLoc.getLatitude();
  135. lon = nextLoc.getLongitude();
  136. }
  137. }
  138. } catch (MalformedURLException mue) {
  139. Log.d("Locator", "Malformed URL: " + mue.getMessage());
  140. } catch (IOException e) {
  141. Log.d("Locator", "Error reading from Map server: " + e.toString());
  142. } catch (JSONException e) {
  143. Log.d("Locator", "Error in JSON response: " + e.getMessage());
  144. }
  145. String[] st = new String[streets.size()];
  146. int i = 0;
  147. for (String s : streets) {
  148. st[i++] = s;
  149. }
  150. return st;
  151. }
  152. /**
  153. * Queries the map server and obtains the street names at the specified
  154. * location. This is done by obtaining street name at specified location,
  155. * and at locations X meters to the N, S, E, and W of the specified
  156. * location.
  157. *
  158. * @param lat The latitude in degrees
  159. * @param lon The longitude in degrees
  160. */
  161. public void getStreetsInFrontAndBack(double lat, double lon, double heading) {
  162. HashSet<String> streetsFront = new HashSet<String>();
  163. HashSet<String> streetsBack = new HashSet<String>();
  164. double searchDistance = 15; // 15m (? - is there really a factor of 10
  165. // here)
  166. try {
  167. // Get the current street
  168. String street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  169. if (street != null) {
  170. streetsFront.add(street);
  171. streetsBack.add(street);
  172. }
  173. Log.i("Current street", "lat: " + Double.toString(lat) + ", lon: "
  174. + Double.toString(lon));
  175. // Get the street in front of the current street
  176. Location nextLoc = endLocation(lat, lon, heading, searchDistance);
  177. lat = nextLoc.getLatitude();
  178. lon = nextLoc.getLongitude();
  179. street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  180. if (street != null) {
  181. streetsFront.add(street);
  182. }
  183. Log
  184. .i("Front street", "lat: " + Double.toString(lat) + ", lon: "
  185. + Double.toString(lon));
  186. // Get the street behind the current street
  187. heading = heading + 180;
  188. if (heading >= 360) {
  189. heading = heading - 360;
  190. }
  191. nextLoc = endLocation(lat, lon, heading, searchDistance);
  192. lat = nextLoc.getLatitude();
  193. lon = nextLoc.getLongitude();
  194. street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  195. if (street != null) {
  196. streetsBack.add(street);
  197. }
  198. Log.i("Back street", "lat: " + Double.toString(lat) + ", lon: " + Double.toString(lon));
  199. String[] sf = new String[streetsFront.size()];
  200. int i = 0;
  201. for (String s : streetsFront) {
  202. sf[i++] = s;
  203. }
  204. String[] sb = new String[streetsBack.size()];
  205. i = 0;
  206. for (String s : streetsBack) {
  207. sb[i++] = s;
  208. }
  209. cb.onFrontBackLocated(sf, sb);
  210. } catch (MalformedURLException e) {
  211. // TODO Auto-generated catch block
  212. e.printStackTrace();
  213. } catch (JSONException e) {
  214. // TODO Auto-generated catch block
  215. e.printStackTrace();
  216. } catch (IOException e) {
  217. // TODO Auto-generated catch block
  218. e.printStackTrace();
  219. }
  220. }
  221. /**
  222. * Queries the map server and obtains the reverse geocoded address of the
  223. * specified location.
  224. *
  225. * @param lat The latitude in degrees
  226. * @param lon The longitude in degrees
  227. * @return Returns the reverse geocoded address
  228. */
  229. public String getAddress(double lat, double lon) {
  230. try {
  231. String resp = getResult(makeGeoURL(lat, lon));
  232. JSONObject jsonObj = new JSONObject(resp);
  233. int code = jsonObj.getJSONObject("Status").getInt("code");
  234. if (code == 200) {
  235. return extendShorts(jsonObj.getJSONArray("Placemark").getJSONObject(0).getString(
  236. "address"));
  237. }
  238. } catch (MalformedURLException mue) {
  239. Log.d("Locator", "Malformed URL: " + mue.getMessage());
  240. } catch (IOException e) {
  241. Log.d("Locator", "Error reading from Map server: " + e.toString());
  242. } catch (JSONException e) {
  243. Log.d("Locator", "Error in JSON response: " + e.getMessage());
  244. }
  245. return null;
  246. }
  247. /**
  248. * Parses the JSON response to extract the street name.
  249. *
  250. * @param resp The String representation of the JSON response
  251. * @return Returns the street name
  252. * @throws JSONException
  253. */
  254. private String parseStreetName(String resp) throws JSONException {
  255. JSONObject jsonObj = new JSONObject(resp);
  256. int code = jsonObj.getJSONObject("Status").getInt("code");
  257. if (code == 200) {
  258. return extendShorts(jsonObj.getJSONArray("Placemark").getJSONObject(0).getString(
  259. "address"));
  260. }
  261. return null;
  262. }
  263. /**
  264. * Sends a request to the specified URL and obtains the result from the
  265. * sever.
  266. *
  267. * @param url The URL to connect to
  268. * @return the server response
  269. * @throws IOException
  270. */
  271. private String getResult(URL url) throws IOException {
  272. Log.d("Locator", url.toString());
  273. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  274. conn.setDoInput(true);
  275. conn.setDoOutput(true);
  276. InputStream is = conn.getInputStream();
  277. String result = toString(is);
  278. return result;
  279. }
  280. /**
  281. * Prepares the URL to connect to navigation server, from the specified
  282. * start and end location coordinates
  283. *
  284. * @param lat1 Start location latitude in degrees
  285. * @param lon1 Start location longitude in degrees
  286. * @param lat2 End location latitude in degrees
  287. * @param lon2 End location longitude in degrees
  288. * @return a well-formed URL
  289. * @throws MalformedURLException
  290. */
  291. private URL makeNavURL(double lat1, double lon1, double lat2, double lon2)
  292. throws MalformedURLException {
  293. StringBuilder url = new StringBuilder();
  294. url.append(URL_NAV_STRING).append("hl=EN&gl=EN&output=js&oe=utf8&q=from%3A").append(lat1)
  295. .append(",").append(lon1).append("+to%3A").append(lat2).append(",").append(lon2);
  296. return new URL(url.toString());
  297. }
  298. /**
  299. * Prepares the URL to connect to the reverse geocoding server from the
  300. * specified location coordinates.
  301. *
  302. * @param lat latitude in degrees of the location to reverse geocode
  303. * @param lon longitude in degrees of the location to reverse geocode
  304. * @return URL The Geo URL created based on the given lat/lon
  305. * @throws MalformedURLException
  306. */
  307. private URL makeGeoURL(double lat, double lon) throws MalformedURLException {
  308. StringBuilder url = new StringBuilder();
  309. url.append(URL_GEO_STRING).append("q=").append(lat).append(",").append(lon);
  310. return new URL(url.toString());
  311. }
  312. /**
  313. * Reads an InputStream and returns its contents as a String.
  314. *
  315. * @param inputStream The InputStream to read from.
  316. * @return The contents of the InputStream as a String.
  317. */
  318. private static String toString(InputStream inputStream) throws IOException {
  319. StringBuilder outputBuilder = new StringBuilder();
  320. String string;
  321. if (inputStream != null) {
  322. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, ENCODING));
  323. while (null != (string = reader.readLine())) {
  324. outputBuilder.append(string).append('\n');
  325. }
  326. }
  327. return outputBuilder.toString();
  328. }
  329. /**
  330. * Replaces the short forms in the address by their longer forms, so that
  331. * TTS speaks the addresses properly
  332. *
  333. * @param addr The address from which to replace short forms
  334. * @return the modified address string
  335. */
  336. private String extendShorts(String addr) {
  337. addr = addr.replace("St,", "Street");
  338. addr = addr.replace("St.", "Street");
  339. addr = addr.replace("Rd", "Road");
  340. addr = addr.replace("Fwy", "Freeway");
  341. addr = addr.replace("Pkwy", "Parkway");
  342. addr = addr.replace("Blvd", "Boulevard");
  343. addr = addr.replace("Expy", "Expressway");
  344. addr = addr.replace("Ave", "Avenue");
  345. addr = addr.replace("Dr", "Drive");
  346. return addr;
  347. }
  348. /**
  349. * Computes the new location at a particular direction and distance from the
  350. * specified location using the inverse Vincenti formula.
  351. *
  352. * @param lat1 latitude of source location in degrees
  353. * @param lon1 longitude of source location in degrees
  354. * @param brng Direction in degrees wrt source location
  355. * @param dist Distance from the source location
  356. * @return the new location
  357. */
  358. private Location endLocation(double lat1, double lon1, double brng, double dist) {
  359. double a = 6378137, b = 6356752.3142, f = 1 / 298.257223563;
  360. double s = dist;
  361. double alpha1 = Math.toRadians(brng);
  362. double sinAlpha1 = Math.sin(alpha1), cosAlpha1 = Math.cos(alpha1);
  363. double tanU1 = (1 - f) * Math.tan(Math.toRadians(lat1));
  364. double cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)), sinU1 = tanU1 * cosU1;
  365. double sigma1 = Math.atan2(tanU1, cosAlpha1);
  366. double sinAlpha = cosU1 * sinAlpha1;
  367. double cosSqAlpha = 1 - sinAlpha * sinAlpha;
  368. double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
  369. double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
  370. double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
  371. double sigma = s / (b * A), sigmaP = 2 * Math.PI;
  372. double cos2SigmaM = 0, sinSigma = 0, deltaSigma = 0, cosSigma = 0;
  373. while (Math.abs(sigma - sigmaP) > 1e-12) {
  374. cos2SigmaM = Math.cos(2 * sigma1 + sigma);
  375. sinSigma = Math.sin(sigma);
  376. cosSigma = Math.cos(sigma);
  377. deltaSigma = B
  378. * sinSigma
  379. * (cos2SigmaM + B
  380. / 4
  381. * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM
  382. * (-3 + 4 * sinSigma * sinSigma)
  383. * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
  384. sigmaP = sigma;
  385. sigma = s / (b * A) + deltaSigma;
  386. }
  387. double tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
  388. double lat2 = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1, (1 - f)
  389. * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp));
  390. double lambda = Math.atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma
  391. * cosAlpha1);
  392. double C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
  393. double L = lambda
  394. - (1 - C)
  395. * f
  396. * sinAlpha
  397. * (sigma + C * sinSigma
  398. * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
  399. Location l = new Location(LocationManager.GPS_PROVIDER);
  400. l.setLatitude(Math.toDegrees(lat2));
  401. l.setLongitude(lon1 + Math.toDegrees(L));
  402. return l;
  403. }
  404. }