MATSIM
Nodes2ESRIShape.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Nodes2ESRIShape.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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 org.matsim.utils.gis.matsim2esri.network;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.geotools.api.feature.simple.SimpleFeature;
28 import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
29 import org.geotools.feature.simple.SimpleFeatureBuilder;
30 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
31 import org.locationtech.jts.geom.Point;
41 
51 public class Nodes2ESRIShape {
52 
53  private final static Logger log = LogManager.getLogger(Nodes2ESRIShape.class);
54 
55  private final Network network;
56  private final String filename;
57  private SimpleFeatureBuilder builder;
58 
59 
60  public Nodes2ESRIShape(final Network network, final String filename, final String coordinateSystem) {
61  this(network, filename, MGC.getCRS(coordinateSystem));
62  }
63 
64  public Nodes2ESRIShape(Network network, String filename, CoordinateReferenceSystem crs) {
65  this.network = network;
66  this.filename = filename;
67  initFeatureType(crs);
68  }
69 
70  public void write() {
71  Collection<SimpleFeature> features = new ArrayList<SimpleFeature>();
72  for (Node node : NetworkUtils.getSortedNodes(this.network)) {
73  features.add(getFeature(node));
74  }
75  GeoFileWriter.writeGeometries(features, this.filename);
76 
77  }
78 
79  private SimpleFeature getFeature(Node node) {
80  Point p = MGC.coord2Point(node.getCoord());
81  try {
82  return this.builder.buildFeature(null, new Object[]{p,node.getId().toString()});
83  } catch (IllegalArgumentException e) {
84  throw new RuntimeException(e);
85  }
86  }
87 
88  private void initFeatureType(final CoordinateReferenceSystem crs) {
89  SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
90  typeBuilder.setName("node");
91  typeBuilder.setCRS(crs);
92  typeBuilder.add("location", Point.class);
93  typeBuilder.add("ID", String.class);
94 
95  this.builder = new SimpleFeatureBuilder(typeBuilder.buildFeatureType());
96  }
97 
98  public static void main(String [] args) {
99  String netfile = null ;
100  String outputFile = null ;
101 
102  if ( args.length == 0 ) {
103  netfile = "./examples/equil/network.xml";
104 // String netfile = "./test/scenarios/berlin/network.xml.gz";
105 
106  outputFile = "./plans/networkNodes.shp";
107  } else if ( args.length == 2 ) {
108  netfile = args[0] ;
109  outputFile = args[1] ;
110  } else {
111  log.error("Arguments cannot be interpreted. Aborting ...") ;
112  System.exit(-1) ;
113  }
114 
116 // scenario.getConfig().global().setCoordinateSystem("DHDN_GK4");
117 
118  log.info("loading network from " + netfile);
119  final Network network = scenario.getNetwork();
120  new MatsimNetworkReader(scenario.getNetwork()).readFile(netfile);
121  log.info("done.");
122 
123  new Nodes2ESRIShape(network,outputFile, "DHDN_GK4").write();
124  }
125 
126 }
static Node [] getSortedNodes(final Network network)
void initFeatureType(final CoordinateReferenceSystem crs)
Nodes2ESRIShape(Network network, String filename, CoordinateReferenceSystem crs)
static Point coord2Point(final Coord coord)
Definition: MGC.java:131
static void writeGeometries(final Collection< SimpleFeature > features, final String filename)
Nodes2ESRIShape(final Network network, final String filename, final String coordinateSystem)
static Scenario createScenario(final Config config)
static Config createConfig(final String context)
static CoordinateReferenceSystem getCRS(final String wktOrAuthorityCodeOrShorthandName)
Definition: MGC.java:169