Example #1
0
  public GHResponse route(
      List<GHPoint> waypoints, List<Double> headings, boolean withInstructions) {
    if (waypoints.size() < 2) {
      throw new IllegalArgumentException("Number of waypoints must be greater or equal 2");
    }

    GHRequest request;
    if (headings != null) {
      request = new GHRequest(waypoints, headings);
      request.setVehicle(FlagEncoderFactory.CAR);
      request.setWeighting("fastest");
      if (!USE_CONTRACTION_HIERARCHIES) {
        request.getHints().put(Parameters.CH.DISABLE, true);
        request.getHints().put("routing.flexible_mode.force", true);
        request.getHints().put(Parameters.Routing.EDGE_BASED, true);
      } else {
        request
            .getHints()
            .put(
                Parameters.CH.FORCE_HEADING,
                true); // Allow headings for routes using CH, but may produce artifacts (see
        // https://github.com/graphhopper/graphhopper/pull/434#issuecomment-110275256)
      }
    } else {
      request = new GHRequest(waypoints);
    }
    request.setLocale(Locale.getDefault()).setAlgorithm(Parameters.Algorithms.DIJKSTRA_BI);
    if (!withInstructions) {
      request.getHints().put(Parameters.Routing.INSTRUCTIONS, false);
    }
    return gh.route(request);
  }
Example #2
0
  @Override
  public GHResponse route(GHRequest request) {
    try {
      String places = "";
      for (GHPoint p : request.getPoints()) {
        places += "point=" + p.lat + "," + p.lon + "&";
      }

      boolean tmpInstructions = request.getHints().getBool("instructions", instructions);
      boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints);

      if (tmpInstructions && !tmpCalcPoints)
        throw new IllegalStateException(
            "Cannot calculate instructions without points (only points without instructions). "
                + "Use calc_points=false and instructions=false to disable point and instruction calculation");

      boolean tmpElevation = request.getHints().getBool("elevation", elevation);

      String url =
          routeServiceUrl
              + "?"
              + places
              + "&type=json"
              + "&instructions="
              + tmpInstructions
              + "&points_encoded=true"
              + "&calc_points="
              + tmpCalcPoints
              + "&algorithm="
              + request.getAlgorithm()
              + "&locale="
              + request.getLocale().toString()
              + "&elevation="
              + tmpElevation;

      if (!request.getVehicle().isEmpty()) url += "&vehicle=" + request.getVehicle();

      if (!key.isEmpty()) url += "&key=" + key;

      for (Entry<String, String> entry : request.getHints().toMap().entrySet()) {
        String urlKey = entry.getKey();
        String urlValue = entry.getValue();

        // use lower case conversion for check only!
        if (ignoreSet.contains(urlKey.toLowerCase())) continue;

        if (urlValue != null && !urlValue.isEmpty())
          url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue);
      }

      String str = downloader.downloadAsString(url, true);
      JSONObject json = new JSONObject(str);

      GHResponse res = new GHResponse();
      res.addErrors(readErrors(json));
      if (res.hasErrors()) return res;

      JSONArray paths = json.getJSONArray("paths");
      for (int index = 0; index < paths.length(); index++) {
        JSONObject path = paths.getJSONObject(index);
        PathWrapper altRsp = createPathWrapper(path, tmpCalcPoints, tmpInstructions, tmpElevation);
        res.add(altRsp);
      }

      return res;

    } catch (Exception ex) {
      throw new RuntimeException(
          "Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex);
    }
  }
Example #3
0
  @Override
  public GHResponse route(GHRequest request) {
    StopWatch sw = new StopWatch().start();
    double took = 0;
    try {
      String url =
          serviceUrl
              + "?point="
              + request.getFrom().lat
              + ","
              + request.getFrom().lon
              + "&point="
              + request.getTo().lat
              + ","
              + request.getTo().lon
              + "&type=json"
              + "&points_encoded="
              + pointsEncoded
              + "&min_path_precision="
              + request.getHint("douglas.minprecision", 1)
              + "&algo="
              + request.getAlgorithm();
      String str = downloader.downloadAsString(url);
      JSONObject json = new JSONObject(str);
      took = json.getJSONObject("info").getDouble("took");
      JSONArray paths = json.getJSONArray("paths");
      JSONObject firstPath = paths.getJSONObject(0);

      boolean is3D = false;
      if (firstPath.has("points_dim")) is3D = "3".equals(firstPath.getString("points_dim"));
      double distance = firstPath.getDouble("distance");
      int time = firstPath.getInt("time");
      PointList pointList;
      if (pointsEncoded) {
        pointList = WebHelper.decodePolyline(firstPath.getString("points"), 100, is3D);
      } else {
        JSONArray coords = firstPath.getJSONObject("points").getJSONArray("coordinates");
        pointList = new PointList(coords.length(), is3D);
        for (int i = 0; i < coords.length(); i++) {
          JSONArray arr = coords.getJSONArray(i);
          double lon = arr.getDouble(0);
          double lat = arr.getDouble(1);
          if (is3D) pointList.add(lat, lon, arr.getDouble(2));
          else pointList.add(lat, lon);
        }
      }
      GHResponse res = new GHResponse();
      if (instructions) {
        JSONArray instrArr = firstPath.getJSONArray("instructions");

        InstructionList il = new InstructionList();
        for (int instrIndex = 0; instrIndex < instrArr.length(); instrIndex++) {
          JSONObject jsonObj = instrArr.getJSONObject(instrIndex);
          double instDist = jsonObj.getDouble("distance");
          String text = jsonObj.getString("text");
          long instTime = jsonObj.getLong("time");
          int sign = jsonObj.getInt("sign");
          JSONArray iv = jsonObj.getJSONArray("interval");
          int from = iv.getInt(0);
          int to = iv.getInt(1);
          PointList instPL = new PointList(to - from, is3D);
          for (int j = from; j <= to; j++) {
            instPL.add(pointList, j);
          }

          // TODO way and payment type
          Instruction instr =
              new Instruction(sign, text, -1, -1, instPL).setDistance(instDist).setTime(instTime);
          il.add(instr);
        }
        res.setInstructions(il);
      }
      return res.setPoints(pointList).setDistance(distance).setMillis(time);
    } catch (Exception ex) {
      throw new RuntimeException(
          "Problem while fetching path " + request.getFrom() + "->" + request.getTo(), ex);
    } finally {
      logger.debug("Full request took:" + sw.stop().getSeconds() + ", API took:" + took);
    }
  }