MATSIM
NetworkFeatureFactory.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2008 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 org.matsim.vis.kml;
21 
22 import org.apache.log4j.Logger;
23 import org.matsim.api.core.v01.Coord;
24 import org.matsim.api.core.v01.Id;
34 import org.matsim.core.utils.misc.Time;
35 
36 import net.opengis.kml.v_2_2_0.AbstractFeatureType;
37 import net.opengis.kml.v_2_2_0.FolderType;
38 import net.opengis.kml.v_2_2_0.LineStringType;
39 import net.opengis.kml.v_2_2_0.ObjectFactory;
40 import net.opengis.kml.v_2_2_0.PlacemarkType;
41 import net.opengis.kml.v_2_2_0.PointType;
42 import net.opengis.kml.v_2_2_0.StyleType;
43 
47 public class NetworkFeatureFactory implements MatsimFactory {
48 
49  private static final Logger log = Logger.getLogger(NetworkFeatureFactory.class);
50 
52 
53  public static final String ENDP = "</p>";
54  public static final String STARTP = "<p>";
55  public static final String ENDH3 = "</h3>";
56  public static final String STARTH3 = "<h3>";
57  public static final String ENDH2 = "</h2>";
58  public static final String ENDCDATA = "]]>";
59  public static final String STARTH2 = "<h2>";
60  public static final String STARTCDATA = "<![CDATA[";
61 
62  private static final String STARTUL = "<ul>";
63  private static final String ENDUL = "</ul>";
64  private static final String STARTLI = "<li>";
65  private static final String ENDLI = "</li>";
66 
67  private ObjectFactory kmlObjectFactory = new ObjectFactory();
68  private final Network network;
69 
70  public NetworkFeatureFactory(CoordinateTransformation coordTransform, final Network network) {
71  this.coordTransform = coordTransform;
72  this.network = network;
73  }
74 
75  public AbstractFeatureType createLinkFeature(final Link l, StyleType networkStyle) {
76  FolderType folder = this.kmlObjectFactory.createFolderType();
77  String description = this.createLinkDescription(l);
78  folder.setName(l.getId().toString());
79 
80  PlacemarkType p = this.kmlObjectFactory.createPlacemarkType();
81  p.setName(l.getId().toString());
82 
83  Coord fromCoord = this.coordTransform.transform(l.getFromNode().getCoord());
84  Coord toCoord = this.coordTransform.transform(l.getToNode().getCoord());
85  LineStringType line = this.kmlObjectFactory.createLineStringType();
86  line.getCoordinates().add(Double.toString(fromCoord.getX()) + "," + Double.toString(fromCoord.getY()) + ",0.0");
87  line.getCoordinates().add(Double.toString(toCoord.getX()) + "," + Double.toString(toCoord.getY()) + ",0.0");
88  p.setAbstractGeometryGroup(this.kmlObjectFactory.createLineString(line));
89  p.setStyleUrl(networkStyle.getId());
90  p.setDescription(description);
91 
92  PlacemarkType pointPlacemark = this.kmlObjectFactory.createPlacemarkType();
93  Coord centerCoord = this.coordTransform.transform(l.getCoord());
94  PointType point = this.kmlObjectFactory.createPointType();
95  point.getCoordinates().add(Double.toString(centerCoord.getX()) + "," + Double.toString(centerCoord.getY()) + ",0.0");
96  pointPlacemark.setAbstractGeometryGroup(this.kmlObjectFactory.createPoint(point));
97  pointPlacemark.setStyleUrl(networkStyle.getId());
98  pointPlacemark.setDescription(description);
99 
100  folder.getAbstractFeatureGroup().add(this.kmlObjectFactory.createPlacemark(pointPlacemark));
101  folder.getAbstractFeatureGroup().add(this.kmlObjectFactory.createPlacemark(p));
102 
103  return folder;
104  }
105 
106  public AbstractFeatureType createNodeFeature(final Node n, StyleType networkStyle) {
107  PlacemarkType p = this.kmlObjectFactory.createPlacemarkType();
108  p.setName(n.getId().toString());
109 
110  Coord coord = this.coordTransform.transform(n.getCoord());
111  PointType point = this.kmlObjectFactory.createPointType();
112  point.getCoordinates().add(Double.toString(coord.getX()) + "," + Double.toString(coord.getY()) + ",0.0");
113  p.setAbstractGeometryGroup(this.kmlObjectFactory.createPoint(point));
114 
115  p.setStyleUrl(networkStyle.getId());
116  p.setDescription(this.createNodeDescription(n));
117 
118  return p;
119  }
120 
121  public AbstractFeatureType createActFeature(Activity act, StyleType style) {
122 
123  PlacemarkType p = this.kmlObjectFactory.createPlacemarkType();
124  p.setName("Activity on link: " + act.getLinkId().toString());
125 
126  Coord coord = this.coordTransform.transform(act.getCoord());
127  PointType point = this.kmlObjectFactory.createPointType();
128  point.getCoordinates().add(Double.toString(coord.getX()) + "," + Double.toString(coord.getY()) + ",0.0");
129  p.setAbstractGeometryGroup(this.kmlObjectFactory.createPoint(point));
130 
131  p.setStyleUrl(style.getId());
132  return p;
133  }
134 
135  public AbstractFeatureType createLegFeature(Leg leg, StyleType style) {
136  FolderType folder = this.kmlObjectFactory.createFolderType();
137  folder.setName(leg.getMode() + "_" + Time.writeTime(leg.getDepartureTime().seconds()));
138 
139  for (Id<Link> linkId : ((NetworkRoute) leg.getRoute()).getLinkIds()) {
140  Link l = this.network.getLinks().get(linkId);
141 
142  AbstractFeatureType abstractFeature = this.createLinkFeature(l, style);
143  if (abstractFeature.getClass().equals(FolderType.class)) {
144  folder.getAbstractFeatureGroup().add(this.kmlObjectFactory.createFolder((FolderType) abstractFeature));
145  } else {
146  log.warn("Not yet implemented: Adding link KML features of type" + abstractFeature.getClass());
147  }
148  }
149  for (Node n : RouteUtils.getNodes((NetworkRoute) leg.getRoute(), this.network)) {
150 
151  AbstractFeatureType abstractFeature = this.createNodeFeature(n, style);
152  if (abstractFeature.getClass().equals(PlacemarkType.class)) {
153  folder.getAbstractFeatureGroup().add(this.kmlObjectFactory.createPlacemark((PlacemarkType) abstractFeature));
154  } else {
155  log.warn("Not yet implemented: Adding node KML features of type" + abstractFeature.getClass());
156  }
157  }
158 
159  return folder;
160  }
161 
162  private String createLinkDescription(Link l) {
163  StringBuilder buffer = new StringBuilder(100);
164 // buffer.append(NetworkFeatureFactory.STARTCDATA);
165  buffer.append(NetworkFeatureFactory.STARTH2);
166  buffer.append("Link: " );
167  buffer.append(l.getId());
168  buffer.append(NetworkFeatureFactory.ENDH2);
169  buffer.append(NetworkFeatureFactory.STARTH3);
170  buffer.append("From Node: ");
171  buffer.append(l.getFromNode().getId());
172  buffer.append(NetworkFeatureFactory.ENDH3);
173  buffer.append(NetworkFeatureFactory.STARTP);
174  buffer.append(NetworkFeatureFactory.ENDP);
175  buffer.append(NetworkFeatureFactory.STARTH3);
176  buffer.append("To Node: ");
177  buffer.append(l.getToNode().getId());
178  buffer.append(NetworkFeatureFactory.ENDH3);
179  buffer.append(NetworkFeatureFactory.STARTP);
180  buffer.append(NetworkFeatureFactory.ENDP);
181 
182  buffer.append(NetworkFeatureFactory.STARTH3);
183  buffer.append("Attributes: ");
184  buffer.append(NetworkFeatureFactory.ENDH3);
185  buffer.append(NetworkFeatureFactory.STARTP);
186  buffer.append(STARTUL);
187  buffer.append(STARTLI);
188  buffer.append("Freespeed: ");
189  buffer.append(l.getFreespeed());
190  buffer.append(ENDLI);
191  buffer.append(STARTLI);
192  buffer.append("Capacity: ");
193  buffer.append(l.getCapacity());
194  buffer.append(ENDLI);
195  buffer.append(STARTLI);
196  buffer.append("Number of Lanes: ");
197  buffer.append(l.getNumberOfLanes());
198  buffer.append(ENDLI);
199  buffer.append(STARTLI);
200  buffer.append("Length: ");
201  buffer.append(l.getLength());
202  buffer.append(ENDLI);
203  buffer.append(ENDUL);
204  buffer.append(NetworkFeatureFactory.ENDP);
205 
206 // buffer.append(NetworkFeatureFactory.ENDCDATA);
207 
208  return buffer.toString();
209  }
210 
211  private String createNodeDescription(Node n) {
212  StringBuilder buffer = new StringBuilder(100);
213 // buffer.append(NetworkFeatureFactory.STARTCDATA);
214  buffer.append(NetworkFeatureFactory.STARTH2);
215  buffer.append("Node: " );
216  buffer.append(n.getId());
217  buffer.append(NetworkFeatureFactory.ENDH2);
218  buffer.append(NetworkFeatureFactory.STARTH3);
219  buffer.append("Inlinks");
220  buffer.append(NetworkFeatureFactory.ENDH3);
221  buffer.append(NetworkFeatureFactory.STARTP);
222  buffer.append(STARTUL);
223  for (Link l : n.getInLinks().values()) {
224  buffer.append(STARTLI);
225  buffer.append("Link: " );
226  buffer.append(l.getId());
227  buffer.append(" from Node: " );
228  buffer.append(l.getFromNode().getId());
229  buffer.append(ENDLI);
230  }
231  buffer.append(ENDUL);
232  buffer.append(NetworkFeatureFactory.ENDP);
233  buffer.append(NetworkFeatureFactory.STARTH3);
234  buffer.append("Outlinks");
235  buffer.append(NetworkFeatureFactory.ENDH3);
236  buffer.append(NetworkFeatureFactory.STARTP);
237  buffer.append(STARTUL);
238  for (Link l : n.getOutLinks().values()) {
239  buffer.append(STARTLI);
240  buffer.append("Link: " );
241  buffer.append(l.getId());
242  buffer.append(" to Node: ");
243  buffer.append(l.getToNode().getId());
244  buffer.append(ENDLI);
245  }
246  buffer.append(ENDUL);
247  buffer.append(NetworkFeatureFactory.ENDP);
248 // buffer.append(NetworkFeatureFactory.ENDCDATA);
249  return buffer.toString();
250  }
251 
252 }
AbstractFeatureType createLinkFeature(final Link l, StyleType networkStyle)
Map< Id< Link >, ? extends Link > getInLinks()
static final String writeTime(final double seconds, final String timeformat)
Definition: Time.java:80
Map< Id< Link >, ? extends Link > getLinks()
AbstractFeatureType createNodeFeature(final Node n, StyleType networkStyle)
static List< Node > getNodes(final NetworkRoute route, final Network network)
Definition: RouteUtils.java:56
NetworkFeatureFactory(CoordinateTransformation coordTransform, final Network network)
AbstractFeatureType createLegFeature(Leg leg, StyleType style)
Map< Id< Link >, ? extends Link > getOutLinks()
AbstractFeatureType createActFeature(Activity act, StyleType style)