1 | package indoor_sweepline;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 | import java.util.Vector;
|
---|
5 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
6 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 |
|
---|
13 |
|
---|
14 | public class ModelGeography
|
---|
15 | {
|
---|
16 | public ModelGeography(DataSet dataSet, LatLon center)
|
---|
17 | {
|
---|
18 | beamsGeography = new Vector<BeamGeography>();
|
---|
19 |
|
---|
20 | this.dataSet = dataSet;
|
---|
21 | this.center = center;
|
---|
22 |
|
---|
23 | wayPool = new Vector<Way>();
|
---|
24 | wayPoolCount = 0;
|
---|
25 | nodePool = new Vector<Node>();
|
---|
26 | nodePoolCount = 0;
|
---|
27 |
|
---|
28 | nodes = null;
|
---|
29 | multipolygon = null;
|
---|
30 | members = null;
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | private Vector<BeamGeography> beamsGeography;
|
---|
35 |
|
---|
36 | private DataSet dataSet;
|
---|
37 | private LatLon center;
|
---|
38 |
|
---|
39 | private Vector<Way> wayPool;
|
---|
40 | private int wayPoolCount;
|
---|
41 | private Vector<Node> nodePool;
|
---|
42 | private int nodePoolCount;
|
---|
43 |
|
---|
44 | private Vector<Node> nodes;
|
---|
45 | private Relation multipolygon;
|
---|
46 | private Vector<RelationMember> members;
|
---|
47 |
|
---|
48 |
|
---|
49 | public void appendNode(Node node)
|
---|
50 | {
|
---|
51 | nodes.add(node);
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | public DataSet getDataSet()
|
---|
56 | {
|
---|
57 | return dataSet;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | public BeamGeography beamAt(int i)
|
---|
62 | {
|
---|
63 | return beamsGeography.elementAt(i);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | public void startGeographyBuild(Vector<Beam> beams, Vector<Strip> strips)
|
---|
68 | {
|
---|
69 | if (beamsGeography.size() < beams.size())
|
---|
70 | beamsGeography.setSize(beams.size());
|
---|
71 |
|
---|
72 | double offset = 0;
|
---|
73 | for (int i = 0; i < beams.size(); ++i)
|
---|
74 | {
|
---|
75 | if (beamsGeography.elementAt(i) == null)
|
---|
76 | beamsGeography.setElementAt(new BeamGeography(dataSet, this), i);
|
---|
77 | beamsGeography.elementAt(i).adjustNodes(new LatLon(center.lat(), addMetersToLon(center, offset)),
|
---|
78 | beams.elementAt(i).getBeamParts());
|
---|
79 |
|
---|
80 | if (i < strips.size())
|
---|
81 | offset += strips.elementAt(i).width;
|
---|
82 | }
|
---|
83 |
|
---|
84 | nodePoolCount = 0;
|
---|
85 | wayPoolCount = 0;
|
---|
86 |
|
---|
87 | members = new Vector<RelationMember>();
|
---|
88 | if (multipolygon != null)
|
---|
89 | multipolygon.setMembers(members);
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | public void startWay()
|
---|
94 | {
|
---|
95 | nodes = new Vector<Node>();
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | public void finishWay(Strip strip, int partIndex, boolean isOuter, String level)
|
---|
100 | {
|
---|
101 | if (nodes.size() > 0)
|
---|
102 | {
|
---|
103 | CorridorPart part = strip.partAt(partIndex);
|
---|
104 | strip.geographyAt(partIndex).appendNodes(part.getType(), part.getSide(), level,
|
---|
105 | nodes.elementAt(nodes.size()-1).getCoor(), nodes.elementAt(0).getCoor(), this);
|
---|
106 | nodes.add(nodes.elementAt(0));
|
---|
107 | }
|
---|
108 | assignNds(nodes);
|
---|
109 | members.add(new RelationMember(isOuter ? "outer" : "inner", wayPool.elementAt(wayPoolCount)));
|
---|
110 | ++wayPoolCount;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | public void appendCorridorPart(CorridorPart part, CorridorGeography partGeography, int beamIndex, int partIndex,
|
---|
115 | String level)
|
---|
116 | {
|
---|
117 | if (nodes.size() > 0)
|
---|
118 | partGeography.appendNodes(part.getType(), part.getSide(), level,
|
---|
119 | nodes.elementAt(nodes.size()-1).getCoor(),
|
---|
120 | beamsGeography.elementAt(beamIndex).coorAt(partIndex), this);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | public void appendUturnNode(Strip strip, int partIndex, int stripIndex, int beamNodeIndex, boolean toTheLeft,
|
---|
125 | String level)
|
---|
126 | {
|
---|
127 | if (toTheLeft)
|
---|
128 | assignCoor(addMeterOffset(beamsGeography.elementAt(stripIndex + 1).coorAt(beamNodeIndex),
|
---|
129 | 0, -strip.width / 2.));
|
---|
130 | else
|
---|
131 | assignCoor(addMeterOffset(beamsGeography.elementAt(stripIndex).coorAt(beamNodeIndex),
|
---|
132 | 0, strip.width / 2.));
|
---|
133 |
|
---|
134 | if (nodes.size() > 0)
|
---|
135 | {
|
---|
136 | CorridorPart part = strip.partAt(partIndex);
|
---|
137 | strip.geographyAt(partIndex).appendNodes(part.getType(), part.getSide(), level,
|
---|
138 | nodes.elementAt(nodes.size()-1).getCoor(), nodePool.elementAt(nodePoolCount).getCoor(), this);
|
---|
139 | }
|
---|
140 | nodes.add(nodePool.elementAt(nodePoolCount));
|
---|
141 | ++nodePoolCount;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | public void finishGeographyBuild(IndoorSweeplineModel.Type type, String level)
|
---|
146 | {
|
---|
147 | for (int i = nodePoolCount; i < nodePool.size(); ++i)
|
---|
148 | nodePool.elementAt(i).setDeleted(true);
|
---|
149 | nodePool.setSize(nodePoolCount);
|
---|
150 |
|
---|
151 | for (int i = wayPoolCount; i < wayPool.size(); ++i)
|
---|
152 | wayPool.elementAt(i).setDeleted(true);
|
---|
153 | wayPool.setSize(wayPoolCount);
|
---|
154 |
|
---|
155 | adjustMultipolygonRelation(type, level);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | private static LatLon addMeterOffset(LatLon latLon, double south, double east)
|
---|
160 | {
|
---|
161 | double scale = Math.cos(latLon.lat() * (Math.PI/180.));
|
---|
162 | return new LatLon(latLon.lat() - south *(360./4e7), latLon.lon() + east / scale *(360./4e7));
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | private static double addMetersToLon(LatLon latLon, double east)
|
---|
167 | {
|
---|
168 | double scale = Math.cos(latLon.lat() * (Math.PI/180.));
|
---|
169 | return latLon.lon() + east / scale *(360./4e7);
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | private void assignCoor(LatLon latLon)
|
---|
174 | {
|
---|
175 | if (nodePoolCount < nodePool.size())
|
---|
176 | nodePool.elementAt(nodePoolCount).setCoor(latLon);
|
---|
177 | else
|
---|
178 | {
|
---|
179 | Node node = new Node(latLon);
|
---|
180 | dataSet.addPrimitive(node);
|
---|
181 | nodePool.add(node);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | private void assignNds(List<Node> nodes)
|
---|
187 | {
|
---|
188 | if (wayPoolCount < wayPool.size())
|
---|
189 | wayPool.elementAt(wayPoolCount).setNodes(nodes);
|
---|
190 | else
|
---|
191 | {
|
---|
192 | Way way = new Way();
|
---|
193 | way.setNodes(nodes);
|
---|
194 | dataSet.addPrimitive(way);
|
---|
195 | wayPool.add(way);
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | private static void addPolygonTags(IndoorSweeplineModel.Type type, String level, OsmPrimitive obj)
|
---|
201 | {
|
---|
202 | if (type == IndoorSweeplineModel.Type.PLATFORM)
|
---|
203 | {
|
---|
204 | obj.put("railway", "platform");
|
---|
205 | obj.put("public_transport", "platform");
|
---|
206 | obj.put("area", "yes");
|
---|
207 | obj.put("level", level);
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | obj.put("highway", "pedestrian");
|
---|
212 | obj.put("indoor", "corridor");
|
---|
213 | obj.put("area", "yes");
|
---|
214 | obj.put("level", level);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | private void adjustMultipolygonRelation(IndoorSweeplineModel.Type type, String level)
|
---|
220 | {
|
---|
221 | if (members.size() > 1)
|
---|
222 | {
|
---|
223 | if (wayPool.size() > 0)
|
---|
224 | wayPool.elementAt(0).removeAll();
|
---|
225 |
|
---|
226 | if (multipolygon == null)
|
---|
227 | {
|
---|
228 | multipolygon = new Relation();
|
---|
229 | dataSet.addPrimitive(multipolygon);
|
---|
230 | }
|
---|
231 |
|
---|
232 | multipolygon.removeAll();
|
---|
233 | multipolygon.put("type", "multipolygon");
|
---|
234 | addPolygonTags(type, level, multipolygon);
|
---|
235 |
|
---|
236 | multipolygon.setMembers(members);
|
---|
237 | }
|
---|
238 | else
|
---|
239 | {
|
---|
240 | if (multipolygon != null)
|
---|
241 | {
|
---|
242 | multipolygon.setDeleted(true);
|
---|
243 | multipolygon = null;
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (wayPool.size() == 1)
|
---|
247 | {
|
---|
248 | wayPool.elementAt(0).removeAll();
|
---|
249 | addPolygonTags(type, level, wayPool.elementAt(0));
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|