Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor wayFromWays method #170

Open
t-ober opened this issue Dec 8, 2021 · 0 comments
Open

Refactor wayFromWays method #170

t-ober opened this issue Dec 8, 2021 · 0 comments

Comments

@t-ober
Copy link
Contributor

t-ober commented Dec 8, 2021

Refactor and move to new scala GeoUtils package

  /**
   * Chains together several ways which are close together (e.g. as part of a relation) to one,
   * correctly ordered way. This is a computational expensive method which might be worth
   * refactoring!
   *
   * @param waysToChain the ways from a relation
   * @param radius the radius that should be considered for search for the next connection point
   *     between two ways
   * @param wayId the id of the newly created way
   * @return A concatenated {@link Way} built from a collection of {@link Way}s
   * @deprecated ATTENTION! DON'T REMOVE: This method is currently not under test and has to be
   *     revised thoroughly
   */
  @Deprecated
  public static Way wayFromWays(List<Way> waysToChain, Quantity<Length> radius, int wayId) {

    LinkedList<Way> waysCopy = new LinkedList<>(waysToChain);

    HashMap<Node, Way> nodeToWayMap = new HashMap<>();
    for (Way way : waysToChain) {
      way.getNodes().forEach(node -> nodeToWayMap.put(node, way));
    }

    LinkedList<Node> nodesList = new LinkedList<>(waysToChain.get(0).getNodes());
    waysCopy.remove(waysToChain.get(0));

    while (!waysCopy.isEmpty()) {
      Node lastNode = nodesList.getLast();

      Polygon circle = radiusWithCircleAsPolygon(lastNode.getLatlon(), radius);

      double distance = Double.POSITIVE_INFINITY;
      Way nextWay = null;
      Node nextLastNode = null;
      for (Map.Entry<Node, Way> entry : nodeToWayMap.entrySet()) {
        if (rayCasting(circle, entry.getKey().getLatlon())) {
          double tempDistance;
          tempDistance =
              calcHaversine(
                      lastNode.getLatlon().getLat(),
                      lastNode.getLatlon().getLon(),
                      entry.getKey().getLatlon().getLat(),
                      entry.getKey().getLatlon().getLon())
                  .to(KILOMETRE)
                  .getValue()
                  .doubleValue();
          if (tempDistance < distance) {
            distance = tempDistance;
            nextWay = entry.getValue();
            nextLastNode = entry.getKey();
          }
        }
      }

      if (nextWay != null) {
        if (nextWay.getNodes().indexOf(nextLastNode) == nextWay.getNodes().size() - 1) {
          Collections.reverse(nextWay.getNodes());
        }
        nodesList.addAll(nextWay.getNodes());

        logger.debug("Removing way with id {}", nextWay.getId());
      }

      nodeToWayMap.values().removeAll(Collections.singleton(nextWay));
      waysCopy.remove(nextWay);
    }

    return new Way(wayId, new Tags(), nodesList);
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant