/** @return converted tower node */
  private int handlePillarNode(
      int tmpNode, long osmId, PointList pointList, boolean convertToTowerNode) {
    tmpNode = tmpNode - 3;
    int intlat = pillarLats.getInt(tmpNode);
    int intlon = pillarLons.getInt(tmpNode);
    if (intlat == Integer.MAX_VALUE || intlon == Integer.MAX_VALUE)
      throw new AssertionError(
          "Conversation pillarNode to towerNode already happended!? "
              + "osmId:"
              + osmId
              + " pillarIndex:"
              + tmpNode);

    double tmpLat = Helper.intToDegree(intlat);
    double tmpLon = Helper.intToDegree(intlon);

    if (convertToTowerNode) {
      // convert pillarNode type to towerNode, make pillar values invalid
      pillarLons.setInt(tmpNode, Integer.MAX_VALUE);
      pillarLats.setInt(tmpNode, Integer.MAX_VALUE);
      tmpNode = addTowerNode(osmId, tmpLat, tmpLon);
    } else pointList.add(tmpLat, tmpLon);

    return tmpNode;
  }
  @Test
  public void testDifferentEdgeFilter() {
    Graph g = new GraphBuilder(encodingManager).levelGraphCreate();
    g.edge(4, 3, 10, true);
    g.edge(3, 6, 10, true);

    g.edge(4, 5, 10, true);
    g.edge(5, 6, 10, true);

    AlgorithmPreparation prep = prepareGraph(g);
    DijkstraOneToMany algo = (DijkstraOneToMany) prep.createAlgo();
    algo.setEdgeFilter(
        new EdgeFilter() {
          @Override
          public boolean accept(EdgeIteratorState iter) {
            return iter.getAdjNode() != 5;
          }
        });
    Path p = algo.calcPath(4, 6);
    assertEquals(Helper.createTList(4, 3, 6), p.calcNodes());

    // important call!
    algo.clear();
    algo.setEdgeFilter(
        new EdgeFilter() {
          @Override
          public boolean accept(EdgeIteratorState iter) {
            return iter.getAdjNode() != 3;
          }
        });
    p = algo.calcPath(4, 6);
    assertEquals(Helper.createTList(4, 5, 6), p.calcNodes());
  }
  @Test
  public void testCopyTo() {
    graph = createGraph();
    initExampleGraph(graph);
    GraphStorage gs = newRAMGraph();
    gs.setSegmentSize(8000);
    gs.create(10);
    try {
      graph.copyTo(gs);
      checkExampleGraph(gs);
    } catch (Exception ex) {
      ex.printStackTrace();
      assertTrue(ex.toString(), false);
    }

    try {
      Helper.close((Closeable) graph);
      graph = createGraph();
      gs.copyTo(graph);
      checkExampleGraph(graph);
    } catch (Exception ex) {
      ex.printStackTrace();
      assertTrue(ex.toString(), false);
    }
    Helper.close((Closeable) graph);
  }
  private void _setNode(int id, double lat, double lon, double ele) {
    long tmp = (long) id * rowSizeInBytes;
    da.incCapacity(tmp + rowSizeInBytes);
    da.setInt(tmp + LAT, Helper.degreeToInt(lat));
    da.setInt(tmp + LON, Helper.degreeToInt(lon));

    if (is3D()) da.setInt(tmp + ELE, Helper.eleToInt(ele));
  }
  @Test
  public void testDecode() throws Exception {
    PointList list = WebHelper.decodePolyline("_p~iF~ps|U", 1);
    assertEquals(Helper.createPointList(38.5, -120.2), list);

    list = WebHelper.decodePolyline("_p~iF~ps|U_ulLnnqC_mqNvxq`@", 3);
    assertEquals(Helper.createPointList(38.5, -120.2, 40.7, -120.95, 43.252, -126.453), list);
  }
  @Test
  public void testEncode() throws Exception {
    assertEquals("_p~iF~ps|U", WebHelper.encodePolyline(Helper.createPointList(38.5, -120.2)));

    assertEquals(
        "_p~iF~ps|U_ulLnnqC_mqNvxq`@",
        WebHelper.encodePolyline(
            Helper.createPointList(38.5, -120.2, 40.7, -120.95, 43.252, -126.453)));
  }
