ArtificiallyConnectedStopFacility(TransitStopFacility facility) {
   myNode =
       networkFactory.createNode(
           Id.create(prefix + facility.getId(), Node.class), facility.getCoord());
   myLink = getNewLink(myNode, myNode);
   facility.setLinkId(myLink.getId());
 }
 private ArtificiallyConnectedStopFacility getArtificiallyConnectedStopFacility(
     TransitStopFacility facility) {
   if (!artificiallyConnectedStopFacilities.containsKey(facility.getId())) {
     artificiallyConnectedStopFacilities.put(
         facility.getId(), new ArtificiallyConnectedStopFacility(facility));
   }
   return artificiallyConnectedStopFacilities.get(facility.getId());
 }
 /**
  * Within search radius search for the closest link that has the current mode as allowed travel
  * mode and return this link.
  *
  * @param stopFacility Stop facility to search a link for.
  * @return Null if no such link could be found.
  */
 private Id<Link> findClosestLink(TransitStopFacility stopFacility) {
   Link nearestLink = NetworkUtils.getNearestLink(this.network, stopFacility.getCoord());
   if (NetworkUtils.getEuclidianDistance(
           stopFacility.getCoord(), nearestLink.getToNode().getCoord())
       <= SEARCH_RADIUS) {
     // If nearest link is within search radius, return it.
     return nearestLink.getId();
   } else {
     return null;
   }
 }
  /**
   * Link the pt-stations in the schedule to the closest network links. Thereby modifies
   * this.schedule.
   */
  protected void linkStationsToNetwork() {
    log.info("Linking pt stations to network...");

    Counter counter = new Counter("route # ");
    Set<TransitStopFacility> newFacilities = new HashSet<>();
    for (TransitStopFacility facility : this.schedule.getFacilities().values()) {

      final Id<Link> closestLink = findClosestLink(facility);
      if (closestLink != null) {
        //	link stop-facilities to the respective links.
        List<Id<TransitStopFacility>> localLinkedFacilities = new ArrayList<>();
        facility.setLinkId(closestLink);
        localLinkedFacilities.add(facility.getId());

        //	if street-link has opposite direction or if we searched for all links in area, then split
        // stop-position before linking.
        final List<Id<Link>> oppositeDirectionLinks = getOppositeDirection(closestLink);
        if (oppositeDirectionLinks != null && !oppositeDirectionLinks.isEmpty()) {
          TransitStopFacility[] newStopFacilities =
              multiplyStop(facility, oppositeDirectionLinks.size());
          for (int i = 0; i < oppositeDirectionLinks.size(); i++) {
            newStopFacilities[i + 1].setLinkId(oppositeDirectionLinks.get(i));
            localLinkedFacilities.add(newStopFacilities[i + 1].getId());
            newFacilities.add(newStopFacilities[i + 1]);
          }
        }

        linkedStopFacilitiesTree.put(facility.getId(), localLinkedFacilities);
      }

      counter.incCounter();
    }
    for (TransitStopFacility facility : newFacilities) {
      this.schedule.addStopFacility(facility);
    }
    counter.printCounter();

    log.info("Linking pt stations to network... done.");

    for (List<Id<TransitStopFacility>> facilityList : linkedStopFacilitiesTree.values()) {
      for (Id<TransitStopFacility> facilityId : facilityList) {
        linkedStopFacilities.add(facilityId);
      }
    }
  }
  private LeastCostPathCalculator.Path getShortestPath(Node startNode, TransitRouteStop toStop) {
    LeastCostPathCalculator.Path shortestPath = null;

    for (Id<TransitStopFacility> toStopFacilityId :
        linkedStopFacilitiesTree.get(toStop.getStopFacility().getId())) {
      TransitStopFacility facility = this.schedule.getFacilities().get(toStopFacilityId);
      Id<Link> linkId = facility.getLinkId();
      Link link = this.network.getLinks().get(linkId);
      Node endNode = link.getFromNode();
      LeastCostPathCalculator.Path tempShortestPath =
          this.router.calcLeastCostPath(startNode, endNode, "", "");
      if (tempShortestPath != null
          && (shortestPath == null || (tempShortestPath.travelCost < shortestPath.travelCost))) {
        shortestPath = tempShortestPath;
        toStop.setStopFacility(this.schedule.getFacilities().get(toStopFacilityId));
      }
    }

    return shortestPath;
  }
 /**
  * Create and add to schedule new stops with the id-endings "_1" to "_numberOfCopies" by copying
  * the provided stopFacility.
  *
  * @param stopFacility which will be copied. The original becomes the first element of the
  *     returned array.
  * @param numberOfCopies of stopFacility which are created.
  * @return TransitStopFacilities stopFacility, and copies stop_1 to stop_numberOfCopies
  */
 private TransitStopFacility[] multiplyStop(TransitStopFacility stopFacility, int numberOfCopies) {
   TransitStopFacility[] facilities = new TransitStopFacility[numberOfCopies + 1];
   facilities[0] = stopFacility;
   for (int i = 1; i <= numberOfCopies; i++) {
     // Copy facility at stopFacility:
     Id<TransitStopFacility> idNewFacility =
         Id.create(stopFacility.getId().toString() + "_" + i, TransitStopFacility.class);
     TransitStopFacility newFacility =
         this.scheduleFactory.createTransitStopFacility(
             idNewFacility, stopFacility.getCoord(), stopFacility.getIsBlockingLane());
     newFacility.setName(stopFacility.getName());
     // Add new facility to schedule and to array:
     facilities[i] = newFacility;
   }
   return facilities;
 }
 private void setConnectedStopFacilitiesToIsBlocking() {
   Set<TransitStopFacility> facilitiesToExchange = new HashSet<>();
   for (TransitStopFacility oldFacility : this.schedule.getFacilities().values()) {
     if (this.network
         .getLinks()
         .get(oldFacility.getLinkId())
         .getAllowedModes()
         .contains(TransportMode.car)) {
       TransitStopFacility newFacility =
           this.scheduleFactory.createTransitStopFacility(
               oldFacility.getId(), oldFacility.getCoord(), true);
       newFacility.setName(oldFacility.getName());
       newFacility.setLinkId(oldFacility.getLinkId());
       newFacility.setStopPostAreaId(oldFacility.getStopPostAreaId());
       facilitiesToExchange.add(newFacility);
     }
   }
   for (TransitStopFacility facility : facilitiesToExchange) {
     TransitStopFacility facilityToRemove = this.schedule.getFacilities().get(facility.getId());
     this.schedule.removeStopFacility(facilityToRemove);
     this.schedule.addStopFacility(facility);
   }
 }