private static Collection<? extends Collection<Id<Person>>> extractGroups(
      final Households households) {
    final List<List<Id<Person>>> groups = new ArrayList<>();

    for (Household hh : households.getHouseholds().values()) {
      groups.add(hh.getMemberIds());
    }

    return groups;
  }
Exemple #2
0
  public Households filterHouseholdsWithPopulation(
      final Households households, final ObjectAttributes householdAttributes) {
    Households filteredHouseholds = new HouseholdsImpl();

    for (Household household : households.getHouseholds().values()) {
      Set<Id<Person>> personIdsToRemove = new HashSet<>();
      for (Id<Person> personId : household.getMemberIds()) {
        if (!filteredAgents.keySet().contains(personId)) {
          personIdsToRemove.add(personId);
        }
      }
      for (Id<Person> personId : personIdsToRemove) {
        household.getMemberIds().remove(personId);
      }
      if (!household.getMemberIds().isEmpty()) {
        filteredHouseholds.getHouseholds().put(household.getId(), household);
      } else {
        householdAttributes.removeAllAttributes(household.getId().toString());
      }
    }

    return filteredHouseholds;
  }
  public void parse(String haushalteFile) throws Exception {

    FileReader fr = new FileReader(haushalteFile);
    BufferedReader br = new BufferedReader(fr);
    String curr_line = br.readLine(); // Skip header

    while ((curr_line = br.readLine()) != null) {

      String[] entries = curr_line.split("\t", -1);

      // household number
      String hhnr = entries[0].trim();

      // household weight
      String hh_weight = entries[1];
      householdAttributes.putAttribute(hhnr, MZConstants.HOUSEHOLD_WEIGHT, hh_weight);

      // household size
      String size = entries[14].trim();
      householdAttributes.putAttribute(hhnr, MZConstants.HOUSEHOLD_SIZE, size);

      // household income
      String income = entries[17].trim();
      householdAttributes.putAttribute(hhnr, MZConstants.HOUSEHOLD_INCOME, income);
      householdAttributes.putAttribute(
          hhnr, MZConstants.HOUSEHOLD_INCOME_MIDDLE_OF_INTERVAL, getIncome(income));

      // location coordinate (round to 1/10 of hectare) - WGS84 (5,6) & CH1903 (7,8)
      // Coord location = new CoordImpl(entries[7].trim(),entries[8].trim());
      // location.setX(Math.round(location.getX()/10.0)*10);
      // location.setY(Math.round(location.getY()/10.0)*10);
      // householdAttributes.putAttribute(hhnr, "coord", location);

      // Kanton
      String kanton = entries[10].trim();
      if (kanton.equals("1")) {
        kanton = MZConstants.ZURICH;
      } else if (kanton.equals("2")) {
        kanton = MZConstants.BERN;
      } else if (kanton.equals("3")) {
        kanton = MZConstants.LUZERN;
      } else if (kanton.equals("4")) {
        kanton = MZConstants.URI;
      } else if (kanton.equals("5")) {
        kanton = MZConstants.SCHWYZ;
      } else if (kanton.equals("6")) {
        kanton = MZConstants.OBWALDEN;
      } else if (kanton.equals("7")) {
        kanton = MZConstants.NIDWALDEN;
      } else if (kanton.equals("8")) {
        kanton = MZConstants.GLARUS;
      } else if (kanton.equals("9")) {
        kanton = MZConstants.ZUG;
      } else if (kanton.equals("10")) {
        kanton = MZConstants.FRIBOURG;
      } else if (kanton.equals("11")) {
        kanton = MZConstants.SOLOTHURN;
      } else if (kanton.equals("12")) {
        kanton = MZConstants.BASEL_STADT;
      } else if (kanton.equals("13")) {
        kanton = MZConstants.BASEL_LAND;
      } else if (kanton.equals("14")) {
        kanton = MZConstants.SCHAFFHAUSEN;
      } else if (kanton.equals("15")) {
        kanton = MZConstants.APPENZELL_AUSSERHODEN;
      } else if (kanton.equals("16")) {
        kanton = MZConstants.APPENZELL_INNERHODEN;
      } else if (kanton.equals("17")) {
        kanton = MZConstants.ST_GALLEN;
      } else if (kanton.equals("18")) {
        kanton = MZConstants.GRAUBUNDEN;
      } else if (kanton.equals("19")) {
        kanton = MZConstants.AARGAU;
      } else if (kanton.equals("20")) {
        kanton = MZConstants.THURGAU;
      } else if (kanton.equals("21")) {
        kanton = MZConstants.TICINO;
      } else if (kanton.equals("22")) {
        kanton = MZConstants.VAUD;
      } else if (kanton.equals("23")) {
        kanton = MZConstants.VALAIS;
      } else if (kanton.equals("24")) {
        kanton = MZConstants.NEUCHATEL;
      } else if (kanton.equals("25")) {
        kanton = MZConstants.GENEVE;
      } else if (kanton.equals("26")) {
        kanton = MZConstants.JURA;
      } else if (kanton.equals("-97")) {
        kanton = MZConstants.UNSPECIFIED;
      }
      householdAttributes.putAttribute(hhnr, MZConstants.CANTON, kanton);

      // municipality BFS number
      String municipality = entries[5].trim();
      householdAttributes.putAttribute(hhnr, MZConstants.MUNICIPALITY, municipality);

      // adress
      String street = entries[6].trim();
      String number = entries[7].trim();
      householdAttributes.putAttribute(hhnr, MZConstants.ADDRESS, street + number);

      // number of cars
      String nr_cars = entries[27];
      if (nr_cars.equals("-2")) {
        nr_cars = MZConstants.NO_ANSWER;
      } else if (nr_cars.equals("-1")) {
        nr_cars = MZConstants.NOT_KNOWN;
      } else if (nr_cars.equals("7")) {
        nr_cars = "more than 6";
      }
      householdAttributes.putAttribute(hhnr, MZConstants.TOTAL_CARS, nr_cars);

      // number of motorcycles
      String nr_mcycles = entries[28];
      if (nr_mcycles.equals("-2")) {
        nr_mcycles = MZConstants.NO_ANSWER;
      } else if (nr_mcycles.equals("-1")) {
        nr_mcycles = MZConstants.NOT_KNOWN;
      } else if (nr_mcycles.equals("7")) {
        nr_mcycles = "more than 6";
      }
      householdAttributes.putAttribute(hhnr, MZConstants.TOTAL_MOTORCYCLES, nr_mcycles);

      // number of small motorcycles
      String nr_smcycles = entries[29];
      if (nr_smcycles.equals("-2")) {
        nr_smcycles = MZConstants.NO_ANSWER;
      } else if (nr_smcycles.equals("-1")) {
        nr_smcycles = MZConstants.NOT_KNOWN;
      } else if (nr_smcycles.equals("-1")) {
        nr_smcycles = "more than 6";
      }
      householdAttributes.putAttribute(hhnr, MZConstants.TOTAL_SMALL_MOTORCYCLES, nr_smcycles);

      // number of mofa
      String nr_mofas = entries[30];
      if (nr_mofas.equals("-2")) {
        nr_mofas = MZConstants.NO_ANSWER;
      } else if (nr_mofas.equals("-1")) {
        nr_mofas = MZConstants.NOT_KNOWN;
      } else if (nr_mofas.equals("7")) {
        nr_mofas = "more than 6";
      }
      householdAttributes.putAttribute(hhnr, MZConstants.TOTAL_MOFAS, nr_mofas);

      // number of bicycles
      String nr_bikes = entries[31];
      if (nr_bikes.equals("-2")) {
        nr_bikes = MZConstants.NO_ANSWER;
      } else if (nr_bikes.equals("-1")) {
        nr_bikes = MZConstants.NOT_KNOWN;
      } else if (nr_bikes.equals("7")) {
        nr_bikes = "more than 6";
      }
      householdAttributes.putAttribute(hhnr, MZConstants.TOTAL_BICYCLES, nr_bikes);

      // creating matsim household
      Household hh = households.getFactory().createHousehold(Id.create(hhnr, Household.class));
      hh.setIncome(new IncomeImpl(Double.parseDouble(income), IncomePeriod.month));
      households.getHouseholds().put(hh.getId(), hh);
    }

    br.close();
    fr.close();
    System.out.println("      done.");

    System.out.println("      # households parsed = " + households.getHouseholds().size());
    System.out.println();
  }