예제 #1
0
  public static void main(final String[] args) {
    final String pathToCSV = args[0];
    final String activityType = args[1];
    final String pathToOutputFacilities = args[2];

    final boolean publicFacilities = true;

    final ActivityFacilitiesFactoryImpl factory = new ActivityFacilitiesFactoryImpl();
    final ActivityFacilities facilities = FacilitiesUtils.createActivityFacilities();
    int idNumber = 0;

    CSVReader reader = new CSVReader(pathToCSV);
    reader.skipLine(); // skip header
    String[] newLine = reader.readLine(); // first line
    while (newLine != null) {

      // EMPFTE;METER_X;METER_Y;NOGA_CD_2008_6;KATEGORIE;NOGA_TAG;CAPACITY;OPEN_FROM;OPEN_TO;METER_X_GERUNDET;METER_Y_GERUNDET
      double xCoord = Double.parseDouble(newLine[1]);
      double yCoord = Double.parseDouble(newLine[2]);
      String desc = newLine[4] + " (" + newLine[5] + ")";
      double capacity = Double.parseDouble(newLine[6]);
      double openFrom = Double.parseDouble(newLine[7]);
      double openTill = Double.parseDouble(newLine[8]);

      // new facility
      ActivityFacilityImpl newFacility =
          (ActivityFacilityImpl)
              factory.createActivityFacility(
                  Id.create(String.format("%06d", ++idNumber), ActivityFacility.class),
                  new Coord(xCoord, yCoord));
      if (!publicFacilities) {
        newFacility.setDesc(desc);
      }
      // new activity
      ActivityOption newActivity = factory.createActivityOption(activityType);
      newActivity.setCapacity(capacity);
      newActivity.addOpeningTime(new OpeningTimeImpl(openFrom, openTill));
      newFacility.addActivityOption(newActivity);
      // add new facility and activity
      facilities.addActivityFacility(newFacility);

      newLine = reader.readLine(); // next line
    }

    // Write facilities
    FacilitiesWriter facilitiesWriter = new FacilitiesWriter(facilities);
    facilitiesWriter.write(pathToOutputFacilities);
    testFacilities(pathToOutputFacilities);
  }
