BordersExtractor.java

  1. package org.heigit.ors.routing.pathprocessors;

  2. import org.heigit.ors.routing.graphhopper.extensions.storages.BordersGraphStorage;

  3. import java.util.List;

  4. public class BordersExtractor {
  5.     public enum Avoid {CONTROLLED, NONE, ALL}

  6.     private final BordersGraphStorage storage;
  7.     private final int[] avoidCountries;

  8.     public BordersExtractor(BordersGraphStorage storage, int[] avoidCountries) {
  9.         this.storage = storage;
  10.         this.avoidCountries = avoidCountries;
  11.     }

  12.     public int getValue(int edgeId) {
  13.         // Get the type of border
  14.         return storage.getEdgeValue(edgeId, BordersGraphStorage.Property.TYPE);
  15.     }

  16.     public boolean isBorder(int edgeId) {
  17.         int type = storage.getEdgeValue(edgeId, BordersGraphStorage.Property.TYPE);
  18.         return (type == BordersGraphStorage.OPEN_BORDER || type == BordersGraphStorage.CONTROLLED_BORDER);
  19.     }

  20.     public boolean isControlledBorder(int edgeId) {
  21.         return storage.getEdgeValue(edgeId, BordersGraphStorage.Property.TYPE) == BordersGraphStorage.CONTROLLED_BORDER;
  22.     }

  23.     public boolean isOpenBorder(int edgeId) {
  24.         return storage.getEdgeValue(edgeId, BordersGraphStorage.Property.TYPE) == BordersGraphStorage.OPEN_BORDER;
  25.     }

  26.     public boolean restrictedCountry(int edgeId) {
  27.         int startCountry = storage.getEdgeValue(edgeId, BordersGraphStorage.Property.START);
  28.         int endCountry = storage.getEdgeValue(edgeId, BordersGraphStorage.Property.END);

  29.         for (int i = 0; i < avoidCountries.length; i++) {
  30.             if (startCountry == avoidCountries[i] || endCountry == avoidCountries[i]) {
  31.                 return true;
  32.             }
  33.         }
  34.         return false;
  35.     }

  36.     /**
  37.      * Check whether the start and end nodes of a list of edges are in the same country.
  38.      *
  39.      * @param edgeIds Edges that the country should be checked for
  40.      * @return true if at least one node is in the same country
  41.      */
  42.     public boolean isSameCountry(List<Integer> edgeIds) {
  43.         if (edgeIds.isEmpty())
  44.             return true;

  45.         short country0 = storage.getEdgeValue(edgeIds.get(0), BordersGraphStorage.Property.START);
  46.         short country1 = storage.getEdgeValue(edgeIds.get(0), BordersGraphStorage.Property.END);
  47.         for (int edgeId : edgeIds) {
  48.             short country2 = storage.getEdgeValue(edgeId, BordersGraphStorage.Property.START);
  49.             short country3 = storage.getEdgeValue(edgeId, BordersGraphStorage.Property.END);
  50.             if (country0 != country2
  51.                     && country0 != country3
  52.                     && country1 != country2
  53.                     && country1 != country3)
  54.                 return false;
  55.         }
  56.         return true;
  57.     }
  58. }