Esempio n. 1
0
  /** @param args */
  public static void main(String[] args) {

    String input = args[0];
    String output = args[1];

    /* Read the network. */
    Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new MatsimNetworkReader(scenario.getNetwork()).readFile(input);

    /* Transform each node. */
    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation(
            TransformationFactory.WGS84, "EPSG:25832");
    //		CoordinateTransformation ct =
    // TransformationFactory.getCoordinateTransformation(TransformationFactory.WGS84,TransformationFactory.DHDN_GK4);

    //		CoordinateTransformation ct =
    // TransformationFactory.getCoordinateTransformation("EPSG:25832",TransformationFactory.WGS84);
    //		CoordinateTransformation ct =
    // TransformationFactory.getCoordinateTransformation(TransformationFactory.DHDN_GK4,TransformationFactory.WGS84);

    for (Node node : scenario.getNetwork().getNodes().values()) {
      ((Node) node).setCoord(ct.transform(node.getCoord()));
    }

    /* Write the resulting network. */
    new NetworkWriter(scenario.getNetwork()).write(output);
  }
Esempio n. 2
0
  public void convertCoordinates(Network net, String outputFile) {

    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation(
            "EPSG:32719", TransformationFactory.WGS84);

    for (Node node : net.getNodes().values()) {
      Coord newCoord = ct.transform(node.getCoord());
      ((NodeImpl) node).setCoord(newCoord);
    }

    new NetworkWriter(net).write(outputFile);
  }
Esempio n. 3
0
  private static final List<Coord> getRandomCoordinatesInZone(
      int numCoordinates, Geometry zoneGeometry, Random random) {

    /*
     * Get the bounding box of the geometry. The returned geometry is a polygon with the following points:
     * (minx, miny), (maxx, miny), (maxx, maxy), (minx, maxy), (minx, miny).
     */
    Geometry envelope = zoneGeometry.getEnvelope();
    Coordinate[] coords = envelope.getCoordinates();

    double minX = Double.MAX_VALUE;
    double maxX = Double.MIN_VALUE;
    double minY = Double.MAX_VALUE;
    double maxY = Double.MIN_VALUE;

    for (Coordinate coord : coords) {
      if (coord.x < minX) minX = coord.x;
      if (coord.x > maxX) maxX = coord.x;
      if (coord.y < minY) minY = coord.y;
      if (coord.y > maxY) maxY = coord.y;
    }

    List<Coord> list = new ArrayList<Coord>();

    // loop until a valid point was found and is returned
    while (list.size() < numCoordinates) {
      double x = minX + random.nextDouble() * (maxX - minX);
      double y = minY + random.nextDouble() * (maxY - minY);

      Point point = geometryFactory.createPoint(new Coordinate(x, y));
      if (zoneGeometry.contains(point))
        list.add(fromWGS84CoordinateTransformation.transform(new Coord(x, y)));
    }
    return list;
  }
  public GeoPosition getNetworkCenter() {
    if (this.networkCenter != null) {
      return this.networkCenter;
    }
    Envelope e = new Envelope();
    for (Node node : this.sc.getNetwork().getNodes().values()) {

      // ignore end nodes
      if (node.getId().toString().contains("en")) continue;

      e.expandToInclude(MGC.coord2Coordinate(node.getCoord()));
    }
    Coord centerC = new CoordImpl((e.getMaxX() + e.getMinX()) / 2, (e.getMaxY() + e.getMinY()) / 2);
    CoordinateTransformation ct2 =
        new GeotoolsTransformation(this.sc.getConfig().global().getCoordinateSystem(), "EPSG:4326");
    centerC = ct2.transform(centerC);
    this.networkCenter = new GeoPosition(centerC.getY(), centerC.getX());

    return this.networkCenter;
  }
Esempio n. 5
0
 @Override
 public void startRow(String[] row) {
   try {
     Double x = Double.parseDouble(row[2]);
     Double y = Double.parseDouble(row[1]);
     Coord coords = new Coord(x, y);
     this.facilityMap.put(row[0], ct.transform(coords));
   } catch (NumberFormatException e) {
     // skips line
   }
 }
 @Override
 public void startCount(final Count count, final BufferedWriter out) throws IOException {
   out.write("\t<count");
   out.write(" loc_id=\"" + count.getLocId() + "\"");
   out.write(" cs_id=\"" + count.getCsId() + "\"");
   if (count.getCoord() != null) {
     final Coord coord = coordinateTransformation.transform(count.getCoord());
     out.write(" x=\"" + coord.getX() + "\"");
     out.write(" y=\"" + coord.getY() + "\"");
   }
   out.write(">\n");
 }
Esempio n. 7
0
  public static void main(String[] args) {

    //		Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    //		NetworkImpl network = (NetworkImpl) scenario.getNetwork();
    //		CoordinateTransformation ct =
    //
    //	TransformationFactory.getCoordinateTransformation(TransformationFactory.GK4,TransformationFactory.WGS84);
    //		OsmNetworkReader reader = new OsmNetworkReader(scenario.getNetwork(), ct);
    //		reader.parse("C:/Users/Tille/WORK/Cottbus/Cottbus-pt/Demand_input/network_pt_cap60.xml");
    //
    //		NetworkWriter writer = new NetworkWriter(scenario.getNetwork());
    //		writer.write("C:/Users/Tille/WORK/Cottbus/Cottbus-pt/Demand_input/transformiert.xml");
    //

    String input = "C:/Users/Tille/WORK/Cottbus/Cottbus-pt/Demand_input/bearbeitetWGS84.xml";
    String output = "C:/Users/Tille/WORK/Cottbus/Cottbus-pt/Demand_input/RUECKtransformiert.xml";

    /* Read the network. */
    Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new MatsimNetworkReader(scenario.getNetwork()).readFile(input);

    /* Transform each node. */
    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation(
            TransformationFactory.WGS84, "EPSG:25833");
    //		CoordinateTransformation ct =
    // TransformationFactory.getCoordinateTransformation(TransformationFactory.WGS84,TransformationFactory.DHDN_GK4);

    //		CoordinateTransformation ct =
    // TransformationFactory.getCoordinateTransformation("EPSG:25833",TransformationFactory.WGS84);
    //		CoordinateTransformation ct =
    // TransformationFactory.getCoordinateTransformation(TransformationFactory.DHDN_GK4,TransformationFactory.WGS84);

    for (Node node : scenario.getNetwork().getNodes().values()) {
      ((Node) node).setCoord(ct.transform(node.getCoord()));
    }

    /* Write the resulting network. */
    new NetworkWriter(scenario.getNetwork()).write(output);
  }
 // Main
 public static void main(String[] args)
     throws SQLException, NoConnectionException, IOException, InstantiationException,
         IllegalAccessException, ClassNotFoundException {
   Map<Id<ActivityFacility>, Double> maximums = new HashMap<Id<ActivityFacility>, Double>();
   Map<Id<ActivityFacility>, Polygon> masterAreas = new HashMap<Id<ActivityFacility>, Polygon>();
   ShapeFileReader shapeFileReader = new ShapeFileReader();
   Collection<SimpleFeature> features = shapeFileReader.readFileAndInitialize(POLYGONS_FILE);
   for (SimpleFeature feature : features)
     masterAreas.put(
         Id.create((Integer) feature.getAttribute(1), ActivityFacility.class),
         (Polygon) ((MultiPolygon) feature.getDefaultGeometry()).getGeometryN(0));
   DataBaseAdmin dataBaseRealState =
       new DataBaseAdmin(new File("./data/facilities/DataBaseRealState.properties"));
   ResultSet buildingsR =
       dataBaseRealState.executeQuery("SELECT type, longitude, latitude FROM building_directory");
   CoordinateTransformation coordinateTransformation =
       TransformationFactory.getCoordinateTransformation(
           TransformationFactory.WGS84, TransformationFactory.WGS84_TM);
   GeometryFactory factory = new GeometryFactory();
   while (buildingsR.next()) {
     Coord buildingCoord =
         coordinateTransformation.transform(
             new Coord(buildingsR.getDouble(2), buildingsR.getDouble(3)));
     Point p = factory.createPoint(new Coordinate(buildingCoord.getX(), buildingCoord.getY()));
     for (Entry<Id<ActivityFacility>, Polygon> polygon : masterAreas.entrySet())
       if (p.within(polygon.getValue())) {
         Double maximum = maximums.get(polygon.getKey());
         if (maximum == null) maximum = 0.0;
         maximum += getMaximum(buildingsR.getString(1), null);
         maximums.put(polygon.getKey(), maximum);
       }
   }
   dataBaseRealState.close();
   DataBaseAdmin dataBaseAux =
       new DataBaseAdmin(new File("./data/facilities/DataBaseAuxiliar.properties"));
   for (Entry<Id<ActivityFacility>, Double> maximum : maximums.entrySet())
     dataBaseAux.executeUpdate("INSERT INTO buildings VALUES");
   dataBaseAux.close();
 }
 // Main
 public static final void main(String[] args) throws NumberFormatException, IOException {
   Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
   new MatsimNetworkReader(scenario).readFile(args[0]);
   CoordinateTransformation coordinateTransformation =
       TransformationFactory.getCoordinateTransformation(
           TransformationFactory.WGS84, TransformationFactory.WGS84_UTM48N);
   Coord c1 =
       coordinateTransformation.transform(
           new Coord(Double.parseDouble(args[2]), Double.parseDouble(args[3])));
   Coord c2 =
       coordinateTransformation.transform(
           new Coord(Double.parseDouble(args[4]), Double.parseDouble(args[5])));
   new BusLaneAdderWindow(
           "Bus lanes adder",
           scenario.getNetwork(),
           new File(args[1]),
           new double[] {c1.getX(), c1.getY()},
           new double[] {c2.getX(), c2.getY()},
           args[6],
           coordinateTransformation)
       .setVisible(true);
 }
