Search in sources :

Example 1 with Coord

use of org.matsim.api.core.v01.Coord in project vsp-playgrounds by matsim-vsp.

the class CropTransitSystem method main.

public static void main(String[] args) {
    System.out.println("STARTED ...");
    final String zoneShapeFileName = "/Users/GunnarF/NoBackup/data-workspace/ihop2/ihop2-data/demand-input/zones_EPSG3857.shp";
    final ZonalSystem zonalSystem = new ZonalSystem(zoneShapeFileName, StockholmTransformationFactory.WGS84_EPSG3857);
    String fullScheduleFile = "/Users/GunnarF/NoBackup/data-workspace/pt/data/output/transitSchedule.xml.gz";
    String fullTransitVehiclesFile = "/Users/GunnarF/NoBackup/data-workspace/pt/data/output/transitVehicles.xml.gz";
    String pointFile = "/Users/GunnarF/NoBackup/data-workspace/pt/data/output/stopPoints.xy";
    String reducedScheduleFile = "/Users/GunnarF/NoBackup/data-workspace/pt/data/output/transitSchedule_reduced.xml.gz";
    String reducedTransitVehiclesFile = "/Users/GunnarF/NoBackup/data-workspace/pt/data/output/transitVehicles_reduced.xml.gz";
    Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new TransitScheduleReader(scenario).readFile(fullScheduleFile);
    new VehicleReaderV1(scenario.getTransitVehicles()).readFile(fullTransitVehiclesFile);
    final CoordinateTransformation network2zonal = StockholmTransformationFactory.getCoordinateTransformation(StockholmTransformationFactory.WGS84_SWEREF99, StockholmTransformationFactory.WGS84_EPSG3857);
    System.out.print("Identifying contained stop facilities ... ");
    final Set<Id<TransitStopFacility>> relevantStopFacilityIds = new LinkedHashSet<>();
    for (TransitStopFacility stopFacility : scenario.getTransitSchedule().getFacilities().values()) {
        final Coord stopCoord = network2zonal.transform(stopFacility.getCoord());
        final Point stopPoint = MGC.xy2Point(stopCoord.getX(), stopCoord.getY());
        if (zonalSystem.entireRegion.contains(stopPoint)) {
            relevantStopFacilityIds.add(stopFacility.getId());
        }
    }
    System.out.println(relevantStopFacilityIds.size() + " out of " + scenario.getTransitSchedule().getFacilities().size() + " found.");
    // [rail, bus, ferry, Communal Taxi Service, tram, subway]
    final Set<String> relevantModes = new LinkedHashSet<String>();
    relevantModes.add("rail");
    relevantModes.add("bus");
    relevantModes.add("ferry");
    relevantModes.add("tram");
    relevantModes.add("subway");
    int routeCnt = 0;
    final Set<TransitLine> reducedLines = new LinkedHashSet<>();
    final Set<TransitRoute> reducedRoutes = new LinkedHashSet<>();
    final Set<TransitRouteStop> reducedStops = new LinkedHashSet<>();
    final Set<TransitRouteStop> allStops = new LinkedHashSet<>();
    final Set<Id<TransitStopFacility>> reducedStopFacilityIds = new LinkedHashSet<>();
    for (TransitLine line : scenario.getTransitSchedule().getTransitLines().values()) {
        boolean foundRelevantRoute = false;
        for (TransitRoute route : line.getRoutes().values()) {
            routeCnt++;
            allStops.addAll(route.getStops());
            if (relevantModes.contains(route.getTransportMode())) {
                boolean foundRelevantStop = false;
                for (Iterator<TransitRouteStop> stopIt = route.getStops().iterator(); stopIt.hasNext() && !foundRelevantStop; ) {
                    final TransitRouteStop stop = stopIt.next();
                    if (relevantStopFacilityIds.contains(stop.getStopFacility().getId())) {
                        foundRelevantStop = true;
                    }
                }
                if (foundRelevantStop) {
                    reducedRoutes.add(route);
                    reducedStops.addAll(route.getStops());
                    for (TransitRouteStop stop : route.getStops()) {
                        reducedStopFacilityIds.add(stop.getStopFacility().getId());
                    }
                    foundRelevantRoute = true;
                }
            }
        }
        if (foundRelevantRoute) {
            reducedLines.add(line);
        }
    }
    System.out.println("all relevant modes: " + relevantModes);
    System.out.println(reducedLines.size() + " out of " + scenario.getTransitSchedule().getTransitLines().size() + " relevant lines");
    System.out.println(reducedRoutes.size() + " out of " + routeCnt + " relevant routes");
    System.out.println(reducedStops.size() + " out of " + allStops.size() + " relevant stops");
    final Set<TransitLine> linesToRemove = new LinkedHashSet<>(scenario.getTransitSchedule().getTransitLines().values());
    linesToRemove.removeAll(reducedLines);
    for (TransitLine removeLine : linesToRemove) {
        scenario.getTransitSchedule().removeTransitLine(removeLine);
    }
    for (TransitLine line : scenario.getTransitSchedule().getTransitLines().values()) {
        final Set<TransitRoute> routesToRemove = new LinkedHashSet<>();
        for (TransitRoute route : line.getRoutes().values()) {
            if (!reducedRoutes.contains(route)) {
                routesToRemove.add(route);
            }
        }
        for (TransitRoute removeRoute : routesToRemove) {
            line.removeRoute(removeRoute);
        }
    }
    final Set<Id<TransitStopFacility>> facilitiesToRemoveIds = new LinkedHashSet<>(scenario.getTransitSchedule().getFacilities().keySet());
    facilitiesToRemoveIds.removeAll(reducedStopFacilityIds);
    for (Id<TransitStopFacility> facilityToRemoveId : facilitiesToRemoveIds) {
        TransitStopFacility facilityToRemove = scenario.getTransitSchedule().getFacilities().get(facilityToRemoveId);
        scenario.getTransitSchedule().removeStopFacility(facilityToRemove);
    }
    try {
        PrintWriter pointWriter = new PrintWriter(new FileWriter(pointFile, false));
        for (TransitStopFacility fac : scenario.getTransitSchedule().getFacilities().values()) {
            pointWriter.println(fac.getCoord().getX() + "," + fac.getCoord().getY());
        }
        pointWriter.flush();
        pointWriter.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    TransitScheduleWriter writer = new TransitScheduleWriter(scenario.getTransitSchedule());
    writer.writeFile(reducedScheduleFile);
    System.out.println("... DONE");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TransitRouteStop(org.matsim.pt.transitSchedule.api.TransitRouteStop) FileWriter(java.io.FileWriter) TransitLine(org.matsim.pt.transitSchedule.api.TransitLine) TransitRoute(org.matsim.pt.transitSchedule.api.TransitRoute) TransitStopFacility(org.matsim.pt.transitSchedule.api.TransitStopFacility) PrintWriter(java.io.PrintWriter) ZonalSystem(gunnar.ihop2.regent.demandreading.ZonalSystem) Point(com.vividsolutions.jts.geom.Point) IOException(java.io.IOException) VehicleReaderV1(org.matsim.vehicles.VehicleReaderV1) Point(com.vividsolutions.jts.geom.Point) Scenario(org.matsim.api.core.v01.Scenario) Coord(org.matsim.api.core.v01.Coord) TransitScheduleReader(org.matsim.pt.transitSchedule.api.TransitScheduleReader) Id(org.matsim.api.core.v01.Id) TransitScheduleWriter(org.matsim.pt.transitSchedule.api.TransitScheduleWriter) CoordinateTransformation(org.matsim.core.utils.geometry.CoordinateTransformation)

Example 2 with Coord

use of org.matsim.api.core.v01.Coord in project vsp-playgrounds by matsim-vsp.

the class SpatialTemporalScoreAnalysis method main.

public static void main(String[] args) {
    /* Mode can be "car", "pt" or "both"
		   If changing scenario, change plans file, events file and cordinates where to check score (if needed); you will need to re-instantiate the stsanalyser object.
		   Scenario is changed when you need to switch from Max PT to Min PT, or you want to change population i.e. employed to unemployed or unemployed to mixed etc., 
		   or you want to increase population elsewhere i,e. check for population increase in Arstafaltet instead of Alvsjo. If you change PT measure, also change config file accordingly
		   between Max and Min accordingly.  
		   If only checking score at a different time or place or for different mode but within existing scenario, you won't need to re-instantiate the stsanalyser object,
		   just call the calculateScore() function with changed coordinates, mode and/or starting and end time.
		   Default scenario is Farsta Centrum, Max PT measure, Employed Population, PT mode, Morning Peak Hour (06:00 to 09:00)
		 */
    SpatialTemporalScoreAnalysis stsanalyser = new SpatialTemporalScoreAnalysis("C:\\Jayanth\\FarstaCentrum\\configFarstaCentrumMax.xml", "C:\\Jayanth\\FarstaCentrum\\employedmax\\1000.plans.xml.gz", "C:\\Jayanth\\FarstaCentrum\\employedmax\\1000.events.xml.gz");
    // 06:00 to 09:00, Morning Peak
    stsanalyser.calculateScore("pt", new Coord(676397, 6571273), 1000, 21600, 32400);
}
Also used : Coord(org.matsim.api.core.v01.Coord)

Example 3 with Coord

use of org.matsim.api.core.v01.Coord in project vsp-playgrounds by matsim-vsp.

the class PopulationModifier method main.

public static void main(String[] args) {
    String path = "./ihop2/matsim-input/config.xml";
    Config config = ConfigUtils.loadConfig(path);
    final Scenario scenario = ScenarioUtils.loadScenario(config);
    // 10% demand simulated so 1500 for Arstafaltet
    int numberppl = 1500;
    // Arstafaltet area
    Coord origincords = new Coord(672930, 6576324);
    PopulationModifier popmod = new PopulationModifier();
    // ArrayList<Person> sortedpersons = popmod.sortPersonsPerDistance(scenario, origincords, numberppl);
    // ArrayList<Person> sortedpersons = popmod.sortEmployedPersonsPerDistance(scenario, origincords, numberppl);
    ArrayList<Person> sortedpersons = popmod.sortUnEmployedPersonsPerDistance(scenario, origincords, numberppl);
    // popmod.duplicatePersons(scenario.getPopulation(), sortedpersons.subList(0, numberppl));
    // popmod.printSortedPersonsCoordinates(origincords, "./ihop2/matsim-input/10mFarstaCentrum.xy", sortedpersons, numberppl);
    // popmod.writePopulation(scenario, "./ihop2/demand-output/10mFarstaCentrum.xml");
    // For Arstafaltet
    ArrayList<Person> addedpersons = popmod.duplicatePersonsArsta(scenario.getNetwork(), scenario.getPopulation(), sortedpersons.subList(0, numberppl));
    popmod.printSortedPersonsCoordinates(origincords, "./ihop2/matsim-input/10uArstafaltet.xy", addedpersons, numberppl);
    popmod.writePopulation(scenario, "./ihop2/demand-output/10uArstafaltet.xml");
}
Also used : Coord(org.matsim.api.core.v01.Coord) Config(org.matsim.core.config.Config) Person(org.matsim.api.core.v01.population.Person) Scenario(org.matsim.api.core.v01.Scenario)

Example 4 with Coord

use of org.matsim.api.core.v01.Coord in project vsp-playgrounds by matsim-vsp.

the class PopulationModifier method duplicatePersonsArsta.

/* Clone selected closest living people, effectively moving new people to the area.
	 * Also re-allocate cloned people to Arstafaltet field, a grid of 1000X1000 meters.
	 * It is because construction is done in the field, and since the field is empty so far,
	 * most likely the closest cloned people are from out of the field area. 

	 */
public ArrayList<Person> duplicatePersonsArsta(Network network, Population population, List<Person> sortedpersons) {
    ArrayList<Person> addedpersons = new ArrayList<Person>();
    for (Person person : sortedpersons) {
        Person newPerson = population.getFactory().createPerson(Id.createPersonId(person.getId().toString() + "a"));
        final List<? extends Plan> fromPlanList = person.getPlans();
        for (Plan fromPlan : fromPlanList) {
            final Plan toPlan = PopulationUtils.createPlan(newPerson);
            PopulationUtils.copyFromTo(fromPlan, toPlan);
            double xrad = (Math.random() - 0.5) * 1000;
            double yrad = (Math.random() - 0.5) * 1000;
            Coord coord = new Coord(672930 + xrad, 6576324 + yrad);
            PopulationUtils.getFirstActivity(toPlan).setCoord(coord);
            PopulationUtils.getLastActivity(toPlan).setCoord(coord);
            // get nearestt link to the cooordinate
            Link link = NetworkUtils.getNearestLink(network, coord);
            PopulationUtils.getFirstActivity(toPlan).setLinkId(link.getId());
            PopulationUtils.getLastActivity(toPlan).setLinkId(link.getId());
            newPerson.addPlan(toPlan);
        }
        population.addPerson(newPerson);
        addedpersons.add(newPerson);
    }
    return addedpersons;
}
Also used : Coord(org.matsim.api.core.v01.Coord) ArrayList(java.util.ArrayList) Plan(org.matsim.api.core.v01.population.Plan) Person(org.matsim.api.core.v01.population.Person) Link(org.matsim.api.core.v01.network.Link)

Example 5 with Coord

use of org.matsim.api.core.v01.Coord in project vsp-playgrounds by matsim-vsp.

the class PopulationModifier method sortUnEmployedPersonsPerDistance.

/* Sort unemployed people over distance from the given coordinate representing centre of the area we are densifying. 
	 * To move unemployed people, we clone numofppl unemployed people living closest to the given coordinate.
	 */
public ArrayList<Person> sortUnEmployedPersonsPerDistance(Scenario scenario, Coord cords, int numofppl) {
    Population population = scenario.getPopulation();
    Map<Id<Person>, ? extends Person> persons = population.getPersons();
    ArrayList<Person> sortedpersons = new ArrayList<Person>();
    ArrayList<Double> distances = new ArrayList<Double>();
    for (Person person : persons.values()) {
        if (!employed(person)) {
            // Home coordinates
            Coord homecords = PopulationUtils.getFirstActivity(person.getSelectedPlan()).getCoord();
            double distance = NetworkUtils.getEuclideanDistance(cords, homecords);
            int i = 0;
            for (; i < distances.size(); i++) {
                if (distances.get(i) < distance) {
                    ;
                } else {
                    break;
                }
            }
            distances.add(i, distance);
            sortedpersons.add(i, person);
        }
    }
    return sortedpersons;
}
Also used : Coord(org.matsim.api.core.v01.Coord) ArrayList(java.util.ArrayList) Population(org.matsim.api.core.v01.population.Population) Id(org.matsim.api.core.v01.Id) Person(org.matsim.api.core.v01.population.Person)

Aggregations

Coord (org.matsim.api.core.v01.Coord)241 Link (org.matsim.api.core.v01.network.Link)82 Person (org.matsim.api.core.v01.population.Person)71 Activity (org.matsim.api.core.v01.population.Activity)70 ArrayList (java.util.ArrayList)65 Id (org.matsim.api.core.v01.Id)65 Plan (org.matsim.api.core.v01.population.Plan)55 Point (org.locationtech.jts.geom.Point)50 Scenario (org.matsim.api.core.v01.Scenario)43 Leg (org.matsim.api.core.v01.population.Leg)36 Config (org.matsim.core.config.Config)33 NetworkRoute (org.matsim.core.population.routes.NetworkRoute)31 LinkNetworkRouteFactory (org.matsim.core.population.routes.LinkNetworkRouteFactory)30 IOException (java.io.IOException)28 Geometry (org.locationtech.jts.geom.Geometry)26 Network (org.matsim.api.core.v01.network.Network)26 Population (org.matsim.api.core.v01.population.Population)26 HashMap (java.util.HashMap)25 Node (org.matsim.api.core.v01.network.Node)25 NetworkFactory (org.matsim.api.core.v01.network.NetworkFactory)22