Example #7
0
  private void optimize() {
    logger.info("optimizing ... (" + Helper.getMemInfo() + ")");
    graph.optimize();
    logger.info("finished optimize (" + Helper.getMemInfo() + ")");

    // move this into the GraphStorage.optimize method?
    if (sortGraph) {
      logger.info("sorting ... (" + Helper.getMemInfo() + ")");
      GraphStorage newGraph = GHUtility.newStorage(graph);
      GHUtility.sortDFS(graph, newGraph);
      graph = newGraph;
    }
  }
  @Test
  public void testBoth() throws Exception {
    PointList list =
        Helper.createPointList(
            38.5, -120.2, 43.252, -126.453, 40.7, -120.95, 50.3139, 10.612793, 50.04303, 9.497681);
    String str = WebHelper.encodePolyline(list);
    assertEquals(list, WebHelper.decodePolyline(str, list.size()));

    list =
        Helper.createPointList(38.5, -120.2, 43.252, -126.453, 40.7, -120.95, 40.70001, -120.95001);
    str = WebHelper.encodePolyline(list);
    assertEquals(list, WebHelper.decodePolyline(str, list.size()));
  }
  @Test
  public void testUseCache() {
    AlgorithmPreparation prep = prepareGraph(createTestGraph());
    RoutingAlgorithm algo = prep.createAlgo();
    Path p = algo.calcPath(0, 4);
    assertEquals(Helper.createTList(0, 4), p.calcNodes());

    // expand SPT
    p = algo.calcPath(0, 7);
    assertEquals(Helper.createTList(0, 4, 6, 5, 7), p.calcNodes());

    // use SPT
    p = algo.calcPath(0, 2);
    assertEquals(Helper.createTList(0, 1, 2), p.calcNodes());
  }
Example #10
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;
 }
Example #11
0
  /**
   * The URLs are a bit ugly and so we need to find out which area name a certain lat,lon coordinate
   * has.
   */
  private SRTMProvider init() {
    try {
      String strs[] = {
        "Africa", "Australia", "Eurasia", "Islands", "North_America", "South_America"
      };
      for (String str : strs) {
        InputStream is = getClass().getResourceAsStream(str + "_names.txt.zip");
        ZipInputStream zis = new ZipInputStream(is);
        zis.getNextEntry();
        for (String line : Helper.readFile(new InputStreamReader(zis, "UTF-8"))) {
          int lat = Integer.parseInt(line.substring(1, 3));
          if (line.substring(0, 1).charAt(0) == 'S') lat = -lat;

          int lon = Integer.parseInt(line.substring(4, 7));
          if (line.substring(3, 4).charAt(0) == 'W') lon = -lon;

          int intKey = calcIntKey(lat, lon);
          String key = areas.put(intKey, str);
          if (key != null)
            throw new IllegalStateException(
                "do not overwrite existing! key " + intKey + " " + key + " vs. " + str);
        }
      }
      return this;
    } catch (Exception ex) {
      throw new IllegalStateException("Cannot load area names from classpath", ex);
    }
  }
  /**
   * Puts a key/value pair into the array, optimizing for the case where the key is greater than all
   * existing keys in the array.
   */
  public int append(long key, long value) {
    if (mSize != 0 && key <= mKeys[mSize - 1]) {
      return put(key, value);
    }

    if (mGarbage && mSize >= mKeys.length) {
      gc();
    }

    int pos = mSize;
    if (pos >= mKeys.length) {
      int n = Helper.idealIntArraySize(pos + 1);

      long[] nkeys = new long[n];
      long[] nvalues = new long[n];

      System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
      System.arraycopy(mValues, 0, nvalues, 0, mValues.length);

      mKeys = nkeys;
      mValues = nvalues;
    }

    mKeys[pos] = key;
    mValues[pos] = value;
    mSize = pos + 1;
    return pos;
  }
Example #13
0
  @Override
  public double getElevation(int id) {
    if (!is3D()) return Double.NaN;

    int intVal = da.getInt((long) id * rowSizeInBytes + ELE);
    return Helper.intToEle(intVal);
  }
Example #14
0
  @Before
  public void setUp() {
    if (!Helper.removeDir(folder))
      throw new IllegalStateException("cannot delete folder " + folder);

    folder.mkdirs();
    directory = folder.getAbsolutePath() + "/";
  }
  @Override
  public boolean addNode(long osmId, double lat, double lon) {
    int nodeType = osmIdToIndexMap.get(osmId);
    if (nodeType == EMPTY) return false;

    if (nodeType == TOWER_NODE) {
      addTowerNode(osmId, lat, lon);
    } else if (nodeType == PILLAR_NODE) {
      int tmp = (pillarId + 1) * 4;
      pillarLats.ensureCapacity(tmp);
      pillarLats.setInt(pillarId, Helper.degreeToInt(lat));
      pillarLons.ensureCapacity(tmp);
      pillarLons.setInt(pillarId, Helper.degreeToInt(lon));
      osmIdToIndexMap.put(osmId, pillarId + 3);
      pillarId++;
    }
    return true;
  }
  @Test
  public void testPathRecursiveUnpacking() {
    // use an encoder where it is possible to store 2 weights per edge
    FlagEncoder encoder = new Bike2WeightFlagEncoder();
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    EncodingManager em = new EncodingManager(encoder);
    GraphHopperStorage ghStorage =
        createGHStorage(em, Collections.<Weighting>singleton(weighting), false);
    CHGraphImpl g2 = (CHGraphImpl) ghStorage.getGraph(CHGraph.class, weighting);
    g2.edge(0, 1, 1, true);
    EdgeIteratorState iter1_1 = g2.edge(0, 2, 1.4, false);
    EdgeIteratorState iter1_2 = g2.edge(2, 5, 1.4, false);
    g2.edge(1, 2, 1, true);
    g2.edge(1, 3, 3, true);
    g2.edge(2, 3, 1, true);
    g2.edge(4, 3, 1, true);
    g2.edge(2, 5, 1.4, true);
    g2.edge(3, 5, 1, true);
    g2.edge(5, 6, 1, true);
    g2.edge(4, 6, 1, true);
    g2.edge(6, 7, 1, true);
    EdgeIteratorState iter2_2 = g2.edge(5, 7);
    iter2_2.setDistance(1.4).setFlags(encoder.setProperties(10, true, false));

    ghStorage.freeze();
    // simulate preparation
    CHEdgeIteratorState iter2_1 = g2.shortcut(0, 5);
    iter2_1.setDistance(2.8).setFlags(encoder.setProperties(10, true, false));
    iter2_1.setSkippedEdges(iter1_1.getEdge(), iter1_2.getEdge());
    CHEdgeIteratorState tmp = g2.shortcut(0, 7);
    tmp.setDistance(4.2).setFlags(encoder.setProperties(10, true, false));
    tmp.setSkippedEdges(iter2_1.getEdge(), iter2_2.getEdge());
    g2.setLevel(1, 0);
    g2.setLevel(3, 1);
    g2.setLevel(4, 2);
    g2.setLevel(6, 3);
    g2.setLevel(2, 4);
    g2.setLevel(5, 5);
    g2.setLevel(7, 6);
    g2.setLevel(0, 7);

    AlgorithmOptions opts = new AlgorithmOptions(AlgorithmOptions.DIJKSTRA_BI, encoder, weighting);
    Path p =
        new PrepareContractionHierarchies(
                new GHDirectory("", DAType.RAM_INT),
                ghStorage,
                g2,
                encoder,
                weighting,
                TraversalMode.NODE_BASED)
            .createAlgo(g2, opts)
            .calcPath(0, 7);

    assertEquals(Helper.createTList(0, 2, 5, 7), p.calcNodes());
    assertEquals(1064, p.getTime());
    assertEquals(4.2, p.getDistance(), 1e-5);
  }
 @Override
 void startWayProcessing() {
   LoggerFactory.getLogger(getClass())
       .info(
           "finished node processing. osmIdMap:"
               + (int) (osmIdToIndexMap.capacity() * (12f + 1) / Helper.MB)
               + "MB, "
               + Helper.getMemInfo());
 }
Example #18
0
  @Test
  public void testHeader() {
    DataAccess da = createDataAccess(name);
    da.create(300);
    da.setHeader(7 * 4, 123);
    assertEquals(123, da.getHeader(7 * 4));
    da.setHeader(10 * 4, Integer.MAX_VALUE / 3);
    assertEquals(Integer.MAX_VALUE / 3, da.getHeader(10 * 4));

    da.setHeader(11 * 4, Helper.degreeToInt(123.321));
    assertEquals(123.321, Helper.intToDegree(da.getHeader(11 * 4)), 1e-4);
    da.flush();
    da.close();

    da = createDataAccess(name);
    assertTrue(da.loadExisting());
    assertEquals(123, da.getHeader(7 * 4));
    da.close();
  }