Esempio n. 10
0
  private static Network filterNetwork(Network network) {
    CoordinateTransformation transform =
        TransformationFactory.getCoordinateTransformation(
            TransformationFactory.CH1903_LV03_GT, TransformationFactory.DHDN_GK4);
    //		CoordinateTransformation transform =
    // TransformationFactory.getCoordinateTransformation(TransformationFactory.CH1903_LV03_GT,
    // "WGS84_UTM32T");

    Network net = NetworkUtils.createNetwork();
    RoadPricingSchemeImpl rps = new RoadPricingSchemeImpl();
    RoadPricingReaderXMLv1 rpr = new RoadPricingReaderXMLv1(rps);
    rpr.parse(linksToFilter);
    Set<Id<Link>> linkList = rps.getTolledLinkIds();
    for (Link link : network.getLinks().values()) {
      Id linkId = link.getId();
      if (linkList.contains(linkId)) {
        Id fromId = link.getFromNode().getId();
        Id toId = link.getToNode().getId();
        Coord fromNodeCoord = link.getFromNode().getCoord();
        Coord toNodeCoord = link.getToNode().getCoord();
        Coord fromNodeTransformed = transform.transform(fromNodeCoord);
        Coord toNodeTransformed = transform.transform(toNodeCoord);
        //				Node newFromNode = net.getFactory().createNode(fromId, fromNodeCoord);
        //				Node newToNode = net.getFactory().createNode(toId, toNodeCoord);
        Node newFromNode = net.getFactory().createNode(fromId, fromNodeTransformed);
        Node newToNode = net.getFactory().createNode(toId, toNodeTransformed);
        if (!net.getNodes().containsKey(fromId)) {
          net.addNode(newFromNode);
        }
        if (!net.getNodes().containsKey(toId)) {
          net.addNode(newToNode);
        }
        Link ll = net.getFactory().createLink(link.getId(), newFromNode, newToNode);
        net.addLink(ll);
      }
    }
    return net;
  }
 private void writeAct(final Activity act, final BufferedWriter out) throws IOException {
   out.write("\t\t\t<act type=\"");
   out.write(act.getType());
   out.write("\"");
   if (act.getLinkId() != null) {
     out.write(" link=\"");
     out.write(act.getLinkId().toString());
     out.write("\"");
   }
   if (act.getFacilityId() != null) {
     out.write(" facility=\"");
     out.write(act.getFacilityId().toString());
     out.write("\"");
   }
   if (act.getCoord() != null) {
     final Coord coord = coordinateTransformation.transform(act.getCoord());
     out.write(" x=\"");
     out.write(Double.toString(coord.getX()));
     out.write("\" y=\"");
     out.write(Double.toString(coord.getY()));
     out.write("\"");
   }
   if (act.getStartTime() != Time.UNDEFINED_TIME) {
     out.write(" start_time=\"");
     out.write(Time.writeTime(act.getStartTime()));
     out.write("\"");
   }
   if (act != null) {
     Activity a = act;
     if (a.getMaximumDuration() != Time.UNDEFINED_TIME) {
       out.write(" max_dur=\"");
       out.write(Time.writeTime(a.getMaximumDuration()));
       out.write("\"");
     }
   }
   if (act.getEndTime() != Time.UNDEFINED_TIME) {
     out.write(" end_time=\"");
     out.write(Time.writeTime(act.getEndTime()));
     out.write("\"");
   }
   out.write(" />\n");
 }
