PageRenderTime 49ms CodeModel.GetById 24ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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