PageRenderTime 76ms CodeModel.GetById 16ms app.highlight 53ms RepoModel.GetById 2ms app.codeStats 0ms

/walkytalky/src/com/googlecode/eyesfree/walkytalky/StreetLocator.java

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