Esempio n. 12
0
  // iterate over all nodes to find all external nodes
  private static final Set<Id<Node>> getExternalNodes(
      Scenario scenario, Map<Integer, SimpleFeature> zonalShapes) {

    Set<Id<Node>> externalNodes = new TreeSet<Id<Node>>();
    for (Node node : scenario.getNetwork().getNodes().values()) {
      Coord pointCoord = toWGS84CoordinateTransformation.transform(node.getCoord());
      Point point =
          geometryFactory.createPoint(new Coordinate(pointCoord.getX(), pointCoord.getY()));

      SimpleFeature pointZone = null;
      for (SimpleFeature zone : zonalShapes.values()) {
        Geometry polygon = (Geometry) zone.getDefaultGeometry();
        if (polygon.contains(point)) {
          pointZone = zone;
          break;
        }
      }

      // if the point is not contained in any Zone it is an external node.
      if (pointZone == null) externalNodes.add(node.getId());
    }

    return externalNodes;
  }
  /** @param args */
  public static void main(String[] args) {
    Header.printHeader(ExtractVehiclesForSollyFrans.class.toString(), args);

    String path = "/home/jwjoubert/Documents/data/Digicore/ByMonth/201403/SollyFrans/xml/";
    String output = "/home/jwjoubert/Documents/data/Digicore/ByMonth/201403/SollyFrans/chains.csv";
    List<File> list =
        FileUtils.sampleFiles(new File(path), 100, FileUtils.getFileFilter(".xml.gz"));

    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation("WGS84_SA_Albers", "WGS84");

    BufferedWriter bw = IOUtils.getBufferedWriter(output);
    try {
      bw.write("Vehicle,Chain,Activity,X,Y,Long,Lat,Start,End");
      bw.newLine();

      for (File f : list) {
        DigicoreVehicleReader_v1 dvr = new DigicoreVehicleReader_v1();
        dvr.readFile(f.getAbsolutePath());
        DigicoreVehicle v = dvr.getVehicle();
        bw.write(v.getId().toString());
        bw.write(",");

        int chain = 1;
        for (DigicoreChain c : v.getChains()) {
          int act = 1;
          for (DigicoreActivity a : c.getAllActivities()) {
            Coord coord = a.getCoord();
            Coord coordWgs84 = ct.transform(coord);

            String s =
                String.format(
                    "%s,%d,%d,%.0f,%.0f,%.6f,%.6f,%s,%s\n",
                    v.getId().toString(),
                    chain,
                    act,
                    coord.getX(),
                    coord.getY(),
                    coordWgs84.getX(),
                    coordWgs84.getY(),
                    getNiceDate(a.getStartTimeGregorianCalendar()),
                    getNiceDate(a.getEndTimeGregorianCalendar()));
            bw.write(s);
            act++;
          }
          chain++;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("Cannot write to " + output);
    } finally {
      try {
        bw.close();
      } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Cannot close " + output);
      }
    }

    Header.printFooter();
  }
Esempio n. 14
0
  public void cleanUpScenario(Scenario sc) {
    LOG.info("Cleaning up scenario...");
    /* TODO Still need to figure out what cleaning up must happen. */

    /* Search for location-less activities, and sample its locations from
     * the kernel density estimates. */
    LOG.info("Sampling locations for those without known zones...");
    int locationsFixed = 0;
    int knownLocations = 0;
    for (Person person : sc.getPopulation().getPersons().values()) {
      Plan plan = person.getSelectedPlan();
      if (plan != null) {
        for (PlanElement pe : plan.getPlanElements()) {
          if (pe instanceof Activity) {
            Activity act = (Activity) pe;
            Coord coord = act.getCoord();
            if (coord.getX() == 0.0 || coord.getY() == 0.0) {
              ((ActivityImpl) act).setCoord(sampleActivityLocation(act.getType()));
              locationsFixed++;
            } else {
              knownLocations++;
            }
          }
        }
      }
    }
    LOG.info("Number of known locations: " + knownLocations);
    LOG.info("Number of locations fixed: " + locationsFixed);
    LOG.info("Done sampling locations.");

    /* Ensure each activity, except the last, has both a start and end time. */
    LOG.info("Ensure each activity has an end time...");
    for (Person person : sc.getPopulation().getPersons().values()) {
      Plan plan = person.getSelectedPlan();
      if (plan != null) {
        List<PlanElement> list = plan.getPlanElements();
        for (int i = 0; i < list.size() - 2; i += 2) {
          PlanElement pe = list.get(i);
          if (pe instanceof Activity) {
            Activity act = (Activity) pe;
            double endTime = act.getEndTime();
            if (endTime < 0.0) {
              double legStart = ((Leg) list.get(i + 1)).getDepartureTime();
              act.setEndTime(legStart);
            }
          } else {
            LOG.warn("Every second PlanElement should be of type 'Activity'.");
          }
        }
      }
    }
    LOG.info("Done fixing activity end times.");

    /* Ensure that the home location/coordinate for all members in the
     * household are the same for all their "h" activities. */
    LOG.info("Fix home locations for each household member. ");
    int homeLocationsFixed = 0;
    int nonTravellers = 0;
    for (Household hh : sc.getHouseholds().getHouseholds().values()) {
      Object o =
          sc.getHouseholds()
              .getHouseholdAttributes()
              .getAttribute(hh.getId().toString(), "transportZone");
      if (o instanceof String) {
        String zoneId = (String) o;
        Coord homeCoord = null;
        if (zoneId != null && !zoneId.equalsIgnoreCase("")) {
          /* There is known home zone. */
          Point p = this.zoneMap.get(zoneId).sampleRandomInteriorPoint();
          homeCoord = CoordUtils.createCoord(p.getX(), p.getY());
        } else {
          /* There is no transport zone. */
          homeCoord = sampleActivityLocation("h");
        }

        /* Now assign the home coordinate to ALL home activities for
         * all members of the household. */
        for (Id<Person> id : hh.getMemberIds()) {
          Person person = sc.getPopulation().getPersons().get(id);
          Plan plan = person.getSelectedPlan();
          if (plan != null) {
            for (PlanElement pe : person.getSelectedPlan().getPlanElements()) {
              if (pe instanceof ActivityImpl) {
                ActivityImpl act = (ActivityImpl) pe;
                if (act.getType().equalsIgnoreCase("h")) {
                  act.setCoord(homeCoord);
                  homeLocationsFixed++;
                }
              }
            }
          } else {
            /* The member does not have ANY plan. We 'fix' this by
             * adding aPlan with a single home-based activity for
             * which not times are specified. */
            Plan stayHomePlan = sc.getPopulation().getFactory().createPlan();
            Activity act = sc.getPopulation().getFactory().createActivityFromCoord("h", homeCoord);
            stayHomePlan.addActivity(act);
            person.addPlan(stayHomePlan);
            nonTravellers++;
          }
        }
      }
    }
    LOG.info("Total number of home locations fixed: " + homeLocationsFixed);
    LOG.info("      Total number of non-travellers: " + nonTravellers);
    LOG.info("Done fixing home locations.");

    /* TODO Check what to do with those household members that are not
     * travelling. Are they just given a single 'h' activities, and
     * left at that? */

    /* Look for erroneous activity times that can/should be fixed in
     * the raw travel diary input data. This will typically include very
     * long activity times or leg durations. */
    LOG.info("Checking leg durations:");
    int remainingLegOddities = 0;
    double legDurationThreshold = Time.parseTime("03:00:00");
    for (Person p : sc.getPopulation().getPersons().values()) {
      Plan plan = p.getSelectedPlan();
      if (plan != null) {
        for (PlanElement pe : plan.getPlanElements()) {
          if (pe instanceof Leg) {
            double duration = ((Leg) pe).getTravelTime();
            if (duration < 0 || duration > legDurationThreshold) {
              LOG.warn(
                  " -> Odd leg duration: "
                      + p.getId().toString()
                      + " ("
                      + Time.writeTime(duration)
                      + ")");
              remainingLegOddities++;
            }
          }
        }
      }
    }
    LOG.info("Done checking leg durations. (" + remainingLegOddities + " remain)");

    /* Parse the activity duration times in an effort to pick up
     * erroneous travel time data that results in negative activity
     * durations. Then fix in input data. */
    LOG.info("Checking activity durations (from leg times):");
    int remainingActivityOddities = 0;
    double activityDurationThreshold = Time.parseTime("16:00:00");
    for (Person p : sc.getPopulation().getPersons().values()) {
      Plan plan = p.getSelectedPlan();
      if (plan != null) {
        for (int i = 1; i < plan.getPlanElements().size() - 2; i += 2) {
          PlanElement pe1 = plan.getPlanElements().get(i);
          PlanElement pe2 = plan.getPlanElements().get(i + 2);
          if (pe1 instanceof Leg && pe2 instanceof Leg) {
            Leg l1 = (Leg) pe1;
            Leg l2 = (Leg) pe2;
            double act = l2.getDepartureTime() - (l1.getDepartureTime() + l1.getTravelTime());
            if (act < 0 || act > activityDurationThreshold) {
              LOG.warn(
                  " -> Odd activity duration: "
                      + p.getId().toString()
                      + " ("
                      + Time.writeTime(act)
                      + ")");
              remainingActivityOddities++;
            }
          } else {
            LOG.error("PlanElements not of type Leg!!");
            LOG.error("   pe1: " + pe1.getClass().toString());
            LOG.error("   pe2: " + pe2.getClass().toString());
          }
        }
      }
    }
    LOG.info("Done checking activity durations. (" + remainingActivityOddities + " remain)");

    /* TODO Fix plans for night workers. They typically start with 'h',
     * but should actually start with 'w'. */

    /* TODO Consider what to do with repeating activities, especially
     * consecutive home activities. */

    /* Convert all activity locations to a projected coordinate system. */
    LOG.info("Converting all activity locations to EPSG:3857...");
    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation("WGS84", "EPSG:3857");
    for (Person person : sc.getPopulation().getPersons().values()) {
      Plan plan = person.getSelectedPlan();
      if (plan != null) {
        for (PlanElement pe : plan.getPlanElements()) {
          if (pe instanceof Activity) {
            Activity act = (Activity) pe;
            ((ActivityImpl) act).setCoord(ct.transform(act.getCoord()));
          }
        }
      }
    }
    LOG.info("Done converting activity locations.");

    LOG.info("Done cleaning up scenario.");
  }
 private Coord parseCoord(Attributes atts) {
   return coordinateTransformation.transform(
       new Coord(
           Double.parseDouble(atts.getValue(ATTR_X100)),
           Double.parseDouble(atts.getValue(ATTR_Y100))));
 }