public static final void moveFacility(ActivityFacilityImpl f, Link link) { double[] vector = new double[2]; vector[0] = (link.getToNode().getCoord().getY() - link.getFromNode().getCoord().getY()); vector[1] = (-(link.getToNode().getCoord().getX() - link.getFromNode().getCoord().getX())); Coord coord = new CoordImpl( link.getCoord().getX() + vector[0] * EPSILON, link.getCoord().getY() + vector[1] * 0.0001D); f.setCoord(coord); f.setLinkId(link.getId()); }
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); }
public static final QuadTree<ActivityFacility> createFacilityQuadTree(Controler controler) { double minx = (1.0D / 0.0D); double miny = (1.0D / 0.0D); double maxx = (-1.0D / 0.0D); double maxy = (-1.0D / 0.0D); for (Link l : controler.getScenario().getNetwork().getLinks().values()) { if (l.getCoord().getX() < minx) minx = l.getCoord().getX(); if (l.getCoord().getY() < miny) miny = l.getCoord().getY(); if (l.getCoord().getX() > maxx) maxx = l.getCoord().getX(); if (l.getCoord().getY() <= maxy) continue; maxy = l.getCoord().getY(); } minx -= 1.0D; miny -= 1.0D; maxx += 1.0D; maxy += 1.0D; QuadTree<ActivityFacility> facilityQuadTree = new QuadTree<ActivityFacility>(minx, miny, maxx, maxy); for (ActivityFacility f : controler.getScenario().getActivityFacilities().getFacilities().values()) { ((ActivityFacilityImpl) f) .setLinkId( NetworkUtils.getNearestLink( ((NetworkImpl) controler.getScenario().getNetwork()), f.getCoord()) .getId()); Coord c = f.getCoord(); facilityQuadTree.put(c.getX(), c.getY(), f); } return facilityQuadTree; }
// Create external Facilities that are used by transit traffic agents. private static void createExternalFacilities(Scenario scenario, Set<Id<Node>> externalNodes) { ActivityFacilities activityFacilities = scenario.getActivityFacilities(); ActivityFacilitiesFactory factory = activityFacilities.getFactory(); /* * We check for all OutLinks of all external nodes if they already host a facility. If not, * a new facility with a tta ActivityOption will be created and added. */ for (Id<Node> id : externalNodes) { Node externalNode = scenario.getNetwork().getNodes().get(id); for (Link externalLink : externalNode.getOutLinks().values()) { ActivityFacility facility = activityFacilities.getFacilities().get(externalLink.getId()); // if already a facility exists we have nothing left to do if (facility != null) continue; /* * No Facility exists at that link therefore we create and add a new one. */ double fromX = externalLink.getFromNode().getCoord().getX(); double fromY = externalLink.getFromNode().getCoord().getY(); double toX = externalLink.getToNode().getCoord().getX(); double toY = externalLink.getToNode().getCoord().getY(); double dX = toX - fromX; double dY = toY - fromY; double length = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2)); double centerX = externalLink.getCoord().getX(); double centerY = externalLink.getCoord().getY(); /* * Unit vector that directs with an angle of 90° away from the link. */ double unitVectorX = dY / length; double unitVectorY = -dX / length; Coord coord = new Coord(centerX + unitVectorX, centerY + unitVectorY); facility = activityFacilities .getFactory() .createActivityFacility( Id.create(externalLink.getId().toString(), ActivityFacility.class), coord); activityFacilities.addActivityFacility(facility); ((ActivityFacilityImpl) facility).setLinkId(externalLink.getId()); ActivityOption activityOption = factory.createActivityOption(ttaActivityType); activityOption.addOpeningTime(new OpeningTimeImpl(0 * 3600, 24 * 3600)); activityOption.setCapacity(capacity); facility.addActivityOption(activityOption); } } }
@Override public void readFacilities(ActivityFacilitiesImpl facilities) { log.fatal("does not work; see javadoc of class. Aborting ..." + this); System.exit(-1); // (these are simply defined as those entities that have x/y coordinates in urbansim) try { BufferedReader reader = IOUtils.getBufferedReader(Matsim4Urbansim.PATH_TO_OPUS_MATSIM + "tmp/gridcells.tab"); String header = reader.readLine(); Map<String, Integer> idxFromKey = Utils.createIdxFromKey(header, null); String line = reader.readLine(); while (line != null) { String[] parts = line.split("[\t]+"); int idx_id = idxFromKey.get("grid_id:i4"); Id<ActivityFacility> id = Id.create(parts[idx_id], ActivityFacility.class); int idx_x = idxFromKey.get("relative_x:i4"); int idx_y = idxFromKey.get("relative_y:i4"); Coord coord = new Coord(Double.parseDouble(parts[idx_x]), Double.parseDouble(parts[idx_y])); ActivityFacilityImpl facility = facilities.createAndAddFacility(id, coord); facility.setDesc("urbansim location"); line = reader.readLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private static void collectStreetParkings(ArrayList<String> outputList) { String streetParkings = Config.baseFolder + "../streetpark_facilities.xml"; ActivityFacilities streetParkingFacilities = GeneralLib.readActivityFacilities(streetParkings); int totalCapacity = 0; for (Id facilityId : streetParkingFacilities.getFacilities().keySet()) { ActivityFacilityImpl facilityImpl = (ActivityFacilityImpl) streetParkingFacilities.getFacilities().get(facilityId); Map<String, ActivityOption> activityOptions = facilityImpl.getActivityOptions(); if (Config.isInsideStudyArea(facilityImpl.getCoord())) { long capacity = Math.round(activityOptions.get("parking").getCapacity()); totalCapacity += capacity; outputList.add( getParkingString( "sp-" + facilityId.toString(), facilityImpl.getCoord().getX(), facilityImpl.getCoord().getY(), capacity)); } } System.out.println("total number of street parking in the scenario:" + totalCapacity); }
// Create Facilities inside the simulated area. private static void createInternalFacilities( Scenario scenario, Map<Integer, Emme2Zone> zonalAttributes, Map<Integer, SimpleFeature> zonalShapes) { // create indices for the zones Map<Integer, Integer> indices = new HashMap<Integer, Integer>(); int index = 0; for (Integer taz : zonalShapes.keySet()) { indices.put(taz, index); index++; } NetworkImpl network = (NetworkImpl) scenario.getNetwork(); ActivityFacilities activityFacilities = scenario.getActivityFacilities(); ObjectAttributes facilitiesAttributes = activityFacilities.getFacilityAttributes(); ActivityFacilitiesFactory factory = activityFacilities.getFactory(); for (Entry<Integer, SimpleFeature> entry : zonalShapes.entrySet()) { int taz = entry.getKey(); SimpleFeature feature = entry.getValue(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); List<Coord> coordinates = getRandomCoordinatesInZone(facilitiesPerZone, geometry, random); int i = 0; for (Coord coord : coordinates) { Id<ActivityFacility> id = Id.create(taz + "_" + i, ActivityFacility.class); ActivityFacility facility = factory.createActivityFacility(id, coord); createAndAddActivityOptions(scenario, facility, zonalAttributes.get(taz)); activityFacilities.addActivityFacility(facility); Link link = network.getNearestLinkExactly(coord); ((ActivityFacilityImpl) facility).setLinkId(link.getId()); i++; // Also add a tta activity to all facilities. ActivityOption activityOption = factory.createActivityOption(ttaActivityType); activityOption.addOpeningTime(new OpeningTimeImpl(0 * 3600, 24 * 3600)); activityOption.setCapacity(capacity); facility.addActivityOption(activityOption); facilitiesAttributes.putAttribute(id.toString(), TAZObjectAttributesName, taz); facilitiesAttributes.putAttribute( id.toString(), indexObjectAttributesName, indices.get(taz)); } } }
/** * @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."); }