MATSIM
CreateDemand.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * RunEmissionToolOffline.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 package tutorial.programming.example21tutorialTUBclass.demand;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Random;
24 
25 import org.matsim.api.core.v01.Coord;
26 import org.matsim.api.core.v01.Id;
27 import org.matsim.api.core.v01.Scenario;
45 import org.opengis.feature.simple.SimpleFeature;
46 
47 import com.vividsolutions.jts.geom.Geometry;
48 import com.vividsolutions.jts.geom.GeometryFactory;
49 import com.vividsolutions.jts.geom.Point;
50 import com.vividsolutions.jts.io.ParseException;
51 import com.vividsolutions.jts.io.WKTReader;
52 
58 public class CreateDemand {
59 
60  private static final String NETWORKFILE = "input/network.xml";
61  private static final String KREISE = "input/Landkreise/Kreise.shp";
62 
63  private static final String PLANSFILEOUTPUT = "input/plans.xml";
64 
65  private static final String SHOPS = "input/shops.txt";
66  private static final String KINDERGARTEN = "input/kindergaerten.txt";
67 
68 
69  private Scenario scenario;
70  private Map<String,Geometry> shapeMap;
71  private static double SCALEFACTOR = 0.1;
72 
73  private Map<String,Coord> kindergartens;
74  private Map<String,Coord> shops;
75 
76 
77  CreateDemand (){
79  new MatsimNetworkReader(scenario.getNetwork()).readFile(NETWORKFILE);
80 
81  }
82 
83  private void run() {
84  this.shapeMap = readShapeFile(KREISE, "Nr");
85  this.shops = readFacilityLocations(SHOPS);
86  this.kindergartens = readFacilityLocations(KINDERGARTEN);
87 
88  //CB-CB
89  double commuters = 22709*SCALEFACTOR;
90  double carcommuters = 0.55 * commuters;
91  //Watch out, counties come with some extra zeros in the end in the shape file
92  System.out.println(this.shapeMap.keySet());
93  Geometry home = this.shapeMap.get("12052000");
94  Geometry work = this.shapeMap.get("12052000");
95  for (int i = 0; i<=commuters;i++){
96  String mode = "car";
97  if (i>carcommuters) mode = "pt";
98  Coord homec = drawRandomPointFromGeometry(home);
99  System.out.println(homec+" : closest kg: "+this.findClosestCoordFromMap(homec, this.kindergartens)+"closest shop:"+this.findClosestCoordFromMap(homec, this.shops));
100 // System.out.println(this.findClosestCoordFromMap(homec, this.kindergartens));
101  Coord workc = drawRandomPointFromGeometry(work);
102  createOnePerson(i, homec, workc, mode, "CB_CB");
103  }
104 
105  PopulationWriter pw = new PopulationWriter(scenario.getPopulation(),scenario.getNetwork());
106  pw.write(PLANSFILEOUTPUT);
107  }
108 
109 
110  private void createOnePerson(int i, Coord coord, Coord coordWork, String mode, String toFromPrefix) {
111  Id<Person> personId = Id.createPersonId(toFromPrefix+i);
112  Person person = scenario.getPopulation().getFactory().createPerson(personId);
113 
114  Plan plan = scenario.getPopulation().getFactory().createPlan();
115 
116 
117  Activity home = scenario.getPopulation().getFactory().createActivityFromCoord("home", coord);
118  home.setEndTime(9*60*60);
119  plan.addActivity(home);
120 
121  Leg hinweg = scenario.getPopulation().getFactory().createLeg(mode);
122  plan.addLeg(hinweg);
123 
124  Activity work = scenario.getPopulation().getFactory().createActivityFromCoord("work", coordWork);
125  work.setEndTime(17*60*60);
126  plan.addActivity(work);
127 
128  Leg rueckweg = scenario.getPopulation().getFactory().createLeg(mode);
129  plan.addLeg(rueckweg);
130 
131  Activity home2 = scenario.getPopulation().getFactory().createActivityFromCoord("home", coord);
132  plan.addActivity(home2);
133 
134  person.addPlan(plan);
135  scenario.getPopulation().addPerson(person);
136  }
137 
138  private Coord drawRandomPointFromGeometry(Geometry g) {
139  Random rnd = MatsimRandom.getLocalInstance();
140  Point p;
141  double x, y;
142  do {
143  x = g.getEnvelopeInternal().getMinX() + rnd.nextDouble() * (g.getEnvelopeInternal().getMaxX() - g.getEnvelopeInternal().getMinX());
144  y = g.getEnvelopeInternal().getMinY() + rnd.nextDouble() * (g.getEnvelopeInternal().getMaxY() - g.getEnvelopeInternal().getMinY());
145  p = MGC.xy2Point(x, y);
146  } while (!g.contains(p));
147  Coord coord = new Coord(p.getX(), p.getY());
148  return coord;
149  }
150 
151 
152 
153  public static void main(String[] args) {
154 
155  CreateDemand cd = new CreateDemand();
156  cd.run();
157  }
158 
159  public Map<String,Geometry> readShapeFile(String filename, String attrString){
160  //attrString: Für Brandenburg: Nr
161  //für OSM: osm_id
162 
163  Map<String,Geometry> shapeMap = new HashMap<String, Geometry>();
164 
165  for (SimpleFeature ft : ShapeFileReader.getAllFeatures(filename)) {
166 
167  GeometryFactory geometryFactory= new GeometryFactory();
168  WKTReader wktReader = new WKTReader(geometryFactory);
169  Geometry geometry;
170 
171  try {
172  geometry = wktReader.read((ft.getAttribute("the_geom")).toString());
173  shapeMap.put(ft.getAttribute(attrString).toString(),geometry);
174 
175  } catch (ParseException e) {
176  // TODO Auto-generated catch block
177  e.printStackTrace();
178  }
179 
180  }
181  return shapeMap;
182  }
183 
184  private Map<String,Coord> readFacilityLocations (String fileName){
185 
186  FacilityParser fp = new FacilityParser();
188  config.setDelimiterRegex("\t");
189  config.setCommentRegex("#");
190  config.setFileName(fileName);
191  new TabularFileParser().parse(config, fp);
192  return fp.getFacilityMap();
193 
194  }
195 
196  private Coord findClosestCoordFromMap(Coord location, Map<String,Coord> coordMap){
197  Coord closest = null;
198  double closestDistance = Double.MAX_VALUE;
199  for (Coord coord : coordMap.values()){
200  double distance = CoordUtils.calcEuclideanDistance(coord, location);
201  if (distance<closestDistance) {
202  closestDistance = distance;
203  closest = coord;
204  }
205  }
206  return closest;
207  }
208 
209 
210 
211 }
212 
213  class FacilityParser implements TabularFileHandler{
214 
215 
216  private Map<String,Coord> facilityMap = new HashMap<String, Coord>();
217  CoordinateTransformation ct = new GeotoolsTransformation("EPSG:4326", "EPSG:32633");
218 
219  @Override
220  public void startRow(String[] row) {
221  try{
222  Double x = Double.parseDouble(row[2]);
223  Double y = Double.parseDouble(row[1]);
224  Coord coords = new Coord(x,y);
225  this.facilityMap.put(row[0],ct.transform(coords));
226  }
227  catch (NumberFormatException e){
228  //skips line
229  }
230  }
231 
232  public Map<String, Coord> getFacilityMap() {
233  return facilityMap;
234  }
235 
236 
237 }
void parse(TabularFileParserConfig config, TabularFileHandler handler)
static double calcEuclideanDistance(Coord coord, Coord other)
Coord findClosestCoordFromMap(Coord location, Map< String, Coord > coordMap)
abstract void addLeg(final Leg leg)
void createOnePerson(int i, Coord coord, Coord coordWork, String mode, String toFromPrefix)
static Point xy2Point(final double x, final double y)
Definition: MGC.java:147
abstract void addActivity(final Activity act)
static Config createConfig(final String filename)
Activity createActivityFromCoord(String actType, Coord coord)
static Collection< SimpleFeature > getAllFeatures(final String filename)
static Scenario createScenario(final Config config)
void setEndTime(final double seconds)
Map< String, Geometry > readShapeFile(String filename, String attrString)