1   package tutorial.programming.example08DemandGeneration;
2   
3   import org.matsim.api.core.v01.Coord;
4   import org.matsim.api.core.v01.Scenario;
5   import org.matsim.api.core.v01.network.Network;
6   import org.matsim.api.core.v01.population.Activity;
7   import org.matsim.api.core.v01.population.Person;
8   import org.matsim.api.core.v01.population.Plan;
9   import org.matsim.api.core.v01.population.Population;
10  import org.matsim.api.core.v01.population.PopulationFactory;
11  import org.matsim.core.api.internal.MatsimWriter;
12  import org.matsim.core.config.Config;
13  import org.matsim.core.scenario.ScenarioUtils;
14  import org.matsim.core.utils.geometry.CoordinateTransformation;
15  import org.matsim.core.utils.geometry.transformations.TransformationFactory;
16  import org.matsim.core.config.ConfigUtils;
17  
18  /**
19   * 
20   * Creates a sample Population of one Person and writes it to a file.
21   * 
22   * @author michaz
23   *
24   */
25  public class POnePersonPopulationGenerator {
26  
27  	public static void main(String[] args) {
28  		
29  		/*
30  		 * We enter coordinates in the WGS84 reference system, but we want them to appear in the population file
31  		 * projected to UTM33N, because we also generated the network that way.
32  		 */
33  		CoordinateTransformation ct = 
34  			 TransformationFactory.getCoordinateTransformation(TransformationFactory.WGS84, TransformationFactory.WGS84_UTM33N);
35  		
36  		/*
37  		 * First, create a new Config and a new Scenario.
38  		 */
39  		Config config = ConfigUtils.createConfig();
40  		Scenario sc = ScenarioUtils.createScenario(config);
41  
42  		/*
43  		 * Pick the Network and the Population out of the Scenario for convenience. 
44  		 */
45  		Network network = sc.getNetwork();
46  		Population population = sc.getPopulation();
47  
48  		/*
49  		 * Pick the PopulationFactory out of the Population for convenience.
50  		 * It contains methods to create new Population items.
51  		 */
52  		PopulationFactory populationFactory = population.getFactory();
53  
54  		/*
55  		 * Create a Person designated "1" and add it to the Population.
56  		 */
57  		Person person = populationFactory.createPerson(sc.createId("1"));
58  		population.addPerson(person);
59  
60  		/*
61  		 * Create a Plan for the Person
62  		 */
63  		Plan plan = populationFactory.createPlan();
64  		
65  		/*
66  		 * Create a "home" Activity for the Person. In order to have the Person end its day at the same location,
67  		 * we keep the home coordinates for later use (see below).
68  		 * Note that we use the CoordinateTransformation created above.
69  		 */
70  		Coord homeCoordinates = sc.createCoord(14.31377, 51.76948);
71  		Activity activity1 = populationFactory.createActivityFromCoord("home", ct.transform(homeCoordinates));
72  		activity1.setEndTime(21600); // leave at 6 o'clock
73  		plan.addActivity(activity1); // add the Activity to the Plan
74  		
75  		/*
76  		 * Create a Leg. A Leg initially hasn't got many attributes. It just says that a car will be used.
77  		 */
78  		plan.addLeg(populationFactory.createLeg("car"));
79  		
80  		/*
81  		 * Create a "work" Activity, at a different location.
82  		 */
83  		Activity activity2 = populationFactory.createActivityFromCoord("work", ct.transform(sc.createCoord(14.34024, 51.75649)));
84  		activity2.setEndTime(57600); // leave at 4 p.m.
85  		plan.addActivity(activity2);
86  		
87  		/*
88  		 * Create another car Leg.
89  		 */
90  		plan.addLeg(populationFactory.createLeg("car"));
91  		
92  		/*
93  		 * End the day with another Activity at home. Note that it gets the same coordinates as the first activity.
94  		 */
95  		Activity activity3 = populationFactory.createActivityFromCoord("home", ct.transform(homeCoordinates));
96  		plan.addActivity(activity3);
97  		person.addPlan(plan);
98  
99  		/*
100 		 * Write the population (of 1 Person) to a file.
101 		 */
102 		MatsimWriter popWriter = new org.matsim.api.core.v01.population.PopulationWriter(population, network);
103 		popWriter.write("./input/population.xml");
104 	}
105 	
106 }