MATSIM
RunCreateNetworkSHP.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2012 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package tutorial.programming.createNetworkSHP;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 
25 import org.matsim.api.core.v01.Scenario;
29 import org.matsim.core.config.Config;
37 import org.opengis.feature.simple.SimpleFeature;
38 import org.opengis.referencing.crs.CoordinateReferenceSystem;
39 
40 import com.vividsolutions.jts.geom.Coordinate;
41 
42 public class RunCreateNetworkSHP {
43 
44  public static void main(String[] args) throws Exception {
45 
46  Config config = ConfigUtils.createConfig();
47  config.network().setInputFile("network.xml");
48  Scenario scenario = ScenarioUtils.loadScenario(config);
49  Network network = scenario.getNetwork();
50 
51  CoordinateReferenceSystem crs = MGC.getCRS("EPSG:21781"); // EPSG Code for Swiss CH1903_LV03 coordinate system
52 
53  Collection<SimpleFeature> features = new ArrayList<SimpleFeature>();
55  setCrs(crs).
56  setName("link").
57  addAttribute("ID", String.class).
58  addAttribute("fromID", String.class).
59  addAttribute("toID", String.class).
60  addAttribute("length", Double.class).
61  addAttribute("type", String.class).
62  addAttribute("capacity", Double.class).
63  addAttribute("freespeed", Double.class).
64  create();
65 
66  for (Link link : network.getLinks().values()) {
67  Coordinate fromNodeCoordinate = new Coordinate(link.getFromNode().getCoord().getX(), link.getFromNode().getCoord().getY());
68  Coordinate toNodeCoordinate = new Coordinate(link.getToNode().getCoord().getX(), link.getToNode().getCoord().getY());
69  Coordinate linkCoordinate = new Coordinate(link.getCoord().getX(), link.getCoord().getY());
70  SimpleFeature ft = linkFactory.createPolyline(new Coordinate [] {fromNodeCoordinate, linkCoordinate, toNodeCoordinate},
71  new Object [] {link.getId().toString(), link.getFromNode().getId().toString(),link.getToNode().getId().toString(), link.getLength(), NetworkUtils.getType(((Link)link)), link.getCapacity(), link.getFreespeed()}, null);
72  features.add(ft);
73  }
74  ShapeFileWriter.writeGeometries(features, "output/network_links.shp");
75 
76  features = new ArrayList<SimpleFeature>();
78  setCrs(crs).
79  setName("nodes").
80  addAttribute("ID", String.class).
81  create();
82 
83  for (Node node : network.getNodes().values()) {
84  SimpleFeature ft = nodeFactory.createPoint(node.getCoord(), new Object[] {node.getId().toString()}, null);
85  features.add(ft);
86  }
87  ShapeFileWriter.writeGeometries(features, "output/network_nodes.shp");
88  }
89 }
Map< Id< Node >,?extends Node > getNodes()
static String getType(Node node)
static Config createConfig(final String filename)
Map< Id< Link >,?extends Link > getLinks()
static void writeGeometries(final Collection< SimpleFeature > features, final String filename)
NetworkConfigGroup network
Definition: Config.java:92
static Scenario loadScenario(final Config config)
static CoordinateReferenceSystem getCRS(final String wktOrAuthorityCodeOrShorthandName)
Definition: MGC.java:158