예제 #2
0
  /**
   * @param args
   * @throws FactoryException
   */
  public static void main(String[] args) throws FactoryException {
    String popFile = args[0];
    String facFile = args[1];
    String netFile = args[2];
    int n = Integer.parseInt(args[3]);
    String outDir = args[4];

    Logger logger = Logger.getLogger(DemoScenario.class);

    MathTransform transform = CRS.findMathTransform(CRSUtils.getCRS(31467), CRSUtils.getCRS(3857));

    Config config = ConfigUtils.createConfig();
    Scenario scenario = ScenarioUtils.createScenario(config);
    /*
     * remove foreign persons and extract subsample
     */
    logger.info("Loading persons...");
    MatsimPopulationReader pReader = new MatsimPopulationReader(scenario);
    pReader.readFile(popFile);
    logger.info("Done.");

    logger.info("Removing foreign persons...");
    Set<Id<Person>> remove = new HashSet<>();
    for (Id<Person> id : scenario.getPopulation().getPersons().keySet()) {
      if (id.toString().startsWith("foreign")) {
        remove.add(id);
      }
    }

    int cnt = 0;
    for (Id<Person> id : remove) {
      if (scenario.getPopulation().getPersons().remove(id) != null) {
        cnt++;
      }
    }
    logger.info(String.format("Done. Removed %s foreign persons.", cnt));

    logger.info("Drawing population subsample...");
    List<Person> persons = new ArrayList<>(scenario.getPopulation().getPersons().values());
    Collections.shuffle(persons);
    Population population = PopulationUtils.createPopulation(config);
    cnt = 0;
    for (int i = 0; i < n; i++) {
      population.addPerson(persons.get(i));
    }
    logger.info("Done.");

    logger.info("Bluring activity end times...");
    Random random = new XORShiftRandom();
    for (Person person : population.getPersons().values()) {
      for (Plan plan : person.getPlans()) {
        for (int i = 0; i < plan.getPlanElements().size(); i += 2) {
          Activity act = (Activity) plan.getPlanElements().get(i);
          double endTim = act.getEndTime() - 15 * 60 + (random.nextDouble() * 30 * 60);
          act.setEndTime(endTim);
          double startTim = act.getStartTime() - 15 * 60 + (random.nextDouble() * 30 * 60);
          act.setStartTime(startTim);
        }
      }
    }
    logger.info("Done.");

    logger.info("Writing population...");
    PopulationWriter pWriter = new PopulationWriter(population);
    pWriter.write(String.format("%s/plans.xml.gz", outDir));
    logger.info("Done.");
    /*
     * filter only used facilities
     */
    logger.info("Loading facilities...");
    MatsimFacilitiesReader fReader = new MatsimFacilitiesReader(scenario);
    fReader.readFile(facFile);
    logger.info("Done.");

    logger.info("Removing unsused facilities...");
    Set<Id<ActivityFacility>> unused =
        new HashSet<>(scenario.getActivityFacilities().getFacilities().keySet());
    for (Person person : population.getPersons().values()) {
      for (Plan plan : person.getPlans()) {
        for (int i = 0; i < plan.getPlanElements().size(); i += 2) {
          Activity act = (Activity) plan.getPlanElements().get(i);
          unused.remove(act.getFacilityId());
        }
      }
    }
    logger.info("Done.");

    logger.info("Transforming facility coordinates...");
    for (ActivityFacility fac : scenario.getActivityFacilities().getFacilities().values()) {
      double[] points = new double[] {fac.getCoord().getX(), fac.getCoord().getY()};
      try {
        transform.transform(points, 0, points, 0, 1);
      } catch (TransformException e) {
        e.printStackTrace();
      }

      ((ActivityFacilityImpl) fac).setCoord(new Coord(points[0], points[1]));
    }
    logger.info("Done.");

    logger.info("Writing facilities...");
    FacilitiesWriter fWrtier = new FacilitiesWriter(scenario.getActivityFacilities());
    fWrtier.write(String.format("%s/facilities.xml.gz", outDir));
    logger.info("Done.");
    /*
     * clean network from foreign links
     */
    logger.info("Loading network...");
    MatsimNetworkReader nReader = new MatsimNetworkReader(scenario);
    nReader.readFile(netFile);
    logger.info("Done.");

    logger.info("Removing foreign links...");
    Set<Id<Link>> linksRemove = new HashSet<>();
    for (Id<Link> id : scenario.getNetwork().getLinks().keySet()) {
      if (id.toString().contains(".l")) {
        linksRemove.add(id);
      }
    }

    for (Id<Link> id : linksRemove) {
      scenario.getNetwork().removeLink(id);
    }
    logger.info("Done.");

    logger.info("Removing foreign nodes...");
    Set<Id<Node>> nodesRemove = new HashSet<>();
    for (Id<Node> id : scenario.getNetwork().getNodes().keySet()) {
      if (id.toString().contains(".n")) {
        nodesRemove.add(id);
      }
    }

    for (Id<Node> id : nodesRemove) {
      scenario.getNetwork().removeNode(id);
    }
    logger.info("Done.");

    logger.info("Transforming node coordinates...");
    for (Node node : scenario.getNetwork().getNodes().values()) {
      double[] points = new double[] {node.getCoord().getX(), node.getCoord().getY()};
      try {
        transform.transform(points, 0, points, 0, 1);
      } catch (TransformException e) {
        e.printStackTrace();
      }

      ((NodeImpl) node).setCoord(new Coord(points[0], points[1]));
    }
    logger.info("Done.");

    logger.info("Writing network...");
    NetworkWriter nWriter = new NetworkWriter(scenario.getNetwork());
    nWriter.write(String.format("%s/network.xml.gz", outDir));
    logger.info("Done.");
  }
