public void writeSimulationResultsToTabSeparated(String path, String appendage)
      throws IOException {
    String actTableName;
    String journeyTableName;
    String transferTableName;
    String tripTableName;
    if (appendage.matches("[a-zA-Z0-9]*[_]*")) {
      actTableName = appendage + "matsim_activities.txt";
      journeyTableName = appendage + "matsim_journeys.txt";
      transferTableName = appendage + "matsim_transfers.txt";
      tripTableName = appendage + "matsim_trips.txt";
    } else {
      if (appendage.matches("[a-zA-Z0-9]*")) appendage = "_" + appendage;
      actTableName = "matsim_activities" + appendage + ".txt";
      journeyTableName = "matsim_journeys" + appendage + ".txt";
      transferTableName = "matsim_transfers" + appendage + ".txt";
      tripTableName = "matsim_trips" + appendage + ".txt";
    }
    BufferedWriter activityWriter = IOUtils.getBufferedWriter(path + "/" + actTableName);

    activityWriter.write(
        "activity_id\tperson_id\tfacility_id\ttype\t"
            + "start_time\tend_time\tx\ty\tsample_selector\n");

    BufferedWriter journeyWriter = IOUtils.getBufferedWriter(path + "/" + journeyTableName);
    journeyWriter.write(
        "journey_id\tperson_id\tstart_time\t"
            + "end_time\tdistance\tmain_mode\tfrom_act\tto_act\t"
            + "in_vehicle_distance\tin_vehicle_time\t"
            + "access_walk_distance\taccess_walk_time\taccess_wait_time\t"
            + "first_boarding_stop\tegress_walk_distance\t"
            + "egress_walk_time\tlast_alighting_stop\t"
            + "transfer_walk_distance\ttransfer_walk_time\t"
            + "transfer_wait_time\tsample_selector\n");

    BufferedWriter tripWriter = IOUtils.getBufferedWriter(path + "/" + tripTableName);
    tripWriter.write(
        "trip_id\tjourney_id\tstart_time\tend_time\t"
            + "distance\tmode\tline\troute\tboarding_stop\t"
            + "alighting_stop\tsample_selector\n");

    BufferedWriter transferWriter = IOUtils.getBufferedWriter(path + "/" + transferTableName);
    transferWriter.write(
        "transfer_id\tjourney_id\tstart_time\t"
            + "end_time\tfrom_trip\tto_trip\twalk_distance\t"
            + "walk_time\twait_time\tsample_selector\n");

    // read a static field that increments with every inheriting object constructed
    Counter counter = new Counter("Output lines written: ");
    for (Entry<Id, TravellerChain> entry : chains.entrySet()) {
      String pax_id = entry.getKey().toString();
      TravellerChain chain = entry.getValue();
      for (Activity act : chain.getActs()) {
        try {
          activityWriter.write(
              String.format(
                  "%d\t%s\t%s\t%s\t%d\t%d\t%f\t%f\t%f\n",
                  act.getElementId(),
                  pax_id,
                  act.getFacility(),
                  act.getType(),
                  (int) act.getStartTime(),
                  (int) act.getEndTime(),
                  act.getCoord().getX(),
                  act.getCoord().getY(),
                  MatsimRandom.getRandom().nextDouble()));
        } catch (Exception e) {
          System.out.println("Couldn't print activity chain!");
        }
      }
      for (Journey journey : chain.getJourneys()) {
        try {
          journeyWriter.write(
              String.format(
                  "%d\t%s\t%d\t%d\t%.3f\t%s\t%d\t%d\t%.3f\t%d\t%.3f\t%d\t%d\t%s\t%.3f\t%d\t%s\t%.3f\t%d\t%d\t%f\n",
                  journey.getElementId(),
                  pax_id,
                  (int) journey.getStartTime(),
                  (int) journey.getEndTime(),
                  journey.getDistance(),
                  journey.getMainMode(),
                  journey.getFromAct().getElementId(),
                  journey.getToAct().getElementId(),
                  journey.getInVehDistance(),
                  (int) journey.getInVehTime(),
                  journey.getAccessWalkDistance(),
                  (int) journey.getAccessWalkTime(),
                  (int) journey.getAccessWaitTime(),
                  journey.getFirstBoardingStop(),
                  journey.getEgressWalkDistance(),
                  (int) journey.getEgressWalkTime(),
                  journey.getLastAlightingStop(),
                  journey.getTransferWalkDistance(),
                  (int) journey.getTransferWalkTime(),
                  (int) journey.getTransferWaitTime(),
                  MatsimRandom.getRandom().nextDouble()));
          counter.incCounter();

          if (!(journey.isCarJourney() || journey.isTeleportJourney())) {
            for (Trip trip : journey.getTrips()) {
              tripWriter.write(
                  String.format(
                      "%d\t%d\t%d\t%d\t%.3f\t%s\t%s\t%s\t%s\t%s\t%f\n",
                      trip.getElementId(),
                      journey.getElementId(),
                      (int) trip.getStartTime(),
                      (int) trip.getEndTime(),
                      trip.getDistance(),
                      trip.getMode(),
                      trip.getLine(),
                      trip.getRoute(),
                      trip.getBoardingStop(),
                      trip.getAlightingStop(),
                      MatsimRandom.getRandom().nextDouble()));
              counter.incCounter();
            }
            for (Transfer transfer : journey.getTransfers()) {
              transferWriter.write(
                  String.format(
                      "%d\t%d\t%d\t%d\t%d\t%d\t%.3f\t%d\t%d\t%f\n",
                      transfer.getElementId(),
                      journey.getElementId(),
                      (int) transfer.getStartTime(),
                      (int) transfer.getEndTime(),
                      transfer.getFromTrip().getElementId(),
                      transfer.getToTrip().getElementId(),
                      transfer.getWalkDistance(),
                      (int) transfer.getWalkTime(),
                      (int) transfer.getWaitTime(),
                      MatsimRandom.getRandom().nextDouble()));

              counter.incCounter();
            }
          } else {
            for (Trip trip : journey.getTrips()) {

              tripWriter.write(
                  String.format(
                      "%d\t%d\t%d\t%d\t%.3f\t%s\t%s\t%s\t%s\t%s\t%f\n",
                      trip.getElementId(),
                      journey.getElementId(),
                      (int) trip.getStartTime(),
                      (int) trip.getEndTime(),
                      journey.isTeleportJourney() ? 0.000 : trip.getDistance(),
                      trip.getMode(),
                      "",
                      "",
                      "",
                      "",
                      MatsimRandom.getRandom().nextDouble()));

              counter.incCounter();
            }
          }
        } catch (NullPointerException e) {
          setStuck(getStuck() + 1);
        }
      }
    }

    activityWriter.close();
    journeyWriter.close();
    tripWriter.close();
    transferWriter.close();
    counter.printCounter();
  }
