Beispiel #1
0
  public void parseHouseholds(String filename) {
    LOG.info("Parsing households from " + filename);
    int counter = 0;

    ObjectAttributes hha = sc.getHouseholds().getHouseholdAttributes();
    BufferedReader br = IOUtils.getBufferedReader(filename);
    try {
      String line = br.readLine(); /* Header. */
      line = br.readLine(); /* Header. */
      while ((line = br.readLine()) != null) {
        counter++;
        String[] sa = line.split(",");
        if (sa.length >= 33) {
          /* Create the household. */
          HouseholdsFactory hf = sc.getHouseholds().getFactory();
          Id<Household> id = Id.create(sa[1], Household.class);
          Household hh = hf.createHousehold(id);

          MonthlyIncome mi = HouseholdEnums.MonthlyIncome.parseFromCode(Integer.parseInt(sa[32]));
          Income matsimIncome = mi.getMatsimIncome();
          if (matsimIncome != null) {
            hh.setIncome(matsimIncome);
          }

          /* Add all the household attributes. */
          hha.putAttribute(id.toString(), "transportZone", sa[7]);
          hha.putAttribute(id.toString(), "enumerationArea", sa[9]);
          hha.putAttribute(id.toString(), "income", mi.getDescription());

          hha.putAttribute(
              id.toString(),
              "completedDiary",
              HouseholdEnums.CompletedDiary.parseFromCode(
                      sa[12].equals("") ? 0 : Integer.parseInt(sa[12]))
                  .getDescription());
          hha.putAttribute(
              id.toString(),
              "completedStatedPreference",
              HouseholdEnums.CompletedStatedPreference.parseFromCode(
                      sa[13].equals("") ? 0 : Integer.parseInt(sa[13]))
                  .getDescription());

          hha.putAttribute(
              id.toString(),
              "dwellingType",
              HouseholdEnums.DwellingType.parseFromCode(
                      sa[18].equals("") ? 0 : Integer.parseInt(sa[18]))
                  .getDescription());

          hha.putAttribute(id.toString(), "householdSize", Integer.parseInt(sa[19]));
          hha.putAttribute(id.toString(), "numberOfEmployedPeople", Integer.parseInt(sa[20]));
          hha.putAttribute(id.toString(), "numberOfHouseholdCarsOwned", Integer.parseInt(sa[21]));
          hha.putAttribute(
              id.toString(), "numberOfHouseholdCarsAccessTo", Integer.parseInt(sa[22]));
          hha.putAttribute(
              id.toString(), "numberOfHouseholdMotorcyclesOwned", Integer.parseInt(sa[23]));
          hha.putAttribute(
              id.toString(), "numberOfHouseholdMotorcyclesAccessTo", Integer.parseInt(sa[24]));
          hha.putAttribute(id.toString(), "numberOfDomesticWorkers", Integer.parseInt(sa[25]));
          hha.putAttribute(id.toString(), "numberOfGardenWorkers", Integer.parseInt(sa[28]));

          sc.getHouseholds().getHouseholds().put(id, hh);
        } else {
          LOG.warn("   person: " + counter + " ==> Line with " + sa.length + " elements.");
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("Cannot read from " + filename);
    } finally {
      try {
        br.close();
      } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Cannot close " + filename);
      }
    }

    LOG.info("Total number of households parsed: " + sc.getHouseholds().getHouseholds().size());
    LOG.info("Done parsing households.");
  }
  public void buildPopulation() {
    if (householdMap.size() == 0 || personMap.size() == 0) {
      throw new RuntimeException("Either the househols or persons are of size ZERO!");
    }

    LOG.info("Checking that each person has an associated household in the map...");
    for (Id<Person> i : personMap.keySet()) {
      String s = i.toString().split("_")[0];
      if (!householdMap.containsKey(Id.create(s, Household.class))) {
        LOG.warn("Could not find household " + s);
      }
    }
    LOG.info("Done checking person-household match.");

    this.households = new HouseholdsImpl();
    HouseholdsFactory hhf = households.getFactory();

    Population population = sc.getPopulation();
    PopulationFactoryImpl pf = (PopulationFactoryImpl) population.getFactory();
    for (Id<Person> pid : personMap.keySet()) {
      String[] sa = personMap.get(pid).split(",");
      String quarterType = sa[0];
      int age = Integer.parseInt(sa[1]);
      String gender = sa[2].equalsIgnoreCase("Male") ? "m" : "f";
      String relationship = sa[3];
      String race = sa[4];
      String school = sa[5];
      boolean employment = sa[6].equalsIgnoreCase("Yes") ? true : false;
      String mainPlaceOfWork = sa[7];
      String modeToMain = sa[8];
      Double income = Double.parseDouble(sa[9]);

      Person person = pf.createPerson(pid);
      PersonUtils.setAge(person, age);
      PersonUtils.setSex(person, gender);
      PersonUtils.setEmployed(person, employment);
      personAttributes.putAttribute(pid.toString(), "quarterType", quarterType);
      personAttributes.putAttribute(pid.toString(), "relationship", relationship);
      personAttributes.putAttribute(pid.toString(), "race", race);
      personAttributes.putAttribute(pid.toString(), "school", school);
      personAttributes.putAttribute(pid.toString(), "mainPlaceOfWork", mainPlaceOfWork);
      personAttributes.putAttribute(pid.toString(), "modeToMain", modeToMain);
      personAttributes.putAttribute(pid.toString(), "income", income);
      population.addPerson(person);

      /* Add to household */
      Id<Household> hid = Id.create(pid.toString().split("_")[0], Household.class);
      if (!this.households.getHouseholds().containsKey(hid)) {
        /* Household data */
        String[] hsa = householdMap.get(hid).split(",");
        int hhSize = Integer.parseInt(hsa[0]);
        String dwellingType = hsa[1];
        String hhPopulation = hsa[2];
        String incomeString = hsa[3];
        Income hhIncome = null;
        if (!incomeString.equalsIgnoreCase("Unknown")) {
          hhIncome = hhf.createIncome(Double.parseDouble(hsa[3]), IncomePeriod.year);
        }
        Household hh = hhf.createHousehold(hid);
        hh.setIncome(hhIncome);
        householdAttributes.putAttribute(hid.toString(), "dwellingType", dwellingType);
        householdAttributes.putAttribute(hid.toString(), "householdSize", hhSize);
        householdAttributes.putAttribute(hid.toString(), "population", hhPopulation);

        /* Geography data */
        String[] gsa = geographyMap.get(hid).split(",");
        String municipalCode = gsa[0];
        String municipalName = gsa[1];
        String magisterialCode = gsa[2];
        String magisterialName = gsa[3];
        String districtCode = gsa[4];
        String districtName = gsa[5];
        String provinceCode = gsa[6];
        String provinceName = gsa[7];
        String eaType = gsa[8];
        householdAttributes.putAttribute(hid.toString(), "municipalCode", municipalCode);
        householdAttributes.putAttribute(
            hid.toString(), "municipalName", correctCase(municipalName));
        householdAttributes.putAttribute(hid.toString(), "magisterialCode", magisterialCode);
        householdAttributes.putAttribute(
            hid.toString(), "magisterialName", correctCase(magisterialName));
        householdAttributes.putAttribute(hid.toString(), "districtCode", districtCode);
        householdAttributes.putAttribute(hid.toString(), "districtName", correctCase(districtName));
        householdAttributes.putAttribute(hid.toString(), "provinceCode", provinceCode);
        householdAttributes.putAttribute(hid.toString(), "provinceName", correctCase(provinceName));
        householdAttributes.putAttribute(hid.toString(), "eaType", eaType);

        this.households.getHouseholds().put(hid, hh);
      }
      this.households.getHouseholds().get(hid).getMemberIds().add(pid);
    }
    /* Validate: */
    LOG.info("================================================================");
    LOG.info("Validating the population and households");
    LOG.info("----------------------------------------------------------------");
    LOG.info("Person map: " + personMap.size());
    LOG.info("Household map: " + householdMap.size());
    LOG.info("----------------------------------------------------------------");
    LOG.info("Population size: " + sc.getPopulation().getPersons().size());
    LOG.info("Number of households: " + households.getHouseholds().size());
    LOG.info("================================================================");
  }