@Override public void readFile(String filename) { reader.readFile(filename); }
public static void readPopulation( Population population, String filename ) { MutableScenario scenario = ScenarioUtils.createMutableScenario( ConfigUtils.createConfig() ) ; scenario.setPopulation( population ); new PopulationReader( scenario ).readFile( filename ); // (yyyy population reader uses network to retrofit some missing geo information such as route lenth. // In my opinion, that should be done in prepareForSim, not in the parser. It is commented as such // in the PopulationReader class. kai, nov'18) }
private static Population readPopulation(final String inPlansFile) { final Scenario s = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( s ).readFile( inPlansFile ); return s.getPopulation(); }
private Population loadPopulation(String filename) { Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); new PopulationReader(scenario).readFile(filename); return scenario.getPopulation(); }
public static void main(final String [] args) { // FIXME hard-coded file names; does this class really need a main-method? final String populationFilename = "./examples/equil/plans100.xml"; final String networkFilename = "./examples/equil/network.xml"; // final String populationFilename = "./test/scenarios/berlin/plans_hwh_1pct.xml.gz"; // final String networkFilename = "./test/scenarios/berlin/network.xml.gz"; final String outputDir = "./plans/"; new File(outputDir).mkdir(); MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); new MatsimNetworkReader(scenario.getNetwork()).readFile(networkFilename); new PopulationReader(scenario).readFile(populationFilename); CoordinateReferenceSystem crs = MGC.getCRS("DHDN_GK4"); SelectedPlans2ESRIShape sp = new SelectedPlans2ESRIShape(scenario.getPopulation(), scenario.getNetwork(), crs, outputDir); sp.setOutputSample(0.05); sp.setActBlurFactor(100); sp.setLegBlurFactor(100); sp.setWriteActs(true); sp.setWriteLegs(true); sp.write(); }
@Test public void testReadFile_v5() { Scenario s = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Assert.assertEquals(0, s.getPopulation().getPersons().size()); new PopulationReader(s).readFile("test/input/org/matsim/core/utils/io/MatsimFileTypeGuesserTest/population_v5_example.xml"); Assert.assertEquals(1, s.getPopulation().getPersons().size()); Person person = s.getPopulation().getPersons().get(Id.create(1, Person.class)); Assert.assertNotNull(person); Plan plan = person.getSelectedPlan(); List<PlanElement> planElements = plan.getPlanElements(); Assert.assertEquals(3, planElements.size()); Assert.assertTrue(planElements.get(0) instanceof Activity); Assert.assertTrue(planElements.get(1) instanceof Leg); Assert.assertTrue(planElements.get(2) instanceof Activity); }
@Test public void testReadFile_v5_multipleSuccessiveLegs() { Scenario s = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Assert.assertEquals(0, s.getPopulation().getPersons().size()); new PopulationReader(s).readFile("test/input/org/matsim/core/utils/io/MatsimFileTypeGuesserTest/population_v5_multipleLegs.xml"); Assert.assertEquals(1, s.getPopulation().getPersons().size()); Person person = s.getPopulation().getPersons().get(Id.create(1, Person.class)); Assert.assertNotNull(person); Plan plan = person.getSelectedPlan(); List<PlanElement> planElements = plan.getPlanElements(); Assert.assertEquals(5, planElements.size()); Assert.assertTrue(planElements.get(0) instanceof Activity); Assert.assertTrue(planElements.get(1) instanceof Leg); Assert.assertTrue(planElements.get(2) instanceof Leg); Assert.assertTrue(planElements.get(3) instanceof Leg); Assert.assertTrue(planElements.get(4) instanceof Activity); }
@Test public void testReadFile_v5_multipleSuccessiveLegsWithTeleportation() { Scenario s = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Assert.assertEquals(0, s.getPopulation().getPersons().size()); new PopulationReader(s).readFile("test/input/org/matsim/core/utils/io/MatsimFileTypeGuesserTest/population_v5_multipleTeleportedLegs.xml"); Assert.assertEquals(1, s.getPopulation().getPersons().size()); Person person = s.getPopulation().getPersons().get(Id.create(1, Person.class)); Assert.assertNotNull(person); Plan plan = person.getSelectedPlan(); List<PlanElement> planElements = plan.getPlanElements(); Assert.assertEquals(5, planElements.size()); Assert.assertTrue(planElements.get(0) instanceof Activity); Assert.assertTrue(planElements.get(1) instanceof Leg); // Assert.assertTrue(((Leg) planElements.get(1)).getRoute() instanceof GenericRouteImpl); Assert.assertTrue(planElements.get(2) instanceof Leg); Assert.assertTrue(((Leg) planElements.get(2)).getRoute() instanceof NetworkRoute); Assert.assertTrue(planElements.get(3) instanceof Leg); // Assert.assertTrue(((Leg) planElements.get(3)).getRoute() instanceof GenericRouteImpl); Assert.assertTrue(planElements.get(4) instanceof Activity); } }
@Test public void testReadFile_v4() { Scenario s = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Assert.assertEquals(0, s.getPopulation().getPersons().size()); new MatsimNetworkReader(s.getNetwork()).readFile("test/scenarios/equil/network.xml"); new PopulationReader(s).readFile("test/scenarios/equil/plans1.xml"); Assert.assertEquals(1, s.getPopulation().getPersons().size()); }
@Test public void testReadFile_v5_multipleSuccessiveLegsWithRoutes() { Scenario s = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Assert.assertEquals(0, s.getPopulation().getPersons().size()); new PopulationReader(s).readFile("test/input/org/matsim/core/utils/io/MatsimFileTypeGuesserTest/population_v5_multipleLegsWithRoutes.xml"); Assert.assertEquals(1, s.getPopulation().getPersons().size()); Person person = s.getPopulation().getPersons().get(Id.create(1, Person.class)); Assert.assertNotNull(person); Plan plan = person.getSelectedPlan(); List<PlanElement> planElements = plan.getPlanElements(); Assert.assertEquals(5, planElements.size()); Assert.assertTrue(planElements.get(0) instanceof Activity); Assert.assertTrue(planElements.get(1) instanceof Leg); Assert.assertEquals(Id.create("1", Link.class), ((Leg) planElements.get(1)).getRoute().getStartLinkId()); Assert.assertEquals(Id.create("2", Link.class), ((Leg) planElements.get(1)).getRoute().getEndLinkId()); Assert.assertTrue(planElements.get(2) instanceof Leg); Assert.assertEquals(Id.create("2", Link.class), ((Leg) planElements.get(2)).getRoute().getStartLinkId()); Assert.assertEquals(Id.create("4", Link.class), ((Leg) planElements.get(2)).getRoute().getEndLinkId()); Assert.assertTrue(planElements.get(3) instanceof Leg); Assert.assertEquals(Id.create("4", Link.class), ((Leg) planElements.get(3)).getRoute().getStartLinkId()); Assert.assertEquals(Id.create("6", Link.class), ((Leg) planElements.get(3)).getRoute().getEndLinkId()); Assert.assertTrue(planElements.get(4) instanceof Activity); }
public void testConversionAtInput(final String inputFile) { final Scenario originalScenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); final Scenario reprojectedScenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); // necessary for v4... URL network = IOUtils.newUrl(ExamplesUtils.getTestScenarioURL("berlin"), NET_FILE); new MatsimNetworkReader(originalScenario.getNetwork()).parse(network); new MatsimNetworkReader(reprojectedScenario.getNetwork()).parse(network); new PopulationReader(originalScenario).readFile(inputFile); new PopulationReader( INITIAL_CRS, TARGET_CRS, reprojectedScenario).readFile(inputFile); final Population originalPopulation = originalScenario.getPopulation(); final Population reprojectedPopulation = reprojectedScenario.getPopulation(); assertPopulationCorrectlyTransformed( originalPopulation , reprojectedPopulation ); }
@Test public void testCompareBigPopulationWithItself() { Scenario s1 = ScenarioUtils.createScenario(ConfigUtils.createConfig()); String netFileName = "test/scenarios/berlin/network.xml"; String popFileName = "test/scenarios/berlin/plans_hwh_1pct.xml.gz"; new MatsimNetworkReader(s1.getNetwork()).readFile(netFileName); new PopulationReader(s1).readFile(popFileName); Assert.assertTrue(PopulationUtils.equalPopulation(s1.getPopulation(), s1.getPopulation())); }
@Test public void testEmptyPersonAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); // just check everything works without attributes (dtd validation etc) final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); }
@Test public void testOutput_V5() { final String testFile = new File(utils.getOutputDirectory() + "/plans.xml.gz").getAbsolutePath(); // read test population Config config = ConfigUtils.createConfig(ExamplesUtils.getTestScenarioURL("berlin")); final Scenario originalScenario = ScenarioUtils.createScenario(config); // necessary for v4... new MatsimNetworkReader(originalScenario.getNetwork()).parse(IOUtils.newUrl(config.getContext(), NET_FILE)); new PopulationReader(originalScenario).parse(IOUtils.newUrl(config.getContext(), BASE_FILE)); // write test population with conversion new PopulationWriter( transformation, originalScenario.getPopulation(), originalScenario.getNetwork()).writeV5( testFile ); // read converted population final Scenario reprojectedScenario = ScenarioUtils.createScenario(config); new PopulationReader(reprojectedScenario).readFile(testFile); assertPopulationCorrectlyTransformed( originalScenario.getPopulation() , reprojectedScenario.getPopulation() ); }
private void doTest(final RouterProvider provider) { // final Config config = loadConfig("test/input/" + this.getClass().getCanonicalName().replace('.', '/') + "/config.xml"); final Config config = ConfigUtils.loadConfig( utils.getClassInputDirectory() + "/config.xml" ); final Scenario scenario = ScenarioUtils.createScenario(config); new MatsimNetworkReader(scenario.getNetwork()).readFile(config.network().getInputFile()); // final String inPlansName = "test/input/" + this.getClass().getCanonicalName().replace('.', '/') + "/plans.xml.gz"; final String inPlansName = utils.getClassInputDirectory() + "/plans.xml.gz" ; new PopulationReader(scenario).readFile(inPlansName); calcRoute(provider, scenario); final Scenario referenceScenario = ScenarioUtils.createScenario(config); new MatsimNetworkReader(referenceScenario.getNetwork()).readFile(config.network().getInputFile()); new PopulationReader(referenceScenario).readFile(inPlansName); final boolean isEqual = PopulationUtils.equalPopulation(referenceScenario.getPopulation(), scenario.getPopulation()); if ( !isEqual ) { new PopulationWriter(referenceScenario.getPopulation(), scenario.getNetwork()).write(this.utils.getOutputDirectory() + "/reference_population.xml.gz"); new PopulationWriter(scenario.getPopulation(), scenario.getNetwork()).write(this.utils.getOutputDirectory() + "/output_population.xml.gz"); } Assert.assertTrue("different plans files.", isEqual); }
@Test public void testOutput_V4() { final String testFile = new File(utils.getOutputDirectory() + "/plans.xml.gz").getAbsolutePath(); // read test population Config config = ConfigUtils.createConfig(ExamplesUtils.getTestScenarioURL("berlin")); final Scenario originalScenario = ScenarioUtils.createScenario(config); // necessary for v4... new MatsimNetworkReader(originalScenario.getNetwork()).parse(IOUtils.newUrl(config.getContext(), NET_FILE)); new PopulationReader(originalScenario).parse(IOUtils.newUrl(config.getContext(), BASE_FILE)); // write test population with conversion new PopulationWriter( transformation, originalScenario.getPopulation(), originalScenario.getNetwork()).writeV4( testFile ); // read converted population final Scenario reprojectedScenario = ScenarioUtils.createScenario(config); // necessary for v4... new MatsimNetworkReader(reprojectedScenario.getNetwork()).parse(IOUtils.newUrl(config.getContext(), NET_FILE)); new PopulationReader(reprojectedScenario).readFile(testFile); assertPopulationCorrectlyTransformed( originalScenario.getPopulation() , reprojectedScenario.getPopulation() ); }
@Test public void testPersonAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); person.getAttributes().putAttribute( "brain" , false ); person.getAttributes().putAttribute( "party" , "republican" ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); final Person readPerson = readScenario.getPopulation().getPersons().get( Id.createPersonId( "Donald Trump" ) ); Assert.assertEquals( "Unexpected boolean attribute in " + readPerson.getAttributes(), person.getAttributes().getAttribute( "brain" ) , readPerson.getAttributes().getAttribute( "brain" ) ); Assert.assertEquals( "Unexpected String attribute in " + readPerson.getAttributes(), person.getAttributes().getAttribute( "party" ) , readPerson.getAttributes().getAttribute( "party" ) ); }
@Test public void testLegAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); final Plan plan = population.getFactory().createPlan(); person.addPlan( plan ); final Leg leg = population.getFactory().createLeg( "SUV" ); plan.addActivity( population.getFactory().createActivityFromLinkId( "speech" , Id.createLinkId( 1 ))); plan.addLeg( leg ); plan.addActivity( population.getFactory().createActivityFromLinkId( "tweet" , Id.createLinkId( 2 ))); leg.getAttributes().putAttribute( "mpg" , 0.000001d ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); final Person readPerson = readScenario.getPopulation().getPersons().get( Id.createPersonId( "Donald Trump" ) ); final Leg readLeg = (Leg) readPerson.getSelectedPlan().getPlanElements().get( 1 ); Assert.assertEquals( "Unexpected Double attribute in " + readLeg.getAttributes(), leg.getAttributes().getAttribute( "mpg" ) , readLeg.getAttributes().getAttribute( "mpg" ) ); }
@Test public void testPlanAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); final Person person = population.getFactory().createPerson(Id.createPersonId( "Donald Trump")); population.addPerson( person ); final Plan plan = population.getFactory().createPlan(); person.addPlan( plan ); final Leg leg = population.getFactory().createLeg( "SUV" ); plan.addActivity( population.getFactory().createActivityFromLinkId( "speech" , Id.createLinkId( 1 ))); plan.addLeg( leg ); plan.addActivity( population.getFactory().createActivityFromLinkId( "tweet" , Id.createLinkId( 2 ))); plan.getAttributes().putAttribute( "beauty" , 0.000001d ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); final Person readPerson = readScenario.getPopulation().getPersons().get( Id.createPersonId( "Donald Trump" ) ); final Plan readPlan = readPerson.getSelectedPlan() ; Assert.assertEquals( plan.getAttributes().getAttribute( "beauty" ) , readPlan.getAttributes().getAttribute( "beauty" ) ); }
@Test public void testPopulationAttributesIO() { final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig() ); population.getAttributes().putAttribute( "type" , "candidates" ); population.getAttributes().putAttribute( "number" , 2 ); final String file = utils.getOutputDirectory()+"/population.xml"; new PopulationWriter( population ).writeV6( file ); final Scenario readScenario = ScenarioUtils.createScenario( ConfigUtils.createConfig() ); new PopulationReader( readScenario ).readFile( file ); Assert.assertEquals( "Unexpected numeric attribute in " + readScenario.getPopulation().getAttributes(), population.getAttributes().getAttribute( "number" ) , readScenario.getPopulation().getAttributes().getAttribute( "number" ) ); Assert.assertEquals( "Unexpected String attribute in " + readScenario.getPopulation().getAttributes(), population.getAttributes().getAttribute( "type" ) , readScenario.getPopulation().getAttributes().getAttribute( "type" ) ); } }