Пример #2
0
 private void parseStopTimes(BufferedReader reader) throws IOException {
   HashSet<String> processedStopTimes = new HashSet<>();
   Calendar wholeDay = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
   wholeDay.setTimeInMillis(0);
   Date dayBeginning = wholeDay.getTime();
   wholeDay.add(Calendar.DAY_OF_YEAR, 1);
   wholeDay.add(Calendar.MILLISECOND, -1);
   Date dayEnd = wholeDay.getTime();
   SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
   String line = reader.readLine(); // skip column names
   while ((line = reader.readLine()) != null) {
     // trip_id,arrival_time,departure_time,stop_id,stop_sequence,shape_dist_traveled
     String[] split = line.split(",");
     if (split.length < 6) {
       write(line);
       continue;
     }
     Integer tripId = trips.get(split[0]);
     Trip trip = tripId != null ? Trip.findTrip(tripId) : null;
     Integer stopId = stops.get(split[3]);
     Stop stop = Stop.findStop(stopId);
     if (trip != null && stop != null) {
       try {
         Date arrival = format.parse(split[1].replace("\"", ""));
         Date startTime = trip.getStartTime();
         if (startTime == null || arrival.before(startTime)) {
           trip.setStartTime(arrival);
         }
       } catch (ParseException e) {
         write(line);
         continue;
       }
       try {
         Date departure = format.parse(split[2].replace("\"", ""));
         Date endTime = trip.getEndTime();
         if (endTime == null || departure.after(endTime)) {
           trip.setEndTime(departure);
         }
       } catch (ParseException e) {
         write(line);
         continue;
       }
       if (trip.getStartTime() != null && trip.getEndTime() != null) {
         Calendar diff = Calendar.getInstance();
         diff.setTimeInMillis(trip.getEndTime().getTime() - trip.getStartTime().getTime());
         if (diff.get(Calendar.DAY_OF_YEAR) > 1) {
           trip.setStartTime(dayBeginning);
           trip.setEndTime(dayEnd);
         }
       }
       String stopTimeKey = split[3] + ":" + tripId + ":" + stopId;
       if (!processedStopTimes.contains(stopTimeKey)) {
         StopTime stopTime = new StopTime();
         stopTime.setSequence(Integer.parseInt(split[4].replace("\"", "").trim()));
         stopTime.setTrip(trip);
         stopTime.setStop(stop);
         processedStopTimes.add(stopTimeKey);
         stopTime.persist();
       }
     }
   }
 }