예제 #3
0
  public static void main(String[] args) {
    try {
      if (args.length > 0) {
        String file = args[0];
        Map<String, String> parameterMap = new XMLParameterParser().parseFile(file);
        String value;

        value = parameterMap.remove("basePath");
        if (value != null) basePath = value;

        value = parameterMap.remove("networkFile");
        if (value != null) networkFile = value;

        value = parameterMap.remove("zonalAttributesFile");
        if (value != null) zonalAttributesFile = value;

        value = parameterMap.remove("separator");
        if (value != null) separator = value;

        value = parameterMap.remove("zonalSHPFile");
        if (value != null) zonalSHPFile = value;

        value = parameterMap.remove("facilitiesFile");
        if (value != null) facilitiesFile = value;

        value = parameterMap.remove("facilitiesAttributesFile");
        if (value != null) facilitiesAttributesFile = value;

        value = parameterMap.remove("f2lFile");
        if (value != null) f2lFile = value;

        value = parameterMap.remove("facilitiesPerZone");
        if (value != null) facilitiesPerZone = Integer.parseInt(value);

        value = parameterMap.remove("validLinkTypes");
        if (value != null) validLinkTypes = CollectionUtils.stringToSet(value);

        for (String key : parameterMap.keySet())
          log.warn("Found parameter " + key + " which is not handled!");
      } else {
        log.error("No input config file was given. Therefore cannot proceed. Aborting!");
        return;
      }

      log.info("loading network ...");
      Config config = ConfigUtils.createConfig();
      config.network().setInputFile(basePath + networkFile);
      Scenario scenario = ScenarioUtils.loadScenario(config);
      log.info("done.\n");

      log.info("loading zonal attributes ...");
      boolean skipHeader = true;
      Map<Integer, Emme2Zone> zonalAttributes =
          new Emme2ZonesFileParser(basePath + zonalAttributesFile, separator).readFile(skipHeader);
      log.info("done.\n");

      log.info("loading zonal shp file ...");
      // use a TreeMap to be deterministic
      Map<Integer, SimpleFeature> zonalShapes = new TreeMap<Integer, SimpleFeature>();
      for (SimpleFeature feature : ShapeFileReader.getAllFeatures(basePath + zonalSHPFile)) {
        zonalShapes.put((Integer) feature.getAttribute(3), feature);
      }
      log.info("done.\n");

      log.info("identify nodes outside the model area ...");
      Set<Id<Node>> externalNodes = getExternalNodes(scenario, zonalShapes);
      log.info("\tfound " + externalNodes.size() + " nodes outside the mapped area");
      log.info("done.\n");

      /*
       * We have to create tta activities BEFORE filtering the network. They might also start
       * and end at highways. We do not know their real start and end positions. The coordinate
       * we know might only be the place where the agents enter the modeled area, which will
       * probably be by using a highway.
       */
      log.info("creating external facilities for tta activities ...");
      createExternalFacilities(scenario, externalNodes);
      log.info("done.\n");

      /*
       * Before creating the internal facilities, we can perform the links filtering.
       */
      log.info("removing links from network where no facilities should be attached to ...");
      List<Id<Link>> linksToRemove = new ArrayList<Id<Link>>();
      for (Link link : scenario.getNetwork().getLinks().values()) {
        String type = ((LinkImpl) link).getType();
        if (!validLinkTypes.contains(type)) linksToRemove.add(link.getId());
      }
      log.info("\tfound " + linksToRemove.size() + " links which do not match the criteria");
      for (Id<Link> linkId : linksToRemove)
        ((NetworkImpl) scenario.getNetwork()).removeLink(linkId);
      log.info(
          "\tprocessed network contains "
              + scenario.getNetwork().getLinks().size()
              + " valid links");
      log.info("done.\n");

      log.info("creating internal facilities ...");
      createInternalFacilities(scenario, zonalAttributes, zonalShapes);
      log.info("done.\n");

      log.info("writing facilities to links mapping to a file ...");
      createAndWriteF2LMapping(scenario);
      log.info("done.\n");

      log.info(
          "writing "
              + scenario.getActivityFacilities().getFacilities().size()
              + " facilities to a file ...");
      FacilitiesWriter facilitiesWriter = new FacilitiesWriter(scenario.getActivityFacilities());
      facilitiesWriter.write(basePath + facilitiesFile);
      new ObjectAttributesXmlWriter(scenario.getActivityFacilities().getFacilityAttributes())
          .writeFile(basePath + facilitiesAttributesFile);
      log.info("done.\n");
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }