1 package nl.tudelft.simulation.dsol.animation.gis.osm;
2
3 import java.awt.geom.Path2D;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.openstreetmap.osmosis.core.container.v0_6.EntityContainer;
11 import org.openstreetmap.osmosis.core.domain.v0_6.Entity;
12 import org.openstreetmap.osmosis.core.domain.v0_6.Node;
13 import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
14 import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
15 import org.openstreetmap.osmosis.core.domain.v0_6.Way;
16 import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
17 import org.openstreetmap.osmosis.core.task.v0_6.Sink;
18
19 import nl.tudelft.simulation.dsol.animation.gis.FeatureInterface;
20 import nl.tudelft.simulation.dsol.animation.gis.GisObject;
21 import nl.tudelft.simulation.dsol.animation.gis.SerializablePath;
22 import nl.tudelft.simulation.dsol.animation.gis.transform.CoordinateTransform;
23
24
25
26
27
28
29
30
31
32
33
34 public class OsmLayerSink implements Sink
35 {
36
37 private Map<Long, MiniWay> ways = new HashMap<Long, MiniWay>();
38
39
40 private Map<Long, MiniNode> nodes = new HashMap<Long, MiniNode>();
41
42
43 private final List<FeatureInterface> featuresToRead;
44
45
46 private final CoordinateTransform coordinateTransform;
47
48
49
50
51
52
53
54
55
56 public OsmLayerSink(final List<FeatureInterface> featuresToRead, final CoordinateTransform coordinateTransform)
57 {
58 this.featuresToRead = featuresToRead;
59 this.coordinateTransform = coordinateTransform;
60 }
61
62
63 @Override
64 public void process(final EntityContainer entityContainer)
65 {
66 Entity entity = entityContainer.getEntity();
67
68 if (entity instanceof Node)
69 {
70 Node node = (Node) entity;
71 MiniNode miniNode = new MiniNode(node.getId(), (float) node.getLatitude(), (float) node.getLongitude());
72 this.nodes.put(miniNode.id, miniNode);
73
74
75
76
77
78
79
80
81
82
83 }
84
85 else if (entity instanceof Way)
86 {
87 boolean read = false;
88 Iterator<Tag> tagIterator = entity.getTags().iterator();
89 FeatureInterface featureToUse = null;
90 while (tagIterator.hasNext())
91 {
92 Tag tag = tagIterator.next();
93 String key = tag.getKey();
94 String value = tag.getValue();
95 for (FeatureInterface feature : this.featuresToRead)
96 {
97 if (feature.getKey().equals("*"))
98 {
99 featureToUse = feature;
100 read = true;
101 break;
102 }
103 if (feature.getKey().equals(key))
104 {
105 if (feature.getValue().equals("*") || feature.getValue().equals(value))
106 {
107 featureToUse = feature;
108 read = true;
109 break;
110 }
111 }
112 }
113 if (read)
114 {
115 break;
116 }
117 }
118 if (read)
119 {
120 Way way = (Way) entity;
121 MiniWay miniWay = new MiniWay(way.getId(), featureToUse, way.getWayNodes());
122 this.ways.put(miniWay.id, miniWay);
123 }
124 }
125
126 else if (entity instanceof Relation)
127 {
128 Iterator<Tag> tagint = entity.getTags().iterator();
129 while (tagint.hasNext())
130 {
131 Tag route = tagint.next();
132 }
133 }
134
135 }
136
137
138 @Override
139 public void initialize(final Map<String, Object> metaData)
140 {
141
142 }
143
144
145 @Override
146 public void complete()
147 {
148 for (MiniWay way : this.ways.values())
149 {
150 addWay(way);
151 }
152 }
153
154
155
156
157
158 private void addWay(final MiniWay way)
159 {
160 SerializablePath path = new SerializablePath(Path2D.WIND_NON_ZERO, way.wayNodesLat.length);
161 boolean start = false;
162 for (int i = 0; i < way.wayNodesLat.length; i++)
163 {
164 float[] coordinate;
165 if (way.wayNodesId[i] != 0)
166 {
167 MiniNode node = this.nodes.get(way.wayNodesId[i]);
168 coordinate = this.coordinateTransform.floatTransform(node.lon, node.lat);
169 }
170 else
171 {
172 coordinate = this.coordinateTransform.floatTransform(way.wayNodesLon[i], way.wayNodesLat[i]);
173 }
174 if (!start)
175 {
176 path.moveTo(coordinate[0], coordinate[1]);
177 start = true;
178 }
179 path.lineTo(coordinate[0], coordinate[1]);
180 }
181 String[] att = new String[0];
182 way.feature.getShapes().add(new GisObject(path, att));
183 }
184
185
186 @Override
187 public void close()
188 {
189
190 }
191
192
193
194
195
196
197 protected static class MiniNode
198 {
199
200 protected long id;
201
202
203 protected float lat;
204
205
206 protected float lon;
207
208
209
210
211
212
213
214 public MiniNode(final long id, final float lat, final float lon)
215 {
216 this.id = id;
217 this.lat = lat;
218 this.lon = lon;
219 }
220 }
221
222
223
224
225
226
227 protected static class MiniWay
228 {
229
230 protected long id;
231
232
233 protected FeatureInterface feature;
234
235
236 protected float[] wayNodesLat;
237
238
239 protected float[] wayNodesLon;
240
241
242 protected long[] wayNodesId;
243
244
245
246
247
248
249
250 public MiniWay(final long id, final FeatureInterface feature, final Collection<WayNode> wayNodes)
251 {
252 this.id = id;
253 this.feature = feature;
254 this.wayNodesLat = new float[wayNodes.size()];
255 this.wayNodesLon = new float[wayNodes.size()];
256 this.wayNodesId = new long[wayNodes.size()];
257 int i = 0;
258 for (WayNode wayNode : wayNodes)
259 {
260 this.wayNodesLat[i] = (float) wayNode.getLatitude();
261 this.wayNodesLon[i] = (float) wayNode.getLongitude();
262 this.wayNodesId[i] = wayNode.getNodeId();
263 i++;
264 }
265 }
266
267 }
268 }