private TransitRoute createRoute(
      Id<TransitRoute> routeID, TransitStopFacility startStop, TransitStopFacility endStop) {

    FreespeedTravelTimeAndDisutility tC = new FreespeedTravelTimeAndDisutility(-6.0, 0.0, 0.0);
    LeastCostPathCalculator routingAlgo = new Dijkstra(this.net, tC, tC);

    Node startNode = this.net.getLinks().get(startStop.getLinkId()).getToNode();
    Node endNode = this.net.getLinks().get(endStop.getLinkId()).getFromNode();

    int startTime = 0 * 3600;

    // get Route
    Path path = routingAlgo.calcLeastCostPath(startNode, endNode, startTime, null, null);
    NetworkRoute route = new LinkNetworkRouteImpl(startStop.getLinkId(), endStop.getLinkId());
    route.setLinkIds(
        startStop.getLinkId(), NetworkUtils.getLinkIds(path.links), endStop.getLinkId());

    // get stops at Route
    List<TransitRouteStop> stops = new LinkedList<TransitRouteStop>();

    // first stop
    TransitRouteStop routeStop =
        this.tS.getFactory().createTransitRouteStop(startStop, startTime, startTime);
    stops.add(routeStop);

    // additional stops
    for (Link link : path.links) {
      startTime += link.getLength() / link.getFreespeed();
      if (this.tS.getFacilities().get(link.getId()) == null) {
        continue;
      }
      routeStop =
          this.tS
              .getFactory()
              .createTransitRouteStop(
                  this.tS.getFacilities().get(link.getId()), startTime, startTime);
      stops.add(routeStop);
    }

    // last stop
    startTime +=
        this.net.getLinks().get(endStop.getLinkId()).getLength()
            / this.net.getLinks().get(endStop.getLinkId()).getFreespeed();
    routeStop = this.tS.getFactory().createTransitRouteStop(endStop, startTime, startTime);
    stops.add(routeStop);

    // register departure
    TransitRoute transitRoute =
        this.tS.getFactory().createTransitRoute(routeID, route, stops, "pt");

    return transitRoute;
  }
Ejemplo n.º 2
0
 public void addShortestPath(int indexI) {
   if (indexI < links.size() - 1) {
     Path path =
         leastCostPathCalculator.calcLeastCostPath(
             links.get(indexI).getToNode(), links.get(indexI + 1).getFromNode(), 0, null, null);
     if (path != null) {
       int i = 1;
       for (Link link : path.links) {
         links.add(indexI + i, link);
         i++;
       }
     }
   }
 }
Ejemplo n.º 3
0
 public void calculatePath() {
   links.clear();
   Iterator<StopTime> prev = trip.getStopTimes().values().iterator(),
       next = trip.getStopTimes().values().iterator();
   List<Link> prevLL, nextLL;
   Link prevL = null, nextL = null;
   next.next();
   Stop prevStop = null, nextStop = null;
   if (next.hasNext()) {
     Path bestPath = null;
     prevStop = stops.get(prev.next().getStopId());
     nextStop = stops.get(next.next().getStopId());
     if (prevStop.getLinkId() != null) {
       prevLL = new ArrayList<Link>();
       prevLL.add(network.getLinks().get(Id.create(prevStop.getLinkId(), Link.class)));
     } else prevLL = getBestLinksMode(network, prevStop.getPoint(), trip.getShape());
     if (nextStop.getLinkId() != null) {
       nextLL = new ArrayList<Link>();
       nextLL.add(network.getLinks().get(Id.create(nextStop.getLinkId(), Link.class)));
     } else nextLL = getBestLinksMode(network, nextStop.getPoint(), trip.getShape());
     List<Tuple<Path, Link[]>> paths = new ArrayList<Tuple<Path, Link[]>>();
     for (int i = 0; i < prevLL.size(); i++)
       for (int j = 0; j < nextLL.size(); j++) {
         prevL = prevLL.get(i);
         nextL = nextLL.get(j);
         Path path;
         if (prevL == nextL)
           path = new Path(new ArrayList<Node>(), new ArrayList<Link>(), 0.0, 0.0);
         else {
           path =
               leastCostPathCalculator.calcLeastCostPath(
                   prevL.getToNode(), nextL.getFromNode(), 0, null, null);
           if (path == null)
             path = new Path(new ArrayList<Node>(), new ArrayList<Link>(), 0.0, 0.0);
           path.links.add(0, prevL);
         }
         path.links.add(nextL);
         paths.add(new Tuple<Path, Link[]>(path, new Link[] {prevL, nextLL.get(j)}));
       }
     double shortestDistance = Double.POSITIVE_INFINITY;
     for (Tuple<Path, Link[]> tuple : paths) {
       if (tuple.getFirst().links.size() > 0) {
         double distance =
             calculateDistance(tuple.getFirst(), prevStop.getPoint(), nextStop.getPoint());
         if (bestPath == null || distance <= shortestDistance) {
           shortestDistance = distance;
           bestPath = tuple.getFirst();
           prevStop.setLinkId(tuple.getSecond()[0].getId().toString());
           nextStop.setLinkId(tuple.getSecond()[1].getId().toString());
         }
       }
     }
     prevL = bestPath.links.get(bestPath.links.size() - 1);
     links.addAll(bestPath.links);
     prevStop = nextStop;
   }
   for (; next.hasNext(); ) {
     Path bestPath = null;
     nextStop = stops.get(next.next().getStopId());
     if (nextStop.getLinkId() != null) {
       nextL = network.getLinks().get(Id.create(nextStop.getLinkId(), Link.class));
       if (prevL.equals(nextL))
         bestPath = new Path(new ArrayList<Node>(), new ArrayList<Link>(), 0.0, 0.0);
       else {
         bestPath =
             leastCostPathCalculator.calcLeastCostPath(
                 prevL.getToNode(), nextL.getFromNode(), 0, null, null);
         if (bestPath == null)
           bestPath = new Path(new ArrayList<Node>(), new ArrayList<Link>(), 0.0, 0.0);
         else bestPath.links.add(0, prevL);
       }
       bestPath.links.add(nextL);
     } else {
       nextLL = getBestLinksMode(network, nextStop.getPoint(), trip.getShape());
       List<Tuple<Path, Link>> paths = new ArrayList<Tuple<Path, Link>>();
       for (int i = 0; i < nextLL.size(); i++) {
         nextL = nextLL.get(i);
         Path path;
         if (prevL.equals(nextL))
           path = new Path(new ArrayList<Node>(), new ArrayList<Link>(), 0.0, 0.0);
         else {
           path =
               leastCostPathCalculator.calcLeastCostPath(
                   prevL.getToNode(), nextL.getFromNode(), 0, null, null);
           if (path == null)
             path = new Path(new ArrayList<Node>(), new ArrayList<Link>(), 0.0, 0.0);
           else path.links.add(0, prevL);
         }
         path.links.add(nextL);
         paths.add(new Tuple<Path, Link>(path, nextL));
       }
       double shortestDistance = Double.POSITIVE_INFINITY;
       for (Tuple<Path, Link> tuple : paths) {
         double distance =
             calculateDistance(tuple.getFirst(), prevStop.getPoint(), nextStop.getPoint());
         if (bestPath == null || distance <= shortestDistance) {
           shortestDistance = distance;
           bestPath = tuple.getFirst();
           nextStop.setLinkId(tuple.getSecond().getId().toString());
         }
       }
     }
     prevL = bestPath.links.get(bestPath.links.size() - 1);
     bestPath.links.remove(0);
     links.addAll(bestPath.links);
     prevStop = nextStop;
   }
 }