protected ArrayList<RoadNode> finalizeNodes(
     ArrayList<RoadNode> mNodes, ArrayList<RoadLink> mLinks, ArrayList<GeoPoint> polyline) {
   int n = mNodes.size();
   if (n == 0) return mNodes;
   ArrayList<RoadNode> newNodes = new ArrayList<RoadNode>(n);
   RoadNode lastNode = null;
   for (int i = 1; i < n - 1; i++) { // 1, n-1 => first and last MapQuest nodes are irrelevant.
     RoadNode node = mNodes.get(i);
     RoadLink link = mLinks.get(node.mNextRoadLink);
     if (lastNode != null && (node.mInstructions == null || node.mManeuverType == 0)) {
       // this node is irrelevant, don't keep it,
       // but update values of last node:
       lastNode.mLength += link.mLength;
       lastNode.mDuration += (node.mDuration + link.mDuration);
     } else {
       // TODO: check not the end node (n-2)
       node.mLength = link.mLength;
       node.mDuration += link.mDuration;
       int locationIndex = link.mShapeIndex;
       node.mLocation = polyline.get(locationIndex);
       newNodes.add(node);
       lastNode = node;
     }
   }
   // switch to the new array of nodes:
   return newNodes;
 }
Exemple #2
0
 /**
  * Return the intersections, which are the vertices in this graph.
  *
  * @return The vertices in this graph as GeographicPoints
  */
 public Set<GeographicPoint> getVertices() {
   Set<GeographicPoint> result = new HashSet<GeographicPoint>();
   // TODO: Implement this method in WEEK 2
   for (RoadNode node : nodes.values()) {
     result.add(node.getLocation());
   }
   return result;
 }
 public void endElement(String uri, String localName, String name) throws SAXException {
   if (localName.equals("points")) {
     if (isPolyline) {
       // detailed piece of road for the step, to add:
       ArrayList<GeoPoint> polyLine = PolylineEncoder.decode(mString, 10);
       mRoad.mRouteHigh.addAll(polyLine);
     } else if (isOverviewPolyline) {
       // low-def polyline for the whole road:
       mRoad.setRouteLow(PolylineEncoder.decode(mString, 10));
     }
   } else if (localName.equals("polyline")) {
     isPolyline = false;
   } else if (localName.equals("overview_polyline")) {
     isOverviewPolyline = false;
   } else if (localName.equals("value")) {
     mValue = Integer.parseInt(mString);
   } else if (localName.equals("duration")) {
     if (isStep) mNode.mDuration = mValue;
     else mLeg.mDuration = mValue;
     isDuration = false;
   } else if (localName.equals("distance")) {
     if (isStep) mNode.mLength = mValue / 1000.0;
     else mLeg.mLength = mValue / 1000.0;
     isDistance = false;
   } else if (localName.equals("html_instructions")) {
     if (isStep) {
       mString = mString.replaceAll("<[^>]*>", " "); // remove everything in <...>
       mString = mString.replaceAll("&nbsp;", " ");
       mNode.mInstructions = mString;
       // Log.d(BonusPackHelper.LOG_TAG, mString);
     }
   } else if (localName.equals("start_location")) {
     if (isStep) mNode.mLocation = new GeoPoint(mLat, mLng);
   } else if (localName.equals("step")) {
     mRoad.mNodes.add(mNode);
     isStep = false;
   } else if (localName.equals("leg")) {
     mRoad.mLegs.add(mLeg);
     isLeg = false;
   } else if (localName.equals("lat")) {
     mLat = Double.parseDouble(mString);
   } else if (localName.equals("lng")) {
     mLng = Double.parseDouble(mString);
   } else if (localName.equals("northeast")) {
     if (isBB) {
       mNorth = mLat;
       mEast = mLng;
     }
   } else if (localName.equals("southwest")) {
     if (isBB) {
       mSouth = mLat;
       mWest = mLng;
     }
   } else if (localName.equals("bounds")) {
     mRoad.mBoundingBox = new BoundingBoxE6(mNorth, mEast, mSouth, mWest);
     isBB = false;
   }
 }
  @Override
  public Road getRoad(ArrayList<GeoPoint> waypoints) {
    String url = getUrl(waypoints);
    Log.d(BonusPackHelper.LOG_TAG, "OSRMRoadManager.getRoad:" + url);

    // String jString = BonusPackHelper.requestStringFromUrl(url);
    HttpConnection connection = new HttpConnection();
    connection.setUserAgent(mUserAgent);
    connection.doGet(url);
    String jString = connection.getContentAsString();
    connection.close();

    if (jString == null) {
      Log.e(BonusPackHelper.LOG_TAG, "OSRMRoadManager::getRoad: request failed.");
      return new Road(waypoints);
    }
    Locale l = Locale.getDefault();
    HashMap<String, String> directions = (HashMap<String, String>) DIRECTIONS.get(l.getLanguage());
    if (directions == null) directions = (HashMap<String, String>) DIRECTIONS.get("en");
    Road road = new Road();
    try {
      JSONObject jObject = new JSONObject(jString);
      road.mStatus = jObject.getInt("status");
      String route_geometry = jObject.getString("route_geometry");
      road.mRouteHigh = PolylineEncoder.decode(route_geometry, 1, false);
      JSONArray jInstructions = jObject.getJSONArray("route_instructions");
      int n = jInstructions.length();
      RoadNode lastNode = null;
      for (int i = 0; i < n; i++) {
        JSONArray jInstruction = jInstructions.getJSONArray(i);
        RoadNode node = new RoadNode();
        int positionIndex = jInstruction.getInt(3);
        node.mLocation = road.mRouteHigh.get(positionIndex);
        node.mLength = jInstruction.getInt(2) / 1000.0;
        node.mDuration = jInstruction.getInt(4); // Segment duration in seconds.
        String direction = jInstruction.getString(0);
        String roadName = jInstruction.getString(1);
        if (lastNode != null && "1".equals(direction) && "".equals(roadName)) {
          // node "Continue" with no road name is useless, don't add it
          lastNode.mLength += node.mLength;
          lastNode.mDuration += node.mDuration;
        } else {
          node.mManeuverType = getManeuverCode(direction);
          node.mInstructions = buildInstructions(direction, roadName, directions);
          // Log.d(BonusPackHelper.LOG_TAG, direction+"=>"+node.mManeuverType+";
          // "+node.mInstructions);
          road.mNodes.add(node);
          lastNode = node;
        }
      }
      JSONObject jSummary = jObject.getJSONObject("route_summary");
      road.mLength = jSummary.getInt("total_distance") / 1000.0;
      road.mDuration = jSummary.getInt("total_time");
    } catch (JSONException e) {
      road.mStatus = Road.STATUS_TECHNICAL_ISSUE;
      e.printStackTrace();
    }
    if (road.mStatus != Road.STATUS_OK) {
      // Create default road:
      int status = road.mStatus;
      road = new Road(waypoints);
      road.mStatus = status;
    } else {
      road.buildLegs(waypoints);
      road.mBoundingBox = BoundingBoxE6.fromGeoPoints(road.mRouteHigh);
      road.mStatus = Road.STATUS_OK;
    }
    Log.d(BonusPackHelper.LOG_TAG, "OSRMRoadManager.getRoad - finished");
    return road;
  }
 @Override
 public void endElement(String uri, String localName, String name) throws SAXException {
   if (localName.equals("lat")) {
     mLat = Double.parseDouble(mStringBuilder.toString());
   } else if (localName.equals("lng")) {
     mLng = Double.parseDouble(mStringBuilder.toString());
   } else if (localName.equals("shapePoints")) {
     mRoad.mRouteHigh = PolylineEncoder.decode(mStringBuilder.toString(), 10);
     // Log.d("DD", "High="+mRoad.mRouteHigh.size());
   } else if (localName.equals("generalizedShape")) {
     mRoad.setRouteLow(PolylineEncoder.decode(mStringBuilder.toString(), 10));
     // Log.d("DD", "Low="+mRoad.mRouteLow.size());
   } else if (localName.equals("length")) {
     mLink.mLength = Double.parseDouble(mStringBuilder.toString());
   } else if (localName.equals("speed")) {
     mLink.mSpeed = Double.parseDouble(mStringBuilder.toString());
   } else if (localName.equals("shapeIndex")) {
     mLink.mShapeIndex = Integer.parseInt(mStringBuilder.toString());
   } else if (localName.equals("link")) {
     // End of a link: update road attributes:
     // GuidanceLinkCollection could in theory contain additional unused links,
     // but normally not with fishbone set to false.
     mLink.mDuration = mLink.mLength / mLink.mSpeed * 3600.0;
     mLinks.add(mLink);
     mRoad.mLength += mLink.mLength;
     mRoad.mDuration += mLink.mDuration;
     mLink = null;
   } else if (localName.equals("turnCost")) {
     int turnCost = Integer.parseInt(mStringBuilder.toString());
     mNode.mDuration += turnCost;
     mRoad.mDuration += turnCost;
   } else if (localName.equals("maneuverType")) {
     mNode.mManeuverType = Integer.parseInt(mStringBuilder.toString());
   } else if (localName.equals("info")) {
     if (isGuidanceNodeCollection) {
       if (mNode.mInstructions == null)
         // this is first "info" value for this node, keep it:
         mNode.mInstructions = mStringBuilder.toString();
     }
   } else if (localName.equals("linkId")) {
     if (isGuidanceNodeCollection)
       mNode.mNextRoadLink = Integer.parseInt(mStringBuilder.toString());
   } else if (localName.equals("node")) {
     mRoad.mNodes.add(mNode);
     mNode = null;
   } else if (localName.equals("GuidanceNodeCollection")) {
     isGuidanceNodeCollection = false;
   } else if (localName.equals("ul")) {
     if (isBB) {
       mNorth = mLat;
       mWest = mLng;
     }
   } else if (localName.equals("lr")) {
     if (isBB) {
       mSouth = mLat;
       mEast = mLng;
     }
   } else if (localName.equals("boundingBox")) {
     mRoad.mBoundingBox = new BoundingBoxE6(mNorth, mEast, mSouth, mWest);
     isBB = false;
   }
 }