/** * Cluster to frames convertor. * * @return the tree map< integer, segment> containing the segment sorted by show first and start * time */ public TreeMap<Integer, Segment> clusterToFrames() { TreeMap<Integer, Segment> segmentTreeMapResult = new TreeMap<Integer, Segment>(); TreeSet<Segment> segmentList = getSegments(); for (Segment segment : segmentList) { int start = segment.getStart(); int length = segment.getLength(); for (int i = start; i < (start + length); i++) { Segment newSegment = (segment.clone()); newSegment.setStart(i); newSegment.setLength(1); newSegment.setCluster(this); // Plus utile le genre porte sur le cluster // newSegment.setGender(getGender()); segmentTreeMapResult.put(i, newSegment); } } return segmentTreeMapResult; }
/** * Collapse the near segments. * * @param delay the possible delay between segment * @return the array list */ public ArrayList<Integer> collapse(int delay) { Iterator<Segment> segmentIterator = iterator(); ArrayList<Integer> list = new ArrayList<Integer>(); Segment previous, current; if (segmentIterator.hasNext()) { previous = segmentIterator.next(); while (segmentIterator.hasNext()) { current = segmentIterator.next(); if (current.getShowName().compareTo(previous.getShowName()) == 0) { int previousStart = previous.getStart(); int previousEnd = previousStart + previous.getLength(); int currentStart = current.getStart(); int currentLength = current.getLength(); list.add(currentLength); if ((previousEnd + delay) >= currentStart) { previous.setLength((currentStart - previousStart) + currentLength); LinkSet linkSetSave = previous.getLinkSet(); EntitySet entitiesSave = previous.getEntities(); previous.getInformation().putAll(current.getInformation()); previous.setLinkSet(linkSetSave); previous.setEntities(entitiesSave); int sizeLink = previous.getLinkSet().size(); for (Link link : current.getLinkSet()) { link.setId(sizeLink++); previous.getLinkSet().add(link); } for (Entity entity : current.getEntities()) { previous.getEntities().add(entity); } segmentIterator.remove(); } else { previous = current; } } else { previous = current; } } } return list; }
/** * Parses a url pointing to a Google JSON object to a Route object. * * @return a Route object based on the JSON object. */ public Route parse() { // turn the stream into a string final String result = convertStreamToString(this.getInputStream()); // Create an empty route final Route route = new Route(); // Create an empty segment final Segment segment = new Segment(); try { // Tranform the string into a json object final JSONObject json = new JSONObject(result); // Get the route object final JSONObject jsonRoute = json.getJSONArray("routes").getJSONObject(0); // Get the leg, only one leg as we don't support waypoints final JSONObject leg = jsonRoute.getJSONArray("legs").getJSONObject(0); // Get the steps for this leg final JSONArray steps = leg.getJSONArray("steps"); // Number of steps for use in for loop final int numSteps = steps.length(); // Set the name of this route using the start & end addresses route.setName(leg.getString("start_address") + " to " + leg.getString("end_address")); // Get google's copyright notice (tos requirement) route.setCopyright(jsonRoute.getString("copyrights")); // Get the total length of the route. route.setLength(leg.getJSONObject("distance").getInt("value")); // Get any warnings provided (tos requirement) if (!jsonRoute.getJSONArray("warnings").isNull(0)) { route.setWarning(jsonRoute.getJSONArray("warnings").getString(0)); } /* * Loop through the steps, creating a segment for each one and * decoding any polylines found as we go to add to the route * object's map array. Using an explicit for loop because it is * faster! */ for (int i = 0; i < numSteps; i++) { // Get the individual step final JSONObject step = steps.getJSONObject(i); // Get the start position for this step and set it on the // segment final JSONObject start = step.getJSONObject("start_location"); final GeoPoint position = new GeoPoint( (int) (start.getDouble("lat") * 1E6), (int) (start.getDouble("lng") * 1E6)); segment.setPoint(position); // Set the length of this segment in metres final int length = step.getJSONObject("distance").getInt("value"); distance += length; segment.setLength(length); segment.setDistance(distance / 1000); // Strip html from google directions and set as turn instruction segment.setInstruction(step.getString("html_instructions").replaceAll("<(.*?)*>", "")); // Retrieve & decode this segment's polyline and add it to the // route. route.addPoints(decodePolyLine(step.getJSONObject("polyline").getString("points"))); // Push a copy of the segment to the route route.addSegment(segment.copy()); } } catch (JSONException e) { if (DEBUG.ON) { Log.e(e.getMessage(), "Google JSON Parser - " + feedUrl); } } return route; }