Example #19
0
 private GPXFile parseJSON(HttpServletRequest httpReq) throws IOException {
   JSONObject json = new JSONObject(Helper.isToString(httpReq.getInputStream()));
   // TODO think about format first:
   // type: bike
   // locale
   // encoded points or not
   // points_encoded
   // points: lat,lon[,ele,millis]
   throw new IllegalStateException("json input not yet supported");
 }
  @Test
  public void testBaseGraphMultipleVehicles() {
    AlgorithmOptions footOptions =
        AlgorithmOptions.start()
            .flagEncoder(footEncoder)
            .weighting(new FastestWeighting(footEncoder))
            .build();
    AlgorithmOptions carOptions =
        AlgorithmOptions.start()
            .flagEncoder(carEncoder)
            .weighting(new FastestWeighting(carEncoder))
            .build();

    GraphHopperStorage g =
        createGHStorage(
            encodingManager,
            Arrays.asList(footOptions.getWeighting(), carOptions.getWeighting()),
            false);
    initFootVsCar(g);

    // do CH preparation for car
    RoutingAlgorithmFactory contractedFactory = createFactory(g, carOptions);

    // use contracted graph
    Path p1 =
        contractedFactory
            .createAlgo(getGraph(g, carOptions.getWeighting()), carOptions)
            .calcPath(0, 7);
    assertEquals(Helper.createTList(0, 4, 6, 7), p1.calcNodes());
    assertEquals(p1.toString(), 15000, p1.getDistance(), 1e-6);

    // use base graph for solving normal Dijkstra via car
    Path p2 = new RoutingAlgorithmFactorySimple().createAlgo(g, carOptions).calcPath(0, 7);
    assertEquals(Helper.createTList(0, 4, 6, 7), p2.calcNodes());
    assertEquals(p2.toString(), 15000, p2.getDistance(), 1e-6);
    assertEquals(p2.toString(), 2700 * 1000, p2.getTime());

    // use base graph for solving normal Dijkstra via foot
    Path p3 = new RoutingAlgorithmFactorySimple().createAlgo(g, footOptions).calcPath(0, 7);
    assertEquals(p3.toString(), 17000, p3.getDistance(), 1e-6);
    assertEquals(p3.toString(), 12240 * 1000, p3.getTime());
    assertEquals(Helper.createTList(0, 4, 5, 7), p3.calcNodes());
  }
Example #21
0
 private void flush() {
   logger.info(
       "flushing graph "
           + graph.toString()
           + ", details:"
           + graph.toDetailsString()
           + ", "
           + Helper.getMemInfo()
           + ")");
   graph.flush();
 }
 /**
  * Creates a new SparseLongLongArray containing no mappings that will not require any additional
  * memory allocation to store the specified number of mappings.
  */
 public SparseLongLongArray(int cap) {
   try {
     cap = Helper.idealIntArraySize(cap);
     mKeys = new long[cap];
     mValues = new long[cap];
     mSize = 0;
   } catch (OutOfMemoryError err) {
     System.err.println("requested capacity " + cap);
     throw err;
   }
 }
 @Test
 public void testUnpackingOrder_Fastest() {
   LevelGraphStorage g = (LevelGraphStorage) createGraph();
   PrepareContractionHierarchies prepare = new PrepareContractionHierarchies().graph(g);
   WeightCalculation calc = new FastestCalc(carEncoder);
   initUnpackingGraph(g, calc);
   RoutingAlgorithm algo = prepare.type(calc).vehicle(carEncoder).createAlgo();
   Path p = algo.calcPath(10, 6);
   assertEquals(7, p.distance(), 1e-1);
   assertEquals(Helper.createTList(10, 0, 1, 2, 3, 4, 5, 6), p.calcNodes());
 }
  @Test
  public void testEnabledElevation() {
    graph = createGraph(defaultGraphLoc, true);
    NodeAccess na = graph.getNodeAccess();
    assertTrue(na.is3D());
    na.setNode(0, 10, 20, -10);
    na.setNode(1, 11, 2, 100);
    assertEquals(-10, na.getEle(0), 1e-1);
    assertEquals(100, na.getEle(1), 1e-1);

    graph.edge(0, 1).setWayGeometry(Helper.createPointList3D(10, 27, 72, 11, 20, 1));
    assertEquals(
        Helper.createPointList3D(10, 27, 72, 11, 20, 1),
        GHUtility.getEdge(graph, 0, 1).fetchWayGeometry(0));
    assertEquals(
        Helper.createPointList3D(10, 20, -10, 10, 27, 72, 11, 20, 1, 11, 2, 100),
        GHUtility.getEdge(graph, 0, 1).fetchWayGeometry(3));
    assertEquals(
        Helper.createPointList3D(11, 2, 100, 11, 20, 1, 10, 27, 72, 10, 20, -10),
        GHUtility.getEdge(graph, 1, 0).fetchWayGeometry(3));
  }
 @Test
 public void testRoundaboutUnpacking() {
   LevelGraph g = createGraph();
   initRoundaboutGraph(g);
   int old = g.getAllEdges().maxId();
   PrepareContractionHierarchies prepare = new PrepareContractionHierarchies().graph(g);
   prepare.doWork();
   assertEquals(old + 25, g.getAllEdges().maxId());
   RoutingAlgorithm algo = prepare.createAlgo();
   Path p = algo.calcPath(4, 7);
   assertEquals(Helper.createTList(4, 5, 6, 7), p.calcNodes());
 }
