Пример #1
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;
   }
 }