Beispiel #1
0
 /**
  * This file can be an osm xml (.osm), a compressed xml (.osm.zip or .osm.gz) or a protobuf file
  * (.pbf).
  */
 public GraphHopper setOSMFile(String osmFileStr) {
   if (Helper.isEmpty(osmFileStr)) {
     throw new IllegalArgumentException("OSM file cannot be empty.");
   }
   osmFile = osmFileStr;
   return this;
 }
Beispiel #2
0
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    String locale = "";
    String path = req.getPathInfo();
    if (!Helper.isEmpty(path) && path.startsWith("/")) locale = path.substring(1);

    if (Helper.isEmpty(locale)) {
      // fall back to language specified in header e.g. via browser settings
      String acceptLang = req.getHeader("Accept-Language");
      if (!Helper.isEmpty(acceptLang)) locale = acceptLang.split(",")[0];
    }

    Translation tr = map.get(locale);
    JSONObject json = new JSONObject();
    if (tr != null && !Locale.US.equals(tr.getLocale())) json.put("default", tr.asMap());

    json.put("locale", locale.toString());
    json.put("en", map.get("en").asMap());
    writeJson(req, res, json);
  }
Beispiel #3
0
  /**
   * Opens or creates a graph. The specified args need a property 'graph' (a folder) and if no such
   * folder exist it'll create a graph from the provided osm file (property 'osm'). A property
   * 'size' is used to preinstantiate a datastructure/graph to avoid over-memory allocation or
   * reallocation (default is 5mio)
   *
   * <p>
   *
   * @param graphHopperFolder is the folder containing graphhopper files (which can be compressed
   *     too)
   */
  @Override
  public boolean load(String graphHopperFolder) {
    if (Helper.isEmpty(graphHopperFolder))
      throw new IllegalStateException("graphHopperLocation is not specified. call init before");

    if (graph != null) throw new IllegalStateException("graph is already loaded");

    if (graphHopperFolder.endsWith("-gh")) {
      // do nothing
    } else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml")) {
      throw new IllegalArgumentException("To import an osm file you need to use importOrLoad");
    } else if (graphHopperFolder.indexOf(".") < 0) {
      if (new File(graphHopperFolder + "-gh").exists()) graphHopperFolder += "-gh";
    } else {
      File compressed = new File(graphHopperFolder + ".ghz");
      if (compressed.exists() && !compressed.isDirectory()) {
        try {
          new Unzipper().unzip(compressed.getAbsolutePath(), graphHopperFolder, removeZipped);
        } catch (IOException ex) {
          throw new RuntimeException(
              "Couldn't extract file " + compressed.getAbsolutePath() + " to " + graphHopperFolder,
              ex);
        }
      }
    }
    setGraphHopperLocation(graphHopperFolder);

    GHDirectory dir = new GHDirectory(ghLocation, dataAccessType);
    if (chUsage) graph = new LevelGraphStorage(dir, encodingManager);
    else graph = new GraphStorage(dir, encodingManager);

    graph.setSegmentSize(defaultSegmentSize);
    if (!graph.loadExisting()) return false;

    postProcessing();
    initIndex();
    return true;
  }
  /** @return the speed in km/h */
  static int parseSpeed(String str) {
    if (Helper.isEmpty(str)) {
      return -1;
    }

    try {
      int val;
      // see https://en.wikipedia.org/wiki/Knot_%28unit%29#Definitions
      int mpInteger = str.indexOf("mp");
      if (mpInteger > 0) {
        str = str.substring(0, mpInteger).trim();
        val = Integer.parseInt(str);
        return (int) Math.round(val * DistanceCalcEarth.KM_MILE);
      }

      int knotInteger = str.indexOf("knots");
      if (knotInteger > 0) {
        str = str.substring(0, knotInteger).trim();
        val = Integer.parseInt(str);
        return (int) Math.round(val * 1.852);
      }

      int kmInteger = str.indexOf("km");
      if (kmInteger > 0) {
        str = str.substring(0, kmInteger).trim();
      } else {
        kmInteger = str.indexOf("kph");
        if (kmInteger > 0) {
          str = str.substring(0, kmInteger).trim();
        }
      }

      return Integer.parseInt(str);
    } catch (Exception ex) {
      return -1;
    }
  }
Beispiel #5
0
  public GraphHopper init(CmdArgs args) throws IOException {
    if (!Helper.isEmpty(args.get("config", ""))) {
      CmdArgs tmp = CmdArgs.readFromConfig(args.get("config", ""), "graphhopper.config");
      // command line configuration overwrites the ones in the config file
      tmp.merge(args);
      args = tmp;
    }

    String tmpOsmFile = args.get("osmreader.osm", "");
    if (!Helper.isEmpty(tmpOsmFile)) osmFile = tmpOsmFile;

    String graphHopperFolder = args.get("graph.location", "");
    if (Helper.isEmpty(graphHopperFolder) && Helper.isEmpty(ghLocation)) {
      if (Helper.isEmpty(osmFile))
        throw new IllegalArgumentException("You need to specify an OSM file.");

      graphHopperFolder = Helper.pruneFileEnd(osmFile) + "-gh";
    }

    // graph
    setGraphHopperLocation(graphHopperFolder);
    expectedCapacity = args.getLong("graph.expectedCapacity", expectedCapacity);
    defaultSegmentSize = args.getInt("graph.dataaccess.segmentSize", defaultSegmentSize);
    String dataAccess = args.get("graph.dataaccess", "RAM_STORE").toUpperCase();
    if (dataAccess.contains("MMAP")) {
      setMemoryMapped();
    } else {
      if (dataAccess.contains("SAVE") || dataAccess.contains("INMEMORY"))
        throw new IllegalStateException(
            "configuration names for dataAccess changed. Use eg. RAM or RAM_STORE");

      if (dataAccess.contains("RAM_STORE")) setInMemory(true, true);
      else setInMemory(true, false);
    }

    if (dataAccess.contains("SYNC")) dataAccessType = new DAType(dataAccessType, true);

    sortGraph = args.getBool("graph.doSort", sortGraph);
    removeZipped = args.getBool("graph.removeZipped", removeZipped);

    // prepare
    doPrepare = args.getBool("prepare.doPrepare", doPrepare);
    String chShortcuts = args.get("prepare.chShortcuts", "no");
    boolean levelGraph =
        "true".equals(chShortcuts)
            || "fastest".equals(chShortcuts)
            || "shortest".equals(chShortcuts);
    if (levelGraph) setCHShortcuts(true, !"shortest".equals(chShortcuts));

    if (args.has("prepare.updates.periodic"))
      periodicUpdates = args.getInt("prepare.updates.periodic", periodicUpdates);

    if (args.has("prepare.updates.lazy"))
      lazyUpdates = args.getInt("prepare.updates.lazy", lazyUpdates);

    if (args.has("prepare.updates.neighbor"))
      neighborUpdates = args.getInt("prepare.updates.neighbor", neighborUpdates);

    // routing
    defaultAlgorithm = args.get("routing.defaultAlgorithm", defaultAlgorithm);

    // osm import
    wayPointMaxDistance = args.getDouble("osmreader.wayPointMaxDistance", wayPointMaxDistance);
    String type = args.get("osmreader.acceptWay", "CAR");
    encodingManager = new EncodingManager(type);
    workerThreads = args.getInt("osmreader.workerThreads", workerThreads);
    enableInstructions = args.getBool("osmreader.instructions", enableInstructions);

    // index
    preciseIndexResolution = args.getInt("index.highResolution", preciseIndexResolution);
    return this;
  }