Example #26
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);
  }
  @Test
  public void testAddSkipNodes() {
    Graph g = createWikipediaTestGraph();
    Path p = prepareGraph(g).createAlgo().calcPath(0, 4);
    assertEquals(p.toString(), 20, p.weight(), 1e-6);
    assertTrue(p.toString(), p.calcNodes().contains(5));

    DijkstraBidirectionRef db = new DijkstraBidirectionRef(g);
    db.addSkipNode(5);
    p = db.calcPath(0, 4);
    assertFalse(p.toString(), p.calcNodes().contains(5));
    assertEquals(Helper.createTList(0, 2, 3, 4), p.calcNodes());
  }
  @Test
  public void testCopyProperties() {
    graph = createGraph();
    EdgeIteratorState edge =
        graph.edge(1, 3, 10, false).setName("testing").setWayGeometry(Helper.createPointList(1, 2));

    EdgeIteratorState newEdge = graph.edge(1, 3, 10, false);
    edge.copyPropertiesTo(newEdge);
    assertEquals(edge.getName(), newEdge.getName());
    assertEquals(edge.getDistance(), newEdge.getDistance(), 1e-7);
    assertEquals(edge.getFlags(), newEdge.getFlags());
    assertEquals(edge.fetchWayGeometry(0), newEdge.fetchWayGeometry(0));
  }
  @Test
  public void testSinglePoints120() {
    Graph g = createSampleGraph(new EncodingManager("car"));
    idx = createIndex(g, -1);

    assertEquals(1, findID(idx, 1.637, 2.23));
    assertEquals(10, findID(idx, 3.649, 1.375));
    assertEquals(9, findID(idx, 3.3, 2.2));
    assertEquals(6, findID(idx, 3.0, 1.5));

    assertEquals(10, findID(idx, 3.8, 0));
    assertEquals(10, findID(idx, 3.8466, 0.021));
    Helper.close((Closeable) g);
  }
  @Test
  public void testSave_and_fileFormat() throws IOException {
    graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), true).create(defaultSize);
    NodeAccess na = graph.getNodeAccess();
    assertTrue(na.is3D());
    na.setNode(0, 10, 10, 0);
    na.setNode(1, 11, 20, 1);
    na.setNode(2, 12, 12, 0.4);

    EdgeIteratorState iter2 = graph.edge(0, 1, 100, true);
    iter2.setWayGeometry(Helper.createPointList3D(1.5, 1, 0, 2, 3, 0));
    EdgeIteratorState iter1 = graph.edge(0, 2, 200, true);
    iter1.setWayGeometry(Helper.createPointList3D(3.5, 4.5, 0, 5, 6, 0));
    graph.edge(9, 10, 200, true);
    graph.edge(9, 11, 200, true);
    graph.edge(1, 2, 120, false);

    iter1.setName("named street1");
    iter2.setName("named street2");

    checkGraph(graph);
    graph.flush();
    graph.close();

    graph = newGHStorage(new MMapDirectory(defaultGraphLoc), true);
    assertTrue(graph.loadExisting());

    assertEquals(12, graph.getNodes());
    checkGraph(graph);

    assertEquals(
        "named street1", graph.getEdgeIteratorState(iter1.getEdge(), iter1.getAdjNode()).getName());
    assertEquals(
        "named street2", graph.getEdgeIteratorState(iter2.getEdge(), iter2.getAdjNode()).getName());
    graph.edge(3, 4, 123, true).setWayGeometry(Helper.createPointList3D(4.4, 5.5, 0, 6.6, 7.7, 0));
    checkGraph(graph);
  }