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; }
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(" ", " "); 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; }