11package org .eqasim .core .tools ;
22
33import java .util .*;
4+ import java .util .function .Function ;
45import java .util .stream .Collectors ;
56
67import org .apache .commons .lang3 .BooleanUtils ;
78import org .geotools .api .referencing .crs .CoordinateReferenceSystem ;
89import org .locationtech .jts .geom .Coordinate ;
10+ import org .matsim .api .core .v01 .Coord ;
911import org .matsim .api .core .v01 .Id ;
1012import org .matsim .api .core .v01 .IdSet ;
1113import org .matsim .api .core .v01 .Scenario ;
2022import org .matsim .core .utils .geometry .geotools .MGC ;
2123import org .matsim .core .utils .gis .PolylineFeatureFactory ;
2224import org .matsim .core .utils .gis .ShapeFileWriter ;
23- import org .matsim .pt .transitSchedule .api .TransitLine ;
24- import org .matsim .pt .transitSchedule .api .TransitRoute ;
25- import org .matsim .pt .transitSchedule .api .TransitScheduleReader ;
25+ import org .matsim .pt .transitSchedule .api .*;
2626import org .geotools .api .feature .simple .SimpleFeature ;
2727
2828public class ExportTransitLinesToShapefile {
@@ -84,25 +84,34 @@ public static void main(String[] args) throws Exception {
8484 continue ;
8585 }
8686 NetworkRoute networkRoute = transitRoute .getRoute ();
87- List <Link > links = new ArrayList <>(networkRoute .getLinkIds ().size () + 2 );
88- links .add (network .getLinks ().get (networkRoute .getStartLinkId ()));
89- networkRoute .getLinkIds ().forEach (id -> links .add (network .getLinks ().get (id )));
90- links .add (network .getLinks ().get (networkRoute .getEndLinkId ()));
91-
92- Coordinate [] coordinates = new Coordinate [links .size () + 1 ];
93-
94- for (int i = 0 ; i < links .size (); i ++) {
95- Link link = links .get (i );
96-
97- if (i == 0 ) {
98- coordinates [i ] = new Coordinate (link .getFromNode ().getCoord ().getX (),
99- link .getFromNode ().getCoord ().getY ());
87+ Coordinate [] coordinates ;
88+ if (networkRoute == null ) {
89+ Function <Coord , Coordinate > coordToCoordinate = coord -> new Coordinate (coord .getX (), coord .getY ());
90+ coordinates = transitRoute .getStops ().stream ()
91+ .map (TransitRouteStop ::getStopFacility )
92+ .map (TransitStopFacility ::getCoord )
93+ .map (coordToCoordinate )
94+ .toArray (Coordinate []::new );
95+ } else {
96+ List <Link > links = new ArrayList <>(networkRoute .getLinkIds ().size () + 2 );
97+ links .add (network .getLinks ().get (networkRoute .getStartLinkId ()));
98+ networkRoute .getLinkIds ().forEach (id -> links .add (network .getLinks ().get (id )));
99+ links .add (network .getLinks ().get (networkRoute .getEndLinkId ()));
100+
101+ coordinates = new Coordinate [links .size () + 1 ];
102+
103+ for (int i = 0 ; i < links .size (); i ++) {
104+ Link link = links .get (i );
105+
106+ if (i == 0 ) {
107+ coordinates [i ] = new Coordinate (link .getFromNode ().getCoord ().getX (),
108+ link .getFromNode ().getCoord ().getY ());
109+ }
110+
111+ coordinates [i + 1 ] = new Coordinate (link .getToNode ().getCoord ().getX (),
112+ link .getToNode ().getCoord ().getY ());
100113 }
101-
102- coordinates [i + 1 ] = new Coordinate (link .getToNode ().getCoord ().getX (),
103- link .getToNode ().getCoord ().getY ());
104114 }
105-
106115 SimpleFeature feature = linkFactory .createPolyline ( //
107116 coordinates , //
108117 new Object [] { //
@@ -112,7 +121,6 @@ public static void main(String[] args) throws Exception {
112121 transitRoute .getTransportMode (), //
113122 transitRoute .getDescription () //
114123 }, null );
115-
116124 features .add (feature );
117125 }
118126 }
0 commit comments