HeatStressWeighting.java

  1. package org.heigit.ors.routing.graphhopper.extensions.weighting;

  2. import com.graphhopper.routing.querygraph.EdgeIteratorStateHelper;
  3. import com.graphhopper.routing.util.FlagEncoder;
  4. import com.graphhopper.routing.weighting.FastestWeighting;
  5. import com.graphhopper.storage.GraphHopperStorage;
  6. import com.graphhopper.util.EdgeIteratorState;
  7. import com.graphhopper.util.PMap;
  8. import org.heigit.ors.routing.graphhopper.extensions.storages.CsvGraphStorage;
  9. import org.heigit.ors.routing.graphhopper.extensions.storages.GraphStorageUtils;

  10. public class HeatStressWeighting extends FastestWeighting {

  11.     private final CsvGraphStorage heatStressStorage;
  12.     private final byte[] buffer;
  13.     private final double weightingFactor;
  14.     private final String columnName;
  15.     private final int columnIndex; // Caches index of columnName for performance reasons

  16.     public HeatStressWeighting(FlagEncoder encoder, PMap map, GraphHopperStorage graphStorage) {
  17.         super(encoder, map);
  18.         heatStressStorage = GraphStorageUtils.getGraphExtension(graphStorage, CsvGraphStorage.class);
  19.         buffer = new byte[heatStressStorage.numEntries()];

  20.         weightingFactor = map.getDouble("factor", 1);
  21.         this.columnName = map.getString("column", "");
  22.         this.columnIndex = heatStressStorage.columnIndex(columnName);
  23.     }

  24.     @Override
  25.     public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
  26.         if (heatStressStorage != null) {
  27.             int stressLevel = heatStressStorage.getEdgeValue(EdgeIteratorStateHelper.getOriginalEdge(edgeState), columnIndex, buffer);
  28.             // Convert value range from [0,100] to [1,2] to avoid large detours and multiply by user weighting in API request
  29.             return (stressLevel * 0.01 * weightingFactor) + 1;
  30.         }

  31.         return 1.0;
  32.     }

  33.     @Override
  34.     public boolean equals(Object obj) {
  35.         if (obj == null)
  36.             return false;
  37.         if (getClass() != obj.getClass())
  38.             return false;
  39.         final HeatStressWeighting other = (HeatStressWeighting) obj;
  40.         return toString().equals(other.toString());
  41.     }

  42.     @Override
  43.     public int hashCode() {
  44.         return ("HeatStressWeighting" + this).hashCode();
  45.     }
  46. }