pastebin - collaborative debugging tool
210.kpaste.net RSS


1.0
Posted by Anonymous on Fri 18th Nov 2011 07:48
raw | new post

  1. package ubc.cs.cpsc210.sustainabilityapp.routing;
  2.  
  3. import java.io.IOException;
  4. import java.net.URI;
  5. import java.net.URISyntaxException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10.  
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.HttpVersion;
  14. import org.apache.http.client.ClientProtocolException;
  15. import org.apache.http.client.HttpClient;
  16. import org.apache.http.client.ResponseHandler;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.conn.scheme.PlainSocketFactory;
  19. import org.apache.http.conn.scheme.Scheme;
  20. import org.apache.http.conn.scheme.SchemeRegistry;
  21. import org.apache.http.impl.client.BasicResponseHandler;
  22. import org.apache.http.impl.client.DefaultHttpClient;
  23. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  24. import org.apache.http.params.BasicHttpParams;
  25. import org.apache.http.params.HttpParams;
  26. import org.apache.http.params.HttpProtocolParams;
  27. import org.apache.http.util.EntityUtils;
  28. import org.json.JSONArray;
  29. import org.json.JSONObject;
  30. import org.json.JSONTokener;
  31.  
  32. import android.util.Log;
  33.  
  34. import ubc.cs.cpsc210.sustainabilityapp.model.LatLong;
  35.  
  36. /**
  37.  * Wrapper around a service which calculates routes between geographic
  38.  * locations. This class may be called concurrently from multiple threads -- it
  39.  * is thread-safe.
  40.  *
  41.  * Currently, this class wraps the www.yournavigation.org API (<a
  42.  * href="http://wiki.openstreetmap.org/wiki/YOURS#Routing_API"
  43.  * >http://wiki.openstreetmap.org/wiki/YOURS#Routing_API</a>).
  44.  */
  45. public class RoutingService {
  46.  
  47.         /**
  48.          * Caches routes retrieved by their endpoints. Access to this map must be
  49.          * synchronized on the map.
  50.          */
  51.         private Map<RouteEndpoints, RouteInfo> routeCache = new HashMap<RouteEndpoints, RouteInfo>();
  52.  
  53.         /**
  54.          * Client for making HTTP requests to the API of the service.
  55.          */
  56.         private HttpClient client;
  57.  
  58.         public RoutingService() {
  59.                 // Create an HttpClient with the ThreadSafeClientConnManager.
  60.                 // This connection manager must be used if more than one thread will
  61.                 // be accessing the HttpClient.
  62.                 HttpParams params = new BasicHttpParams();
  63.                 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  64.  
  65.                 SchemeRegistry schemeRegistry = new SchemeRegistry();
  66.                 schemeRegistry.register(new Scheme("http", PlainSocketFactory
  67.                                 .getSocketFactory(), 80));
  68.  
  69.                 ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
  70.                                 params, schemeRegistry);
  71.                 client = new DefaultHttpClient(cm, params);
  72.         }
  73.  
  74.         public void shutdown() {
  75.                 if (client != null) {
  76.                         client.getConnectionManager().shutdown();
  77.                 }
  78.         }
  79.  
  80.         /**
  81.          * Calculate route for given start point and end point. An internet
  82.          * connection must be available. See {@link getRouteFromService} for further
  83.          * information on route generation.
  84.          *
  85.          * @param start
  86.          *            The start point of the route.
  87.          * @param end
  88.          *            The end point of the route.
  89.          * @param useCache
  90.          *            Indicates whether the service should return a cached route, if
  91.          *            one exists. If this flag is set to true, and a cached route is
  92.          *            not available, then the new route obtained from the server
  93.          *            will be cached.
  94.          * @return Information on the route calculated, including the waypoints.
  95.          * @throws IOException
  96.          *             If an error occurs while retrieving the route from the
  97.          *             server.
  98.          */
  99.         public RouteInfo getRoute(LatLong start, LatLong end, boolean useCache)
  100.                         throws IOException {
  101.                 RouteEndpoints endpoints = new RouteEndpoints(start,end);
  102.                 RouteInfo ri;
  103.                
  104.                 try {
  105.                         ri=getRouteFromService(endpoints);
  106.                 } catch (URISyntaxException e) {
  107.                         // TODO Auto-generated catch block
  108.                         e.printStackTrace();
  109.                 }
  110.                
  111.                 return null;
  112.         }
  113.  
  114.         /**
  115.          * Calculate route for given endpoints. Currently, we use the
  116.          * www.yournavigation.org API (<a
  117.          * href="http://wiki.openstreetmap.org/wiki/YOURS#Routing_API"
  118.          * >http://wiki.openstreetmap.org/wiki/YOURS#Routing_API</a>), with result
  119.          * format set to geojson, vehicle set to foot and route type set to shortest
  120.          * (rather than fastest). Using route type of fastest can result in
  121.          * different routes between the same two points, depending on the direction
  122.          * traveled.
  123.          *
  124.          * Subclasses can override this method to connect to alternate routing
  125.          * services.
  126.          *
  127.          * @param endpoints
  128.          *            Endpoints of the route.
  129.          * @return Information on the route calculated, including waypoints.
  130.          * @throws IOException
  131.          *             If an error occurs while retrieving the route from the
  132.          *             server.
  133.          * @throws URISyntaxException
  134.          */
  135.         protected RouteInfo getRouteFromService(RouteEndpoints endpoints) throws IOException, URISyntaxException {
  136.                 String requestString, returnedString;
  137.                 HttpGet hg;
  138.                 URI uri = null;
  139.                 List<LatLong> waypoints = new ArrayList<LatLong>();
  140.                
  141.                 requestString = "http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson";
  142.                 requestString += "&flat=" + endpoints.getStart().getLatitude();
  143.                 requestString += "&flon=" + endpoints.getStart().getLongitude();
  144.                 requestString += "&tlat=" + endpoints.getEnd().getLatitude();
  145.                 requestString += "&tlon=" + endpoints.getEnd().getLongitude();
  146.                 requestString += "&v=foot";
  147.                 requestString += "&fast=0";
  148.                 requestString += "&layer=mapnik";
  149.                
  150.                 System.out.println(requestString);
  151.                
  152.                 try {
  153.                 uri = new URI(requestString);
  154.                 }
  155.                 catch (URISyntaxException e)
  156.                 {
  157.                         // handle exception
  158.                 }
  159.                
  160.                 hg = new HttpGet(uri);
  161.                 hg.addHeader("X-Yours-client", "UBC CPSC 210");
  162.                
  163. /*              ResponseHandler<String> handler = new ResponseHandler<String>() {
  164.                     public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
  165.                         HttpEntity entity = response.getEntity();
  166.                         if (entity != null) {
  167.                                 return EntityUtils.toString(entity);
  168.                         } else {
  169.                             return null;
  170.                         }
  171.                     }
  172.                 }; */
  173.                
  174.                 ResponseHandler<String> responseHandler = new BasicResponseHandler();
  175.                
  176.                 returnedString = client.execute(hg, responseHandler);
  177.                
  178.                 System.out.println(returnedString);
  179.                
  180.                 JSONObject json = new JSONObject();
  181.                
  182.                 return null;
  183.                
  184.         }
  185.  
  186.         private RouteInfo getCachedRoute(RouteEndpoints endpoints) {
  187.                 synchronized (routeCache) {
  188.                         return routeCache.get(endpoints);
  189.                 }
  190.         }
  191.  
  192.         private void addRouteToCache(RouteEndpoints endpoints, RouteInfo routeInfo) {
  193.                 synchronized (routeCache) {
  194.                         routeCache.put(endpoints, routeInfo);
  195.                 }
  196.         }
  197. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at